Skip to content

Commit de407e3

Browse files
authored
Merge branch 'pybricks:master' into ev3-bluetooth-scan
2 parents 5bb845e + a17c1d2 commit de407e3

127 files changed

Lines changed: 1664 additions & 3469 deletions

File tree

Some content is hidden

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

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Added
1010
- Added preliminary BTstack support for EV3 ([pybricks-micropython#405]).
11+
- Enabled NXT Light Sensor, NXT Touch Sensor, NXT Sound Sensor on NXT.
12+
- Added `host_connected_usb` to `hub.system.info()` dictionary.
1113

1214
### Changed
1315
- Make 3D orientation default for heading and drivebase heading control ([support#1962]).
@@ -16,15 +18,19 @@
1618
more similar. User code should measure HSV values again in old code ([pybricks-micropython#421]).
1719

1820
### Fixed
19-
- Fixed `race=False` ignored in `pybricks.tools.multitask()`. ([support#2468])
21+
- Fixed `race=False` ignored in `pybricks.tools.multitask()` ([support#2468]).
2022
- Fixed Essential Hub button being disabled after power off so it could not be
2123
switched on again.
2224
- Fixed `TechnicHub` class being unavailable on Technic Hub in a recent
2325
pre-release ([pybricks-micropython#425]).
24-
26+
- Fixed internal rounding error that could could cause a Drive Base to be 1 mm
27+
off after driving 3 meters, depending on configuration parameters ([support#2500]).
28+
- Fixed Powered Up remote light getting the wrong color ([support#2497]).
2529

2630
[support#1962]: https://github.com/pybricks/support/issues/1962
2731
[support#2468]: https://github.com/pybricks/support/issues/2468
32+
[support#2497]: https://github.com/pybricks/support/issues/2497
33+
[support#2500]: https://github.com/pybricks/support/issues/2500
2834
[pybricks-micropython#405]: https://github.com/pybricks/pybricks-micropython/pull/405
2935
[pybricks-micropython#421]: https://github.com/pybricks/pybricks-micropython/pull/421
3036
[pybricks-micropython#425]: https://github.com/pybricks/pybricks-micropython/pull/425

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ doc:
2828
clean-doc:
2929
@$(MAKE) -C lib/pbio/doc clean
3030

31-
all: movehub cityhub technichub primehub essentialhub virtualhub nxt ev3 doc
31+
all: movehub cityhub technichub primehub essentialhub virtualhub nxt ev3 buildhat doc
3232

33-
clean-all: clean-movehub clean-cityhub clean-technichub clean-primehub clean-essentialhub clean-virtualhub clean-nxt clean-ev3 clean-doc
33+
clean-all: clean-movehub clean-cityhub clean-technichub clean-primehub clean-essentialhub clean-virtualhub clean-nxt clean-ev3 clean-buildhat clean-doc
3434

3535
ev3: mpy-cross
3636
@$(MAKE) -C bricks/ev3
@@ -82,6 +82,12 @@ clean-virtualhub: clean-mpy-cross
8282
@$(MAKE) -C bricks/virtualhub clean BUILD=build-debug
8383
@$(MAKE) -C bricks/virtualhub clean BUILD=build-coverage
8484

85+
buildhat: mpy-cross
86+
@$(MAKE) -C bricks/buildhat
87+
88+
clean-buildhat: clean-mpy-cross
89+
@$(MAKE) -C bricks/buildhat clean
90+
8591
mpy-cross:
8692
@$(MAKE) -C micropython/mpy-cross CROSS_COMPILE=$(HOST_CROSS_COMPILE)
8793

bricks/_common/common.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ NXOS_SRC_C = $(addprefix lib/pbio/platform/nxt/nxos/,\
489489
drivers/_twi.c \
490490
drivers/_uart.c \
491491
drivers/aic.c \
492-
drivers/avr.c \
493492
drivers/bt.c \
494493
drivers/i2c_memory.c \
495494
drivers/i2c.c \

bricks/_common/qstrdefs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Q(pybricks.nxtdevices)
3939
Q(pybricks.parameters)
4040
#endif
4141

42-
#if PYBRICKS_PY_PUPDEVICES || PYBRICKS_PY_EV3_PUP_ALIAS
42+
#if PYBRICKS_PY_PUPDEVICES || PYBRICKS_PY_EV3_PUP_ALIAS || PYBRICKS_PY_NXT_PUP_ALIAS
4343
Q(pybricks.pupdevices)
4444
#endif
4545

bricks/_common/sources.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ PYBRICKS_PYBRICKS_SRC_C = $(addprefix pybricks/,\
104104

105105
PBIO_SRC_C = $(addprefix lib/pbio/,\
106106
drv/adc/adc_ev3.c \
107+
drv/adc/adc_nxt.c \
107108
drv/adc/adc_stm32_hal.c \
108109
drv/adc/adc_stm32f0.c \
109110
drv/battery/battery_adc.c \
@@ -146,6 +147,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
146147
drv/display/display_ev3.c \
147148
drv/display/display_virtual.c \
148149
drv/gpio/gpio_ev3.c \
150+
drv/gpio/gpio_nxt.c \
149151
drv/gpio/gpio_pico.c \
150152
drv/gpio/gpio_stm32f0.c \
151153
drv/gpio/gpio_stm32f4.c \
@@ -176,6 +178,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
176178
drv/reset/reset_stm32.c \
177179
drv/resistor_ladder/resistor_ladder.c \
178180
drv/rproc/rproc_ev3.c \
181+
drv/rproc/rproc_nxt.c \
179182
drv/sound/beep_sampled.c \
180183
drv/sound/sound_ev3.c \
181184
drv/sound/sound_nxt.c \
@@ -203,6 +206,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
203206
src/color/util.c \
204207
src/control_settings.c \
205208
src/control.c \
209+
src/debug.c \
206210
src/dcmotor.c \
207211
src/differentiator.c \
208212
src/drivebase.c \

bricks/nxt/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#define PYBRICKS_HUB_CLASS_NAME (MP_QSTR_NXTBrick)
99

1010
#define PYBRICKS_HUB_NXTBRICK (1)
11+
// On EV3, allow PUP imports for easy beta testing
12+
#define PYBRICKS_PY_NXT_PUP_ALIAS (1)
13+
#define PYBRICKS_HUB_CLASS_NAME_ALIAS (MP_QSTR_PrimeHub)
1114

1215
// Pybricks modules
1316
#define PYBRICKS_PY_COMMON (1)

bricks/virtualhub/mpconfigport.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
// Pybricks modules
1313
#define PYBRICKS_PY_COMMON (1)
14-
#define PYBRICKS_PY_COMMON_BLE (0)
14+
#define PYBRICKS_PY_COMMON_BLE (1)
1515
#define PYBRICKS_PY_COMMON_CHARGER (1)
1616
#define PYBRICKS_PY_COMMON_COLOR_LIGHT (1)
1717
#define PYBRICKS_PY_COMMON_CONTROL (1)
@@ -29,14 +29,22 @@
2929
#define PYBRICKS_PY_EV3DEVICES (0)
3030
#define PYBRICKS_PY_EXPERIMENTAL (1)
3131
#define PYBRICKS_PY_HUBS (1)
32-
#define PYBRICKS_PY_IODEVICES (0)
32+
#define PYBRICKS_PY_IODEVICES (1)
33+
#define PYBRICKS_PY_IODEVICES_ANALOG_SENSOR (0)
34+
#define PYBRICKS_PY_IODEVICES_DC_MOTOR (0)
35+
#define PYBRICKS_PY_IODEVICES_I2C_DEVICE (0)
36+
#define PYBRICKS_PY_IODEVICES_LUMP_DEVICE (0)
37+
#define PYBRICKS_PY_IODEVICES_LWP3_DEVICE (1)
38+
#define PYBRICKS_PY_IODEVICES_PUP_DEVICE (0)
39+
#define PYBRICKS_PY_IODEVICES_UART_DEVICE (0)
40+
#define PYBRICKS_PY_IODEVICES_XBOX_CONTROLLER (1)
3341
#define PYBRICKS_PY_NXTDEVICES (0)
3442
#define PYBRICKS_PY_PARAMETERS (1)
3543
#define PYBRICKS_PY_PARAMETERS_BUTTON (1)
3644
#define PYBRICKS_PY_PARAMETERS_ICON (0)
3745
#define PYBRICKS_PY_PARAMETERS_IMAGE (1)
3846
#define PYBRICKS_PY_PUPDEVICES (1)
39-
#define PYBRICKS_PY_PUPDEVICES_REMOTE (0)
47+
#define PYBRICKS_PY_PUPDEVICES_REMOTE (1)
4048
#define PYBRICKS_PY_DEVICES (1)
4149
#define PYBRICKS_PY_ROBOTICS (1)
4250
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE (0)

lib/pbio/drv/adc/adc_nxt.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2018-2023 The Pybricks Authors
3+
4+
#include <pbdrv/config.h>
5+
6+
#if PBDRV_CONFIG_ADC_NXT
7+
8+
#include <stdbool.h>
9+
#include <stdint.h>
10+
11+
#include <pbdrv/adc.h>
12+
#include <pbdrv/clock.h>
13+
14+
#include <pbio/error.h>
15+
#include <pbio/util.h>
16+
17+
#include "../rproc/rproc_nxt.h"
18+
19+
void pbdrv_adc_init(void) {
20+
}
21+
22+
pbio_error_t pbdrv_adc_await_new_samples(pbio_os_state_t *state, uint32_t *start_time_us, uint32_t future_us) {
23+
24+
PBIO_OS_ASYNC_BEGIN(state);
25+
*start_time_us = pbdrv_clock_get_ms();
26+
// REVISIT: Pass channel ID to this function so we can adjust the wait time
27+
// to the specific sample coming in. The AVR adc samples will be slower.
28+
PBIO_OS_AWAIT_UNTIL(state, pbio_util_time_has_passed(pbdrv_clock_get_ms(), *start_time_us + 7));
29+
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
30+
}
31+
32+
pbio_error_t pbdrv_adc_get_ch(uint8_t ch, uint16_t *value) {
33+
34+
if (ch < 4) {
35+
return pbdrv_rproc_nxt_get_sensor_adc(ch, value);
36+
}
37+
38+
// TODO: AT91SAM7S256 internal ADC.
39+
*value = 0;
40+
return PBIO_SUCCESS;
41+
}
42+
43+
#endif // PBDRV_CONFIG_ADC_NXT

lib/pbio/drv/battery/battery_nxt.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010
#include <stdbool.h>
1111
#include <stdio.h>
1212

13-
#include <nxos/drivers/avr.h>
14-
1513
#include <pbdrv/battery.h>
1614
#include <pbio/error.h>
1715

16+
#include "../rproc/rproc_nxt.h"
17+
1818
void pbdrv_battery_init(void) {
1919
}
2020

2121
pbio_error_t pbdrv_battery_get_voltage_now(uint16_t *value) {
22-
// REVISIT: Voltage appears off by a factor 2
23-
*value = nx_avr_get_battery_voltage();
22+
pbdrv_rproc_nxt_get_battery_info(value);
2423
return PBIO_SUCCESS;
2524
}
2625

@@ -30,7 +29,8 @@ pbio_error_t pbdrv_battery_get_current_now(uint16_t *value) {
3029
}
3130

3231
pbio_error_t pbdrv_battery_get_type(pbdrv_battery_type_t *value) {
33-
*value = nx_avr_battery_is_accu_pack() ? PBDRV_BATTERY_TYPE_LIION : PBDRV_BATTERY_TYPE_ALKALINE;
32+
uint16_t voltage;
33+
*value = pbdrv_rproc_nxt_get_battery_info(&voltage) ? PBDRV_BATTERY_TYPE_LIION : PBDRV_BATTERY_TYPE_ALKALINE;
3434
return PBIO_SUCCESS;
3535
}
3636

lib/pbio/drv/bluetooth/bluetooth.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#define DEBUG 0
2424

2525
#if DEBUG
26-
#include <pbdrv/../../drv/uart/uart_debug_first_port.h>
27-
#define DEBUG_PRINT pbdrv_uart_debug_printf
26+
#include <pbio/debug.h>
27+
#define DEBUG_PRINT pbio_debug
2828
#else
2929
#define DEBUG_PRINT(...)
3030
#endif
@@ -158,6 +158,10 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_periphe
158158

159159
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
160160

161+
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
162+
return PBIO_ERROR_INVALID_OP;
163+
}
164+
161165
// Can't connect if already connected or already busy.
162166
if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL) || peri->func) {
163167
return PBIO_ERROR_BUSY;
@@ -188,8 +192,12 @@ pbio_error_t pbdrv_bluetooth_peripheral_disconnect(void) {
188192
return PBIO_SUCCESS;
189193
}
190194

195+
// Clear notification handler to avoid receiving further data.
196+
if (peri->config) {
197+
peri->config->notification_handler = NULL;
198+
}
199+
191200
// Initialize operation for handling on the main thread.
192-
peri->config->notification_handler = NULL;
193201
peri->func = pbdrv_bluetooth_peripheral_disconnect_func;
194202
peri->err = PBIO_ERROR_AGAIN;
195203
pbio_os_request_poll();
@@ -296,6 +304,10 @@ pbdrv_bluetooth_advertising_state_t pbdrv_bluetooth_advertising_state;
296304

297305
pbio_error_t pbdrv_bluetooth_start_advertising(bool start) {
298306

307+
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
308+
return PBIO_ERROR_INVALID_OP;
309+
}
310+
299311
bool is_advertising = pbdrv_bluetooth_advertising_state == PBDRV_BLUETOOTH_ADVERTISING_STATE_ADVERTISING_PYBRICKS;
300312

301313
// Already in requested state. This makes it safe to call stop advertising
@@ -326,6 +338,10 @@ uint8_t pbdrv_bluetooth_broadcast_data_size;
326338

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

341+
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
342+
return PBIO_ERROR_INVALID_OP;
343+
}
344+
329345
if (advertising_or_scan_func) {
330346
return PBIO_ERROR_BUSY;
331347
}
@@ -370,6 +386,10 @@ pbdrv_bluetooth_start_observing_callback_t pbdrv_bluetooth_observe_callback;
370386

371387
pbio_error_t pbdrv_bluetooth_start_observing(pbdrv_bluetooth_start_observing_callback_t callback) {
372388

389+
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
390+
return PBIO_ERROR_INVALID_OP;
391+
}
392+
373393
if (advertising_or_scan_func) {
374394
return PBIO_ERROR_BUSY;
375395
}
@@ -589,14 +609,19 @@ pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_ti
589609

590610
void pbdrv_bluetooth_deinit(void) {
591611

612+
// If Bluetooth is not even initialized, nothing to do.
613+
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
614+
return;
615+
}
616+
592617
// Under normal operation ::pbdrv_bluetooth_close_user_tasks completes
593618
// normally and there should be no user activity at this point. If there
594619
// is, a task got stuck, so exit forcefully.
595620
pbio_os_state_t unused;
596621
if (pbdrv_bluetooth_await_advertise_or_scan_command(&unused, NULL) != PBIO_SUCCESS ||
597622
pbdrv_bluetooth_await_peripheral_command(&unused, NULL) != PBIO_SUCCESS) {
598623

599-
// Hard reset without waitng on completion of any process.
624+
// Hard reset without waiting on completion of any process.
600625
pbdrv_bluetooth_controller_reset_hard();
601626
return;
602627
}

0 commit comments

Comments
 (0)