Skip to content

Commit b8f99e4

Browse files
committed
pybricks.ev3devices: Add UltrasonicSensor.
1 parent faaff26 commit b8f99e4

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

pybricks/ev3devices.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
extern const mp_obj_type_t pb_type_ev3devices_ColorSensor;
1414
extern const mp_obj_type_t pb_type_ev3devices_InfraredSensor;
1515
extern const mp_obj_type_t pb_type_ev3devices_TouchSensor;
16+
extern const mp_obj_type_t pb_type_ev3devices_UltrasonicSensor;
1617

1718
#endif // PYBRICKS_PY_EV3DEVICES
1819

1920
#if PYBRICKS_PY_EV3DEVDEVICES
2021

2122
extern const mp_obj_type_t pb_type_ev3devices_GyroSensor;
22-
extern const mp_obj_type_t pb_type_ev3devices_UltrasonicSensor;
2323

2424
#endif // PYBRICKS_PY_EV3DEVDEVICES
2525

pybricks/ev3devices/pb_module_ev3devices.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ static const mp_rom_map_elem_t ev3devices_globals_table[] = {
1616
{ MP_ROM_QSTR(MP_QSTR_ColorSensor), MP_ROM_PTR(&pb_type_ev3devices_ColorSensor) },
1717
{ MP_ROM_QSTR(MP_QSTR_InfraredSensor), MP_ROM_PTR(&pb_type_ev3devices_InfraredSensor) },
1818
{ MP_ROM_QSTR(MP_QSTR_TouchSensor), MP_ROM_PTR(&pb_type_ev3devices_TouchSensor) },
19-
#if PYBRICKS_PY_EV3DEVDEVICES
2019
{ MP_ROM_QSTR(MP_QSTR_UltrasonicSensor), MP_ROM_PTR(&pb_type_ev3devices_UltrasonicSensor)},
20+
#if PYBRICKS_PY_EV3DEVDEVICES
2121
{ MP_ROM_QSTR(MP_QSTR_GyroSensor), MP_ROM_PTR(&pb_type_ev3devices_GyroSensor) },
2222
#endif
2323
};

pybricks/ev3devices/pb_type_ev3devices_ultrasonicsensor.c

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

44
#include "py/mpconfig.h"
55

6-
#if PYBRICKS_PY_EV3DEVDEVICES
6+
#if PYBRICKS_PY_EV3DEVICES
77

88
#include <pybricks/common.h>
99
#include <pybricks/ev3devices.h>
@@ -29,25 +29,43 @@ static mp_obj_t ev3devices_UltrasonicSensor_make_new(const mp_obj_type_t *type,
2929
return MP_OBJ_FROM_PTR(self);
3030
}
3131

32+
// pybricks.ev3devices.UltrasonicSensor.distance(silent=True)
33+
static mp_obj_t ev3devices_UltrasonicSensor_distance_silent_true(mp_obj_t self_in) {
34+
int16_t *distance = pb_type_device_get_data(self_in, LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__SI_CM);
35+
return mp_obj_new_int(distance[0]);
36+
}
37+
static PB_DEFINE_CONST_TYPE_DEVICE_METHOD_OBJ(ev3devices_UltrasonicSensor_distance_silent_true_obj, LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__SI_CM, ev3devices_UltrasonicSensor_distance_silent_true);
38+
39+
// pybricks.ev3devices.UltrasonicSensor.distance(silent=False)
40+
static mp_obj_t ev3devices_UltrasonicSensor_distance_silent_false(mp_obj_t self_in) {
41+
int16_t *distance = pb_type_device_get_data(self_in, LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__DIST_CM);
42+
return mp_obj_new_int(distance[0]);
43+
}
44+
static PB_DEFINE_CONST_TYPE_DEVICE_METHOD_OBJ(ev3devices_UltrasonicSensor_distance_silent_false_obj, LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__DIST_CM, ev3devices_UltrasonicSensor_distance_silent_false);
45+
3246
// pybricks.ev3devices.UltrasonicSensor.distance
3347
static mp_obj_t ev3devices_UltrasonicSensor_distance(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
3448

3549
PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
3650
ev3devices_UltrasonicSensor_obj_t, self,
3751
PB_ARG_DEFAULT_FALSE(silent));
3852

39-
uint8_t mode = mp_obj_is_true(silent_in) ?LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__SI_CM :LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__DIST_CM;
40-
int16_t *distance = pb_type_device_get_data_blocking(MP_OBJ_FROM_PTR(self), mode);
41-
return mp_obj_new_int(distance[0]);
53+
(void)self;
54+
55+
if (mp_obj_is_true(silent_in)) {
56+
return pb_type_device_method_call(MP_OBJ_FROM_PTR(&ev3devices_UltrasonicSensor_distance_silent_true_obj), 1, 0, pos_args);
57+
} else {
58+
return pb_type_device_method_call(MP_OBJ_FROM_PTR(&ev3devices_UltrasonicSensor_distance_silent_false_obj), 1, 0, pos_args);
59+
}
4260
}
4361
static MP_DEFINE_CONST_FUN_OBJ_KW(ev3devices_UltrasonicSensor_distance_obj, 1, ev3devices_UltrasonicSensor_distance);
4462

4563
// pybricks.ev3devices.UltrasonicSensor.presence
4664
static mp_obj_t ev3devices_UltrasonicSensor_presence(mp_obj_t self_in) {
47-
int8_t *presence = pb_type_device_get_data_blocking(self_in, LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__LISTEN);
65+
int8_t *presence = pb_type_device_get_data(self_in, LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__LISTEN);
4866
return mp_obj_new_bool(presence[0]);
4967
}
50-
static MP_DEFINE_CONST_FUN_OBJ_1(ev3devices_UltrasonicSensor_presence_obj, ev3devices_UltrasonicSensor_presence);
68+
static PB_DEFINE_CONST_TYPE_DEVICE_METHOD_OBJ(ev3devices_UltrasonicSensor_presence_obj, LEGO_DEVICE_MODE_EV3_ULTRASONIC_SENSOR__LISTEN, ev3devices_UltrasonicSensor_presence);
5169

5270
// dir(pybricks.ev3devices.UltrasonicSensor)
5371
static const mp_rom_map_elem_t ev3devices_UltrasonicSensor_locals_dict_table[] = {
@@ -63,4 +81,4 @@ MP_DEFINE_CONST_OBJ_TYPE(pb_type_ev3devices_UltrasonicSensor,
6381
make_new, ev3devices_UltrasonicSensor_make_new,
6482
locals_dict, &ev3devices_UltrasonicSensor_locals_dict);
6583

66-
#endif // PYBRICKS_PY_EV3DEVDEVICES
84+
#endif // PYBRICKS_PY_EV3DEVICES

0 commit comments

Comments
 (0)