Skip to content

Commit 2bc5f87

Browse files
committed
pbio/sys/hmi_lcd: Update state on connection change.
This uses the callbacks introduced in 0b7eec0, which had not yet been applied to EV3. Fixes pybricks/support#2567
1 parent a2c0e23 commit 2bc5f87

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

lib/pbio/sys/hmi_lcd.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include <stdint.h>
1414
#include <string.h>
1515

16-
#include <pbdrv/bluetooth.h>
1716
#include <pbdrv/display.h>
17+
#include <pbdrv/usb.h>
1818

1919
#include <pbio/button.h>
2020
#include <pbio/os.h>
@@ -105,10 +105,35 @@ static void hmi_lcd_grid_show_pixel(uint8_t row, uint8_t col, bool on) {
105105
}
106106
#endif
107107

108+
static void pbsys_hmi_host_update_indications(void) {
109+
if (pbdrv_usb_connection_is_active()) {
110+
pbsys_status_set(PBIO_PYBRICKS_STATUS_USB_HOST_CONNECTED);
111+
} else {
112+
pbsys_status_clear(PBIO_PYBRICKS_STATUS_USB_HOST_CONNECTED);
113+
}
114+
}
115+
116+
static bool pbsys_hmi_handle_connection_change;
117+
118+
/**
119+
* Called from the USB and Bluetooth driver if a host connection state changes.
120+
*/
121+
static void pbsys_hmi_connection_changed_callback(void) {
122+
DEBUG_PRINT("A host connected or disconnected.\n");
123+
pbsys_hmi_handle_connection_change = true;
124+
pbsys_hmi_host_update_indications();
125+
}
126+
127+
108128
void pbsys_hmi_init(void) {
129+
pbdrv_usb_set_host_connection_changed_callback(pbsys_hmi_connection_changed_callback);
109130
}
110131

111132
void pbsys_hmi_deinit(void) {
133+
134+
pbdrv_usb_set_host_connection_changed_callback(NULL);
135+
pbsys_status_clear(PBIO_PYBRICKS_STATUS_USB_HOST_CONNECTED);
136+
112137
pbio_image_t *display = pbdrv_display_get_image();
113138
pbio_image_fill(display, 0);
114139
pbdrv_display_update();
@@ -137,6 +162,8 @@ static pbio_error_t run_ui(pbio_os_state_t *state, pbio_os_timer_t *timer) {
137162

138163
pbdrv_display_update();
139164

165+
pbsys_hmi_host_update_indications();
166+
140167
// Buttons could be pressed at the end of the user program, so wait for
141168
// a release and then a new press, or until we have to exit early.
142169
DEBUG_PRINT("Waiting for initial button release.\n");
@@ -165,9 +192,15 @@ static pbio_error_t run_ui(pbio_os_state_t *state, pbio_os_timer_t *timer) {
165192
}
166193

167194
// Wait for button press, external program start, or connection change.
168-
pbdrv_button_get_pressed() || pbsys_main_program_start_is_requested();
195+
pbdrv_button_get_pressed() || pbsys_main_program_start_is_requested() || pbsys_hmi_handle_connection_change;
169196
}));
170197

198+
// On setting or closing a connection, start from a clean slate.
199+
if (pbsys_hmi_handle_connection_change) {
200+
pbsys_hmi_handle_connection_change = false;
201+
continue;
202+
}
203+
171204
// External progran request takes precedence over buttons.
172205
if (pbsys_main_program_start_is_requested()) {
173206
DEBUG_PRINT("Start program from Pybricks Code.\n");

0 commit comments

Comments
 (0)