Advertisement
joric

Arduino_BLE_KBD_Test.ino

Nov 12th, 2016
1,895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. // Turning HM-10, HM-11 into Bluetooth HID modules
  2. // see http://geekhack.org/index.php?topic=62236
  3. // pushbutton attached to GND/D3
  4. // HM-10 pins connected to Arduino Pro Micro as RXD to TX0, TXD to RX1
  5. // see keycodes at https://github.com/rampadc/cc254x-hidKbdM/blob/master/Example/BLE_KBD_Test/KBD_HUT.h
  6.  
  7. #define hutLeftGUI 0xE3
  8. #define BT Serial1
  9.  
  10. void cmd(const char * s, int timeout=100, bool bSendCR=true) {
  11.   Serial.write("> ");
  12.   Serial.write(s);
  13.   Serial.write("\r\n");
  14.  
  15.   BT.write(s);
  16.   if (bSendCR) {
  17.     BT.write("\r\n");
  18.   }
  19.  
  20.   // serial output is tricky, let's use read timeout for now
  21.   long t = millis();
  22.   while(millis() < t + timeout) {
  23.     while (BT.available()) {
  24.       Serial.write(BT.read());
  25.     }
  26.     delay(1);
  27.   }
  28. }
  29.  
  30. void keyCommand(uint8_t modifiers, uint8_t key1, uint8_t key2 = 0, uint8_t key3 = 0, uint8_t key4 = 0, uint8_t key5 = 0, uint8_t key6 = 0) {
  31.   char buf[16];
  32.   sprintf(buf, "%s%c", modifiers==0x80 ? "KD" : "KU", key1);  
  33.   cmd(buf);
  34.   cmd("KUPDATE");
  35. }
  36.  
  37. void setup() {  
  38.   Serial.begin(115200); // This pipes to the serial monitor  
  39.   BT.begin(57600); // This is the UART, attached to HM-10  
  40.   pinMode(3, INPUT); // input mode for the button pin (D3)
  41.   digitalWrite(3, HIGH); // set button pin to HIGH (software pullup) so it triggers when pulled to GND
  42. }
  43.  
  44. bool wasPressed = false;
  45. void loop() {
  46.   int keyCode = hutLeftGUI;  // winkey
  47.   bool pressed = digitalRead(3)==LOW;
  48.   if (pressed != wasPressed) {
  49.     keyCommand(pressed ? 0x80 : keyCode, keyCode);
  50.     wasPressed = pressed;
  51.     delay(25);
  52.   }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement