add hid_keyscan
This commit is contained in:
1 parent
34d68dddd1
commit
4a5b114982
1 file changed
+170
@@ -0,0 +1,170 @@
|
||||
/*********************************************************************
|
||||
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
|
||||
*********************************************************************/
|
||||
|
||||
/* This sketch demonstate how to use BLE HID to scan an array of pin
|
||||
* and send its keycode. It is essentially an implementation of hid keyboard,
|
||||
* useful reference if you want to make an BLE keyboard.
|
||||
*/
|
||||
|
||||
#include <bluefruit.h>
|
||||
|
||||
BLEHidAdafruit blehid;
|
||||
|
||||
// Array of pins and its keycode
|
||||
// For keycode definition see BLEHidGeneric.h
|
||||
uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
|
||||
uint8_t hidcode[] = { HID_KEY_A, HID_KEY_B, HID_KEY_C ,HID_KEY_D, HID_KEY_E, HID_KEY_F };
|
||||
|
||||
uint8_t pincount = sizeof(pins)/sizeof(pins[0]);
|
||||
|
||||
// Modifier keys, only take cares of Shift
|
||||
// ATL, CTRL, CMD keys are left for user excersie.
|
||||
uint8_t shiftPin = 11;
|
||||
|
||||
bool keyPressedPreviously = false;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println("Bluefruit52 HID Keyscan 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();
|
||||
Serial.println("Wire configured Pin to GND to send key");
|
||||
Serial.println("Wire Shift Keky to GND if you want to send it in upper case");
|
||||
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");
|
||||
|
||||
// set up pin as input
|
||||
for (uint8_t i=0; i<pincount; i++)
|
||||
{
|
||||
pinMode(pins[i], INPUT_PULLUP);
|
||||
}
|
||||
|
||||
// set up modifier key
|
||||
pinMode(shiftPin, INPUT_PULLUP);
|
||||
|
||||
/* 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 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();
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
/*-------------- San Pin Array and send report ---------------------*/
|
||||
bool anyKeyPressed = false;
|
||||
|
||||
uint8_t modifier = 0;
|
||||
uint8_t count=0;
|
||||
uint8_t keycode[6] = { 0 };
|
||||
|
||||
// scan mofidier key (only SHIFT), user implement ATL, CTRL, CMD if needed
|
||||
if ( 0 == digitalRead(shiftPin) )
|
||||
{
|
||||
modifier |= KEYBOARD_MODIFIER_LEFTSHIFT;
|
||||
}
|
||||
|
||||
// scan normal key and send report
|
||||
for(uint8_t i=0; i < pincount; i++)
|
||||
{
|
||||
if ( 0 == digitalRead(pins[i]) )
|
||||
{
|
||||
// if pin is active (low), add its hid code to key report
|
||||
keycode[count++] = hidcode[i];
|
||||
|
||||
// 6 is max keycode per report
|
||||
if ( count == 6)
|
||||
{
|
||||
blehid.keyboardReport(modifier, keycode);
|
||||
|
||||
// reset report
|
||||
count = 0;
|
||||
memset(keycode, 0, 6);
|
||||
}
|
||||
|
||||
// used later
|
||||
anyKeyPressed = true;
|
||||
keyPressedPreviously = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Send any remaining keys (not accumulated up to 6)
|
||||
if ( count )
|
||||
{
|
||||
blehid.keyboardReport(modifier, keycode);
|
||||
}
|
||||
|
||||
// Send All-zero report to indicate there is no keys pressed
|
||||
// Most of the time, it is, though we don't need to send zero report
|
||||
// every loop(), only a key is pressed in previous loop()
|
||||
if ( !anyKeyPressed && keyPressedPreviously )
|
||||
{
|
||||
keyPressedPreviously = false;
|
||||
blehid.keyRelease();
|
||||
}
|
||||
|
||||
// Poll interval
|
||||
delay(10);
|
||||
}
|
||||
Reference in new issue
Block a user