diff --git a/libraries/Bluefruit52Lib/examples/hid_keyboard/hid_keyboard.ino b/libraries/Bluefruit52Lib/examples/hid_keyboard/hid_keyboard.ino new file mode 100644 index 00000000..861cde25 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/hid_keyboard/hid_keyboard.ino @@ -0,0 +1,92 @@ +/********************************************************************* + 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 + +BLEDis bledis; +BLEUart bleuart; +BLEHid blehid; + +bool hasKeyPressed = false; + +void setup() +{ + Serial.begin(115200); + + Serial.println("Bluefruit52 HID Keyboard Example"); + + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52"); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather 52"); + bledis.setHardwareRev("Rev E"); + bledis.setFirmwareRev("0.1.0"); + bledis.start(); + + // Configure and Start BLE Uart Service + bleuart.start(); + + // BLE Hid + blehid.start(); + + // Set up Advertising Packet + setupAdv(); + + // Start Advertising + Bluefruit.startAdvertising(); +} + +void setupAdv(void) +{ + Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.addAdvTxPower(); + + Bluefruit.addAdvApperance(BLE_APPEARANCE_HID_KEYBOARD); + + // Include ble hid service + Bluefruit.addAdvService(blehid); + + // There is enough room for Name in Advertising packet + Bluefruit.addAdvName(); +} + +void loop() +{ + // Only send KeyRelease if previously pressed, to avoid sending + // multiple keyRelease report (that consume memories and bandwidth) + if ( hasKeyPressed ) + { + hasKeyPressed = false; + blehid.keyRelease(); + + // delay a bit after a report + delay(5); + } + + if (Serial.available()) + { + char ch = (char) Serial.read(); + + // ehco + Serial.write(ch); + + blehid.keyPress(ch); + hasKeyPressed = true; + + // delay a bit after a report + delay(5); + } +} + diff --git a/libraries/Bluefruit52Lib/examples/neopixel_picker/neopixel_picker.ino b/libraries/Bluefruit52Lib/examples/neopixel_picker/neopixel_picker.ino new file mode 100644 index 00000000..8c35468b --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/neopixel_picker/neopixel_picker.ino @@ -0,0 +1,115 @@ +/********************************************************************* + This is an example for our nRF51822 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 +#include +#include +#include + +/*========================================================================= + PIN Which pin on the Arduino is connected to the NeoPixels? + NUMPIXELS How many NeoPixels are attached to the Arduino? + -----------------------------------------------------------------------*/ + #define FACTORYRESET_ENABLE 1 + + #define PIN 5 + #define NUMPIXELS 1 +/*=========================================================================*/ + +Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUMPIXELS, PIN); + +BLEUart bleuart; + +// function prototypes over in packetparser.cpp +uint8_t readPacket(BLEUart *ble_uart, uint16_t timeout); +float parsefloat(uint8_t *buffer); +void printHex(const uint8_t * data, const uint32_t numBytes); + +// the packet buffer +extern uint8_t packetbuffer[]; + + +/**************************************************************************/ +/*! + @brief Sets up the HW an the BLE module (this function is called + automatically on startup) +*/ +/**************************************************************************/ +void setup(void) +{ + Serial.begin(115200); + Serial.println(F("Adafruit Bluefruit52 App Controller Example")); + Serial.println(F("-------------------------------------------")); + + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52"); + + // Configure and Start BLE Uart Service + bleuart.start(); + + // Set up Advertising Packet + setupAdv(); + + // Start Advertising + Bluefruit.startAdvertising(); +} + +void setupAdv(void) +{ + //Bluefruit.addAdvTxPower(); + Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.addAdvTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.addAdvService(bleuart); + + // There is no room for Name in Advertising packet + // Use Scan response for Name + Bluefruit.addScanName(); +} + +/**************************************************************************/ +/*! + @brief Constantly poll for new command or response data +*/ +/**************************************************************************/ +void loop(void) +{ + /* Wait for new data to arrive */ + uint8_t len = readPacket(&bleuart, 500); + if (len == 0) return; + + /* Got a packet! */ + // printHex(packetbuffer, len); + + // Color + if (packetbuffer[1] == 'C') { + uint8_t red = packetbuffer[2]; + uint8_t green = packetbuffer[3]; + uint8_t blue = packetbuffer[4]; + Serial.print ("RGB #"); + if (red < 0x10) Serial.print("0"); + Serial.print(red, HEX); + if (green < 0x10) Serial.print("0"); + Serial.print(green, HEX); + if (blue < 0x10) Serial.print("0"); + Serial.println(blue, HEX); + + for(uint8_t i=0; i +#include +#include + + +#define PACKET_ACC_LEN (15) +#define PACKET_GYRO_LEN (15) +#define PACKET_MAG_LEN (15) +#define PACKET_QUAT_LEN (19) +#define PACKET_BUTTON_LEN (5) +#define PACKET_COLOR_LEN (6) +#define PACKET_LOCATION_LEN (15) + +// READ_BUFSIZE Size of the read buffer for incoming packets +#define READ_BUFSIZE (20) + + +/* Buffer to hold incoming characters */ +uint8_t packetbuffer[READ_BUFSIZE+1]; + +/**************************************************************************/ +/*! + @brief Casts the four bytes at the specified address to a float +*/ +/**************************************************************************/ +float parsefloat(uint8_t *buffer) +{ + float f = ((float *)buffer)[0]; + return f; +} + +/**************************************************************************/ +/*! + @brief Prints a hexadecimal value in plain characters + @param data Pointer to the byte data + @param numBytes Data length in bytes +*/ +/**************************************************************************/ +void printHex(const uint8_t * data, const uint32_t numBytes) +{ + uint32_t szPos; + for (szPos=0; szPos < numBytes; szPos++) + { + Serial.print(F("0x")); + // Append leading 0 for small values + if (data[szPos] <= 0xF) + { + Serial.print(F("0")); + Serial.print(data[szPos] & 0xf, HEX); + } + else + { + Serial.print(data[szPos] & 0xff, HEX); + } + // Add a trailing space if appropriate + if ((numBytes > 1) && (szPos != numBytes - 1)) + { + Serial.print(F(" ")); + } + } + Serial.println(); +} + +/**************************************************************************/ +/*! + @brief Waits for incoming data and parses it +*/ +/**************************************************************************/ +uint8_t readPacket(BLEUart *ble_uart, uint16_t timeout) +{ + uint16_t origtimeout = timeout, replyidx = 0; + + memset(packetbuffer, 0, READ_BUFSIZE); + + while (timeout--) { + if (replyidx >= 20) break; + if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN)) + break; + if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN)) + break; + if ((packetbuffer[1] == 'M') && (replyidx == PACKET_MAG_LEN)) + break; + if ((packetbuffer[1] == 'Q') && (replyidx == PACKET_QUAT_LEN)) + break; + if ((packetbuffer[1] == 'B') && (replyidx == PACKET_BUTTON_LEN)) + break; + if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN)) + break; + if ((packetbuffer[1] == 'L') && (replyidx == PACKET_LOCATION_LEN)) + break; + + while (ble_uart->available()) { + char c = ble_uart->read(); + if (c == '!') { + replyidx = 0; + } + packetbuffer[replyidx] = c; + replyidx++; + timeout = origtimeout; + } + + if (timeout == 0) break; + delay(1); + } + + packetbuffer[replyidx] = 0; // null term + + if (!replyidx) // no data or timeout + return 0; + if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning + return 0; + + // check checksum! + uint8_t xsum = 0; + uint8_t checksum = packetbuffer[replyidx-1]; + + for (uint8_t i=0; i + {1, HID_KEY_SLASH }, // 0x3F ? + + {1, HID_KEY_2 }, // 0x40 @ + {1, HID_KEY_A }, // 0x41 A + {1, HID_KEY_B }, // 0x42 B + {1, HID_KEY_C }, // 0x43 C + {1, HID_KEY_D }, // 0x44 D + {1, HID_KEY_E }, // 0x45 E + {1, HID_KEY_F }, // 0x46 F + {1, HID_KEY_G }, // 0x47 G + {1, HID_KEY_H }, // 0x48 H + {1, HID_KEY_I }, // 0x49 I + {1, HID_KEY_J }, // 0x4A J + {1, HID_KEY_K }, // 0x4B K + {1, HID_KEY_L }, // 0x4C L + {1, HID_KEY_M }, // 0x4D M + {1, HID_KEY_N }, // 0x4E N + {1, HID_KEY_O }, // 0x4F O + {1, HID_KEY_P }, // 0x50 P + {1, HID_KEY_Q }, // 0x51 Q + {1, HID_KEY_R }, // 0x52 R + {1, HID_KEY_S }, // 0x53 S + {1, HID_KEY_T }, // 0x55 T + {1, HID_KEY_U }, // 0x55 U + {1, HID_KEY_V }, // 0x56 V + {1, HID_KEY_W }, // 0x57 W + {1, HID_KEY_X }, // 0x58 X + {1, HID_KEY_Y }, // 0x59 Y + {1, HID_KEY_Z }, // 0x5A Z + {0, HID_KEY_BRACKET_LEFT }, // 0x5B [ + {0, HID_KEY_BACKSLASH }, // 0x5C '\' + {0, HID_KEY_BRACKET_RIGHT }, // 0x5D ] + {1, HID_KEY_6 }, // 0x5E ^ + {1, HID_KEY_MINUS }, // 0x5F _ + + {0, HID_KEY_GRAVE }, // 0x60 ` + {0, HID_KEY_A }, // 0x61 a + {0, HID_KEY_B }, // 0x62 b + {0, HID_KEY_C }, // 0x63 c + {0, HID_KEY_D }, // 0x66 d + {0, HID_KEY_E }, // 0x65 e + {0, HID_KEY_F }, // 0x66 f + {0, HID_KEY_G }, // 0x67 g + {0, HID_KEY_H }, // 0x68 h + {0, HID_KEY_I }, // 0x69 i + {0, HID_KEY_J }, // 0x6A j + {0, HID_KEY_K }, // 0x6B k + {0, HID_KEY_L }, // 0x6C l + {0, HID_KEY_M }, // 0x6D m + {0, HID_KEY_N }, // 0x6E n + {0, HID_KEY_O }, // 0x6F o + {0, HID_KEY_P }, // 0x70 p + {0, HID_KEY_Q }, // 0x71 q + {0, HID_KEY_R }, // 0x72 r + {0, HID_KEY_S }, // 0x73 s + {0, HID_KEY_T }, // 0x75 t + {0, HID_KEY_U }, // 0x75 u + {0, HID_KEY_V }, // 0x76 v + {0, HID_KEY_W }, // 0x77 w + {0, HID_KEY_X }, // 0x78 x + {0, HID_KEY_Y }, // 0x79 y + {0, HID_KEY_Z }, // 0x7A z + {1, HID_KEY_BRACKET_LEFT }, // 0x7B { + {1, HID_KEY_BACKSLASH }, // 0x7C | + {1, HID_KEY_BRACKET_RIGHT }, // 0x7D } + {1, HID_KEY_GRAVE }, // 0x7E ~ + {0, HID_KEY_DELETE } // 0x7F Delete +}; + uint8_t const hid_report_descriptor[] = { @@ -204,8 +339,8 @@ uint8_t const hid_report_descriptor[] = BLEHid::BLEHid(void) : BLEService(UUID16_SVC_HUMAN_INTERFACE_DEVICE), - _chr_protocol(UUID16_CHR_PROTOCOL_MODE), _chr_control(UUID16_CHR_HID_CONTROL_POINT), - _chr_input() + _chr_protocol(UUID16_CHR_PROTOCOL_MODE), _chr_input(), + _chr_control(UUID16_CHR_HID_CONTROL_POINT) { _keyboard_en = true; _mouse_en = false; @@ -281,3 +416,43 @@ err_t BLEHid::start(void) return NRF_SUCCESS; } + +err_t BLEHid::sendReport(uint8_t id, void const* data, int len) +{ + return _chr_input.notify( (uint8_t const*) data, len); +} + +err_t BLEHid::keyboardReport(uint8_t modifier, uint8_t keycode[6]) +{ + hid_keyboard_report_t report = + { + .modifier = modifier, + }; + memcpy(report.keycode, keycode, 6); + + return keyboardReport(&report); +} + +err_t BLEHid::keyboardReport(hid_keyboard_report_t* report) +{ + return sendReport( REPORT_ID_KEYBOARD, report, sizeof(hid_keyboard_report_t)); +} + +err_t BLEHid::keyPress(char ch) +{ + hid_keyboard_report_t report; + varclr(&report); + + report.modifier = ( HID_ASCII_TO_KEYCODE[ch].shift ) ? KEYBOARD_MODIFIER_LEFTSHIFT : 0; + report.keycode[0] = HID_ASCII_TO_KEYCODE[ch].keycode; + + return keyboardReport(&report); +} + +err_t BLEHid::keyRelease(void) +{ + hid_keyboard_report_t report; + varclr(&report); + + return keyboardReport(&report); +} diff --git a/libraries/Bluefruit52Lib/src/services/BLEHid.h b/libraries/Bluefruit52Lib/src/services/BLEHid.h index 22cc00fe..17816df5 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEHid.h +++ b/libraries/Bluefruit52Lib/src/services/BLEHid.h @@ -42,6 +42,36 @@ #include "BLECharacteristic.h" #include "BLEService.h" +/// Standard HID Boot Protocol Mouse Report. +typedef ATTR_PACKED_STRUCT(struct) +{ + uint8_t buttons; /**< buttons mask for currently pressed buttons in the mouse. */ + int8_t x; /**< Current delta x movement of the mouse. */ + int8_t y; /**< Current delta y movement on the mouse. */ + int8_t wheel; /**< Current delta vertical wheel movement on the mouse. */ + int8_t pan; /**< Current delta horizontal wheel movement on the mouse. */ +} hid_mouse_report_t; + +/// Standard HID Boot Protocol Keyboard Report. +typedef ATTR_PACKED_STRUCT(struct) +{ + uint8_t modifier; /**< Keyboard modifier byte, indicating pressed modifier keys (a combination of HID_KEYBOARD_MODIFER_* masks). */ + uint8_t reserved; /**< Reserved for OEM use, always set to 0. */ + uint8_t keycode[6]; /**< Key codes of the currently pressed keys. */ +} hid_keyboard_report_t; + +/// Gamepad report +typedef ATTR_PACKED_STRUCT(struct) +{ + ATTR_PACKED_STRUCT(struct){ + uint8_t x : 2; + uint8_t y : 2; + uint8_t : 4; + }; + + uint8_t buttons; +}hid_gamepad_report_t; + class BLEHid : public BLEService { @@ -59,22 +89,19 @@ class BLEHid : public BLEService BLEHid(void); virtual err_t start(void); + err_t sendReport(uint8_t id, void const* data, int len); + + // Keyboard + err_t keyboardReport(uint8_t modifier, uint8_t keycode[6]); + err_t keyboardReport(hid_keyboard_report_t* report); + err_t keyPress(char ch); + err_t keyRelease(void); }; //--------------------------------------------------------------------+ // MOUSE //--------------------------------------------------------------------+ -/// Standard HID Boot Protocol Mouse Report. -typedef ATTR_PACKED_STRUCT(struct) -{ - uint8_t buttons; /**< buttons mask for currently pressed buttons in the mouse. */ - int8_t x; /**< Current delta x movement of the mouse. */ - int8_t y; /**< Current delta y movement on the mouse. */ - int8_t wheel; /**< Current delta vertical wheel movement on the mouse. */ - int8_t pan; /**< Current delta horizontal wheel movement on the mouse. */ -} hid_mouse_report_t; - /// Standard Mouse Buttons Bitmap typedef enum { MOUSE_BUTTON_LEFT = bit(0), ///< Left button @@ -89,15 +116,6 @@ typedef enum { //--------------------------------------------------------------------+ // Keyboard //--------------------------------------------------------------------+ -/// Standard HID Boot Protocol Keyboard Report. -typedef ATTR_PACKED_STRUCT(struct) -{ - uint8_t modifier; /**< Keyboard modifier byte, indicating pressed modifier keys (a combination of HID_KEYBOARD_MODIFER_* masks). */ - uint8_t reserved; /**< Reserved for OEM use, always set to 0. */ - uint8_t keycode[6]; /**< Key codes of the currently pressed keys. */ -} hid_keyboard_report_t; - - /// HID Consumer Control Report typedef ATTR_PACKED_STRUCT(struct) { @@ -124,19 +142,127 @@ typedef enum { KEYBOARD_LED_KANA = bit(4) ///< Kana mode }hid_keyboard_led_bm_t; -/// Gamepad report -typedef ATTR_PACKED_STRUCT(struct) -{ - ATTR_PACKED_STRUCT(struct){ - uint8_t x : 2; - uint8_t y : 2; - uint8_t : 4; - }; +//--------------------------------------------------------------------+ +// HID KEYCODE +//--------------------------------------------------------------------+ +#define HID_KEY_NONE 0x00 +#define HID_KEY_A 0x04 +#define HID_KEY_B 0x05 +#define HID_KEY_C 0x06 +#define HID_KEY_D 0x07 +#define HID_KEY_E 0x08 +#define HID_KEY_F 0x09 +#define HID_KEY_G 0x0A +#define HID_KEY_H 0x0B +#define HID_KEY_I 0x0C +#define HID_KEY_J 0x0D +#define HID_KEY_K 0x0E +#define HID_KEY_L 0x0F +#define HID_KEY_M 0x10 +#define HID_KEY_N 0x11 +#define HID_KEY_O 0x12 +#define HID_KEY_P 0x13 +#define HID_KEY_Q 0x14 +#define HID_KEY_R 0x15 +#define HID_KEY_S 0x16 +#define HID_KEY_T 0x17 +#define HID_KEY_U 0x18 +#define HID_KEY_V 0x19 +#define HID_KEY_W 0x1A +#define HID_KEY_X 0x1B +#define HID_KEY_Y 0x1C +#define HID_KEY_Z 0x1D +#define HID_KEY_1 0x1E +#define HID_KEY_2 0x1F +#define HID_KEY_3 0x20 +#define HID_KEY_4 0x21 +#define HID_KEY_5 0x22 +#define HID_KEY_6 0x23 +#define HID_KEY_7 0x24 +#define HID_KEY_8 0x25 +#define HID_KEY_9 0x26 +#define HID_KEY_0 0x27 +#define HID_KEY_RETURN 0x28 +#define HID_KEY_ESCAPE 0x29 +#define HID_KEY_BACKSPACE 0x2A +#define HID_KEY_TAB 0x2B +#define HID_KEY_SPACE 0x2C +#define HID_KEY_MINUS 0x2D +#define HID_KEY_EQUAL 0x2E +#define HID_KEY_BRACKET_LEFT 0x2F +#define HID_KEY_BRACKET_RIGHT 0x30 +#define HID_KEY_BACKSLASH 0x31 +#define HID_KEY_EUROPE_1 0x32 +#define HID_KEY_SEMICOLON 0x33 +#define HID_KEY_APOSTROPHE 0x34 +#define HID_KEY_GRAVE 0x35 +#define HID_KEY_COMMA 0x36 +#define HID_KEY_PERIOD 0x37 +#define HID_KEY_SLASH 0x38 +#define HID_KEY_CAPS_LOCK 0x39 +#define HID_KEY_F1 0x3A +#define HID_KEY_F2 0x3B +#define HID_KEY_F3 0x3C +#define HID_KEY_F4 0x3D +#define HID_KEY_F5 0x3E +#define HID_KEY_F6 0x3F +#define HID_KEY_F7 0x40 +#define HID_KEY_F8 0x41 +#define HID_KEY_F9 0x42 +#define HID_KEY_F10 0x43 +#define HID_KEY_F11 0x44 +#define HID_KEY_F12 0x45 +#define HID_KEY_PRINT_SCREEN 0x46 +#define HID_KEY_SCROLL_LOCK 0x47 +#define HID_KEY_PAUSE 0x48 +#define HID_KEY_INSERT 0x49 +#define HID_KEY_HOME 0x4A +#define HID_KEY_PAGE_UP 0x4B +#define HID_KEY_DELETE 0x4C +#define HID_KEY_END 0x4D +#define HID_KEY_PAGE_DOWN 0x4E +#define HID_KEY_ARROW_RIGHT 0x4F +#define HID_KEY_ARROW_LEFT 0x50 +#define HID_KEY_ARROW_DOWN 0x51 +#define HID_KEY_ARROW_UP 0x52 +#define HID_KEY_NUM_LOCK 0x53 +#define HID_KEY_KEYPAD_DIVIDE 0x54 +#define HID_KEY_KEYPAD_MULTIPLY 0x55 +#define HID_KEY_KEYPAD_SUBTRACT 0x56 +#define HID_KEY_KEYPAD_ADD 0x57 +#define HID_KEY_KEYPAD_ENTER 0x58 +#define HID_KEY_KEYPAD_1 0x59 +#define HID_KEY_KEYPAD_2 0x5A +#define HID_KEY_KEYPAD_3 0x5B +#define HID_KEY_KEYPAD_4 0x5C +#define HID_KEY_KEYPAD_5 0x5D +#define HID_KEY_KEYPAD_6 0x5E +#define HID_KEY_KEYPAD_7 0x5F +#define HID_KEY_KEYPAD_8 0x60 +#define HID_KEY_KEYPAD_9 0x61 +#define HID_KEY_KEYPAD_0 0x62 +#define HID_KEY_KEYPAD_DECIMAL 0x63 +#define HID_KEY_EUROPE_2 0x64 +#define HID_KEY_APPLICATION 0x65 +#define HID_KEY_POWER 0x66 +#define HID_KEY_KEYPAD_EQUAL 0x67 +#define HID_KEY_F13 0x68 +#define HID_KEY_F14 0x69 +#define HID_KEY_F15 0x6A +#define HID_KEY_CONTROL_LEFT 0xE0 +#define HID_KEY_SHIFT_LEFT 0xE1 +#define HID_KEY_ALT_LEFT 0xE2 +#define HID_KEY_GUI_LEFT 0xE3 +#define HID_KEY_CONTROL_RIGHT 0xE4 +#define HID_KEY_SHIFT_RIGHT 0xE5 +#define HID_KEY_ALT_RIGHT 0xE6 +#define HID_KEY_GUI_RIGHT 0xE7 - uint8_t buttons; -}hid_gamepad_report_t; - -//ASSERT_STATIC(sizeof(hid_gamepad_report_t) == 2,); +typedef struct{ + uint8_t shift; + uint8_t keycode; +}hid_ascii_to_keycode_entry_t; +extern const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128]; //--------------------------------------------------------------------+ // REPORT DESCRIPTOR