Skip to content

Commit

Permalink
fix(ble): add ble hid change battery level api
Browse files Browse the repository at this point in the history
Close #385
  • Loading branch information
loop233 committed Dec 9, 2024
1 parent 64848f4 commit 5eaa5b6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
21 changes: 20 additions & 1 deletion examples/bluetooth/ble_remote_control/main/hidd.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ client_info_t client;
extern bool is_connected; // Need this here in the case of disconnection

// Characteristic Values
static const uint8_t val_batt_level = 0;
static uint8_t val_batt_level = 0;
// 0% battery level (assume battery not present, for user to modify if need)
static uint8_t val_bat_cccd[] = {0x01, 0x00};
// CCCD allows the Central Device to enable/disable notifications/indications
Expand Down Expand Up @@ -479,6 +479,7 @@ esp_err_t copy_attribute_handles(int instance_id, uint16_t *handles, int num_han
switch (instance_id) {
case INST_ID_BAT_SVC:
memcpy(handle_table.handles_bat_svc, handles, num_handle * sizeof(uint16_t));
client.bat_attr_handle = handles[IDX_BAT_LVL_VAL];
return ESP_OK;
case INST_ID_HID:
memcpy(handle_table.handles_hid_svc, handles, num_handle * sizeof(uint16_t));
Expand Down Expand Up @@ -558,3 +559,21 @@ esp_err_t send_user_input(void)
false // need_confirm
);
}

esp_err_t set_hid_battery_level(uint8_t value)
{
if (value > 100) {
value = 100;
}

val_batt_level = value;

return esp_ble_gatts_send_indicate(
client.gatt_if, // gatts_if
client.connection_id, // conn_id
client.bat_attr_handle, // attr_handle
1, // value_len
&val_batt_level, // value
false // need_confirm
);
}
11 changes: 10 additions & 1 deletion examples/bluetooth/ble_remote_control/main/hidd.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -140,6 +140,7 @@ typedef struct {
esp_gatt_if_t gatt_if;
uint16_t connection_id;
uint16_t attr_handle;
uint16_t bat_attr_handle;
} client_info_t;

/**
Expand All @@ -164,3 +165,11 @@ void set_hid_report_values(uint8_t joystick_x, uint8_t joystick_y, uint8_t butto
* @brief Send the HID Report to the client (notification)
*/
esp_err_t send_user_input(void);

/**
* @brief Set the hid battery level value to be sent
*
* @note Values to be set depends on the device specification
* @note The value is capped at 100
*/
esp_err_t set_hid_battery_level(uint8_t value);
4 changes: 3 additions & 1 deletion examples/bluetooth/ble_remote_control/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void joystick_task()
uint8_t hat_switch = 0; // unused in this example
uint8_t button_in = 0;
uint8_t throttle = 0; // unused in this example

uint8_t battery_level = 0;
while (true) {
DELAY(HID_LATENCY);

Expand Down Expand Up @@ -121,6 +121,8 @@ void joystick_task()
set_hid_report_values(x_axis, y_axis, button_in, hat_switch, throttle);
print_user_input_report(x_axis, y_axis, hat_switch, button_in, throttle);
ret = send_user_input();
battery_level = (battery_level + 1) % 100;
set_hid_battery_level(battery_level);
}

// Alternatively, to simply poll user input can do:
Expand Down

0 comments on commit 5eaa5b6

Please sign in to comment.