Skip to content

Commit 625440c

Browse files
authored
Merge pull request #12 from asterics/newBLE
NewBLE branch merged. Before merging, the master branch was tagged with "PreMergeNewBLE".
2 parents 09509cd + 232ce4f commit 625440c

File tree

96 files changed

+20220
-2712
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+20220
-2712
lines changed

README.md

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
# ESP32 Mouse/Keyboard for BLE HID
22
ESP32 implementation for HID over GATT Keyboard and Mouse (Bluetooth Low Energy). Including serial API for external modules (similar to Adafruit EZKey HID).
33

4-
A great thank you to Paul Stoffregen for the implementation of the keyboard layouts for his Teensyduino project:
5-
www.pjrc.com
4+
### Credits and many thanks to:
5+
- Paul Stoffregen for the implementation of the keyboard layouts for his Teensyduino project: www.pjrc.com
6+
- Neil Kolban for his great contributions to the ESP32 SW (in particular the Bluetooth support): https://github.com/nkolban
7+
- Chegewara for help and support
8+
69
and to Espressif for providing the HID implementation within the esp-idf.
710

811

912
# Control via stdin (make monitor)
1013

11-
For basic mouse and keyboard testing, this project includes some commands which
12-
can be triggered via the make monitor console.
13-
If there is no command found via this parser, the data is forwarded to the
14-
parser for the second UART (see following chapter).
14+
For basic mouse and keyboard testing, some Bluettooh HID reports can be triggered via the
15+
keyboard when the make monitor console is running (see Espressiv IDF: https://github.com/espressif/esp-idf).
1516

16-
Note: To be sure there is no garbage sent to the second parser, please flush
17-
the buffer via the Enter key. Then type the command and send it via Enter.
1817

1918
|Key|Function |Description|
2019
|---|-----------|-----------|
@@ -24,34 +23,29 @@ the buffer via the Enter key. Then type the command and send it via Enter.
2423
|w |Mouse up |Move mouse up by 30px |
2524
|l |Click left |Mouse click right |
2625
|r |Click right|Mouse click left |
27-
|Q |Type 'y' |Type the key y with a 1s delay (avoiding endless loops)|
26+
|q |Type 'y' |just for testing keyboard reports|
2827

29-
Any none listed keys are forwarded to the second parser.
3028

3129

3230
# Control via 2nd UART
3331

34-
This interface is primarily used to control this module by an external microcontroller (in our case
35-
either a AVR Mega32U4 in the FABI device or a TeensyLC in the FLipMouse device).
36-
Each command is started with one or two upper letters and a variable number of parameter bytes.
37-
A command must be terminated by a '\n' character!
32+
This interface is primarily used to control mouse / keyboard activities via an external microcontroller.
33+
Each command is started with a '$' character, followed by a command name (uppercase letters) and a variable number of parameters.
34+
A command must be terminated by a '\n' character (LF, ASCII value 10)!
3835

39-
TBD: currently the UART is not configured, this parser is just used by the stdin console. After testing all
40-
commands, it will be changed and the configured GPIO pins are stated here.
4136

4237
|Command|Function|Parameters|Description|
4338
|-------|--------|----------|-----------|
44-
|ID|Get ID|--|Prints out the ID of this module, used to determine version or attached module type|
39+
|ID|Get ID|--|Prints out the ID of this module (firmware version number)|
4540
|GP|Get BLE pairings|--|Prints out all paired devices' MAC adress. The order is used for DP as well, starting with 0|
46-
|DP|Delete one pairing|number of pairing, either as ASCII '0'-'9' or 0x00-0x09|Deletes one pairing. The pairing number is determined by the command GP|
47-
|PM|Set pairing mode|'0'/'1' or 0x00/0x01|Enables (1) or disables (0) discovery/advertising and terminates an exisiting connection if enabled|
48-
|ID_FABI|Set BLE device name|--|Set the device name to "FABI" and store permanently. Restarting required.|
49-
|ID_FLIPMOUSE|Set BLE device name|--|Set the device name to "FLipMouse" and store permanently. Restarting required.|
50-
|M|Mouse control|4 Bytes|Issue a mouse HID report, parameter description is below|
51-
|KR|Keyboard, release all|--|Releases all keys & modifiers and sends a HID report|
52-
|KD|Keyboard, key down|1Byte keycode|Adds this keycode to the 6 available key slots and sends a HID report.|
53-
|KU|Keyboard, key up|1Byte keycode|Removes this keycode from the 6 available key slots and sends a HID report.|
54-
|KL|Keyboard, set layout/locale|1Byte|Set the keyboard layout to the given locale number (see below), stored permanently. Restarting required.|
41+
|DP|Delete one pairing|number of pairing, given as ASCII-characer '0'-'9'|Deletes one pairing. The pairing number is determined by the command GP|
42+
|PM|Set pairing mode|'0' / '1'|Enables (1) or disables (0) discovery/advertising and terminates an exisiting connection if enabled|
43+
|NAME|Set BLE device name|name as ASCII string|Set the device name to the given name. Restart required.|
44+
|M|Mouse control|4 ASCII-integer values (seperated by space or comma)|Issue a mouse HID report, parameter description is below|
45+
|KA|Keyboard, release all|--|Releases all keys & modifiers and sends a HID report|
46+
|KH|Keyboard, key hold|keycode as ASCII integer value|Adds this keycode to the 6 available key slots and sends a HID report.|
47+
|KR|Keyboard, key release|keycode as ASCII integer value|Removes this keycode from the 6 available key slots and sends a HID report.|
48+
|KL|Keyboard, set layout/locale|locale code as ASCII integer value|Set the keyboard layout to the given locale number (see below), stored permanently. Restarting required.|
5549
|KW|Keyboard write text|n Bytes|Write a text via the keyboard. Supported are ASCII as well as UTF8 character streams.|
5650
|KC|Keyboard get keycode for character|2 Bytes|Get a corresponding keycode for the given character. 2 Bytes are parsed for UTF8, if there is only a ASCII character, use the first sent byte only.|
5751

@@ -60,16 +54,18 @@ commands, it will be changed and the configured GPIO pins are stated here.
6054

6155
|Command byte|Param1|Param2|Param3|Param4|
6256
|------------|------|------|------|------|
63-
|'M'/0x4D |uint8 buttons|int8 X|int8 Y|int8 scroll wheel|
57+
|'M' |uint8 buttons|int8 X|int8 Y|int8 scroll wheel|
6458

65-
Mouse buttons are assigned as following:
59+
Mouse buttons are encoded as bit values in the parameter 'buttons' as follows:
6660
(1<<0): Left mouse button
6761
(1<<1): Right mouse button
6862
(1<<2): Middle mouse button
6963

70-
Due to the button masks, more than one button might be sent in one command.
71-
Releasing the mouse buttons is done via setting the corresponding mask to zero and
72-
send the mouse command again.
64+
Example: "M 3 10 -10 0"
65+
66+
Due to this encoding in a bitmask, more than one button might be sent in one command.
67+
Releasing the mouse buttons is done via setting the corresponding bit postition to zero and
68+
sending the mouse command again.
7369

7470

7571
## Keyboard layouts/locales

components/nkolban_BLE/BLE2902.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* BLE2902.cpp
3+
*
4+
* Created on: Jun 25, 2017
5+
* Author: kolban
6+
*/
7+
8+
/*
9+
* See also:
10+
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
11+
*/
12+
#include "sdkconfig.h"
13+
#if defined(CONFIG_BT_ENABLED)
14+
15+
#include "BLE2902.h"
16+
17+
BLE2902::BLE2902() : BLEDescriptor(BLEUUID((uint16_t) 0x2902)) {
18+
uint8_t data[2] = {0,0};
19+
setValue(data, 2);
20+
} // BLE2902
21+
22+
23+
/**
24+
* @brief Get the notifications value.
25+
* @return The notifications value. True if notifications are enabled and false if not.
26+
*/
27+
bool BLE2902::getNotifications() {
28+
return (getValue()[0] & (1 << 0)) != 0;
29+
} // getNotifications
30+
31+
32+
/**
33+
* @brief Get the indications value.
34+
* @return The indications value. True if indications are enabled and false if not.
35+
*/
36+
bool BLE2902::getIndications() {
37+
return (getValue()[0] & (1 << 1)) != 0;
38+
} // getIndications
39+
40+
41+
/**
42+
* @brief Set the indications flag.
43+
* @param [in] flag The indications flag.
44+
*/
45+
void BLE2902::setIndications(bool flag) {
46+
uint8_t *pValue = getValue();
47+
if (flag) {
48+
pValue[0] |= 1<<1;
49+
} else {
50+
pValue[0] &= ~(1<<1);
51+
}
52+
} // setIndications
53+
54+
55+
/**
56+
* @brief Set the notifications flag.
57+
* @param [in] flag The notifications flag.
58+
*/
59+
void BLE2902::setNotifications(bool flag) {
60+
uint8_t *pValue = getValue();
61+
if (flag) {
62+
pValue[0] |= 1<<0;
63+
} else {
64+
pValue[0] &= ~(1<<0);
65+
}
66+
} // setNotifications
67+
68+
69+
#endif

components/nkolban_BLE/BLE2902.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* BLE2902.h
3+
*
4+
* Created on: Jun 25, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_BLE2902_H_
9+
#define COMPONENTS_CPP_UTILS_BLE2902_H_
10+
#include "sdkconfig.h"
11+
#if defined(CONFIG_BT_ENABLED)
12+
13+
#include "BLEDescriptor.h"
14+
15+
/**
16+
* @brief Descriptor for Client Characteristic Configuration.
17+
*
18+
* This is a convenience descriptor for the Client Characteristic Configuration which has a UUID of 0x2902.
19+
*
20+
* See also:
21+
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
22+
*/
23+
class BLE2902: public BLEDescriptor {
24+
public:
25+
BLE2902();
26+
bool getNotifications();
27+
bool getIndications();
28+
void setNotifications(bool flag);
29+
void setIndications(bool flag);
30+
31+
}; // BLE2902
32+
33+
#endif /* CONFIG_BT_ENABLED */
34+
#endif /* COMPONENTS_CPP_UTILS_BLE2902_H_ */

components/nkolban_BLE/BLE2904.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* BLE2904.cpp
3+
*
4+
* Created on: Dec 23, 2017
5+
* Author: kolban
6+
*/
7+
8+
/*
9+
* See also:
10+
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml
11+
*/
12+
#include "sdkconfig.h"
13+
#if defined(CONFIG_BT_ENABLED)
14+
15+
#include "BLE2904.h"
16+
17+
18+
BLE2904::BLE2904() : BLEDescriptor(BLEUUID((uint16_t) 0x2904)) {
19+
m_data.m_format = 0;
20+
m_data.m_exponent = 0;
21+
m_data.m_namespace = 1; // 1 = Bluetooth SIG Assigned Numbers
22+
m_data.m_unit = 0;
23+
m_data.m_description = 0;
24+
setValue((uint8_t*)&m_data, sizeof(m_data));
25+
} // BLE2902
26+
27+
28+
/**
29+
* @brief Set the description.
30+
*/
31+
void BLE2904::setDescription(uint16_t description) {
32+
m_data.m_description = description;
33+
setValue((uint8_t*)&m_data, sizeof(m_data));
34+
}
35+
36+
37+
/**
38+
* @brief Set the exponent.
39+
*/
40+
void BLE2904::setExponent(int8_t exponent) {
41+
m_data.m_exponent = exponent;
42+
setValue((uint8_t*)&m_data, sizeof(m_data));
43+
} // setExponent
44+
45+
46+
/**
47+
* @brief Set the format.
48+
*/
49+
void BLE2904::setFormat(uint8_t format) {
50+
m_data.m_format = format;
51+
setValue((uint8_t*)&m_data, sizeof(m_data));
52+
} // setFormat
53+
54+
55+
/**
56+
* @brief Set the namespace.
57+
*/
58+
void BLE2904::setNamespace(uint8_t namespace_value) {
59+
m_data.m_namespace = namespace_value;
60+
setValue((uint8_t*)&m_data, sizeof(m_data));
61+
} // setNamespace
62+
63+
64+
/**
65+
* @brief Set the units for this value. It should be one of the encoded values defined here:
66+
* https://www.bluetooth.com/specifications/assigned-numbers/units
67+
* @param [in] uint The type of units of this characteristic as defined by assigned numbers.
68+
*/
69+
void BLE2904::setUnit(uint16_t unit) {
70+
m_data.m_unit = unit;
71+
setValue((uint8_t*)&m_data, sizeof(m_data));
72+
} // setUnit
73+
74+
#endif

components/nkolban_BLE/BLE2904.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* BLE2904.h
3+
*
4+
* Created on: Dec 23, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_BLE2904_H_
9+
#define COMPONENTS_CPP_UTILS_BLE2904_H_
10+
#include "sdkconfig.h"
11+
#if defined(CONFIG_BT_ENABLED)
12+
13+
#include "BLEDescriptor.h"
14+
15+
struct BLE2904_Data {
16+
uint8_t m_format;
17+
int8_t m_exponent;
18+
uint16_t m_unit; // See https://www.bluetooth.com/specifications/assigned-numbers/units
19+
uint8_t m_namespace;
20+
uint16_t m_description;
21+
22+
} __attribute__((packed));
23+
24+
/**
25+
* @brief Descriptor for Characteristic Presentation Format.
26+
*
27+
* This is a convenience descriptor for the Characteristic Presentation Format which has a UUID of 0x2904.
28+
*
29+
* See also:
30+
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml
31+
*/
32+
class BLE2904: public BLEDescriptor {
33+
public:
34+
BLE2904();
35+
static const uint8_t FORMAT_BOOLEAN = 1;
36+
static const uint8_t FORMAT_UINT2 = 2;
37+
static const uint8_t FORMAT_UINT4 = 3;
38+
static const uint8_t FORMAT_UINT8 = 4;
39+
static const uint8_t FORMAT_UINT12 = 5;
40+
static const uint8_t FORMAT_UINT16 = 6;
41+
static const uint8_t FORMAT_UINT24 = 7;
42+
static const uint8_t FORMAT_UINT32 = 8;
43+
static const uint8_t FORMAT_UINT48 = 9;
44+
static const uint8_t FORMAT_UINT64 = 10;
45+
static const uint8_t FORMAT_UINT128 = 11;
46+
static const uint8_t FORMAT_SINT8 = 12;
47+
static const uint8_t FORMAT_SINT12 = 13;
48+
static const uint8_t FORMAT_SINT16 = 14;
49+
static const uint8_t FORMAT_SINT24 = 15;
50+
static const uint8_t FORMAT_SINT32 = 16;
51+
static const uint8_t FORMAT_SINT48 = 17;
52+
static const uint8_t FORMAT_SINT64 = 18;
53+
static const uint8_t FORMAT_SINT128 = 19;
54+
static const uint8_t FORMAT_FLOAT32 = 20;
55+
static const uint8_t FORMAT_FLOAT64 = 21;
56+
static const uint8_t FORMAT_SFLOAT16 = 22;
57+
static const uint8_t FORMAT_SFLOAT32 = 23;
58+
static const uint8_t FORMAT_IEEE20601 = 24;
59+
static const uint8_t FORMAT_UTF8 = 25;
60+
static const uint8_t FORMAT_UTF16 = 26;
61+
static const uint8_t FORMAT_OPAQUE = 27;
62+
63+
void setDescription(uint16_t);
64+
void setExponent(int8_t exponent);
65+
void setFormat(uint8_t format);
66+
void setNamespace(uint8_t namespace_value);
67+
void setUnit(uint16_t unit);
68+
69+
private:
70+
BLE2904_Data m_data;
71+
}; // BLE2904
72+
73+
#endif /* CONFIG_BT_ENABLED */
74+
#endif /* COMPONENTS_CPP_UTILS_BLE2904_H_ */

0 commit comments

Comments
 (0)