Advertisement
kubbur

Untitled

Aug 17th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1.  
  2. #include <BLEDevice.h>
  3. #include <BLEUtils.h>
  4. #include <BLEServer.h>
  5. #include "BLE2902.h"
  6. #include "BLEHIDDevice.h"
  7. #include "HIDTypes.h"
  8. #include "HIDKeyboardTypes.h"
  9. #include <driver/adc.h>
  10.  
  11. BLEHIDDevice* hid;
  12. BLECharacteristic* input;
  13. BLECharacteristic* output;
  14.  
  15. uint8_t buttons = 0;
  16. uint8_t button1 = 0;
  17. uint8_t button2 = 0;
  18. uint8_t button3 = 0;
  19. bool connected = false;
  20.  
  21. class MyCallbacks : public BLEServerCallbacks {
  22. void onConnect(BLEServer* pServer){
  23. connected = true;
  24. BLE2902* desc = (BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
  25. desc->setNotifications(true);
  26. }
  27.  
  28. void onDisconnect(BLEServer* pServer){
  29. connected = false;
  30. BLE2902* desc = (BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
  31. desc->setNotifications(false);
  32. }
  33. };
  34.  
  35. /*
  36. * This callback is connect with output report. In keyboard output report report special keys changes, like CAPSLOCK, NUMLOCK
  37. * We can add digital pins with LED to show status
  38. * bit 0 - NUM LOCK
  39. * bit 1 - CAPS LOCK
  40. * bit 2 - SCROLL LOCK
  41. */
  42. class MyOutputCallbacks : public BLECharacteristicCallbacks {
  43. void onWrite(BLECharacteristic* me){
  44. uint8_t* value = (uint8_t*)(me->getValue().c_str());
  45. ESP_LOGI(LOG_TAG, "special keys: %d", *value);
  46. }
  47. };
  48.  
  49. void taskServer(void*){
  50.  
  51.  
  52. BLEDevice::init("ESP32-keyboard");
  53. BLEServer *pServer = BLEDevice::createServer();
  54. pServer->setCallbacks(new MyCallbacks());
  55.  
  56. hid = new BLEHIDDevice(pServer);
  57. input = hid->inputReport(1); // <-- input REPORTID from report map
  58. output = hid->outputReport(1); // <-- output REPORTID from report map
  59.  
  60. output->setCallbacks(new MyOutputCallbacks());
  61.  
  62. std::string name = "chegewara";
  63. hid->manufacturer()->setValue(name);
  64.  
  65. hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
  66. hid->hidInfo(0x00,0x02);
  67.  
  68. BLESecurity *pSecurity = new BLESecurity();
  69. // pSecurity->setKeySize();
  70. pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
  71.  
  72. const uint8_t report[] = {
  73. USAGE_PAGE(1), 0x01, // Generic Desktop Ctrls
  74. USAGE(1), 0x06, // Keyboard
  75. COLLECTION(1), 0x01, // Application
  76. REPORT_ID(1), 0x01, // Report ID (1)
  77. USAGE_PAGE(1), 0x07, // Kbrd/Keypad
  78. USAGE_MINIMUM(1), 0xE0,
  79. USAGE_MAXIMUM(1), 0xE7,
  80. LOGICAL_MINIMUM(1), 0x00,
  81. LOGICAL_MAXIMUM(1), 0x01,
  82. REPORT_SIZE(1), 0x01, // 1 byte (Modifier)
  83. REPORT_COUNT(1), 0x08,
  84. HIDINPUT(1), 0x02, // Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position
  85. REPORT_COUNT(1), 0x01, // 1 byte (Reserved)
  86. REPORT_SIZE(1), 0x08,
  87. HIDINPUT(1), 0x01, // Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position
  88. REPORT_COUNT(1), 0x06, // 6 bytes (Keys)
  89. REPORT_SIZE(1), 0x08,
  90. LOGICAL_MINIMUM(1), 0x00,
  91. LOGICAL_MAXIMUM(1), 0x65, // 101 keys
  92. USAGE_MINIMUM(1), 0x00,
  93. USAGE_MAXIMUM(1), 0x65,
  94. HIDINPUT(1), 0x00, // Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position
  95. REPORT_COUNT(1), 0x05, // 5 bits (Num lock, Caps lock, Scroll lock, Compose, Kana)
  96. REPORT_SIZE(1), 0x01,
  97. USAGE_PAGE(1), 0x08, // LEDs
  98. USAGE_MINIMUM(1), 0x01, // Num Lock
  99. USAGE_MAXIMUM(1), 0x05, // Kana
  100. HIDOUTPUT(1), 0x02, // Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile
  101. REPORT_COUNT(1), 0x01, // 3 bits (Padding)
  102. REPORT_SIZE(1), 0x03,
  103. HIDOUTPUT(1), 0x01, // Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile
  104. END_COLLECTION(0)
  105. };
  106.  
  107. hid->reportMap((uint8_t*)report, sizeof(report));
  108. hid->startServices();
  109.  
  110. BLEAdvertising *pAdvertising = pServer->getAdvertising();
  111. pAdvertising->setAppearance(HID_KEYBOARD);
  112. pAdvertising->addServiceUUID(hid->hidService()->getUUID());
  113. pAdvertising->start();
  114. hid->setBatteryLevel(7);
  115.  
  116. ESP_LOGD(LOG_TAG, "Advertising started!");
  117. delay(portMAX_DELAY);
  118.  
  119. };
  120.  
  121. void setup() {
  122. Serial.begin(115200);
  123. Serial.println("Starting BLE work!");
  124.  
  125. pinMode(0, INPUT_PULLDOWN);
  126. // attachInterrupt(digitalPinToInterrupt(12), clickCapsLock, CHANGE); // Num Lock
  127. int pin = 0;
  128. xTaskCreate(taskServer, "server", 20000, NULL, 5, NULL);
  129. }
  130.  
  131. void loop() {
  132.  
  133. if(connected){
  134. int pin = 0;
  135. if(pin == HIGH)
  136. {
  137. const char* hello = "g";
  138.  
  139. while(*hello){
  140. KEYMAP map = keymap[(uint8_t)*hello];
  141. Serial.println(buttons);
  142. uint8_t msg[] = {map.modifier || buttons, 0x0, map.usage, 0x0,0x0,0x0,0x0,0x0};
  143. input->setValue(msg,sizeof(msg));
  144. input->notify();
  145. hello++;
  146. uint8_t msg1[] = {0x0, 0x0, 0x0, 0x0,0x0,0x0,0x0,0x0};
  147.  
  148. input->setValue(msg1,sizeof(msg1));
  149. input->notify();
  150. delay(10);
  151. }
  152. }
  153. delay(50);
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement