Skip to content

Commit 45e2d46

Browse files
committed
pybricks.nxtdevices.UltrasonicSensor: Implement in C.
Given the firmware size limitations on NXT, using Python wasn't going to be scalable. With the recent changes to the I2CDevice implementation, we can use instances of it for sensor classes written in C. This reverts commit a37f46f756cd8453ff8f99193263ac68f9b5a057. This is reverted instead of dropped so we maintain the logic to import Python classes into the C module in git history for future reference.
1 parent 505f3ab commit 45e2d46

File tree

7 files changed

+69
-42
lines changed

7 files changed

+69
-42
lines changed

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ PYBRICKS_PYBRICKS_SRC_C = $(addprefix pybricks/,\
7070
nxtdevices/pb_type_nxtdevices_soundsensor.c \
7171
nxtdevices/pb_type_nxtdevices_temperaturesensor.c \
7272
nxtdevices/pb_type_nxtdevices_touchsensor.c \
73+
nxtdevices/pb_type_nxtdevices_ultrasonicsensor.c \
7374
parameters/pb_module_parameters.c \
7475
parameters/pb_type_axis.c \
7576
parameters/pb_type_button.c \

bricks/ev3/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ PBIO_PLATFORM = ev3
22
PB_MCU_FAMILY = TIAM1808
33

44
PB_LIB_UMM_MALLOC = 1
5-
PB_FROZEN_MODULES = 1
65

76
include ../_common/arm_none_eabi.mk

bricks/ev3/manifest.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
# Common modules
21
include("../_common/manifest.py")
3-
4-
freeze_as_mpy("../ev3/modules", "_i2c_sensors.py")

bricks/ev3/modules/_i2c_sensors.py

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

pybricks/nxtdevices/nxtdevices.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
extern const mp_obj_type_t pb_type_nxtdevices_TouchSensor;
1414
extern const mp_obj_type_t pb_type_nxtdevices_LightSensor;
1515
extern const mp_obj_type_t pb_type_nxtdevices_ColorSensor;
16+
extern const mp_obj_type_t pb_type_nxtdevices_UltrasonicSensor;
1617

1718
#if PYBRICKS_PY_EV3DEVDEVICES
1819

pybricks/nxtdevices/pb_module_nxtdevices.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
#if PYBRICKS_PY_NXTDEVICES
77

8-
#include "py/builtin.h"
9-
#include "py/runtime.h"
10-
118
#include <pybricks/common.h>
129
#include <pybricks/nxtdevices/nxtdevices.h>
1310

@@ -19,6 +16,7 @@ static const mp_rom_map_elem_t nxtdevices_globals_table[] = {
1916
{ MP_ROM_QSTR(MP_QSTR_TouchSensor), MP_ROM_PTR(&pb_type_nxtdevices_TouchSensor) },
2017
{ MP_ROM_QSTR(MP_QSTR_LightSensor), MP_ROM_PTR(&pb_type_nxtdevices_LightSensor) },
2118
{ MP_ROM_QSTR(MP_QSTR_ColorSensor), MP_ROM_PTR(&pb_type_nxtdevices_ColorSensor) },
19+
{ MP_ROM_QSTR(MP_QSTR_UltrasonicSensor), MP_ROM_PTR(&pb_type_nxtdevices_UltrasonicSensor) },
2220
#if PYBRICKS_PY_EV3DEVDEVICES
2321
{ MP_ROM_QSTR(MP_QSTR_EnergyMeter), MP_ROM_PTR(&pb_type_nxtdevices_EnergyMeter) },
2422
{ MP_ROM_QSTR(MP_QSTR_SoundSensor), MP_ROM_PTR(&pb_type_nxtdevices_SoundSensor) },
@@ -32,21 +30,8 @@ const mp_obj_module_t pb_module_nxtdevices = {
3230
.globals = (mp_obj_dict_t *)&pb_module_nxtdevices_globals,
3331
};
3432

35-
void pb_module_nxtdevices_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
36-
// Support only load.
37-
if (dest[0] != MP_OBJ_NULL) {
38-
return;
39-
}
40-
41-
// Load from the frozen _i2c_sensors module.
42-
const mp_obj_t import_args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__i2c_sensors) };
43-
mp_obj_t module = mp_builtin___import__(MP_ARRAY_SIZE(import_args), import_args);
44-
mp_load_method_maybe(module, attr, dest);
45-
}
46-
4733
#if !MICROPY_MODULE_BUILTIN_SUBPACKAGES
4834
MP_REGISTER_MODULE(MP_QSTR_pybricks_dot_nxtdevices, pb_module_nxtdevices);
4935
#endif
50-
MP_REGISTER_MODULE_DELEGATION(pb_module_nxtdevices, pb_module_nxtdevices_attr);
5136

5237
#endif // PYBRICKS_PY_NXTDEVICES
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2018-2025 The Pybricks Authors
3+
4+
#include "py/mpconfig.h"
5+
6+
#if PYBRICKS_PY_NXTDEVICES
7+
8+
#include "py/mphal.h"
9+
10+
#include <pbdrv/i2c.h>
11+
#include <pbio/port_interface.h>
12+
13+
#include <pybricks/common.h>
14+
#include <pybricks/iodevices/iodevices.h>
15+
#include <pybricks/parameters.h>
16+
17+
#include <pybricks/util_mp/pb_kwarg_helper.h>
18+
#include <pybricks/util_mp/pb_obj_helper.h>
19+
#include <pybricks/util_pb/pb_error.h>
20+
21+
// pybricks.nxtdevices.UltrasonicSensor class object
22+
typedef struct _nxtdevices_UltrasonicSensor_obj_t {
23+
mp_obj_base_t base;
24+
mp_obj_t *i2c_device_obj;
25+
} nxtdevices_UltrasonicSensor_obj_t;
26+
27+
// pybricks.nxtdevices.UltrasonicSensor.__init__
28+
static mp_obj_t nxtdevices_UltrasonicSensor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
29+
PB_PARSE_ARGS_CLASS(n_args, n_kw, args,
30+
PB_ARG_REQUIRED(port));
31+
32+
nxtdevices_UltrasonicSensor_obj_t *self = mp_obj_malloc(nxtdevices_UltrasonicSensor_obj_t, type);
33+
self->i2c_device_obj = pb_type_i2c_device_make_new(port_in, mp_obj_new_int(0x01), false, true, true);
34+
35+
// NXT Ultrasonic Sensor appears to need some time after initializing I2C pins before it can receive data.
36+
mp_hal_delay_ms(100);
37+
38+
return MP_OBJ_FROM_PTR(self);
39+
}
40+
41+
static mp_obj_t map_distance(const uint8_t *data, size_t len) {
42+
return mp_obj_new_int(data[0] * 10);
43+
}
44+
45+
// pybricks.nxtdevices.UltrasonicSensor.distance
46+
static mp_obj_t nxtdevices_UltrasonicSensor_distance(mp_obj_t self_in) {
47+
nxtdevices_UltrasonicSensor_obj_t *self = MP_OBJ_TO_PTR(self_in);
48+
const uint8_t write_data[] = { 0x42 };
49+
return pb_type_i2c_device_start_operation(self->i2c_device_obj, write_data, MP_ARRAY_SIZE(write_data), 1, map_distance);
50+
}
51+
static MP_DEFINE_CONST_FUN_OBJ_1(nxtdevices_UltrasonicSensor_distance_obj, nxtdevices_UltrasonicSensor_distance);
52+
53+
// dir(pybricks.nxtdevices.UltrasonicSensor)
54+
static const mp_rom_map_elem_t nxtdevices_UltrasonicSensor_locals_dict_table[] = {
55+
{ MP_ROM_QSTR(MP_QSTR_distance), MP_ROM_PTR(&nxtdevices_UltrasonicSensor_distance_obj) },
56+
};
57+
static MP_DEFINE_CONST_DICT(nxtdevices_UltrasonicSensor_locals_dict, nxtdevices_UltrasonicSensor_locals_dict_table);
58+
59+
// type(pybricks.nxtdevices.UltrasonicSensor)
60+
MP_DEFINE_CONST_OBJ_TYPE(pb_type_nxtdevices_UltrasonicSensor,
61+
MP_QSTR_UltrasonicSensor,
62+
MP_TYPE_FLAG_NONE,
63+
make_new, nxtdevices_UltrasonicSensor_make_new,
64+
locals_dict, &nxtdevices_UltrasonicSensor_locals_dict);
65+
66+
#endif // PYBRICKS_PY_NXTDEVICES

0 commit comments

Comments
 (0)