|
22 | 22 | BLEDis bledis; |
23 | 23 | BLEHidAdafruit blehid; |
24 | 24 |
|
| 25 | +// use on-board button if available |
| 26 | +#ifdef PIN_BUTTON1 |
| 27 | + #define BTN1 PIN_BUTTON1 |
| 28 | +#else |
| 29 | + #define BTN1 A0 |
| 30 | +#endif |
| 31 | + |
| 32 | +#ifdef PIN_BUTTON2 |
| 33 | + #define BTN2 PIN_BUTTON2 |
| 34 | +#else |
| 35 | + #define BTN2 A1 |
| 36 | +#endif |
| 37 | + |
| 38 | +// Circuit Play Bluefruit has button active state = high |
| 39 | +#ifdef ARDUINO_NRF52840_CIRCUITPLAY |
| 40 | + #define BTN_ACTIVE HIGH |
| 41 | +#else |
| 42 | + #define BTN_ACTIVE LOW |
| 43 | +#endif |
| 44 | + |
25 | 45 | // Array of pins and its keycode |
26 | | -// For keycode definition see BLEHidGeneric.h |
27 | | -uint8_t pins[] = { A0, A1, A2, A3, A4, A5 }; |
28 | | -uint8_t hidcode[] = { HID_KEY_A, HID_KEY_B, HID_KEY_C ,HID_KEY_D, HID_KEY_E, HID_KEY_F }; |
| 46 | +// For keycode definition: https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h |
| 47 | +uint8_t pins[] = { BTN1, BTN2 }; |
| 48 | +uint8_t hidcode[] = { HID_KEY_KEYPAD_0, HID_KEY_KEYPAD_1 }; |
29 | 49 |
|
30 | 50 | uint8_t pincount = sizeof(pins)/sizeof(pins[0]); |
31 | 51 |
|
32 | | -// Modifier keys, only take cares of Shift |
33 | | -// ATL, CTRL, CMD keys are left for user excersie. |
34 | | -uint8_t shiftPin = 11; |
35 | | - |
36 | 52 | bool keyPressedPreviously = false; |
37 | 53 |
|
38 | 54 | void setup() |
39 | 55 | { |
40 | 56 | Serial.begin(115200); |
41 | | - while ( !Serial ) delay(10); // for nrf52840 with native usb |
| 57 | + |
| 58 | +#if CFG_DEBUG |
| 59 | + // Blocking wait for connection when debug mode is enabled via IDE |
| 60 | + while ( !Serial ) delay(10); |
| 61 | +#endif |
42 | 62 |
|
43 | 63 | Serial.println("Bluefruit52 HID Keyscan Example"); |
44 | 64 | Serial.println("-------------------------------\n"); |
@@ -66,9 +86,6 @@ void setup() |
66 | 86 | pinMode(pins[i], INPUT_PULLUP); |
67 | 87 | } |
68 | 88 |
|
69 | | - // set up modifier key |
70 | | - pinMode(shiftPin, INPUT_PULLUP); |
71 | | - |
72 | 89 | /* Start BLE HID |
73 | 90 | * Note: Apple requires BLE device must have min connection interval >= 20m |
74 | 91 | * ( The smaller the connection interval the faster we could send data). |
@@ -128,16 +145,10 @@ void loop() |
128 | 145 | uint8_t count=0; |
129 | 146 | uint8_t keycode[6] = { 0 }; |
130 | 147 |
|
131 | | - // scan modifier key (only SHIFT), user implement ATL, CTRL, CMD if needed |
132 | | - if ( 0 == digitalRead(shiftPin) ) |
133 | | - { |
134 | | - modifier |= KEYBOARD_MODIFIER_LEFTSHIFT; |
135 | | - } |
136 | | - |
137 | 148 | // scan normal key and send report |
138 | 149 | for(uint8_t i=0; i < pincount; i++) |
139 | 150 | { |
140 | | - if ( 0 == digitalRead(pins[i]) ) |
| 151 | + if ( BTN_ACTIVE == digitalRead(pins[i]) ) |
141 | 152 | { |
142 | 153 | // if pin is active (low), add its hid code to key report |
143 | 154 | keycode[count++] = hidcode[i]; |
|
0 commit comments