From 56beb6a63371eb9dce7aa7ac1bd34a74bdf0eaee Mon Sep 17 00:00:00 2001 From: fauxpark Date: Thu, 30 Sep 2021 19:51:34 +1000 Subject: [PATCH 1/3] Initial AdafruitBLE GATT Battery Service support --- drivers/bluetooth/adafruit_ble.cpp | 15 ++++++++++++++- drivers/bluetooth/adafruit_ble.h | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/bluetooth/adafruit_ble.cpp b/drivers/bluetooth/adafruit_ble.cpp index 3f2cc35734a..ebf185199b8 100644 --- a/drivers/bluetooth/adafruit_ble.cpp +++ b/drivers/bluetooth/adafruit_ble.cpp @@ -443,6 +443,8 @@ bool adafruit_ble_enable_keyboard(void) { static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" STR(PRODUCT); // Turn on keyboard support static const char kHidEnOn[] PROGMEM = "AT+BLEHIDEN=1"; + // Turn on battery service + static const char kBattEnOn[] PROGMEM = "AT+BLEBATTEN=1"; // Adjust intervals to improve latency. This causes the "central" // system (computer/tablet) to poll us every 10-30 ms. We can't @@ -457,7 +459,7 @@ bool adafruit_ble_enable_keyboard(void) { // Turn down the power level a bit static const char kPower[] PROGMEM = "AT+BLEPOWERLEVEL=-12"; static PGM_P const configure_commands[] PROGMEM = { - kEcho, kGapIntervals, kGapDevName, kHidEnOn, kPower, kATZ, + kEcho, kGapIntervals, kGapDevName, kHidEnOn, kBattEnOn, kPower, kATZ, }; uint8_t i; @@ -556,6 +558,7 @@ void adafruit_ble_task(void) { state.last_battery_update = timer_read(); state.vbat = analogReadPin(BATTERY_LEVEL_PIN); + adafruit_ble_set_battery_level(100); // ??? } #endif } @@ -699,3 +702,13 @@ bool adafruit_ble_set_power_level(int8_t level) { snprintf(cmd, sizeof(cmd), "AT+BLEPOWERLEVEL=%d", level); return at_command(cmd, NULL, 0, false); } + +bool adafruit_ble_set_battery_level(uint8_t level) { + char cmd[18]; + if(!state.configured) { + return false; + } + + snprintf(cmd, sizeof(cmd), "AT+BLEBATTVAL=%d", level); + return at_command(cmd, NULL, 0, false); +} diff --git a/drivers/bluetooth/adafruit_ble.h b/drivers/bluetooth/adafruit_ble.h index b43e0771d99..8775e8982db 100644 --- a/drivers/bluetooth/adafruit_ble.h +++ b/drivers/bluetooth/adafruit_ble.h @@ -53,6 +53,7 @@ extern uint32_t adafruit_ble_read_battery_voltage(void); extern bool adafruit_ble_set_mode_leds(bool on); extern bool adafruit_ble_set_power_level(int8_t level); +extern bool adafruit_ble_set_battery_level(uint8_t level); #ifdef __cplusplus } From 0749aa229059ac88b67c66c5498d85330a478a10 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Thu, 7 Oct 2021 02:39:38 +1100 Subject: [PATCH 2/3] Formatting --- drivers/bluetooth/adafruit_ble.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/bluetooth/adafruit_ble.cpp b/drivers/bluetooth/adafruit_ble.cpp index ebf185199b8..bfb3ed5adec 100644 --- a/drivers/bluetooth/adafruit_ble.cpp +++ b/drivers/bluetooth/adafruit_ble.cpp @@ -558,7 +558,7 @@ void adafruit_ble_task(void) { state.last_battery_update = timer_read(); state.vbat = analogReadPin(BATTERY_LEVEL_PIN); - adafruit_ble_set_battery_level(100); // ??? + adafruit_ble_set_battery_level(100); // ??? } #endif } @@ -705,7 +705,7 @@ bool adafruit_ble_set_power_level(int8_t level) { bool adafruit_ble_set_battery_level(uint8_t level) { char cmd[18]; - if(!state.configured) { + if (!state.configured) { return false; } From b6cdbaa45d050b5e1a11dc9f23d69054faa0a7b7 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sun, 27 Aug 2023 17:41:21 +1000 Subject: [PATCH 3/3] Format and tweak todo comment --- drivers/bluetooth/bluefruit_le.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/bluetooth/bluefruit_le.cpp b/drivers/bluetooth/bluefruit_le.cpp index f3078003cba..5a1b8ff0242 100644 --- a/drivers/bluetooth/bluefruit_le.cpp +++ b/drivers/bluetooth/bluefruit_le.cpp @@ -557,7 +557,7 @@ void bluefruit_le_task(void) { state.last_battery_update = timer_read(); state.vbat = analogReadPin(BATTERY_LEVEL_PIN); - bluefruit_le_set_battery_level(100); // ??? + bluefruit_le_set_battery_level(100); // TODO } #endif }