Advertisement
Guest User

Untitled

a guest
Aug 4th, 2019
55
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEServer.h>
  4. #include "BLE2902.h"
  5. #include "BLEHIDDevice.h"
  6. #include "HIDTypes.h"
  7. #include "HIDKeyboardTypes.h"
  8. #include <driver/adc.h>
  9. #include "sdkconfig.h"
  10.  
  11. BLEHIDDevice* hid;
  12. BLECharacteristic* inputMouse;
  13.  
  14. bool connected = false;
  15.  
  16. class MyCallbacks : public BLEServerCallbacks {
  17.   void onConnect(BLEServer* pServer){
  18.     connected = true;
  19.     BLE2902* desc = (BLE2902*)inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
  20.     desc->setNotifications(true);
  21.   }
  22.  
  23.   void onDisconnect(BLEServer* pServer){
  24.     connected = false;
  25.     BLE2902* desc = (BLE2902*)inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
  26.     desc->setNotifications(false);
  27.   }
  28. };
  29.  
  30. void taskServer(void*) {
  31.   BLEDevice::init("Flip-O-Matic");
  32.   BLEServer *pServer = BLEDevice::createServer();
  33.   pServer->setCallbacks(new MyCallbacks());
  34.  
  35.   hid = new BLEHIDDevice(pServer);
  36.   inputMouse = hid->inputReport(1); // <-- input REPORTID from report map
  37.  
  38.   std::string name = "chegewara";
  39.   hid->manufacturer()->setValue(name);
  40.  
  41.   hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
  42.   hid->hidInfo(0x00,0x02);
  43.  
  44.   BLESecurity *pSecurity = new BLESecurity();
  45.  
  46.   pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
  47.  
  48.   const uint8_t reportMapMouse[] = {
  49.     USAGE_PAGE(1),       0x01,
  50.     USAGE(1),            0x02,
  51.     COLLECTION(1),       0x01,
  52.     REPORT_ID(1),        0x01,
  53.     USAGE(1),            0x01,
  54.     COLLECTION(1),       0x00,
  55.     USAGE_PAGE(1),       0x09,
  56.     USAGE_MINIMUM(1),    0x1,
  57.     USAGE_MAXIMUM(1),    0x3,
  58.     LOGICAL_MINIMUM(1),  0x0,
  59.     LOGICAL_MAXIMUM(1),  0x1,
  60.     REPORT_COUNT(1),     0x3,
  61.     REPORT_SIZE(1),      0x1,
  62.     0x80|0x01,           0x2,    // (Data, Variable, Absolute), ;3 button bits
  63.     REPORT_COUNT(1),     0x1,
  64.     REPORT_SIZE(1),      0x5,
  65.     0x80|0x01,           0x1,    //(Constant), ;5 bit padding
  66.     USAGE_PAGE(1),       0x1,    //(Generic Desktop),
  67.     USAGE(1),            0x30,
  68.     USAGE(1),            0x31,
  69.     LOGICAL_MINIMUM(1),  0x81,
  70.     LOGICAL_MAXIMUM(1),  0x7f,
  71.     REPORT_SIZE(1),      0x8,
  72.     REPORT_COUNT(1),     0x2,
  73.     0x80|0x01,           0x6,    //(Data, Variable, Relative), ;2 position bytes (X & Y)
  74.     END_COLLECTION(0),
  75.     END_COLLECTION(0)
  76.   };
  77.  
  78.   hid->reportMap((uint8_t*)reportMapMouse, sizeof(reportMapMouse));
  79.   hid->startServices();
  80.  
  81.   BLEAdvertising *pAdvertising = pServer->getAdvertising();
  82.   pAdvertising->setAppearance(HID_MOUSE);
  83.   pAdvertising->addServiceUUID(hid->hidService()->getUUID());
  84.   pAdvertising->start();
  85.   hid->setBatteryLevel(7);
  86.  
  87.   ESP_LOGD(LOG_TAG, "Advertising started!");
  88.   delay(portMAX_DELAY);
  89. };
  90.  
  91. void setup() {
  92.   Serial.begin(115200);
  93.   Serial.println("Starting BLE work!");
  94.   xTaskCreate(taskServer, "server", 20000, NULL, 5, NULL);
  95. }
  96.  
  97. void loop() {
  98.   if(connected) {
  99.     Serial.println("Scroll Up by 1 unit");
  100.     uint8_t msg[] = { 0x00, 0x00, 0x00, 0x01 };
  101.     inputMouse->setValue(msg,4);
  102.     inputMouse->notify();
  103.    
  104.   }
  105.   delay(2000);
  106. }
Advertisement
RAW Paste Data Copied
Advertisement