Skip to content

Commit d5dc0de

Browse files
committed
Some Refactoring
- printf replaced with std::cout - Added comments - Removed unused code - Removed unused files - private members renamed with underscore suffix - Some new handlers for the application Remains to do: - resolve ble device reconnection not working fine - synchronize the code with the esp-idf example - issues with the new handlers not called on connected_ changes
1 parent 9096151 commit d5dc0de

File tree

8 files changed

+263
-2835
lines changed

8 files changed

+263
-2835
lines changed

CMakePresets.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
2727
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
2828
"CMAKE_BUILD_TYPE": "Debug",
29-
"BUILD_TYPE": "INKPLATE_BUILD"
29+
"BUILD_TYPE": "INKPLATE_BUILD",
30+
"SDKCONFIG": "${sourceDir}/build/sdkconfig"
3031
}
3132
}
3233
],

README.md

+33-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
# ESP32 ESP-IDF Bluetooth keyboard input demo
1+
# ESP32 ESP-IDF Bluetooth Keyboard Input Demo For Receiving And Processing Keyboard Inputs
22

3-
(Updated 2022/02/02)
3+
(Updated 2025-02-09)
44

5-
(Work in progress... updated for ESP-IDF V4.4)
5+
- Now using `ESP-IDF V5.4` for development.
6+
- Partial support of CMake Presets (compiling using VSCode CMake extensions instead of idf.py)
7+
- The `BTKeyboard` class is now located in the components folder
8+
- `keys` renamed to `keys_data` and is now 20 bytes in size. It may need to be larger depending on the keyboard in use.
9+
- All printf use is gone. Now using ostream.
10+
- BLE keyboard `Logitech MX Keys Mini` to do pairing. Still doesn't reconnect through the bonding process (connection recovery doesn't work, still investigating).
11+
- BT keyboard `Logitech K380` works well in both pairing and bonding (recovering after signal lost or ESP32 reboot).
12+
- Apple Wireless keyboard not working
13+
14+
----
615

716
This is a demonstration of an external Bluetooth keyboard sending characters to an ESP32. The code is mainly based on the ESP-IDF's bluetooth/esp_hid_host example, packaged into a class with added support for easier integration with a user application.
817

9-
Please look at the `main/main.cpp` file on how to use the class. Note that only one instance of the class can be used.
18+
Please look at the `main/main.cpp` file on how to use the class. Only one instance of the class should be used to avoid conflicts and resource limitations within the Bluetooth stack.
19+
20+
The class named BTKeyboard waits for a keyboard to be available for pairing through the `BTKeyboard::devices_scan()` method (must be called by the application). It will then accumulate scan codes in a queue to be processed. The class methods available allow for:
21+
- Retrieval of the low-level key scan codes transmitted by the keyboard (`bool wait_for_low_event(BTKeyboard::KeyInfo & inf)` method)
22+
- Retrieval of the ASCII characters augmented with function keys values (`char wait_for_ascii_char()` or `char get_ascii_char()` methods)
1023

11-
The class named BTKeyboard waits for a keyboard to be available for pairing through the `BTKeyboard::devices_scan()` method (must be called by the application). It will then cumulate scan codes in a queue to be processed. The class methods available allow for the retrieval of the low-level key scan codes transmitted by the keyboard (`bool wait_for_low_event(BTKeyboard::KeyInfo & inf)` method) or the ASCII characters augmented with function keys values (`char wait_for_ascii_char()` or `char get_ascii_char()` methods). The following list the character values returned (support of other keyboard keys may be added in a future release):
24+
The following list the character values returned (support of other keyboard keys may be added in a future release):
1225

1326
| Values | Description |
1427
|:-----------:|------------------|
@@ -35,38 +48,41 @@ The class named BTKeyboard waits for a keyboard to be available for pairing thro
3548
| 0x97 | DownArrow key |
3649
| 0x98 | UpArrow key |
3750

38-
The returned scan codes in the BTKeyboard::KeyInfo structure are defined in chapter 10 of https://usb.org/sites/default/files/hut1_22.pdf and are usually supplied directly by the keyboard. The BTKeyboard class supports up to three keys pressed at the same time. The corresponging scan codes are located in the `keys[3]` field. The `modifier` field contains the CTRL/SHIFT/ALT/META left and right key modifier info.
51+
The returned scan codes in the BTKeyboard::KeyInfo structure are defined in chapter 10 of the [USB HID Usage Tables document](https://usb.org/sites/default/files/hut1_22.pdf) and are typically provided directly by the keyboard. The BTKeyboard class supports up to three keys pressed at the same time. The corresponding scan codes are located in the `keys_data` field. The `modifier` field contains the CTRL/SHIFT/ALT/META left and right key modifier info.
52+
53+
The main program is a simple demonstration of the usage of the class. The author is using a Logitech K380 (standard Bluetooth keyboard, not a BLE) and the recent Logitech MX Keys Mini (BLE) keyboards for this demo. Other Bluetooth keyboards may work but may require some modification (mainly adding other key scan codes).
3954

40-
The main program is a simple demonstration of the usage of the class. The author is using a Logitech K380 keyboard (standard Bluetooth keyboard, not a BLE) for this demo. Other Bluetooth keyboards may work but may require some modification (mainly adding other key scan codes).
55+
The first-generation Apple wireless keyboard is known to not function correctly.
4156

42-
The `sdkconfig.defaults` file identifies the ESP-IDF sdkconfig parameters that are required to have this demo working.
57+
The `sdkconfig.defaults` file specifies the ESP-IDF sdkconfig parameters required for this demo to work.
4358

44-
A bug with ESP-IDF 4.3.x may cause an internal stack overflow. Seems to be corrected in 4.4.
59+
A bug in `ESP-IDF 4.3.x` may cause an internal stack overflow due to improper Bluetooth stack resource handling. This issue has been fixed in `ESP-IDF 4.4`.
4560

46-
This is not ready yet as testing with ESP-IDF 4.4 is ongoing. Seems to work fine at this point in time.
61+
Testing with `ESP-IDF V5.4` appears to be stable.
4762

48-
Please be sure that you have installed the ESP-IDF v4.4 (examples are for Linux/MacOS):
63+
Ensure that you have installed ESP-IDF V4.4 or later (example provided is for Linux/MacOS and V5.4):
4964

5065
```
5166
cd ~/esp
52-
git clone -b release/v4.4 --recursive https://github.com/espressif/esp-idf.git
67+
git clone -b release/v5.4 --recursive https://github.com/espressif/esp-idf.git
5368
```
5469

55-
... and update it to the last v4.4 updates from time to time (git pull):
70+
... and periodically update it to the latest version using `git pull`:
5671

5772
```
5873
cd ~/esp/esp-idf
5974
git pull
60-
git submodule --update --init --recursive
75+
git submodule update --init --recursive
6176
```
6277

6378
Some work remains to be done:
6479

6580
- [x] Add pairing code retrieval by the application.
6681
- [x] CapsLock management
82+
- [ ] Synchronize the code with the `esp_hid_host` example of ESP-IDF
6783
- [ ] Some more key mapping
68-
- [ ] Isolate the class into a ESP-IDF Component
84+
- [x] Isolate the class into an ESP-IDF Component
6985

70-
Issue(s)
86+
Issue:
7187

72-
- Typing way too fast (beyond normal keyboard usage) may cause a double panic reset. Still investigating...
88+
- BLE Keyboard's connexion (`MX Keys Mini`) recovering doesn't seem to work. Still investigating.

0 commit comments

Comments
 (0)