Advertisement
j0h

bleHID

j0h
Jul 31st, 2022
1,686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************************************************
  2.  This is an example for our nRF52 based Bluefruit LE modules
  3.  
  4.  Pick one up today in the adafruit shop!
  5.  
  6.  Adafruit invests time and resources providing this open source code,
  7.  please support Adafruit and open-source hardware by purchasing
  8.  products from Adafruit!
  9.  
  10.  MIT license, check LICENSE for more information
  11.  All text above, and the splash screen below must be included in
  12.  any redistribution
  13. *********************************************************************/
  14. #include <bluefruit.h>
  15.  
  16. BLEDis bledis;
  17. BLEHidAdafruit blehid;
  18.  
  19. #define MOVE_STEP    10
  20.  
  21. void setup()
  22. {
  23.   Serial.begin(115200);
  24.   while ( !Serial ) delay(10);   // for nrf52840 with native usb
  25.  
  26.   Serial.println("Bluefruit52 HID Mouse Example");
  27.   Serial.println("-----------------------------\n");
  28.   Serial.println("Go to your phone's Bluetooth settings to pair your device");
  29.   Serial.println("then open an application that accepts mouse input");
  30.   Serial.println();
  31.  
  32.   Serial.println("Enter following characters");
  33.   Serial.println("- 'WASD'  to move mouse (up, left, down, right)");
  34.   Serial.println("- 'LRMBF' to press mouse button(s) (left, right, middle, backward, forward)");
  35.   Serial.println("- 'X'     to release mouse button(s)");
  36.  
  37.   Bluefruit.begin();
  38.   // HID Device can have a min connection interval of 9*1.25 = 11.25 ms
  39.   Bluefruit.Periph.setConnInterval(9, 16); // min = 9*1.25=11.25 ms, max = 16*1.25=20ms
  40.   Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values
  41.  
  42.   // Configure and Start Device Information Service
  43.   bledis.setManufacturer("Adafruit Industries");
  44.   bledis.setModel("Bluefruit Feather 52");
  45.   bledis.begin();
  46.  
  47.   // BLE HID
  48.   blehid.begin();
  49.  
  50.   // Set up and start advertising
  51.   startAdv();
  52. }
  53.  
  54. void startAdv(void)
  55. {  
  56.   // Advertising packet
  57.   Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  58.   Bluefruit.Advertising.addTxPower();
  59.   Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_MOUSE);
  60.  
  61.   // Include BLE HID service
  62.   Bluefruit.Advertising.addService(blehid);
  63.  
  64.   // There is enough room for 'Name' in the advertising packet
  65.   Bluefruit.Advertising.addName();
  66.  
  67.   /* Start Advertising
  68.    * - Enable auto advertising if disconnected
  69.    * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
  70.    * - Timeout for fast mode is 30 seconds
  71.    * - Start(timeout) with timeout = 0 will advertise forever (until connected)
  72.    *
  73.    * For recommended advertising interval
  74.    * https://developer.apple.com/library/content/qa/qa1931/_index.html  
  75.    */
  76.   Bluefruit.Advertising.restartOnDisconnect(true);
  77.   Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms
  78.   Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
  79.   Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds  
  80. }
  81.  
  82. void loop()
  83. {    
  84.   if (Serial.available())
  85.   {
  86.     char ch = (char) Serial.read();
  87.  
  88.     // convert to upper case
  89.     ch = (char) toupper(ch);
  90.    
  91.     // echo
  92.     Serial.println(ch);
  93.  
  94.     switch(ch){
  95.       // WASD to move the mouse
  96.       case 'W':
  97.         blehid.mouseMove(0, -MOVE_STEP);
  98.       break;
  99.  
  100.       case 'A':
  101.         blehid.mouseMove(-MOVE_STEP, 0);
  102.       break;
  103.  
  104.       case 'S':
  105.         blehid.mouseMove(0, MOVE_STEP);
  106.       break;
  107.  
  108.       case 'D':
  109.         blehid.mouseMove(MOVE_STEP, 0);
  110.       break;
  111.  
  112.       // LRMBF for mouse button(s)
  113.       case 'L':
  114.         blehid.mouseButtonPress(MOUSE_BUTTON_LEFT);
  115.       break;
  116.  
  117.       case 'R':
  118.         blehid.mouseButtonPress(MOUSE_BUTTON_RIGHT);
  119.       break;
  120.  
  121.       case 'M':
  122.         blehid.mouseButtonPress(MOUSE_BUTTON_MIDDLE);
  123.       break;
  124.  
  125.       case 'B':
  126.         blehid.mouseButtonPress(MOUSE_BUTTON_BACKWARD);
  127.       break;
  128.  
  129.       case 'F':
  130.         // This key is not always supported by every OS
  131.         blehid.mouseButtonPress(MOUSE_BUTTON_FORWARD);
  132.       break;
  133.  
  134.       case 'X':
  135.         // X to release all buttons
  136.         blehid.mouseButtonRelease();
  137.       break;
  138.  
  139.       default: break;
  140.     }
  141.   }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement