|
13 | 13 | #include <stdint.h> |
14 | 14 | #include <string.h> |
15 | 15 |
|
16 | | -#include <pbdrv/bluetooth.h> |
17 | 16 | #include <pbdrv/display.h> |
| 17 | +#include <pbdrv/usb.h> |
18 | 18 |
|
19 | 19 | #include <pbio/button.h> |
20 | 20 | #include <pbio/os.h> |
@@ -105,10 +105,35 @@ static void hmi_lcd_grid_show_pixel(uint8_t row, uint8_t col, bool on) { |
105 | 105 | } |
106 | 106 | #endif |
107 | 107 |
|
| 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 | + |
108 | 128 | void pbsys_hmi_init(void) { |
| 129 | + pbdrv_usb_set_host_connection_changed_callback(pbsys_hmi_connection_changed_callback); |
109 | 130 | } |
110 | 131 |
|
111 | 132 | 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 | + |
112 | 137 | pbio_image_t *display = pbdrv_display_get_image(); |
113 | 138 | pbio_image_fill(display, 0); |
114 | 139 | pbdrv_display_update(); |
@@ -137,6 +162,8 @@ static pbio_error_t run_ui(pbio_os_state_t *state, pbio_os_timer_t *timer) { |
137 | 162 |
|
138 | 163 | pbdrv_display_update(); |
139 | 164 |
|
| 165 | + pbsys_hmi_host_update_indications(); |
| 166 | + |
140 | 167 | // Buttons could be pressed at the end of the user program, so wait for |
141 | 168 | // a release and then a new press, or until we have to exit early. |
142 | 169 | 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) { |
165 | 192 | } |
166 | 193 |
|
167 | 194 | // 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; |
169 | 196 | })); |
170 | 197 |
|
| 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 | + |
171 | 204 | // External progran request takes precedence over buttons. |
172 | 205 | if (pbsys_main_program_start_is_requested()) { |
173 | 206 | DEBUG_PRINT("Start program from Pybricks Code.\n"); |
|
0 commit comments