extmod/modev3devices: Ultrasonic Sensor methods
@@ -397,17 +397,37 @@ STATIC void ev3devices_UltrasonicSensor_print(const mp_print_t *print, mp_obj_t
}
// pybricks.ev3devices.UltrasonicSensor.distance
-STATIC mp_obj_t ev3devices_UltrasonicSensor_distance(mp_obj_t self_in) {
- ev3devices_UltrasonicSensor_obj_t *self = MP_OBJ_TO_PTR(self_in);
+STATIC mp_obj_t ev3devices_UltrasonicSensor_distance(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+
+ PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
+ PB_ARG_DEFAULT_FALSE(silent)
+ );
+ ev3devices_UltrasonicSensor_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
+
int16_t distance;
+ if (mp_obj_is_true(silent)) {
+ pb_assert(ev3device_get_values_at_mode(self->iodev, PBIO_IODEV_MODE_EV3_ULTRASONIC_SENSOR__SI_CM, &distance));
+ }
+ else {
pb_assert(ev3device_get_values_at_mode(self->iodev, PBIO_IODEV_MODE_EV3_ULTRASONIC_SENSOR__DIST_CM, &distance));
+ }
return mp_obj_new_int(distance);
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(ev3devices_UltrasonicSensor_distance_obj, ev3devices_UltrasonicSensor_distance);
+STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ev3devices_UltrasonicSensor_distance_obj, 0, ev3devices_UltrasonicSensor_distance);
+
+// pybricks.ev3devices.UltrasonicSensor.presence
+STATIC mp_obj_t ev3devices_UltrasonicSensor_presence(mp_obj_t self_in) {
+ ev3devices_UltrasonicSensor_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ int8_t presence;
+ pb_assert(ev3device_get_values_at_mode(self->iodev, PBIO_IODEV_MODE_EV3_ULTRASONIC_SENSOR__LISTEN, &presence));
+ return mp_obj_new_bool(presence);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(ev3devices_UltrasonicSensor_presence_obj, ev3devices_UltrasonicSensor_presence);
// dir(pybricks.ev3devices.UltrasonicSensor)
STATIC const mp_rom_map_elem_t ev3devices_UltrasonicSensor_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_distance), MP_ROM_PTR(&ev3devices_UltrasonicSensor_distance_obj) },
+ { MP_ROM_QSTR(MP_QSTR_presence), MP_ROM_PTR(&ev3devices_UltrasonicSensor_presence_obj) },
{ MP_ROM_QSTR(MP_QSTR_light), MP_ROM_ATTRIBUTE_OFFSET(ev3devices_UltrasonicSensor_obj_t, light) },
};
STATIC MP_DEFINE_CONST_DICT(ev3devices_UltrasonicSensor_locals_dict, ev3devices_UltrasonicSensor_locals_dict_table);
