Skip to content

Commit

Permalink
refactor: change micro controller to esp32-s3wroom
Browse files Browse the repository at this point in the history
Add feature:
- Support for config push buttons, bi-directional buttons and onboard rotary encoder.
- USB HID keyboard mode
- Use onboard status LED (leds[0])

Temporary removed feature:
- Multi-ble connection
- Extension board support
  • Loading branch information
DriftKingTW committed Dec 8, 2023
1 parent 00134ae commit 8dc6fb7
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 261 deletions.
80 changes: 40 additions & 40 deletions libdeps-mod/ESP32 BLE Keyboard/BleKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ void BleKeyboard::set_current_active_device(uint16_t currentDeviceIndex) {
this->currentActiveDevice);
}

void BleKeyboard::sendReport(KeyReport* keys) {
void BleKeyboard::sendReport(BLEKeyReport* keys) {
if (this->isConnected()) {
this->inputKeyboard->setValue((uint8_t*)keys, sizeof(KeyReport));
this->inputKeyboard->setValue((uint8_t*)keys, sizeof(BLEKeyReport));
this->inputKeyboard->notify(this->currentActiveDevice);
#if defined(USE_NIMBLE)
// vTaskDelay(delayTicks);
Expand All @@ -211,9 +211,9 @@ void BleKeyboard::sendReport(KeyReport* keys) {
}
}

void BleKeyboard::sendReport(MediaKeyReport* keys) {
void BleKeyboard::sendReport(MediaBLEKeyReport* keys) {
if (this->isConnected()) {
this->inputMediaKeys->setValue((uint8_t*)keys, sizeof(MediaKeyReport));
this->inputMediaKeys->setValue((uint8_t*)keys, sizeof(MediaBLEKeyReport));
this->inputMediaKeys->notify(this->currentActiveDevice);
#if defined(USE_NIMBLE)
// vTaskDelay(delayTicks);
Expand Down Expand Up @@ -368,7 +368,7 @@ size_t BleKeyboard::press(uint8_t k) {
if (k >= 136) { // it's a non-printing key (not a modifier)
k = k - 136;
} else if (k >= 128) { // it's a modifier key
_keyReport.modifiers |= (1 << (k - 128));
_BLEKeyReport.modifiers |= (1 << (k - 128));
k = 0;
} else { // it's a printing key
k = pgm_read_byte(_asciimap + k);
Expand All @@ -378,19 +378,19 @@ size_t BleKeyboard::press(uint8_t k) {
}
if (k & 0x80) { // it's a capital letter or other character reached
// with shift
_keyReport.modifiers |= 0x02; // the left shift modifier
_BLEKeyReport.modifiers |= 0x02; // the left shift modifier
k &= 0x7F;
}
}

// Add k to the key report only if it's not already present
// and if there is an empty slot.
if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
_keyReport.keys[2] != k && _keyReport.keys[3] != k &&
_keyReport.keys[4] != k && _keyReport.keys[5] != k) {
if (_BLEKeyReport.keys[0] != k && _BLEKeyReport.keys[1] != k &&
_BLEKeyReport.keys[2] != k && _BLEKeyReport.keys[3] != k &&
_BLEKeyReport.keys[4] != k && _BLEKeyReport.keys[5] != k) {
for (i = 0; i < 6; i++) {
if (_keyReport.keys[i] == 0x00) {
_keyReport.keys[i] = k;
if (_BLEKeyReport.keys[i] == 0x00) {
_BLEKeyReport.keys[i] = k;
break;
}
}
Expand All @@ -399,19 +399,19 @@ size_t BleKeyboard::press(uint8_t k) {
return 0;
}
}
sendReport(&_keyReport);
sendReport(&_BLEKeyReport);
return 1;
}

size_t BleKeyboard::press(const MediaKeyReport k) {
size_t BleKeyboard::press(const MediaBLEKeyReport k) {
uint16_t k_16 = k[1] | (k[0] << 8);
uint16_t mediaKeyReport_16 = _mediaKeyReport[1] | (_mediaKeyReport[0] << 8);
uint16_t mediaBLEKeyReport_16 = _mediaBLEKeyReport[1] | (_mediaBLEKeyReport[0] << 8);

mediaKeyReport_16 |= k_16;
_mediaKeyReport[0] = (uint8_t)((mediaKeyReport_16 & 0xFF00) >> 8);
_mediaKeyReport[1] = (uint8_t)(mediaKeyReport_16 & 0x00FF);
mediaBLEKeyReport_16 |= k_16;
_mediaBLEKeyReport[0] = (uint8_t)((mediaBLEKeyReport_16 & 0xFF00) >> 8);
_mediaBLEKeyReport[1] = (uint8_t)(mediaBLEKeyReport_16 & 0x00FF);

sendReport(&_mediaKeyReport);
sendReport(&_mediaBLEKeyReport);
return 1;
}

Expand All @@ -423,7 +423,7 @@ size_t BleKeyboard::release(uint8_t k) {
if (k >= 136) { // it's a non-printing key (not a modifier)
k = k - 136;
} else if (k >= 128) { // it's a modifier key
_keyReport.modifiers &= ~(1 << (k - 128));
_BLEKeyReport.modifiers &= ~(1 << (k - 128));
k = 0;
} else { // it's a printing key
k = pgm_read_byte(_asciimap + k);
Expand All @@ -432,7 +432,7 @@ size_t BleKeyboard::release(uint8_t k) {
}
if (k & 0x80) { // it's a capital letter or other character reached
// with shift
_keyReport.modifiers &= ~(0x02); // the left shift modifier
_BLEKeyReport.modifiers &= ~(0x02); // the left shift modifier
k &= 0x7F;
}
}
Expand All @@ -441,37 +441,37 @@ size_t BleKeyboard::release(uint8_t k) {
// Check all positions in case the key is present more than once (which it
// shouldn't be)
for (i = 0; i < 6; i++) {
if (0 != k && _keyReport.keys[i] == k) {
_keyReport.keys[i] = 0x00;
if (0 != k && _BLEKeyReport.keys[i] == k) {
_BLEKeyReport.keys[i] = 0x00;
}
}

sendReport(&_keyReport);
sendReport(&_BLEKeyReport);
return 1;
}

size_t BleKeyboard::release(const MediaKeyReport k) {
size_t BleKeyboard::release(const MediaBLEKeyReport k) {
uint16_t k_16 = k[1] | (k[0] << 8);
uint16_t mediaKeyReport_16 = _mediaKeyReport[1] | (_mediaKeyReport[0] << 8);
mediaKeyReport_16 &= ~k_16;
_mediaKeyReport[0] = (uint8_t)((mediaKeyReport_16 & 0xFF00) >> 8);
_mediaKeyReport[1] = (uint8_t)(mediaKeyReport_16 & 0x00FF);
uint16_t mediaBLEKeyReport_16 = _mediaBLEKeyReport[1] | (_mediaBLEKeyReport[0] << 8);
mediaBLEKeyReport_16 &= ~k_16;
_mediaBLEKeyReport[0] = (uint8_t)((mediaBLEKeyReport_16 & 0xFF00) >> 8);
_mediaBLEKeyReport[1] = (uint8_t)(mediaBLEKeyReport_16 & 0x00FF);

sendReport(&_mediaKeyReport);
sendReport(&_mediaBLEKeyReport);
return 1;
}

void BleKeyboard::releaseAll(void) {
_keyReport.keys[0] = 0;
_keyReport.keys[1] = 0;
_keyReport.keys[2] = 0;
_keyReport.keys[3] = 0;
_keyReport.keys[4] = 0;
_keyReport.keys[5] = 0;
_keyReport.modifiers = 0;
_mediaKeyReport[0] = 0;
_mediaKeyReport[1] = 0;
sendReport(&_keyReport);
_BLEKeyReport.keys[0] = 0;
_BLEKeyReport.keys[1] = 0;
_BLEKeyReport.keys[2] = 0;
_BLEKeyReport.keys[3] = 0;
_BLEKeyReport.keys[4] = 0;
_BLEKeyReport.keys[5] = 0;
_BLEKeyReport.modifiers = 0;
_mediaBLEKeyReport[0] = 0;
_mediaBLEKeyReport[1] = 0;
sendReport(&_BLEKeyReport);
}

size_t BleKeyboard::write(uint8_t c) {
Expand All @@ -481,7 +481,7 @@ size_t BleKeyboard::write(uint8_t c) {
// always returns 1
}

size_t BleKeyboard::write(const MediaKeyReport c) {
size_t BleKeyboard::write(const MediaBLEKeyReport c) {
uint16_t p = press(c); // Keydown
release(c); // Keyup
return p; // just return the result of press() since release() almost
Expand Down
52 changes: 26 additions & 26 deletions libdeps-mod/ESP32 BLE Keyboard/BleKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,24 @@ const uint8_t KEY_NUM_PLUS = 0xDF;
const uint8_t KEY_NUM_ENTER = 0xE0;
const uint8_t KEY_NUM_PERIOD = 0xEB;

typedef uint8_t MediaKeyReport[2];

const MediaKeyReport KEY_MEDIA_NEXT_TRACK = {1, 0};
const MediaKeyReport KEY_MEDIA_PREVIOUS_TRACK = {2, 0};
const MediaKeyReport KEY_MEDIA_STOP = {4, 0};
const MediaKeyReport KEY_MEDIA_PLAY_PAUSE = {8, 0};
const MediaKeyReport KEY_MEDIA_MUTE = {16, 0};
const MediaKeyReport KEY_MEDIA_VOLUME_UP = {32, 0};
const MediaKeyReport KEY_MEDIA_VOLUME_DOWN = {64, 0};
const MediaKeyReport KEY_MEDIA_WWW_HOME = {128, 0};
const MediaKeyReport KEY_MEDIA_LOCAL_MACHINE_BROWSER = {0, 1}; // Opens "My Computer" on Windows
const MediaKeyReport KEY_MEDIA_CALCULATOR = {0, 2};
const MediaKeyReport KEY_MEDIA_WWW_BOOKMARKS = {0, 4};
const MediaKeyReport KEY_MEDIA_WWW_SEARCH = {0, 8};
const MediaKeyReport KEY_MEDIA_WWW_STOP = {0, 16};
const MediaKeyReport KEY_MEDIA_WWW_BACK = {0, 32};
const MediaKeyReport KEY_MEDIA_CONSUMER_CONTROL_CONFIGURATION = {0, 64}; // Media Selection
const MediaKeyReport KEY_MEDIA_EMAIL_READER = {0, 128};
typedef uint8_t MediaBLEKeyReport[2];

const MediaBLEKeyReport KEY_MEDIA_NEXT_TRACK = {1, 0};
const MediaBLEKeyReport KEY_MEDIA_PREVIOUS_TRACK = {2, 0};
const MediaBLEKeyReport KEY_MEDIA_STOP = {4, 0};
const MediaBLEKeyReport KEY_MEDIA_PLAY_PAUSE = {8, 0};
const MediaBLEKeyReport KEY_MEDIA_MUTE = {16, 0};
const MediaBLEKeyReport KEY_MEDIA_VOLUME_UP = {32, 0};
const MediaBLEKeyReport KEY_MEDIA_VOLUME_DOWN = {64, 0};
const MediaBLEKeyReport KEY_MEDIA_WWW_HOME = {128, 0};
const MediaBLEKeyReport KEY_MEDIA_LOCAL_MACHINE_BROWSER = {0, 1}; // Opens "My Computer" on Windows
const MediaBLEKeyReport KEY_MEDIA_CALCULATOR = {0, 2};
const MediaBLEKeyReport KEY_MEDIA_WWW_BOOKMARKS = {0, 4};
const MediaBLEKeyReport KEY_MEDIA_WWW_SEARCH = {0, 8};
const MediaBLEKeyReport KEY_MEDIA_WWW_STOP = {0, 16};
const MediaBLEKeyReport KEY_MEDIA_WWW_BACK = {0, 32};
const MediaBLEKeyReport KEY_MEDIA_CONSUMER_CONTROL_CONFIGURATION = {0, 64}; // Media Selection
const MediaBLEKeyReport KEY_MEDIA_EMAIL_READER = {0, 128};


// Low level key report: up to 6 keys and shift, ctrl etc at once
Expand All @@ -126,7 +126,7 @@ typedef struct
uint8_t modifiers;
uint8_t reserved;
uint8_t keys[6];
} KeyReport;
} BLEKeyReport;

class BleKeyboard : public Print, public BLEServerCallbacks, public BLECharacteristicCallbacks
{
Expand All @@ -137,8 +137,8 @@ class BleKeyboard : public Print, public BLEServerCallbacks, public BLECharacter
BLECharacteristic* inputMediaKeys;
BLEAdvertising* advertising;
std::array<std::string, 2> devicesAddress;
KeyReport _keyReport;
MediaKeyReport _mediaKeyReport;
BLEKeyReport _BLEKeyReport;
MediaBLEKeyReport _mediaBLEKeyReport;
std::string deviceName;
std::string deviceManufacturer;
uint8_t batteryLevel;
Expand All @@ -161,14 +161,14 @@ class BleKeyboard : public Print, public BLEServerCallbacks, public BLECharacter
void end(void);
size_t getCounnectedCount(void);
std::array<std::string, 2> getDevicesAddress(void);
void sendReport(KeyReport* keys);
void sendReport(MediaKeyReport* keys);
void sendReport(BLEKeyReport* keys);
void sendReport(MediaBLEKeyReport* keys);
size_t press(uint8_t k);
size_t press(const MediaKeyReport k);
size_t press(const MediaBLEKeyReport k);
size_t release(uint8_t k);
size_t release(const MediaKeyReport k);
size_t release(const MediaBLEKeyReport k);
size_t write(uint8_t c);
size_t write(const MediaKeyReport c);
size_t write(const MediaBLEKeyReport c);
size_t write(const uint8_t* buffer, size_t size);
void releaseAll(void);
bool isConnected(void);
Expand Down
12 changes: 7 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:tinypico]
[env:esp32-s3-wroom-1-n4r2]
platform = espressif32
board = tinypico
board = esp32-s3-wroom-1-n4r2
framework = arduino
lib_deps =
NimBLE-Arduino
t-vk/ESP32 BLE Keyboard@^0.3.0
t-vk/ESP32 BLE Keyboard@0.3.0
olikraus/U8g2@^2.28.8
tinypico/TinyPICO Helper Library@^1.4.0
bblanchon/ArduinoJson@^6.19.4
xreef/PCF8574 library@^2.3.4
jnthas/Improv WiFi Library@^0.0.1
madhephaestus/ESP32Encoder@^0.10.2
paulstoffregen/Encoder@^1.4.2
fastled/FastLED@^3.6.0
build_flags = -D USE_NIMBLE
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
board_build.partitions = no_ota.csv
Loading

0 comments on commit 8dc6fb7

Please sign in to comment.