Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Bluetooth light UI. Use SPIKE Prime Bluetooth Light. #261

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
the hub ([pybricks-micropython#250]).
- Improved font for the digits ``0--9`` when displaying them
with `hub.display.char(str(x))` ([pybricks-micropython#253]).
- On SPIKE Prime Hub and Robot Inventor Hub, moved Bluetooth indications to
the Bluetooth light. Only warning lights will be shown on the main button
light. See ([support#1716]) and ([pybricks-micropython#261]).

### Fixed
- Fixed not able to connect to new Technic Move hub with `LWP3Device()`.
Expand All @@ -44,11 +47,13 @@
[pybricks-micropython#250]: https://github.com/pybricks/pybricks-micropython/pull/250
[pybricks-micropython#253]: https://github.com/pybricks/pybricks-micropython/pull/253
[pybricks-micropython#254]: https://github.com/pybricks/pybricks-micropython/pull/254
[pybricks-micropython#261]: https://github.com/pybricks/pybricks-micropython/pull/261
[support#1429]: https://github.com/pybricks/support/issues/1429
[support#1460]: https://github.com/pybricks/support/issues/1460
[support#1615]: https://github.com/pybricks/support/issues/1615
[support#1622]: https://github.com/pybricks/support/issues/1622
[support#1678]: https://github.com/pybricks/support/issues/1678
[support#1716]: https://github.com/pybricks/support/issues/1716

## [3.5.0] - 2024-04-11

Expand Down
8 changes: 7 additions & 1 deletion lib/pbio/include/pbio/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ typedef enum {
*/
PBIO_PYBRICKS_STATUS_BLE_ADVERTISING = 3,
/**
* Bluetooth Low Energy has low signal.
* Bluetooth Low Energy has low signal. Not implemented or used anywhere.
*
* @since Pybricks Profile v1.0.0
*/
Expand Down Expand Up @@ -323,6 +323,12 @@ typedef enum {
* @since Pybricks Profile v1.2.0
*/
PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST = 8,
/**
* Hub is connected to a host (like Pybricks Code) via BLE.
*
* @since Pybricks Profile v1.4.0
*/
PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED = 9,
/** Total number of indications. */
NUM_PBIO_PYBRICKS_STATUS,
} pbio_pybricks_status_t;
Expand Down
2 changes: 1 addition & 1 deletion lib/pbio/include/pbsys/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#if PBSYS_CONFIG_STATUS_LIGHT
#include <pbio/light.h>
extern pbio_color_light_t *pbsys_status_light;
extern pbio_color_light_t *pbsys_status_light_main;
#endif

#if PBSYS_CONFIG_HUB_LIGHT_MATRIX
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/platform/city_hub/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#define PBSYS_CONFIG_STATUS_LIGHT (1)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (1)
#define PBSYS_CONFIG_PROGRAM_STOP (1)
1 change: 1 addition & 0 deletions lib/pbio/platform/debug/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
#define PBSYS_CONFIG_STATUS_LIGHT (1)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (1)
#define PBSYS_CONFIG_PROGRAM_STOP (1)
1 change: 1 addition & 0 deletions lib/pbio/platform/essential_hub/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
#define PBSYS_CONFIG_STATUS_LIGHT (1)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (1)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (1)
#define PBSYS_CONFIG_PROGRAM_STOP (1)
1 change: 1 addition & 0 deletions lib/pbio/platform/ev3/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define PBSYS_CONFIG_STATUS_LIGHT (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (1)
#define PBSYS_CONFIG_USER_PROGRAM_AUTO_START (1)
#define PBSYS_CONFIG_PROGRAM_STOP (0)
1 change: 1 addition & 0 deletions lib/pbio/platform/ev3rt/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
#define PBSYS_CONFIG_STATUS_LIGHT (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (0)
#define PBSYS_CONFIG_PROGRAM_STOP (1)
1 change: 1 addition & 0 deletions lib/pbio/platform/move_hub/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#define PBSYS_CONFIG_STATUS_LIGHT (1)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (1)
#define PBSYS_CONFIG_PROGRAM_STOP (1)
1 change: 1 addition & 0 deletions lib/pbio/platform/nxt/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define PBSYS_CONFIG_STATUS_LIGHT (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (1)
#define PBSYS_CONFIG_USER_PROGRAM_AUTO_START (1)
#define PBSYS_CONFIG_PROGRAM_STOP (1)
1 change: 1 addition & 0 deletions lib/pbio/platform/prime_hub/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#define PBSYS_CONFIG_STATUS_LIGHT (1)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (1)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (1)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (0)
#define PBSYS_CONFIG_USER_PROGRAM (1)
#define PBSYS_CONFIG_PROGRAM_STOP (1)
1 change: 1 addition & 0 deletions lib/pbio/platform/technic_hub/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#define PBSYS_CONFIG_STATUS_LIGHT (1)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (1)
#define PBSYS_CONFIG_PROGRAM_STOP (1)
1 change: 1 addition & 0 deletions lib/pbio/platform/virtual_hub/pbsysconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
#define PBSYS_CONFIG_STATUS_LIGHT (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BATTERY (0)
#define PBSYS_CONFIG_STATUS_LIGHT_BLUETOOTH (0)
#define PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS (1)
#define PBSYS_CONFIG_USER_PROGRAM (0)
#define PBSYS_CONFIG_PROGRAM_STOP (0)
34 changes: 14 additions & 20 deletions lib/pbio/sys/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <pbsys/status.h>
#include <pbsys/storage.h>

#include "light.h"
#include "storage.h"

// REVISIT: this can be the negotiated MTU - 3 to allow for better throughput
Expand Down Expand Up @@ -291,34 +290,23 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {

PROCESS_BEGIN();

// Ensures Bluetooth preferences are loaded before we read them.
#if PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
PROCESS_WAIT_EVENT_UNTIL(pbsys_storage_settings_get_settings() != NULL);
#endif // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE

pbdrv_bluetooth_set_on_event(pbsys_bluetooth_process_poll);
pbdrv_bluetooth_set_receive_handler(handle_receive);

while (!pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {

// Show inactive status only if user requested Bluetooth as disabled to
// avoid always flashing red in between program runs when disconnected.
if (!pbsys_storage_settings_bluetooth_enabled()) {
pbsys_status_light_bluetooth_set_color(PBIO_COLOR_RED);
}
while (!pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)) {

// make sure the Bluetooth chip is in reset long enough to actually reset
etimer_set(&timer, 150);
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));

// Wait until Bluetooth enabled requested by user, but stop waiting on shutdown.
PROCESS_WAIT_UNTIL(pbsys_storage_settings_bluetooth_enabled() || pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN));
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {
// If storage is not yet loaded, this will wait for that too.
PROCESS_WAIT_UNTIL(pbsys_storage_settings_bluetooth_enabled() || pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST));
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)) {
break;
}

// Enable Bluetooth, and show on Bluetooth light if available.
pbsys_status_light_bluetooth_set_color(PBIO_COLOR_BLUE);
// Enable Bluetooth.
pbdrv_bluetooth_power_on(true);
PROCESS_WAIT_UNTIL(pbdrv_bluetooth_is_ready());

Expand All @@ -332,16 +320,19 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
PROCESS_WAIT_UNTIL(
pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)
|| !pbsys_storage_settings_bluetooth_enabled());

// Now change the state depending on which of the above was triggered.

// If connected, advertising stops automatically. Otherwise manually
// stop advertising (if the user code started using the button or we
// are shutting down or or BLE became disabled).
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)) {
if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)) {
pbsys_status_set(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
} else {
pbdrv_bluetooth_stop_advertising();
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED); // REVISIT: also via state...
}

// In all cases, clear the advertising flag to stop blinking.
Expand All @@ -354,7 +345,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
// Bluetooth loop below and go directly to the disable step below it.
while (pbsys_storage_settings_bluetooth_enabled()
&& pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
&& !pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {
&& !pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)) {

if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
// Since pbsys status events are broadcast to all processes, this
Expand Down Expand Up @@ -388,6 +379,9 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
PROCESS_WAIT_EVENT();
}

pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING);

reset_all();
PROCESS_WAIT_WHILE(pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING));

Expand Down
1 change: 0 additions & 1 deletion lib/pbio/sys/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ void pbsys_init(void) {

void pbsys_deinit(void) {

pbsys_status_light_bluetooth_deinit();
pbsys_storage_deinit();

uint32_t start = pbdrv_clock_get_ms();
Expand Down
Loading
Loading