Skip to content

Commit 8587ea5

Browse files
committed
Merge branch 'contrib/github_pr_529' into 'master'
feat(keyboard): Add ble battery level report (GitHub PR) Closes AEGHB-1109 See merge request ae_group/esp-iot-solution!1338
2 parents f10c99c + afa5437 commit 8587ea5

File tree

10 files changed

+140
-9
lines changed

10 files changed

+140
-9
lines changed

examples/keyboard/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The ESP-KeyBoard project is a keyboard project using the ESP32-S3, supporting N-
88
* Supports low-power key scanning
99
* Supports USB 1K report rate
1010
* Supports BLE 125Hz report rate
11+
* Supports BLE battery level reporting
1112
* Supports local lighting control with over 40 types
1213
* Supports `Windows 11` dynamic lighting control
1314

@@ -78,6 +79,11 @@ Note: keyboard_rgb_martix comes from the **QMK** project. Due to the use of the
7879

7980
### Change LOG
8081

82+
* v0.3.0 - 2025-08-04
83+
84+
* Added BLE battery level reporting with ADC-based battery monitoring.
85+
* Improved battery capacity estimation using resistor voltage divider circuit.
86+
8187
* v0.2.2 - 2024-11-21
8288

8389
* Added support for waking up a PC from sleep mode (remote wakeup).

examples/keyboard/README_cn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ESP-KeyBoard 项目是使用 ESP32-S3 的键盘项目,支持全键无冲,USB
88
* 支持按键扫描低功耗
99
* 支持 USB 1K 回报率
1010
* 支持 BLE 125hz 回报率
11+
* 支持 BLE 电池电量报告
1112
* 支持本地灯效控制 40+ 种
1213
* 支持 `Windows 11` 动态灯效控制
1314

@@ -78,6 +79,11 @@ Note: keyboard_rgb_martix 来自 **QMK** 工程,由于使用 GPL 协议,如
7879

7980
#### Change LOG
8081

82+
* v0.3.0 - 2025-8-4
83+
84+
* 增加了基于 ADC 的 BLE 电池电量监控和报告功能
85+
* 改进了使用电阻分压电路的电池容量估算
86+
8187
* v0.2.2 - 2024-11-21
8288

8389
* 支持唤醒睡眠状态下的 PC (remote wakeup)

examples/keyboard/components/esp32_s3_kbd_kit/include/bsp/keyboard.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -49,7 +49,9 @@ extern "C" {
4949
#define KBD_WS2812_POWER_IO 1
5050

5151
/*!< Battery monitor GPIO */
52-
#define KBD_BATTERY_MONITOR_IO 2
52+
#define KBD_BATTERY_MONITOR_ADC_UNIT ADC_UNIT_1
53+
#define KBD_BATTERY_MONITOR_IO 2
54+
#define KBD_BATTERY_MONITOR_CHANNEL ADC_CHANNEL_1
5355

5456
#ifdef __cplusplus
5557
}

examples/keyboard/main/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
idf_component_register(SRCS "main.c" "../hid_device/usb_descriptors.c" "tinyusb_hid.c" "ble_hid.c" "esp_hid_gap.c" "btn_progress.c" "settings.c"
2-
INCLUDE_DIRS "../hid_device")
1+
idf_component_register(SRCS "main.c" "../hid_device/usb_descriptors.c" "tinyusb_hid.c" "ble_hid.c" "esp_hid_gap.c" "btn_progress.c" "settings.c" "battery_adc.c"
2+
INCLUDE_DIRS "../hid_device")
33

