Skip to content

Commit 5a5cccf

Browse files
committed
pbio/sys/host: Introduce layer for routing or duplicating BLE and USB.
MicroPython's HAL shouldn't have to know how this works. It would just want to call pbsys_host_tx for stdout. This way we can also have i/o without MicroPython. For now just map to the existing Bluetooth system functions. We haven't decided yet how this should work. This just moves the decision away from MicroPython's platform code.
1 parent 031aa95 commit 5a5cccf

File tree

21 files changed

+172
-80
lines changed

21 files changed

+172
-80
lines changed

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
233233
sys/command.c \
234234
sys/core.c \
235235
sys/hmi.c \
236+
sys/host.c \
236237
sys/light_matrix.c \
237238
sys/light.c \
238239
sys/main.c \

bricks/_common_stm32/mphalport.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <pbdrv/clock.h>
1212
#include <pbdrv/config.h>
1313
#include <pbio/main.h>
14-
#include <pbsys/bluetooth.h>
14+
#include <pbsys/host.h>
1515

1616
#include "py/runtime.h"
1717
#include "py/mphal.h"
@@ -43,7 +43,7 @@ void mp_hal_delay_ms(mp_uint_t Delay) {
4343
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
4444
uintptr_t ret = 0;
4545

46-
if ((poll_flags & MP_STREAM_POLL_RD) && pbsys_bluetooth_rx_get_available()) {
46+
if ((poll_flags & MP_STREAM_POLL_RD) && pbsys_host_rx_get_available()) {
4747
ret |= MP_STREAM_POLL_RD;
4848
}
4949

@@ -56,7 +56,7 @@ int mp_hal_stdin_rx_chr(void) {
5656
uint8_t c;
5757

5858
// wait for rx interrupt
59-
while (size = 1, pbsys_bluetooth_rx(&c, &size) != PBIO_SUCCESS) {
59+
while (size = 1, pbsys_host_rx(&c, &size) != PBIO_SUCCESS) {
6060
MICROPY_EVENT_POLL_HOOK
6161
}
6262

@@ -67,7 +67,7 @@ int mp_hal_stdin_rx_chr(void) {
6767
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
6868
while (len) {
6969
uint32_t size = len;
70-
pbio_error_t err = pbsys_bluetooth_tx((const uint8_t *)str, &size);
70+
pbio_error_t err = pbsys_host_tx((const uint8_t *)str, &size);
7171

7272
if (err == PBIO_SUCCESS) {
7373
str += size;
@@ -86,7 +86,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
8686
}
8787

8888
void mp_hal_stdout_tx_flush(void) {
89-
while (!pbsys_bluetooth_tx_is_idle()) {
89+
while (!pbsys_host_tx_is_idle()) {
9090
MICROPY_EVENT_POLL_HOOK
9191
}
9292
}

lib/pbio/include/pbsys/bluetooth.h

Lines changed: 0 additions & 57 deletions
This file was deleted.

lib/pbio/include/pbsys/host.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025 The Pybricks Authors
3+
4+
/**
5+
* @addtogroup SystemHost System: Host
6+
* @{
7+
*/
8+
9+
#ifndef _PBSYS_HOST_H_
10+
#define _PBSYS_HOST_H_
11+
12+
#include <stdbool.h>
13+
#include <stdint.h>
14+
15+
#include <pbio/error.h>
16+
#include <pbsys/config.h>
17+
18+
/**
19+
* Callback function to handle stdin events.
20+
* @param [in] c the character received
21+
* @return *true* if the character was handled and should not be placed
22+
* in the stdin buffer, otherwise *false*.
23+
*/
24+
typedef bool (*pbsys_host_stdin_event_callback_t)(uint8_t c);
25+
26+
#if PBSYS_CONFIG_HOST
27+
28+
void pbsys_host_init(void);
29+
void pbsys_host_rx_set_callback(pbsys_host_stdin_event_callback_t callback);
30+
void pbsys_host_rx_flush(void);
31+
uint32_t pbsys_host_rx_get_available(void);
32+
uint32_t pbsys_host_rx_get_free(void);
33+
void pbsys_host_rx_write(const uint8_t *data, uint32_t size);
34+
pbio_error_t pbsys_host_rx(uint8_t *data, uint32_t *size);
35+
pbio_error_t pbsys_host_tx(const uint8_t *data, uint32_t *size);
36+
bool pbsys_host_tx_is_idle(void);
37+
38+
#else // PBSYS_CONFIG_HOST
39+
40+
#define pbsys_host_init()
41+
#define pbsys_host_rx_set_callback(callback)
42+
#define pbsys_host_rx_flush()
43+
#define pbsys_host_rx_get_available() 0
44+
#define pbsys_host_rx_get_free() 0
45+
#define pbsys_host_rx_write(data, size)
46+
47+
static inline pbio_error_t pbsys_host_rx(uint8_t *data, uint32_t *size) {
48+
return PBIO_ERROR_NOT_SUPPORTED;
49+
}
50+
static inline pbio_error_t pbsys_host_tx(const uint8_t *data, uint32_t *size) {
51+
return PBIO_ERROR_NOT_SUPPORTED;
52+
}
53+
static inline bool pbsys_host_tx_is_idle(void) {
54+
return false;
55+
}
56+
57+
#endif // PBSYS_CONFIG_HOST
58+
59+
#endif // _PBSYS_HOST_H_
60+
61+
/** @} */

lib/pbio/platform/city_hub/pbsysconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define PBSYS_CONFIG_BLUETOOTH (1)
1313
#define PBSYS_CONFIG_HMI_NUM_SLOTS (0)
1414
#define PBSYS_CONFIG_HUB_LIGHT_MATRIX (0)
15+
#define PBSYS_CONFIG_HOST (1)
1516
#define PBSYS_CONFIG_MAIN (1)
1617
#define PBSYS_CONFIG_STORAGE (1)
1718
#define PBSYS_CONFIG_STORAGE_NUM_SLOTS (1)

lib/pbio/platform/essential_hub/pbsysconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define PBSYS_CONFIG_BLUETOOTH (1)
1111
#define PBSYS_CONFIG_HMI_NUM_SLOTS (0)
1212
#define PBSYS_CONFIG_HUB_LIGHT_MATRIX (0)
13+
#define PBSYS_CONFIG_HOST (1)
1314
#define PBSYS_CONFIG_MAIN (1)
1415
#define PBSYS_CONFIG_STORAGE (1)
1516
#define PBSYS_CONFIG_STORAGE_NUM_SLOTS (1)

lib/pbio/platform/ev3/pbsysconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define PBSYS_CONFIG_FEATURE_BUILTIN_USER_PROGRAM_IMU_CALIBRATION (0)
77
#define PBSYS_CONFIG_FEATURE_PROGRAM_FORMAT_MULTI_MPY_V6 (1)
88
#define PBSYS_CONFIG_FEATURE_PROGRAM_FORMAT_MULTI_MPY_V6_1_NATIVE (0)
9+
#define PBSYS_CONFIG_HOST (1)
910
#define PBSYS_CONFIG_MAIN (1)
1011
#define PBSYS_CONFIG_STORAGE (1)
1112
#define PBSYS_CONFIG_STORAGE_NUM_SLOTS (1)

lib/pbio/platform/move_hub/pbsysconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define PBSYS_CONFIG_BLUETOOTH (1)
1313
#define PBSYS_CONFIG_HMI_NUM_SLOTS (0)
1414
#define PBSYS_CONFIG_HUB_LIGHT_MATRIX (0)
15+
#define PBSYS_CONFIG_HOST (1)
1516
#define PBSYS_CONFIG_MAIN (1)
1617
#define PBSYS_CONFIG_STORAGE (1)
1718
#define PBSYS_CONFIG_STORAGE_NUM_SLOTS (1)

lib/pbio/platform/nxt/pbsysconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define PBSYS_CONFIG_FEATURE_BUILTIN_USER_PROGRAM_IMU_CALIBRATION (0)
77
#define PBSYS_CONFIG_FEATURE_PROGRAM_FORMAT_MULTI_MPY_V6 (1)
88
#define PBSYS_CONFIG_FEATURE_PROGRAM_FORMAT_MULTI_MPY_V6_1_NATIVE (0)
9+
#define PBSYS_CONFIG_HOST (1)
910
#define PBSYS_CONFIG_MAIN (1)
1011
#define PBSYS_CONFIG_STORAGE (1)
1112
#define PBSYS_CONFIG_STORAGE_NUM_SLOTS (1)

lib/pbio/platform/prime_hub/pbsysconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define PBSYS_CONFIG_BLUETOOTH_TOGGLE_BUTTON (512) // PBIO_BUTTON_RIGHT_UP, but enum value cannot be used here.
1313
#define PBSYS_CONFIG_HMI_NUM_SLOTS (0)
1414
#define PBSYS_CONFIG_HUB_LIGHT_MATRIX (1)
15+
#define PBSYS_CONFIG_HOST (1)
1516
#define PBSYS_CONFIG_MAIN (1)
1617
#define PBSYS_CONFIG_STORAGE (1)
1718
#define PBSYS_CONFIG_STORAGE_NUM_SLOTS (5)

0 commit comments

Comments
 (0)