|
14 | 14 |
|
15 | 15 | #include <pbdrv/bluetooth.h> |
16 | 16 | #include <pbdrv/led.h> |
| 17 | +#include <pbdrv/usb.h> |
17 | 18 |
|
18 | 19 | #include <pbio/button.h> |
19 | 20 | #include <pbio/busy_count.h> |
@@ -133,13 +134,25 @@ void pbsys_hmi_init(void) { |
133 | 134 | void pbsys_hmi_deinit(void) { |
134 | 135 | pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING); |
135 | 136 | pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED); |
| 137 | + pbsys_status_clear(PBIO_PYBRICKS_STATUS_USB_HOST_CONNECTED); |
136 | 138 |
|
137 | 139 | #if PBIO_CONFIG_LIGHT_MATRIX |
138 | 140 | pbio_busy_count_up(); |
139 | 141 | pbio_os_process_start(&boot_animation_process, boot_animation_process_boot_thread, (void *)false); |
140 | 142 | #endif |
141 | 143 | } |
142 | 144 |
|
| 145 | +/** |
| 146 | + * Tests if the BLE or USB connection has changed. |
| 147 | + * |
| 148 | + * @return @c true if the system status is different from the driver status. @c false if unchanged. |
| 149 | + */ |
| 150 | +static bool host_connection_changed(void) { |
| 151 | + return |
| 152 | + pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS) != pbsys_status_test(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED) || |
| 153 | + pbdrv_usb_connection_is_active() != pbsys_status_test(PBIO_PYBRICKS_STATUS_USB_HOST_CONNECTED); |
| 154 | +} |
| 155 | + |
143 | 156 | /** |
144 | 157 | * The HMI is a loop running the following steps: |
145 | 158 | * |
@@ -168,28 +181,36 @@ static pbio_error_t run_ui(pbio_os_state_t *state, pbio_os_timer_t *timer) { |
168 | 181 | light_matrix_show_idle_ui(100); |
169 | 182 | #endif |
170 | 183 |
|
171 | | - // Initialize Bluetooth depending on current state. |
172 | | - if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)) { |
173 | | - DEBUG_PRINT("Connected: yes\n"); |
| 184 | + // Update USB state. |
| 185 | + DEBUG_PRINT("USB Connected: "); |
| 186 | + if (pbdrv_usb_connection_is_active()) { |
| 187 | + DEBUG_PRINT("Yes.\n"); |
| 188 | + pbsys_status_set(PBIO_PYBRICKS_STATUS_USB_HOST_CONNECTED); |
| 189 | + } else { |
| 190 | + DEBUG_PRINT("No.\n"); |
| 191 | + pbsys_status_clear(PBIO_PYBRICKS_STATUS_USB_HOST_CONNECTED); |
| 192 | + } |
| 193 | + |
| 194 | + // Update BLE state. |
| 195 | + DEBUG_PRINT("BLE Connected: "); |
| 196 | + if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) { |
| 197 | + DEBUG_PRINT("Yes.\n"); |
174 | 198 | pbsys_status_set(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED); |
175 | | - pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING); |
176 | | - // No need to stop advertising since this is automatic. |
177 | 199 | } else { |
178 | | - // Not connected right now. |
179 | | - DEBUG_PRINT("Connected: No\n"); |
| 200 | + DEBUG_PRINT("No.\n"); |
180 | 201 | pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED); |
| 202 | + } |
181 | 203 |
|
182 | | - // Enable or disable advertising depending on user setting. |
183 | | - bool do_advertise = pbsys_storage_settings_bluetooth_enabled_get(); |
184 | | - DEBUG_PRINT("Advertising is configured to be: %s. \n", do_advertise ? "on" : "off"); |
185 | | - if (do_advertise) { |
186 | | - pbsys_status_set(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING); |
187 | | - } else { |
188 | | - pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING); |
189 | | - } |
190 | | - pbdrv_bluetooth_start_advertising(do_advertise); |
191 | | - PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_advertise_or_scan_command(&sub, NULL)); |
| 204 | + // Advertise if BLE enabled and there is no host connection. |
| 205 | + bool advertise = pbsys_storage_settings_bluetooth_enabled_get() && !pbsys_host_is_connected(); |
| 206 | + DEBUG_PRINT("BLE Advertising: %s. \n", advertise ? "on" : "off"); |
| 207 | + if (advertise) { |
| 208 | + pbsys_status_set(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING); |
| 209 | + } else { |
| 210 | + pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING); |
192 | 211 | } |
| 212 | + pbdrv_bluetooth_start_advertising(advertise); |
| 213 | + PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_advertise_or_scan_command(&sub, NULL)); |
193 | 214 |
|
194 | 215 | // Buttons could be pressed at the end of the user program, so wait for |
195 | 216 | // a release and then a new press, or until we have to exit early. |
@@ -221,24 +242,24 @@ static pbio_error_t run_ui(pbio_os_state_t *state, pbio_os_timer_t *timer) { |
221 | 242 | // Wait for button press, external program start, or connection change. |
222 | 243 | pbdrv_button_get_pressed() || |
223 | 244 | pbsys_main_program_start_is_requested() || |
224 | | - pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE) != pbsys_status_test(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED); |
| 245 | + host_connection_changed(); |
225 | 246 | })); |
226 | 247 |
|
227 | 248 | // Became connected or disconnected, so go back to handle it. |
228 | | - if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE) != pbsys_status_test(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED)) { |
| 249 | + if (host_connection_changed()) { |
229 | 250 | DEBUG_PRINT("Connection changed.\n"); |
230 | 251 | continue; |
231 | 252 | } |
232 | 253 |
|
233 | | - // External progran request takes precedence over buttons. |
| 254 | + // External program request takes precedence over buttons. |
234 | 255 | if (pbsys_main_program_start_is_requested()) { |
235 | 256 | DEBUG_PRINT("Start program from Pybricks Code.\n"); |
236 | 257 | break; |
237 | 258 | } |
238 | 259 |
|
239 | 260 | #if PBSYS_CONFIG_HMI_PUP_BLUETOOTH_BUTTON |
240 | | - // Toggle Bluetooth enable setting if Bluetooth button pressed. Only if disconnected. |
241 | | - if ((pbdrv_button_get_pressed() & PBSYS_CONFIG_HMI_PUP_BLUETOOTH_BUTTON) && !pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)) { |
| 261 | + // Toggle Bluetooth enable setting if Bluetooth button pressed. Only when disconnected. |
| 262 | + if ((pbdrv_button_get_pressed() & PBSYS_CONFIG_HMI_PUP_BLUETOOTH_BUTTON) && !pbsys_host_is_connected()) { |
242 | 263 | pbsys_storage_settings_bluetooth_enabled_set(!pbsys_storage_settings_bluetooth_enabled_get()); |
243 | 264 | DEBUG_PRINT("Toggling Bluetooth to: %s. \n", pbsys_storage_settings_bluetooth_enabled_get() ? "on" : "off"); |
244 | 265 | continue; |
|
0 commit comments