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
3347static 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}
4361static MP_DEFINE_CONST_FUN_OBJ_KW (ev3devices_UltrasonicSensor_distance_obj , 1 , ev3devices_UltrasonicSensor_distance ) ;
4462
4563// pybricks.ev3devices.UltrasonicSensor.presence
4664static 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)
5371static 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