Skip to content

Commit 10d9c52

Browse files
committed
refactor(keyboard): use adc battery estimation
1 parent b3afa82 commit 10d9c52

File tree

3 files changed

+32
-62
lines changed

3 files changed

+32
-62
lines changed

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

Lines changed: 4 additions & 3 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,8 +49,9 @@ extern "C" {
4949
#define KBD_WS2812_POWER_IO 1
5050

5151
/*!< Battery monitor GPIO */
52-
#define KBD_BATTERY_MONITOR_IO 2
53-
#define KBD_BATTERY_MONITOR_CHANNEL ADC_CHANNEL_1
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
5455

5556
#ifdef __cplusplus
5657
}

examples/keyboard/main/battery_adc.c

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,29 @@
1313
#include <esp_adc/adc_oneshot.h>
1414

1515
#include "bsp/esp-bsp.h"
16+
#include "adc_battery_estimation.h"
1617

1718
int g_battery_percent;
1819

1920
static const char *TAG = "BATTERY-ADC";
2021

21-
static adc_oneshot_unit_handle_t adc1_handle;
22-
static TaskHandle_t battery_task_handle = NULL;
22+
static TaskHandle_t battery_monitor_task_handle;
2323

2424
static void battery_monitor_task(void *pvParameters);
2525

2626
/* Pull up resistor value. Unit: KOhm */
2727
#define PULL_UP_RESISTOR (51)
2828
/* Pull down resistor value. Unit: KOhm */
2929
#define PULL_DOWN_RESISTOR (100)
30-
/* Max voltage value. Unit: mV */
31-
#define MAX_REF_VOLTAGE (3469)
3230

3331
esp_err_t battery_adc_init(void)
3432
{
35-
if (battery_task_handle != NULL) {
36-
ESP_LOGW(TAG, "Battery ADC already initialized");
33+
if (battery_monitor_task_handle != NULL) {
34+
ESP_LOGW(TAG, "Battery monitor task already created! ");
3735
return ESP_OK;
3836
}
3937

40-
/* ADC1 Init */
41-
adc_oneshot_unit_init_cfg_t unit_init_cfg = {
42-
.unit_id = ADC_UNIT_1,
43-
.clk_src = ADC_RTC_CLK_SRC_DEFAULT,
44-
.ulp_mode = ADC_ULP_MODE_DISABLE
45-
};
46-
ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_init_cfg, &adc1_handle));
47-
48-
/* ADC1 Config */
49-
adc_oneshot_chan_cfg_t channel_config = {
50-
/* Theoretical battery voltage measurement range is about 2.45 to 2.78V.
51-
* So set the input attenuation to 12 dB (about 4x) */
52-
.atten = ADC_ATTEN_DB_12,
53-
.bitwidth = ADC_BITWIDTH_12
54-
};
55-
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, KBD_BATTERY_MONITOR_CHANNEL, &channel_config));
56-
57-
if (xTaskCreate(battery_monitor_task, "battery_monitor", 4096, NULL, 2, &battery_task_handle) != pdPASS) {
38+
if (xTaskCreate(battery_monitor_task, "battery_monitor", 4096, NULL, 2, &battery_monitor_task_handle) != pdPASS) {
5839
return ESP_ERR_NO_MEM;
5940
}
6041

@@ -64,44 +45,31 @@ esp_err_t battery_adc_init(void)
6445
static void battery_monitor_task(void *pvParameters)
6546
{
6647
ESP_UNUSED(pvParameters);
67-
/* The table of voltage convert percent. */
68-
const static int ocv_capacity_table[] = {
69-
3680, 3700, 3740, 3770, 3790, 3820, 3870, 3920, 3980, 4060, 4200
48+
adc_battery_estimation_t config = {
49+
.internal = {
50+
.adc_unit = KBD_BATTERY_MONITOR_ADC_UNIT,
51+
.adc_bitwidth = ADC_BITWIDTH_DEFAULT,
52+
/* Theoretical battery voltage measurement range is about 2.45 to 2.78V.
53+
* So set the input attenuation to 12 dB (about 4x) */
54+
.adc_atten = ADC_ATTEN_DB_12
55+
},
56+
.adc_channel = KBD_BATTERY_MONITOR_CHANNEL,
57+
.lower_resistor = PULL_DOWN_RESISTOR,
58+
.upper_resistor = PULL_UP_RESISTOR,
7059
};
7160

72-
int adc_raw_value;
73-
int adc_voltage;
74-
int battery_voltage;
61+
adc_battery_estimation_handle_t adc_battery_estimation_handle = adc_battery_estimation_create(&config);
62+
if (adc_battery_estimation_handle == NULL) {
63+
ESP_LOGE(TAG, "Initialize adc battery estimation failed! ");
64+
vTaskDelete(battery_monitor_task_handle);
65+
vTaskDelay(portMAX_DELAY);
66+
}
7567

76-
int battery_level;
68+
float capacity = 0.0f;
7769

7870
while (1) {
79-
adc_oneshot_read(adc1_handle, KBD_BATTERY_MONITOR_CHANNEL, &adc_raw_value);
80-
81-
/* Get the battery voltage. */
82-
adc_voltage = MAX_REF_VOLTAGE * adc_raw_value / 4095;
83-
battery_voltage = adc_voltage * (PULL_UP_RESISTOR + PULL_DOWN_RESISTOR) / PULL_DOWN_RESISTOR;
84-
85-
/* Get the percent. */
86-
for (battery_level = 0; battery_level < 11; ++battery_level) {
87-
if (battery_voltage <= ocv_capacity_table[battery_level]) {
88-
break;
89-
}
90-
}
91-
92-
if (battery_level == 0) {
93-
/* Low battery. */
94-
g_battery_percent = 0;
95-
} else if (battery_level >= 11) {
96-
/* Over voltage. */
97-
g_battery_percent = 100;
98-
} else {
99-
g_battery_percent = (battery_level - 1) * 10;
100-
g_battery_percent += ((battery_voltage - ocv_capacity_table[battery_level]) * 10 / (
101-
ocv_capacity_table[battery_level] - ocv_capacity_table[battery_level - 1]));
102-
}
103-
104-
ble_hid_battery_report(g_battery_percent);
71+
adc_battery_estimation_get_capacity(adc_battery_estimation_handle, &capacity);
72+
ble_hid_battery_report((int) capacity);
10573

10674
/* Sample period. */
10775
vTaskDelay(pdMS_TO_TICKS(10000));
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
version: 0.2.2
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: =*

0 commit comments

Comments
 (0)