add Mouse support for blehidadafruit
This commit is contained in:
1 parent
0ebf2d4dca
commit
13bb0e3066
5 files changed
+220
-16
No files matched your search
@@ -0,0 +1,131 @@
|
||||
/*********************************************************************
|
||||
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;
|
||||
BLEUart bleuart;
|
||||
BLEHidAdafruit blehid;
|
||||
|
||||
#define MOVE_STEP 10
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println("Bluefruit52 HID Mouse Example");
|
||||
Serial.println("- Enter 'WASD' for move mouse (up, left, down, right)");
|
||||
Serial.println("- Enter 'LRMBF' for pressing mouse buttons (left, right, middle, backward, forward)");
|
||||
Serial.println("- Enter 'X' for releasing mouse buttons");
|
||||
|
||||
Bluefruit.begin();
|
||||
// HID Device can have min connection interval up to 9*1.25 = 11.25 ms
|
||||
Bluefruit.setConnInterval(9, 16); // min = 11.25 ms, max = 20ms
|
||||
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_MOUSE);
|
||||
|
||||
// Include ble hid service
|
||||
Bluefruit.addAdvService(blehid);
|
||||
|
||||
// There is enough room for Name in Advertising packet
|
||||
Bluefruit.addAdvName();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (Serial.available())
|
||||
{
|
||||
char ch = (char) Serial.read();
|
||||
|
||||
// convert to upper case
|
||||
ch = (char) toupper(ch);
|
||||
|
||||
// ehco
|
||||
Serial.println(ch);
|
||||
|
||||
switch(ch)
|
||||
{
|
||||
// WASD is mouse move
|
||||
case 'W':
|
||||
blehid.mouseMove(0, -MOVE_STEP);
|
||||
break;
|
||||
|
||||
case 'A':
|
||||
blehid.mouseMove(-MOVE_STEP, 0);
|
||||
break;
|
||||
|
||||
case 'S':
|
||||
blehid.mouseMove(0, MOVE_STEP);
|
||||
break;
|
||||
|
||||
case 'D':
|
||||
blehid.mouseMove(MOVE_STEP, 0);
|
||||
break;
|
||||
|
||||
// LRM is mouse button
|
||||
case 'L':
|
||||
blehid.mouseButtonPress(MOUSE_BUTTON_LEFT);
|
||||
break;
|
||||
|
||||
case 'R':
|
||||
blehid.mouseButtonPress(MOUSE_BUTTON_RIGHT);
|
||||
break;
|
||||
|
||||
case 'M':
|
||||
blehid.mouseButtonPress(MOUSE_BUTTON_MIDDLE);
|
||||
break;
|
||||
|
||||
case 'B':
|
||||
blehid.mouseButtonPress(MOUSE_BUTTON_BACKWARD);
|
||||
break;
|
||||
|
||||
case 'F':
|
||||
// This key is not always supported by OS
|
||||
blehid.mouseButtonPress(MOUSE_BUTTON_FORWARD);
|
||||
break;
|
||||
|
||||
case 'X':
|
||||
blehid.mouseButtonPress(0);
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +106,6 @@ uint8_t const hid_report_descriptor[] =
|
||||
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ),
|
||||
HID_COLLECTION_END,
|
||||
|
||||
#if 0
|
||||
//------------- Mouse Report: buttons + dx + dy + scroll + pan -------------//
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ),
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ),
|
||||
@@ -155,6 +154,7 @@ uint8_t const hid_report_descriptor[] =
|
||||
HID_COLLECTION_END,
|
||||
HID_COLLECTION_END,
|
||||
|
||||
#if 0
|
||||
//------------- Gamepad Report -------------//
|
||||
/* Byte 0: 4 pad | 2 Y-axis | 2 X-axis
|
||||
* Byte 1: Button7-Button0
|
||||
@@ -199,22 +199,26 @@ uint8_t const hid_report_descriptor[] =
|
||||
};
|
||||
|
||||
BLEHidAdafruit::BLEHidAdafruit(void)
|
||||
: BLEHidGeneric(2, 1, 0)
|
||||
: BLEHidGeneric(3, 1, 0)
|
||||
{
|
||||
_mse_buttons = 0;
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::start(void)
|
||||
{
|
||||
uint16_t input_len [] = { sizeof(hid_keyboard_report_t), sizeof(hid_consumer_control_report_t) };
|
||||
uint16_t input_len [] = { sizeof(hid_keyboard_report_t), sizeof(hid_consumer_control_report_t), sizeof(hid_mouse_report_t) };
|
||||
uint16_t output_len[] = { 1 };
|
||||
|
||||
setReportLen(input_len, output_len, NULL);
|
||||
setBootProtocol(true, false);
|
||||
enableBootProtocol(true, true);
|
||||
setReportMap(hid_report_descriptor, sizeof(hid_report_descriptor));
|
||||
|
||||
return BLEHidGeneric::start();
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Keyboard
|
||||
*------------------------------------------------------------------*/
|
||||
err_t BLEHidAdafruit::keyboardReport(hid_keyboard_report_t* report)
|
||||
{
|
||||
return inputReport( REPORT_ID_KEYBOARD, report, sizeof(hid_keyboard_report_t));
|
||||
@@ -231,7 +235,7 @@ err_t BLEHidAdafruit::keyboardReport(uint8_t modifier, uint8_t keycode[6])
|
||||
return keyboardReport(&report);
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::keyHIDCode(uint8_t modifier, uint8_t keycode0, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5)
|
||||
err_t BLEHidAdafruit::keyboardReport(uint8_t modifier, uint8_t keycode0, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5)
|
||||
{
|
||||
hid_keyboard_report_t report =
|
||||
{
|
||||
@@ -248,8 +252,8 @@ err_t BLEHidAdafruit::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;
|
||||
report.modifier = ( HID_ASCII_TO_KEYCODE[(uint8_t)ch].shift ) ? KEYBOARD_MODIFIER_LEFTSHIFT : 0;
|
||||
report.keycode[0] = HID_ASCII_TO_KEYCODE[(uint8_t)ch].keycode;
|
||||
|
||||
return keyboardReport(&report);
|
||||
}
|
||||
@@ -281,8 +285,13 @@ err_t BLEHidAdafruit::keySequence(const char* str, int interal)
|
||||
delay(interal);
|
||||
}
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Consumer Media Key
|
||||
*------------------------------------------------------------------*/
|
||||
err_t BLEHidAdafruit::consumerReport(uint16_t usage_code)
|
||||
{
|
||||
return inputReport( REPORT_ID_CONSUMER_CONTROL, &usage_code, sizeof(usage_code));
|
||||
@@ -298,3 +307,53 @@ err_t BLEHidAdafruit::consumerKeyRelease(void)
|
||||
uint16_t usage = 0;
|
||||
return consumerReport(usage);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Mouse
|
||||
*------------------------------------------------------------------*/
|
||||
err_t BLEHidAdafruit::mouseReport(hid_mouse_report_t* report)
|
||||
{
|
||||
return inputReport( REPORT_ID_MOUSE, report, sizeof(hid_mouse_report_t));
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::mouseReport(uint8_t buttons, int8_t x, int8_t y, int8_t wheel, int8_t pan)
|
||||
{
|
||||
hid_mouse_report_t report =
|
||||
{
|
||||
.buttons = buttons,
|
||||
.x = x,
|
||||
.y = y,
|
||||
.wheel = wheel,
|
||||
.pan = pan
|
||||
};
|
||||
|
||||
_mse_buttons = buttons;
|
||||
|
||||
return mouseReport(&report);
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::mouseButtonPress(uint8_t buttons)
|
||||
{
|
||||
_mse_buttons = buttons;
|
||||
mouseReport(buttons, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::mouseButtonRelease(void)
|
||||
{
|
||||
mouseReport(0, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::mouseMove(int8_t x, int8_t y)
|
||||
{
|
||||
mouseReport(_mse_buttons, x, y, 0, 0);
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::mouseScroll(int8_t scroll)
|
||||
{
|
||||
mouseReport(_mse_buttons, 0, 0, scroll, 0);
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::mousePan(int8_t pan)
|
||||
{
|
||||
mouseReport(_mse_buttons, 0, 0, 0, pan);
|
||||
}
|
||||
@@ -47,6 +47,7 @@
|
||||
class BLEHidAdafruit : public BLEHidGeneric
|
||||
{
|
||||
protected:
|
||||
uint8_t _mse_buttons;
|
||||
|
||||
public:
|
||||
BLEHidAdafruit(void);
|
||||
@@ -56,8 +57,7 @@ class BLEHidAdafruit : public BLEHidGeneric
|
||||
// Keyboard
|
||||
err_t keyboardReport(hid_keyboard_report_t* report);
|
||||
err_t keyboardReport(uint8_t modifier, uint8_t keycode[6]);
|
||||
|
||||
err_t keyHIDCode(uint8_t modifier, uint8_t keycode0, uint8_t keycode1=0, uint8_t keycode2=0, uint8_t keycode3=0, uint8_t keycode4=0, uint8_t keycode5=0);
|
||||
err_t keyboardReport(uint8_t modifier, uint8_t keycode0, uint8_t keycode1=0, uint8_t keycode2=0, uint8_t keycode3=0, uint8_t keycode4=0, uint8_t keycode5=0);
|
||||
|
||||
err_t keyPress(char ch);
|
||||
err_t keyRelease(void);
|
||||
@@ -67,6 +67,17 @@ class BLEHidAdafruit : public BLEHidGeneric
|
||||
err_t consumerReport(uint16_t usage_code);
|
||||
err_t consumerKeyPress(uint16_t usage_code);
|
||||
err_t consumerKeyRelease(void);
|
||||
|
||||
// Mouse
|
||||
err_t mouseReport(hid_mouse_report_t* report);
|
||||
err_t mouseReport(uint8_t buttons, int8_t x, int8_t y, int8_t wheel=0, int8_t pan=0);
|
||||
|
||||
err_t mouseButtonPress(uint8_t buttons);
|
||||
err_t mouseButtonRelease(void);
|
||||
|
||||
err_t mouseMove(int8_t x, int8_t y);
|
||||
err_t mouseScroll(int8_t scroll);
|
||||
err_t mousePan(int8_t pan);
|
||||
};
|
||||
|
||||
#endif /* BLEHIDADAFRUIT_H_ */
|
||||
@@ -88,7 +88,7 @@ BLEHidGeneric::BLEHidGeneric(uint8_t num_input, uint8_t num_output, uint8_t num_
|
||||
}
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setBootProtocol(bool bootKeyboard, bool bootMouse)
|
||||
void BLEHidGeneric::enableBootProtocol(bool bootKeyboard, bool bootMouse)
|
||||
{
|
||||
_boot_keyboard = bootKeyboard;
|
||||
_boot_mouse = bootMouse;
|
||||
@@ -121,8 +121,10 @@ void BLEHidGeneric::setOutputReportCallback(uint8_t reportID, output_report_cb_t
|
||||
|
||||
void blehidgeneric_output_cb(BLECharacteristic& chr, ble_gatts_evt_write_t* request)
|
||||
{
|
||||
BLEHidGeneric& hid = (BLEHidGeneric&) chr.parentService();
|
||||
PRINT_BUFFER(request->data, request->len);
|
||||
(void) chr;
|
||||
(void) request;
|
||||
// BLEHidGeneric& hid = (BLEHidGeneric&) chr.parentService();
|
||||
// PRINT_BUFFER(request->data, request->len);
|
||||
}
|
||||
|
||||
err_t BLEHidGeneric::start(void)
|
||||
@@ -183,7 +185,7 @@ err_t BLEHidGeneric::start(void)
|
||||
VERIFY_STATUS( report_map.start() );
|
||||
report_map.write(_report_map, _report_map_len);
|
||||
|
||||
// Boot Keyboard Input Report
|
||||
// Boot Keyboard Input & Output Report
|
||||
if ( _boot_keyboard )
|
||||
{
|
||||
_chr_boot_keyboard_input = new BLECharacteristic(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT);
|
||||
@@ -231,6 +233,9 @@ err_t BLEHidGeneric::start(void)
|
||||
|
||||
err_t BLEHidGeneric::inputReport(uint8_t reportID, void const* data, int len)
|
||||
{
|
||||
// 0 will treated as report ID = 1
|
||||
if ( reportID == 0 ) reportID++;
|
||||
|
||||
return _chr_inputs[reportID-1].notify( (uint8_t const*) data, len);
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ class BLEHidGeneric : public BLEService
|
||||
|
||||
BLEHidGeneric(uint8_t num_input, uint8_t num_output = 0, uint8_t num_feature = 0);
|
||||
|
||||
void setBootProtocol(bool bootKeyboard, bool bootMouse);
|
||||
void enableBootProtocol(bool bootKeyboard, bool bootMouse);
|
||||
void setHidInfo(uint16_t bcd, uint8_t country, uint8_t flags);
|
||||
|
||||
void setReportLen(uint16_t input_len[], uint16_t output_len[] = NULL, uint16_t feature_len[] = NULL);
|
||||
@@ -148,8 +148,6 @@ typedef enum {
|
||||
MOUSE_BUTTON_MIDDLE = bit(2), ///< Middle button,
|
||||
MOUSE_BUTTON_BACKWARD = bit(3), ///< Backward button,
|
||||
MOUSE_BUTTON_FORWARD = bit(4), ///< Forward button,
|
||||
|
||||
MOUSE_BUTTON_UNCHANGED = 0xff, ///< Not specified by HID but useful for holding mouse buttons
|
||||
}hid_mouse_button_bm_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
Reference in new issue
Block a user