11// SPDX-License-Identifier: MIT
2- // Copyright (c) 2018-2023 The Pybricks Authors
2+ // Copyright (c) 2018-2025 The Pybricks Authors
33
44#include "py/mpconfig.h"
55
6- #if PYBRICKS_PY_NXTDEVICES && PYBRICKS_PY_EV3DEVDEVICES
6+ #if PYBRICKS_PY_NXTDEVICES
7+
8+ #include <pbio/int_math.h>
9+ #include <pbio/port_interface.h>
10+ #include <pbio/port_dcm.h>
711
812#include <pybricks/common.h>
9- #include <pybricks/nxtdevices.h>
1013#include <pybricks/parameters.h>
1114
1215#include <pybricks/util_mp/pb_kwarg_helper.h>
1316#include <pybricks/util_mp/pb_obj_helper.h>
14- #include <pybricks/common/pb_type_device .h>
17+ #include <pybricks/util_pb/pb_error .h>
1518
1619// pybricks.nxtdevices.SoundSensor class object
1720typedef struct _nxtdevices_SoundSensor_obj_t {
18- pb_type_device_obj_base_t device_base ;
21+ mp_obj_base_t base ;
22+ pbio_port_t * port ;
1923} nxtdevices_SoundSensor_obj_t ;
2024
25+ // Generic linear scaling of an analog value between a known min and max to a percentage
26+ static int32_t analog_scale (int32_t mvolts , int32_t mvolts_min , int32_t mvolts_max , bool invert ) {
27+ int32_t scaled = (100 * (mvolts - mvolts_min )) / (mvolts_max - mvolts_min );
28+ if (invert ) {
29+ scaled = 100 - scaled ;
30+ }
31+ return pbio_int_math_bind (scaled , 0 , 100 );
32+ }
33+
2134// pybricks.nxtdevices.SoundSensor.intensity
2235static mp_obj_t nxtdevices_SoundSensor_intensity (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
2336
2437 PB_PARSE_ARGS_METHOD (n_args , pos_args , kw_args ,
2538 nxtdevices_SoundSensor_obj_t , self ,
2639 PB_ARG_DEFAULT_TRUE (audible_only ));
2740
28- uint8_t mode = mp_obj_is_true (audible_only_in ) ?LEGO_DEVICE_MODE_NXT_ANALOG__ACTIVE :LEGO_DEVICE_MODE_NXT_ANALOG__PASSIVE ;
29- int32_t * analog = pb_type_device_get_data_blocking (MP_OBJ_FROM_PTR (self ), mode );
30-
31- return mp_obj_new_int (analog_scale (analog [0 ], 650 , 4860 , true));
41+ uint32_t analog ;
42+ pb_assert (pbio_port_get_analog_value (self -> port , LEGO_DEVICE_TYPE_ID_NXT_SOUND_SENSOR , mp_obj_is_true (audible_only_in ), & analog ));
43+ return mp_obj_new_int (analog_scale (analog , 650 , 4860 , true));
3244}
3345static MP_DEFINE_CONST_FUN_OBJ_KW (nxtdevices_SoundSensor_intensity_obj , 1 , nxtdevices_SoundSensor_intensity ) ;
3446
@@ -38,28 +50,26 @@ static mp_obj_t nxtdevices_SoundSensor_make_new(const mp_obj_type_t *type, size_
3850 PB_ARG_REQUIRED (port ));
3951
4052 nxtdevices_SoundSensor_obj_t * self = mp_obj_malloc (nxtdevices_SoundSensor_obj_t , type );
41- pb_type_device_init_class (& self -> device_base , port_in , LEGO_DEVICE_TYPE_ID_NXT_SOUND_SENSOR );
53+ pbio_port_id_t port_id = pb_type_enum_get_value (port_in , & pb_enum_type_Port );
54+ pb_assert (pbio_port_get_port (port_id , & self -> port ));
4255
4356 // Do one reading for consistent initial mode
44- mp_obj_t pos_args [1 ] = { self };
45- mp_map_t kwd_args ;
46- mp_map_init_fixed_table (& kwd_args , 0 , NULL );
47- nxtdevices_SoundSensor_intensity (1 , pos_args , & kwd_args );
48-
57+ mp_obj_t pos_args [] = { MP_OBJ_FROM_PTR (self ) };
58+ nxtdevices_SoundSensor_intensity (1 , pos_args , (mp_map_t * )& mp_const_empty_map );
4959 return MP_OBJ_FROM_PTR (self );
5060}
5161
52- // dir(pybricks.ev3devices .SoundSensor)
62+ // dir(pybricks.nxtdevices .SoundSensor)
5363static const mp_rom_map_elem_t nxtdevices_SoundSensor_locals_dict_table [] = {
54- { MP_ROM_QSTR (MP_QSTR_intensity ), MP_ROM_PTR (& nxtdevices_SoundSensor_intensity_obj ) },
64+ { MP_ROM_QSTR (MP_QSTR_intensity ), MP_ROM_PTR (& nxtdevices_SoundSensor_intensity_obj ) },
5565};
5666static MP_DEFINE_CONST_DICT (nxtdevices_SoundSensor_locals_dict , nxtdevices_SoundSensor_locals_dict_table ) ;
5767
58- // type(pybricks.ev3devices .SoundSensor)
68+ // type(pybricks.nxtdevices .SoundSensor)
5969MP_DEFINE_CONST_OBJ_TYPE (pb_type_nxtdevices_SoundSensor ,
6070 MP_QSTR_SoundSensor ,
6171 MP_TYPE_FLAG_NONE ,
6272 make_new , nxtdevices_SoundSensor_make_new ,
6373 locals_dict , & nxtdevices_SoundSensor_locals_dict );
6474
65- #endif // PYBRICKS_PY_NXTDEVICES && PYBRICKS_PY_EV3DEVDEVICES
75+ #endif // PYBRICKS_PY_NXTDEVICES
0 commit comments