Skip to content

Commit f5ca5b5

Browse files
committed
Merge branch 'fix/ble_hid_add_battery_level_set_api' into 'master'
fix(ble): add ble hid change battery level api Closes AEGHB-744 See merge request ae_group/esp-iot-solution!1164
2 parents 32ef7ed + 42e0564 commit f5ca5b5

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

examples/bluetooth/ble_remote_control/main/hidd.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ client_info_t client;
2323
extern bool is_connected; // Need this here in the case of disconnection
2424

2525
// Characteristic Values
26-
static const uint8_t val_batt_level = 0;
26+
static uint8_t val_batt_level = 0;
2727
// 0% battery level (assume battery not present, for user to modify if need)
2828
static uint8_t val_bat_cccd[] = {0x01, 0x00};
2929
// CCCD allows the Central Device to enable/disable notifications/indications
@@ -479,6 +479,7 @@ esp_err_t copy_attribute_handles(int instance_id, uint16_t *handles, int num_han
479479
switch (instance_id) {
480480
case INST_ID_BAT_SVC:
481481
memcpy(handle_table.handles_bat_svc, handles, num_handle * sizeof(uint16_t));
482+
client.bat_attr_handle = handles[IDX_BAT_LVL_VAL];
482483
return ESP_OK;
483484
case INST_ID_HID:
484485
memcpy(handle_table.handles_hid_svc, handles, num_handle * sizeof(uint16_t));
@@ -558,3 +559,21 @@ esp_err_t send_user_input(void)
558559
false // need_confirm
559560
);
560561
}
562+
563+
esp_err_t set_hid_battery_level(uint8_t value)
564+
{
565+
if (value > 100) {
566+
value = 100;
567+
}
568+
569+
val_batt_level = value;
570+
571+
return esp_ble_gatts_send_indicate(
572+
client.gatt_if, // gatts_if
573+
client.connection_id, // conn_id
574+
client.bat_attr_handle, // attr_handle
575+
1, // value_len
576+
&val_batt_level, // value
577+
false // need_confirm
578+
);
579+
}

examples/bluetooth/ble_remote_control/main/hidd.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -140,6 +140,7 @@ typedef struct {
140140
esp_gatt_if_t gatt_if;
141141
uint16_t connection_id;
142142
uint16_t attr_handle;
143+
uint16_t bat_attr_handle;
143144
} client_info_t;
144145

145146
/**
@@ -164,3 +165,11 @@ void set_hid_report_values(uint8_t joystick_x, uint8_t joystick_y, uint8_t butto
164165
* @brief Send the HID Report to the client (notification)
165166
*/
166167
esp_err_t send_user_input(void);
168+
169+
/**
170+
* @brief Set the hid battery level value to be sent
171+
*
172+
* @note Values to be set depends on the device specification
173+
* @note The value is capped at 100
174+
*/
175+
esp_err_t set_hid_battery_level(uint8_t value);

examples/bluetooth/ble_remote_control/main/main.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
bool is_connected = false;
3636
QueueHandle_t input_queue = NULL;
37-
37+
static uint8_t s_battery_level = 0;
3838
const char *DEVICE_NAME = "ESP32 Remote";
3939

4040
/**
@@ -76,7 +76,6 @@ void joystick_task()
7676
uint8_t hat_switch = 0; // unused in this example
7777
uint8_t button_in = 0;
7878
uint8_t throttle = 0; // unused in this example
79-
8079
while (true) {
8180
DELAY(HID_LATENCY);
8281

@@ -140,6 +139,18 @@ void joystick_task()
140139
}
141140
}
142141

142+
static void battery_timer_cb(TimerHandle_t xTimer)
143+
{
144+
s_battery_level = (s_battery_level + 1) % 100;
145+
ESP_LOGI(HID_DEMO_TAG, "Change battery level to %d", s_battery_level);
146+
if (is_connected) {
147+
esp_err_t ret = set_hid_battery_level(s_battery_level);
148+
if (ret != ESP_OK) {
149+
ESP_LOGE(HID_DEMO_TAG, "Failed to set battery level");
150+
}
151+
}
152+
}
153+
143154
/**
144155
* @brief Initialize bluetooth resources
145156
* @return ESP_OK on success; any other value indicates error
@@ -250,7 +261,11 @@ void app_main(void)
250261
#else //CONFIG_JOYSTICK_INPUT_MODE_ADC
251262
xTaskCreate(joystick_ext_read, "ext_hardware_joystick", 2048, NULL, tskIDLE_PRIORITY, NULL);
252263
#endif
253-
264+
// Create a timer to update battery level periodically (add 1 each time as an example)
265+
TimerHandle_t battery_timer = xTimerCreate(NULL, pdMS_TO_TICKS(5000), true, NULL, battery_timer_cb);
266+
if (xTimerStart(battery_timer, 0) != pdPASS) {
267+
ESP_LOGE(HID_DEMO_TAG, "Failed to start battery timer");
268+
}
254269
// Main joystick task
255270
joystick_task();
256271

0 commit comments

Comments
 (0)