44
idf_component_get_property(tusb_lib espressif__tinyusb COMPONENT_LIB)
55

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <esp_log.h>
8+
#include <esp_adc/adc_oneshot.h>
9+
#include "ble_hid.h"
10+
#include "bsp/esp-bsp.h"
11+
#include "battery_adc.h"
12+
#include "adc_battery_estimation.h"
13+
14+
static const char *TAG = "BATTERY-ADC";
15+
16+
static TaskHandle_t battery_monitor_task_handle;
17+
static void battery_monitor_task(void *pvParameters);
18+
19+
/* Pull up resistor value. Unit: KOhm */
20+
#define PULL_UP_RESISTOR (51)
21+
/* Pull down resistor value. Unit: KOhm */
22+
#define PULL_DOWN_RESISTOR (100)
23+
24+
esp_err_t battery_adc_init(void)
25+
{
26+
if (battery_monitor_task_handle != NULL) {
27+
ESP_LOGW(TAG, "Battery monitor task already created! ");
28+
return ESP_OK;
29+
}
30+
31+
if (xTaskCreate(battery_monitor_task, "battery_monitor", 4096, NULL, 2, &battery_monitor_task_handle) != pdPASS) {
32+
return ESP_ERR_NO_MEM;
33+
}
34+
35+
return ESP_OK;
36+
}
37+
38+
static void battery_monitor_task(void *pvParameters)
39+
{
40+
ESP_UNUSED(pvParameters);
41+
adc_battery_estimation_t config = {
42+
.internal = {
43+
.adc_unit = KBD_BATTERY_MONITOR_ADC_UNIT,
44+
.adc_bitwidth = ADC_BITWIDTH_DEFAULT,
45+
/* Theoretical battery voltage measurement range is about 2.45 to 2.78V.
46+
* So set the input attenuation to 12 dB (about 4x) */
47+
.adc_atten = ADC_ATTEN_DB_12
48+
},
49+
.adc_channel = KBD_BATTERY_MONITOR_CHANNEL,
50+
.lower_resistor = PULL_DOWN_RESISTOR,
51+
.upper_resistor = PULL_UP_RESISTOR,
52+
};
53+
54+
adc_battery_estimation_handle_t adc_battery_estimation_handle = adc_battery_estimation_create(&config);
55+
if (adc_battery_estimation_handle == NULL) {
56+
ESP_LOGE(TAG, "Initialize adc battery estimation failed! ");
57+
vTaskDelete(battery_monitor_task_handle);
58+
vTaskDelay(portMAX_DELAY);
59+
}
60+
61+
float capacity = 0.0f;
62+
63+
while (1) {
64+
adc_battery_estimation_get_capacity(adc_battery_estimation_handle, &capacity);
65+
ble_hid_battery_report((int) capacity);
66+
67+
/* Sample period. */
68+
vTaskDelay(pdMS_TO_TICKS(10000));
69+
}
70+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#include "esp_err.h"
14+
15+
/**
16+
* @brief Initialize battery ADC.
17+
*
18+
* @return
19+
* - ESP_OK: Success
20+
* - ESP_ERR_NO_MEM: Out of memory.
21+
*/
22+
esp_err_t battery_adc_init(void);
23+
24+
#ifdef __cpluscpls
25+
}
26+
#endif

examples/keyboard/main/ble_hid.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -161,3 +161,13 @@ void ble_hid_keyboard_report(hid_report_t report)
161161
break;
162162
}
163163
}
164+
165+
void ble_hid_battery_report(int battery_level)
166+
{
167+
if (s_ble_hid_param.is_connected == false) {
168+
return;
169+
}
170+
171+
ESP_LOGI(TAG, "Report level: %d", battery_level);
172+
esp_hidd_dev_battery_set(s_ble_hid_param.hid_dev, (uint8_t)battery_level);
173+
}

examples/keyboard/main/ble_hid.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
1+
/*
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
23
*
34
* SPDX-License-Identifier: Apache-2.0
45
*/
@@ -36,6 +37,13 @@ esp_err_t ble_hid_deinit(void);
3637
*/
3738
void ble_hid_keyboard_report(hid_report_t report);
3839

40+
/**
41+
* @brief Send ble hid battery level
42+
*
43+
* @param battery_level
44+
*/
45+
void ble_hid_battery_report(int battery_level);
46+
3947
#ifdef __cplusplus
4048
}
4149
#endif
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
version: 0.2.2
1+
version: 0.3.0
22
dependencies:
3-
idf: ">=5.2"
3+
idf: '>=5.2'
44
espressif/tinyusb:
5-
version: ">=0.15.0~2"
5+
version: '>=0.15.0~2'
6+
espressif/adc_battery_estimation: =*

examples/keyboard/main/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "bsp/esp-bsp.h"
1111
#include "bsp/keycodes.h"
1212
#include "btn_progress.h"
13+
#include "battery_adc.h"
1314
#include "esp_log.h"
1415
#include "esp_pm.h"
1516
#include "keyboard_button.h"
@@ -115,4 +116,5 @@ void app_main(void)
115116
keyboard_button_register_cb(kbd_handle, cb_cfg, NULL);
116117

117118
xTaskCreate(light_progress_task, "light_progress_task", 4096, NULL, 5, &light_progress_task_handle);
119+
battery_adc_init();
118120
}

0 commit comments

Comments
 (0)