Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <BLEDevice.h>
- #include <BLEUtils.h>
- #include <BLEServer.h>
- #include "BLE2902.h"
- #include "BLEHIDDevice.h"
- #include "HIDTypes.h"
- #include "HIDKeyboardTypes.h"
- #include <driver/adc.h>
- #include "sdkconfig.h"
- BLEHIDDevice* hid;
- BLECharacteristic* inputMouse;
- bool connected = false;
- class MyCallbacks : public BLEServerCallbacks {
- void onConnect(BLEServer* pServer){
- connected = true;
- BLE2902* desc = (BLE2902*)inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
- desc->setNotifications(true);
- }
- void onDisconnect(BLEServer* pServer){
- connected = false;
- BLE2902* desc = (BLE2902*)inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
- desc->setNotifications(false);
- }
- };
- void taskServer(void*) {
- BLEDevice::init("Flip-O-Matic");
- BLEServer *pServer = BLEDevice::createServer();
- pServer->setCallbacks(new MyCallbacks());
- hid = new BLEHIDDevice(pServer);
- inputMouse = hid->inputReport(1); // <-- input REPORTID from report map
- std::string name = "chegewara";
- hid->manufacturer()->setValue(name);
- hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
- hid->hidInfo(0x00,0x02);
- BLESecurity *pSecurity = new BLESecurity();
- pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
- const uint8_t reportMapMouse[] = {
- USAGE_PAGE(1), 0x01,
- USAGE(1), 0x02,
- COLLECTION(1), 0x01,
- REPORT_ID(1), 0x01,
- USAGE(1), 0x01,
- COLLECTION(1), 0x00,
- USAGE_PAGE(1), 0x09,
- USAGE_MINIMUM(1), 0x1,
- USAGE_MAXIMUM(1), 0x3,
- LOGICAL_MINIMUM(1), 0x0,
- LOGICAL_MAXIMUM(1), 0x1,
- REPORT_COUNT(1), 0x3,
- REPORT_SIZE(1), 0x1,
- 0x80|0x01, 0x2, // (Data, Variable, Absolute), ;3 button bits
- REPORT_COUNT(1), 0x1,
- REPORT_SIZE(1), 0x5,
- 0x80|0x01, 0x1, //(Constant), ;5 bit padding
- USAGE_PAGE(1), 0x1, //(Generic Desktop),
- USAGE(1), 0x30,
- USAGE(1), 0x31,
- LOGICAL_MINIMUM(1), 0x81,
- LOGICAL_MAXIMUM(1), 0x7f,
- REPORT_SIZE(1), 0x8,
- REPORT_COUNT(1), 0x2,
- 0x80|0x01, 0x6, //(Data, Variable, Relative), ;2 position bytes (X & Y)
- END_COLLECTION(0),
- END_COLLECTION(0)
- };
- hid->reportMap((uint8_t*)reportMapMouse, sizeof(reportMapMouse));
- hid->startServices();
- BLEAdvertising *pAdvertising = pServer->getAdvertising();
- pAdvertising->setAppearance(HID_MOUSE);
- pAdvertising->addServiceUUID(hid->hidService()->getUUID());
- pAdvertising->start();
- hid->setBatteryLevel(7);
- ESP_LOGD(LOG_TAG, "Advertising started!");
- delay(portMAX_DELAY);
- };
- void setup() {
- Serial.begin(115200);
- Serial.println("Starting BLE work!");
- xTaskCreate(taskServer, "server", 20000, NULL, 5, NULL);
- }
- void loop() {
- if(connected) {
- Serial.println("Scroll Up by 1 unit");
- uint8_t msg[] = { 0x00, 0x00, 0x00, 0x01 };
- inputMouse->setValue(msg,4);
- inputMouse->notify();
- }
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement