Advertisement
kubbur

Untitled

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