Skip to content

Commit 5b20c79

Browse files
committed
pbio/port: Rework ports as pbio module.
In the long run, we want ports to work not just on Powered UP but also EV3. We also want to allow direct user access to UART and I2C ports, so we need a mechanism to switch modes. If the 6-wire port is abstracted by pbio/drv, then all detection and data alhorithms can be shared and placed in pbio. Flattening all these abstractions also reduces code size and complexity despite adding quite a bit more functionality. We could disable unused functions on some ports to save even more space. ------------------------------------------------- Also squashes the following commits since it did not ultimately help simplify the diff as intended. pbio/drv/legodev_pup_uart: Rename files to lump. It gets rather confusing if we have uart_dev_t, uart_device_dev_t, and lego_uart_dev_t when they are all different things. Also, these are not restructed to pup. So rename to LEGO UART Messaging Protocol (lump) as we do internally in a few places. pbio/drv/legodev_pup_uart: Rename refs to lump. It gets rather confusing if we have uart_dev_t, uart_device_dev_t, and lego_uart_dev_t when they are all different things. Also, these are not restricted to pup. So rename to LEGO UART Messaging Protocol (lump) as we do internally in a few places.
1 parent aae52d0 commit 5b20c79

File tree

127 files changed

+4158
-4786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+4158
-4786
lines changed

.vscode/launch.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"request": "launch",
6060
"program": "${workspaceFolder}/bricks/virtualhub/build-debug/virtualhub-micropython",
6161
"args": [
62-
"${workspaceFolder}/tests/virtualhub/motor/drivebase.py"
62+
"${workspaceFolder}/tests/virtualhub/motor/test_1.py"
6363
],
6464
"stopAtEntry": false,
6565
"cwd": "${workspaceFolder}",
@@ -145,6 +145,10 @@
145145
"type": "pickString",
146146
// should match output of `./test-pbio.sh --list-tests`
147147
"options": [
148+
"src/port_lump/test_boost_color_distance_sensor",
149+
"src/port_lump/test_boost_interactive_motor",
150+
"src/port_lump/test_technic_large_motor",
151+
"src/port_lump/test_technic_xl_motor",
148152
"drv/bluetooth/test_btstack_run_loop_contiki_timer",
149153
"drv/bluetooth/test_btstack_run_loop_contiki_poll",
150154
"drv/pwm/test_pwm_get",
@@ -178,10 +182,6 @@
178182
"src/trajectory/test_simple_trajectory",
179183
"src/trajectory/test_infinite_trajectory",
180184
"src/trajectory/test_position_trajectory",
181-
"src/uartdev/test_boost_color_distance_sensor",
182-
"src/uartdev/test_boost_interactive_motor",
183-
"src/uartdev/test_technic_large_motor",
184-
"src/uartdev/test_technic_xl_motor",
185185
"src/util/test_uuid128_reverse_compare",
186186
"src/util/test_uuid128_reverse_copy",
187187
"src/util/test_oneshot",

bricks/_common/arm_none_eabi.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ OBJ += $(addprefix $(BUILD)/, $(PYBRICKS_PYBRICKS_SRC_C:.c=.o))
434434
OBJ += $(addprefix $(BUILD)/, $(CONTIKI_SRC_C:.c=.o))
435435
OBJ += $(addprefix $(BUILD)/, $(LWRB_SRC_C:.c=.o))
436436
OBJ += $(addprefix $(BUILD)/, $(PBIO_SRC_C:.c=.o))
437+
OBJ += $(addprefix $(BUILD)/, $(LEGO_SPEC_SRC_C:.c=.o))
437438
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
438439

439440
ifeq ($(PB_LIB_BLUENRG),1)

bricks/_common/sources.mk

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,15 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
149149
drv/gpio/gpio_stm32f0.c \
150150
drv/gpio/gpio_stm32f4.c \
151151
drv/gpio/gpio_stm32l4.c \
152+
drv/gpio/gpio_virtual.c \
152153
drv/imu/imu_lsm6ds3tr_c_stm32.c \
153-
drv/ioport/ioport_pup.c \
154+
drv/ioport/ioport.c \
154155
drv/led/led_array_pwm.c \
155156
drv/led/led_array.c \
156157
drv/led/led_core.c \
157158
drv/led/led_dual.c \
158159
drv/led/led_pwm.c \
159160
drv/led/led_virtual.c \
160-
drv/legodev/legodev_nxt.c \
161-
drv/legodev/legodev_pup.c \
162-
drv/legodev/legodev_pup_uart.c \
163-
drv/legodev/legodev_spec.c \
164-
drv/legodev/legodev_test.c \
165-
drv/legodev/legodev_virtual.c \
166161
drv/motor_driver/motor_driver_hbridge_pwm.c \
167162
drv/motor_driver/motor_driver_nxt.c \
168163
drv/motor_driver/motor_driver_virtual_cpython.c \
@@ -209,6 +204,9 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
209204
src/motor/servo_settings.c \
210205
src/observer.c \
211206
src/parent.c \
207+
src/port.c \
208+
src/port_dcm_pup.c \
209+
src/port_lump.c \
212210
src/protocol/nus.c \
213211
src/protocol/pybricks.c \
214212
src/servo.c \
@@ -221,7 +219,6 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
221219
sys/command.c \
222220
sys/core.c \
223221
sys/hmi.c \
224-
sys/io_ports.c \
225222
sys/light_matrix.c \
226223
sys/light.c \
227224
sys/main.c \
@@ -232,6 +229,12 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
232229
sys/supervisor.c \
233230
)
234231

