Created
May 18, 2019 17:54
-
-
Save bigjosh/5d8575f26988228dcf9d40c3795d0928 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************* | |
This is an example for our nRF52 based Bluefruit LE modules | |
Pick one up today in the adafruit shop! | |
Adafruit invests time and resources providing this open source code, | |
please support Adafruit and open-source hardware by purchasing | |
products from Adafruit! | |
MIT license, check LICENSE for more information | |
All text above, and the splash screen below must be included in | |
any redistribution | |
*********************************************************************/ | |
#include <bluefruit.h> | |
BLEDis bledis; | |
BLEHidAdafruit blehid; | |
struct key_stroke { | |
char *name; | |
uint8_t modifiers; | |
uint8_t code; | |
}; | |
// Modifiers are here.. | |
// https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/200b3aaefb3256ac26df82ebc9b5b58923d9c37c/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/hid/hid.h#L188 | |
// Keycodes are here.. | |
// https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/200b3aaefb3256ac26df82ebc9b5b58923d9c37c/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/hid/hid.h#L212 | |
key_stroke key_strokes[] = { | |
{ "'a'" , 0 , HID_KEY_A }, | |
{ "'A'" , KEYBOARD_MODIFIER_LEFTSHIFT , HID_KEY_A }, | |
{ "'b'" , 0 , HID_KEY_B }, | |
{ "'B'" , KEYBOARD_MODIFIER_LEFTSHIFT , HID_KEY_B }, | |
{ "'1'" , 0 , HID_KEY_1 }, | |
{ "'!'" , KEYBOARD_MODIFIER_LEFTSHIFT , HID_KEY_1 }, | |
{ "F1" , 0 , HID_KEY_F1 }, | |
{ "Shift F1" , KEYBOARD_MODIFIER_LEFTSHIFT , HID_KEY_F1 }, | |
}; | |
#define KEY_STROKE_COUNT (sizeof(key_strokes)/sizeof(key_stroke)) | |
void printPrompt() { | |
for( int x=0; x < KEY_STROKE_COUNT ; x++ ) { | |
Serial.print(x); | |
Serial.print(". "); | |
Serial.println(key_strokes[x].name); | |
} | |
Serial.println(); | |
Serial.println("Enter the number of the keystroke you want to send:"); | |
Serial.println(); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
while ( !Serial ) delay(10); // for nrf52840 with native usb | |
Serial.println("Bluefruit52 HID Keyboard Report Example"); | |
Serial.println("---------------------------------------\n"); | |
Serial.println(); | |
Serial.println("Go to your phone's Bluetooth settings to pair your device"); | |
Serial.println("then open an application that accepts keyboard input"); | |
Serial.println(); | |
Bluefruit.begin(); | |
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 | |
Bluefruit.setTxPower(4); | |
Bluefruit.setName("Bluefruit52"); | |
// Configure and Start Device Information Service | |
bledis.setManufacturer("Adafruit Industries"); | |
bledis.setModel("Bluefruit Feather 52"); | |
bledis.begin(); | |
/* Start BLE HID | |
* Note: Apple requires BLE device must have min connection interval >= 20m | |
* ( The smaller the connection interval the faster we could send data). | |
* However for HID and MIDI device, Apple could accept min connection interval | |
* up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max | |
* connection interval to 11.25 ms and 15 ms respectively for best performance. | |
*/ | |
blehid.begin(); | |
// Set callback for set LED from central | |
blehid.setKeyboardLedCallback(set_keyboard_led); | |
/* Set connection interval (min, max) to your perferred value. | |
* Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms | |
* min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms | |
*/ | |
/* Bluefruit.setConnInterval(9, 12); */ | |
// Set up and start advertising | |
startAdv(); | |
printPrompt(); | |
} | |
void startAdv(void) | |
{ | |
// Advertising packet | |
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); | |
Bluefruit.Advertising.addTxPower(); | |
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD); | |
// Include BLE HID service | |
Bluefruit.Advertising.addService(blehid); | |
// There is enough room for the dev name in the advertising packet | |
Bluefruit.Advertising.addName(); | |
/* Start Advertising | |
* - Enable auto advertising if disconnected | |
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms | |
* - Timeout for fast mode is 30 seconds | |
* - Start(timeout) with timeout = 0 will advertise forever (until connected) | |
* | |
* For recommended advertising interval | |
* https://developer.apple.com/library/content/qa/qa1931/_index.html | |
*/ | |
Bluefruit.Advertising.restartOnDisconnect(true); | |
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms | |
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode | |
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds | |
} | |
void loop() | |
{ | |
if (Serial.available()) { | |
char i = Serial.read(); | |
if (isdigit( i ) ) { | |
// Convert from ASCII to a number | |
int x = i - '0'; | |
key_stroke k = key_strokes[x]; | |
// The keyboardReport() function takes an array of 6 keycodes. From what I can tell, it does not | |
// press the keys consecutively,. it presses them simultainiously. | |
// Here we only want to send one code, so we back-fill the array with HID_KEY_NONE | |
uint8_t keycodes[6] = { k.code , HID_KEY_NONE , HID_KEY_NONE , HID_KEY_NONE , HID_KEY_NONE , HID_KEY_NONE }; | |
blehid.keyboardReport( k.modifiers , keycodes ); | |
// This release seems to be required. Without it, the last pressed key just repeats forever. | |
blehid.keyRelease(); | |
} | |
} | |
} | |
/** | |
* Callback invoked when received Set LED from central. | |
* Must be set previously with setKeyboardLedCallback() | |
* | |
The LED bit map is as follows: (also defined by KEYBOARD_LED_ ) | |
* Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0) | |
*/ | |
void set_keyboard_led(uint8_t led_bitmap) | |
{ | |
// light up Red Led if any bits is set | |
if ( led_bitmap ) | |
{ | |
ledOn( LED_RED ); | |
} | |
else | |
{ | |
ledOff( LED_RED ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment