Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
- Reduced user stack size to `12 KB` on SPIKE Prime Hub to make it the same as
SPIKE Essential Hub. This frees up some RAM for system resources, and we never
use this much in practice.
- Allow simultaneous USB and up to two Bluetooth connections to SPIKE Prime.
Press the Bluetooth button to allow a connection when already connected.

### Fixed
- Fixed `race=False` ignored in `pybricks.tools.multitask()` ([support#2468]).
Expand Down
34 changes: 23 additions & 11 deletions lib/pbio/drv/bluetooth/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,22 @@ void pbdrv_bluetooth_init(void) {
pbdrv_bluetooth_init_hci();
}

static pbio_util_void_callback_t pbdrv_bluetooth_host_connection_changed_callback;

void pbdrv_bluetooth_set_host_connection_changed_callback(pbio_util_void_callback_t callback) {
pbdrv_bluetooth_host_connection_changed_callback = callback;
}

void pbdrv_bluetooth_host_connection_changed(void) {
if (pbdrv_bluetooth_host_connection_changed_callback) {
pbdrv_bluetooth_host_connection_changed_callback();
}
}

pbio_error_t pbdrv_bluetooth_tx(const uint8_t *data, uint32_t *size) {

// make sure we have a Bluetooth connection
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
if (!pbdrv_bluetooth_host_is_connected()) {
return PBIO_ERROR_INVALID_OP;
}

Expand All @@ -100,15 +112,15 @@ pbio_error_t pbdrv_bluetooth_tx(const uint8_t *data, uint32_t *size) {
}

uint32_t pbdrv_bluetooth_tx_available(void) {
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
if (!pbdrv_bluetooth_host_is_connected()) {
return UINT32_MAX;
}

return lwrb_get_free(&stdout_ring_buf);
}

bool pbdrv_bluetooth_tx_is_idle(void) {
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
if (!pbdrv_bluetooth_host_is_connected()) {
return true;
}

Expand All @@ -118,7 +130,7 @@ bool pbdrv_bluetooth_tx_is_idle(void) {
pbio_error_t pbdrv_bluetooth_send_event_notification(pbio_os_state_t *state, pbio_pybricks_event_t event_type, const uint8_t *data, size_t size) {
PBIO_OS_ASYNC_BEGIN(state);

if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
if (!pbdrv_bluetooth_host_is_connected()) {
return PBIO_ERROR_INVALID_OP;
}

Expand Down Expand Up @@ -182,7 +194,7 @@ const char *pbdrv_bluetooth_peripheral_get_name(pbdrv_bluetooth_peripheral_t *pe

pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_peripheral_t *peri, pbdrv_bluetooth_peripheral_connect_config_t *config) {

if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
if (!pbdrv_bluetooth_hci_is_enabled()) {
return PBIO_ERROR_INVALID_OP;
}

Expand Down Expand Up @@ -320,7 +332,7 @@ pbdrv_bluetooth_advertising_state_t pbdrv_bluetooth_advertising_state;

pbio_error_t pbdrv_bluetooth_start_advertising(bool start) {

if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
if (!pbdrv_bluetooth_hci_is_enabled()) {
return PBIO_ERROR_INVALID_OP;
}

Expand Down Expand Up @@ -355,7 +367,7 @@ uint8_t pbdrv_bluetooth_broadcast_data_size;

pbio_error_t pbdrv_bluetooth_start_broadcasting(const uint8_t *data, size_t size) {

if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
if (!pbdrv_bluetooth_hci_is_enabled()) {
return PBIO_ERROR_INVALID_OP;
}

Expand Down Expand Up @@ -405,7 +417,7 @@ pbdrv_bluetooth_start_observing_callback_t pbdrv_bluetooth_observe_callback;

pbio_error_t pbdrv_bluetooth_start_observing(pbdrv_bluetooth_start_observing_callback_t callback) {

if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
if (!pbdrv_bluetooth_hci_is_enabled()) {
return PBIO_ERROR_INVALID_OP;
}

Expand Down Expand Up @@ -502,7 +514,7 @@ static pbdrv_bluetooth_classic_task_context_t pbdrv_bluetooth_classic_task_conte

pbio_error_t pbdrv_bluetooth_start_inquiry_scan(pbdrv_bluetooth_inquiry_result_t *results, uint32_t *results_count, uint32_t *results_count_max, uint32_t duration_ms) {

if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
if (!pbdrv_bluetooth_hci_is_enabled()) {
return PBIO_ERROR_INVALID_OP;
}

Expand Down Expand Up @@ -570,7 +582,7 @@ pbio_error_t pbdrv_bluetooth_process_thread(pbio_os_state_t *state, void *contex
pbio_error_t err;

// Shorthand notation accessible throughout.
bool can_send = pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS);
bool can_send = pbdrv_bluetooth_host_is_connected();

// For looping over peripherals.
static uint8_t peri_index;
Expand Down Expand Up @@ -712,7 +724,7 @@ pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_ti
void pbdrv_bluetooth_deinit(void) {

// If Bluetooth is not even initialized, nothing to do.
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
if (!pbdrv_bluetooth_hci_is_enabled()) {
return;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/pbio/drv/bluetooth/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic_func(pbio_os_state_

pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *state, const uint8_t *data, uint16_t size);

void pbdrv_bluetooth_host_connection_changed(void);

extern pbdrv_bluetooth_receive_handler_t pbdrv_bluetooth_receive_handler;

extern uint8_t pbdrv_bluetooth_broadcast_data[PBDRV_BLUETOOTH_MAX_ADV_SIZE];
Expand Down
Loading