Skip to content

feat(keyboard): Add ble battery level report (AEGHB-1109) #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -49,7 +49,9 @@ extern "C" {
#define KBD_WS2812_POWER_IO 1

/*!< Battery monitor GPIO */
#define KBD_BATTERY_MONITOR_IO 2
#define KBD_BATTERY_MONITOR_ADC_UNIT ADC_UNIT_1
#define KBD_BATTERY_MONITOR_IO 2
#define KBD_BATTERY_MONITOR_CHANNEL ADC_CHANNEL_1

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions examples/keyboard/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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"
INCLUDE_DIRS "../hid_device")
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"
INCLUDE_DIRS "../hid_device")

idf_component_get_property(tusb_lib espressif__tinyusb COMPONENT_LIB)

Expand Down
74 changes: 74 additions & 0 deletions examples/keyboard/main/battery_adc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <driver/adc.h>

#include "battery_adc.h"

#include "ble_hid.h"
#include <esp_log.h>
#include <esp_adc/adc_oneshot.h>

#include "bsp/esp-bsp.h"
#include "adc_battery_estimation.h"

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

static TaskHandle_t battery_monitor_task_handle;
static void battery_monitor_task(void *pvParameters);

/* Pull up resistor value. Unit: KOhm */
#define PULL_UP_RESISTOR (51)
/* Pull down resistor value. Unit: KOhm */
#define PULL_DOWN_RESISTOR (100)

esp_err_t battery_adc_init(void)
{
if (battery_monitor_task_handle != NULL) {
ESP_LOGW(TAG, "Battery monitor task already created! ");
return ESP_OK;
}

if (xTaskCreate(battery_monitor_task, "battery_monitor", 4096, NULL, 2, &battery_monitor_task_handle) != pdPASS) {
return ESP_ERR_NO_MEM;
}

return ESP_OK;
}

static void battery_monitor_task(void *pvParameters)
{
ESP_UNUSED(pvParameters);
adc_battery_estimation_t config = {
.internal = {
.adc_unit = KBD_BATTERY_MONITOR_ADC_UNIT,
.adc_bitwidth = ADC_BITWIDTH_DEFAULT,
/* Theoretical battery voltage measurement range is about 2.45 to 2.78V.
* So set the input attenuation to 12 dB (about 4x) */
.adc_atten = ADC_ATTEN_DB_12
},
.adc_channel = KBD_BATTERY_MONITOR_CHANNEL,
.lower_resistor = PULL_DOWN_RESISTOR,
.upper_resistor = PULL_UP_RESISTOR,
};

adc_battery_estimation_handle_t adc_battery_estimation_handle = adc_battery_estimation_create(&config);
if (adc_battery_estimation_handle == NULL) {
ESP_LOGE(TAG, "Initialize adc battery estimation failed! ");
vTaskDelete(battery_monitor_task_handle);
vTaskDelay(portMAX_DELAY);
}

float capacity = 0.0f;

while (1) {
adc_battery_estimation_get_capacity(adc_battery_estimation_handle, &capacity);
ble_hid_battery_report((int) capacity);

/* Sample period. */
vTaskDelay(pdMS_TO_TICKS(10000));
}
}
26 changes: 26 additions & 0 deletions examples/keyboard/main/battery_adc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include "esp_err.h"

/**
* @brief Initialize battery ADC.
*
* @return
* - ESP_OK: Success
* - ESP_ERR_NO_MEM: Out of memory.
*/
esp_err_t battery_adc_init(void);

#ifdef __cpluscpls
}
#endif
12 changes: 11 additions & 1 deletion examples/keyboard/main/ble_hid.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -161,3 +161,13 @@ void ble_hid_keyboard_report(hid_report_t report)
break;
}
}

void ble_hid_battery_report(int battery_level)
{
if (s_ble_hid_param.is_connected == false) {
return;
}

ESP_LOGI(TAG, "Report level: %d", battery_level);
esp_hidd_dev_battery_set(s_ble_hid_param.hid_dev, (uint8_t)battery_level);
}
10 changes: 9 additions & 1 deletion examples/keyboard/main/ble_hid.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
/*
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -36,6 +37,13 @@ esp_err_t ble_hid_deinit(void);
*/
void ble_hid_keyboard_report(hid_report_t report);

/**
* @brief Send ble hid battery level
*
* @param battery_level
*/
void ble_hid_battery_report(int battery_level);

#ifdef __cplusplus
}
#endif
5 changes: 3 additions & 2 deletions examples/keyboard/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
version: 0.2.2
dependencies:
idf: ">=5.2"
idf: '>=5.2'
espressif/tinyusb:
version: ">=0.15.0~2"
version: '>=0.15.0~2'
espressif/adc_battery_estimation: =*
2 changes: 2 additions & 0 deletions examples/keyboard/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "bsp/esp-bsp.h"
#include "bsp/keycodes.h"
#include "btn_progress.h"
#include "battery_adc.h"
#include "esp_log.h"
#include "esp_pm.h"
#include "keyboard_button.h"
Expand Down Expand Up @@ -115,4 +116,5 @@ void app_main(void)
keyboard_button_register_cb(kbd_handle, cb_cfg, NULL);

xTaskCreate(light_progress_task, "light_progress_task", 4096, NULL, 5, &light_progress_task_handle);
battery_adc_init();
}