Skip to content

Commit dffda67

Browse files
committed
pbio/platform/nxt: Don't block until user connects RFCOMM.
Until now, pbio did not start until a Bluetooth classic connection was established. This makes development cumbersome since connecting is required every time. Now the connection is accepted in the background while pbio is running. This is expected to go away once USB is enabled.
1 parent 2b2ae66 commit dffda67

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

bricks/nxt/mphalport.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
4747
return ret;
4848
}
4949

50+
extern bool nx_bt_is_ready(void);
51+
5052
// Receive single character
5153
int mp_hal_stdin_rx_chr(void) {
5254

55+
while (!nx_bt_is_ready()) {
56+
MICROPY_EVENT_POLL_HOOK
57+
}
58+
5359
uint8_t rx_char;
5460

5561
// Start reading again for next char
@@ -66,6 +72,10 @@ int mp_hal_stdin_rx_chr(void) {
6672
// Send string of given length
6773
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
6874

75+
while (!nx_bt_is_ready()) {
76+
MICROPY_EVENT_POLL_HOOK
77+
}
78+
6979
// Nothing to do if disconnected or empty data
7080
if (!nx_bt_stream_opened() || len == 0) {
7181
return len;

lib/pbio/platform/nxt/platform.c

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <stdbool.h>
77
#include <stdint.h>
8+
#include <string.h>
89

910
#include <pbdrv/reset.h>
1011
#include <pbio/button.h>
@@ -28,16 +29,12 @@
2829
#include <nxos/drivers/systick.h>
2930
#include <nxos/interrupts.h>
3031

31-
// FIXME: Needs to use a process very similar to pbsys/bluetooth
32-
static bool bluetooth_connect(void) {
33-
34-
int port_handle = -1;
35-
int connection_handle = -1;
32+
const char *pin = "1234";
3633

34+
static void legacy_bluetooth_init_blocking(void) {
3735
nx_bt_init();
3836

3937
char *name = "Pybricks NXT";
40-
char *pin = "1234";
4138
nx_bt_set_friendly_name(name);
4239

4340
nx_display_string("Bluetooth name:\n");
@@ -56,39 +53,63 @@ static bool bluetooth_connect(void) {
5653

5754
nx_bt_set_discoverable(true);
5855

59-
port_handle = nx_bt_open_port();
60-
(void)port_handle;
56+
nx_bt_open_port();
57+
}
58+
59+
// REVISIT: This process waits for the user to connect to the NXT brick with
60+
// Bluetooth classic (RFCOMM). This allows basic I/O until proper Pybricks USB
61+
// or Bluetooth classic solutions are implemented. Then this process will be
62+
// removed.
63+
static pbio_os_process_t legacy_bluetooth_connect_process;
64+
65+
static pbio_error_t legacy_bluetooth_connect_process_thread(pbio_os_state_t *state, void *context) {
66+
67+
PBIO_OS_ASYNC_BEGIN(state);
68+
69+
static pbio_os_timer_t timer;
70+
71+
static int connection_handle = -1;
6172

6273
while (!nx_bt_stream_opened()) {
63-
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)) {
64-
return false;
65-
}
6674

6775
if (nx_bt_has_dev_waiting_for_pin()) {
68-
nx_bt_send_pin(pin);
76+
nx_bt_send_pin((char *)pin);
6977
nx_display_string("Please enter pin.\n");
7078
} else if (nx_bt_connection_pending()) {
7179
nx_display_string("Connecting ...\n");
7280
nx_bt_accept_connection(true);
7381

7482
while ((connection_handle = nx_bt_connection_established()) < 0) {
75-
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)) {
76-
return false;
77-
}
78-
79-
pbio_os_run_processes_once();
83+
PBIO_OS_AWAIT_MS(state, &timer, 2);
8084
}
8185

8286
nx_bt_stream_open(connection_handle);
8387
}
8488

85-
pbio_os_run_processes_once();
89+
PBIO_OS_AWAIT_MS(state, &timer, 100);
8690
}
8791

8892
nx_display_clear();
8993
nx_display_cursor_set_pos(0, 0);
9094

91-
return true;
95+
nx_display_string("RFCOMM ready.\n");
96+
nx_display_string("Press a key.\n");
97+
98+
// Receive one character to get going.
99+
static uint8_t flush_buf[1];
100+
nx_bt_stream_read(flush_buf, sizeof(flush_buf));
101+
102+
while (nx_bt_stream_data_read() != sizeof(flush_buf)) {
103+
PBIO_OS_AWAIT_MS(state, &timer, 2);
104+
}
105+
106+
nx_display_string("Let's code!\n");
107+
108+
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
109+
}
110+
111+
bool nx_bt_is_ready(void) {
112+
return legacy_bluetooth_connect_process.err == PBIO_SUCCESS;
92113
}
93114

94115
// Called from assembly code in startup.S
@@ -119,24 +140,9 @@ void SystemInit(void) {
119140
/* Delay a little post-init, to let all the drivers settle down. */
120141
nx_systick_wait_ms(100);
121142

122-
// REVISIT: Integrate via pbsys/bluetooth
123-
124-
// Accept incoming serial connection and get ready to read first byte.
125-
if (bluetooth_connect()) {
126-
// Receive one character to get going...
127-
uint8_t flush_buf[1];
128-
nx_display_string("Press a key.\n");
129-
nx_bt_stream_read(flush_buf, sizeof(flush_buf));
143+
// Blocking Bluetooth setup, then await user connection without blocking,
144+
// allowing pbio processes to start even if nothing is connected.
145+
legacy_bluetooth_init_blocking();
146+
pbio_os_process_start(&legacy_bluetooth_connect_process, legacy_bluetooth_connect_process_thread, NULL);
130147

131-
while (nx_bt_stream_data_read() != sizeof(flush_buf)) {
132-
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)) {
133-
goto out;
134-
}
135-
136-
pbio_os_run_processes_once();
137-
}
138-
139-
nx_display_string("Connected. REPL.\n");
140-
out:;
141-
}
142148
}

0 commit comments

Comments
 (0)