232+
# LEGO specification library
233+
234+
LEGO_SPEC_SRC_C = $(addprefix lib/lego/,\
235+
device.c \
236+
)
237+
235238
# MicroPython math library
236239

237240
SRC_LIBM = $(addprefix lib/libm/,\

bricks/virtualhub/mp_port.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <contiki.h>
1616

1717
#include <pbio/main.h>
18-
#include <pbdrv/legodev.h>
1918
#include <pbsys/core.h>
2019
#include <pbsys/program_stop.h>
2120
#include <pbsys/status.h>
@@ -68,7 +67,7 @@ bool pbsys_main_stdin_event(uint8_t c) {
6867
// MICROPY_PORT_INIT_FUNC
6968
void pb_virtualhub_port_init(void) {
7069

71-
pbio_init();
70+
pbio_init(true);
7271

7372
pbsys_init();
7473

@@ -77,6 +76,10 @@ void pb_virtualhub_port_init(void) {
7776
}
7877

7978
pb_package_pybricks_init(true);
79+
80+
// HACK: Motor simulation needs some time after pbio init to register
81+
// one sample of position data.
82+
mp_hal_delay_ms(100);
8083
}
8184

8285
// MICROPY_PORT_DEINIT_FUNC

bricks/virtualhub/mpconfigvariant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define PYBRICKS_PY_EV3DEVICES (0)
2626
#define PYBRICKS_PY_EXPERIMENTAL (1)
2727
#define PYBRICKS_PY_HUBS (1)
28-
#define PYBRICKS_PY_IODEVICES (1)
28+
#define PYBRICKS_PY_IODEVICES (0)
2929
#define PYBRICKS_PY_MEDIA (1)
3030
#define PYBRICKS_PY_NXTDEVICES (0)
3131
#define PYBRICKS_PY_PARAMETERS (1)

lib/lego/device.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdint.h>
2+
#include <lego/device.h>
3+
4+
/**
5+
* Gets the minimum time needed before stale data is discarded.
6+
*
7+
* This is empirically determined based on sensor experiments.
8+
*
9+
* @param [in] id The device type ID.
10+
* @param [in] mode The device mode.
11+
* @return Required delay in milliseconds.
12+
*/
13+
uint32_t lego_device_stale_data_delay(lego_device_type_id_t id, uint8_t mode) {
14+
switch (id) {
15+
case LEGO_DEVICE_TYPE_ID_COLOR_DIST_SENSOR:
16+
return mode == LEGO_DEVICE_MODE_PUP_COLOR_DISTANCE_SENSOR__IR_TX ? 0 : 30;
17+
case LEGO_DEVICE_TYPE_ID_SPIKE_COLOR_SENSOR:
18+
return mode == LEGO_DEVICE_MODE_PUP_COLOR_SENSOR__LIGHT ? 0 : 30;
19+
case LEGO_DEVICE_TYPE_ID_SPIKE_ULTRASONIC_SENSOR:
20+
return mode == LEGO_DEVICE_MODE_PUP_ULTRASONIC_SENSOR__LIGHT ? 0 : 50;
21+
case LEGO_DEVICE_TYPE_ID_EV3_COLOR_SENSOR:
22+
return 30;
23+
case LEGO_DEVICE_TYPE_ID_EV3_IR_SENSOR:
24+
return 1100;
25+
case LEGO_DEVICE_TYPE_ID_NXT_LIGHT_SENSOR:
26+
return 20;
27+
case LEGO_DEVICE_TYPE_ID_NXT_SOUND_SENSOR:
28+
return 300;
29+
case LEGO_DEVICE_TYPE_ID_NXT_ENERGY_METER:
30+
return 200;
31+
default:
32+
// Default delay for other sensors and modes.
33+
return 0;
34+
}
35+
}
36+
37+
/**
38+
* Gets the minimum time needed for the device to handle written data.
39+
*
40+
* This is empirically determined based on sensor experiments.
41+
*
42+
* @param [in] id The device type ID.
43+
* @param [in] mode The device mode.
44+
* @return Required delay in milliseconds.
45+
*/
46+
uint32_t lego_device_data_set_delay(lego_device_type_id_t id, uint8_t mode) {
47+
// The Boost Color Distance Sensor requires a long delay or successive
48+
// writes are ignored.
49+
if (id == LEGO_DEVICE_TYPE_ID_COLOR_DIST_SENSOR && mode == LEGO_DEVICE_MODE_PUP_COLOR_DISTANCE_SENSOR__IR_TX) {
50+
return 250;
51+
}
52+
53+
// Default delay for setting data. In practice, this is the delay for setting
54+
// the light on the color sensor and ultrasonic sensor.
55+
return 10;
56+
}

0 commit comments

Comments
 (0)