3434
3535bool is_connected = false;
3636QueueHandle_t input_queue = NULL ;
37-
37+ static uint8_t s_battery_level = 0 ;
3838const 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- uint8_t battery_level = 0 ;
8079 while (true) {
8180 DELAY (HID_LATENCY );
8281
@@ -121,8 +120,6 @@ void joystick_task()
121120 set_hid_report_values (x_axis , y_axis , button_in , hat_switch , throttle );
122121 print_user_input_report (x_axis , y_axis , hat_switch , button_in , throttle );
123122 ret = send_user_input ();
124- battery_level = (battery_level + 1 ) % 100 ;
125- set_hid_battery_level (battery_level );
126123 }
127124
128125 // Alternatively, to simply poll user input can do:
@@ -142,6 +139,18 @@ void joystick_task()
142139 }
143140}
144141
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+
145154/**
146155 * @brief Initialize bluetooth resources
147156 * @return ESP_OK on success; any other value indicates error
@@ -252,7 +261,10 @@ void app_main(void)
252261#else //CONFIG_JOYSTICK_INPUT_MODE_ADC
253262 xTaskCreate (joystick_ext_read , "ext_hardware_joystick" , 2048 , NULL , tskIDLE_PRIORITY , NULL );
254263#endif
255-
264+ TimerHandle_t battery_timer = xTimerCreate (NULL , pdMS_TO_TICKS (5000 ), true, NULL , battery_timer_cb );
265+ if (xTimerStart (battery_timer , 0 ) != pdPASS ) {
266+ ESP_LOGE (HID_DEMO_TAG , "Failed to start battery timer" );
267+ }
256268 // Main joystick task
257269 joystick_task ();
258270
0 commit comments