5757///
5858/// Usage:
5959///
60- /// adc = pyb.ADC(channel) # create an adc object on the given channel (1 to 4)
61- /// this automatically configures the pin associated to
62- /// that analog channel.
63- /// adc.read() # read channel value
60+ /// adc = pyb.ADC('GP5') # create an adc object on the given pin (GP2, GP3, GP4 o GP5)
61+ /// adc.read() # read channel value
6462///
6563/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits.
6664
7573 ******************************************************************************/
7674typedef struct {
7775 mp_obj_base_t base ;
76+ pin_obj_t * pin ;
7877 byte channel ;
79- byte idx ;
78+ byte id ;
8079} pyb_adc_obj_t ;
8180
81+ /******************************************************************************
82+ DECLARE PRIVATE DATA
83+ ******************************************************************************/
84+ STATIC pyb_adc_obj_t pyb_adc_obj [PYB_ADC_NUM_CHANNELS ] = { {.pin = & pin_GP2 , .channel = ADC_CH_0 , .id = 1 }, {.pin = & pin_GP3 , .channel = ADC_CH_1 , .id = 2 },
85+ {.pin = & pin_GP4 , .channel = ADC_CH_2 , .id = 2 }, {.pin = & pin_GP5 , .channel = ADC_CH_3 , .id = 4 } };
86+
8287/******************************************************************************
8388 DEFINE PUBLIC FUNCTIONS
8489 ******************************************************************************/
8590STATIC void pybadc_init (pyb_adc_obj_t * self ) {
91+ // configure the pin in analog mode
92+ pin_config (self -> pin , PIN_MODE_0 , GPIO_DIR_MODE_IN , PYBPIN_ANALOG_TYPE , PIN_STRENGTH_2MA );
8693 // enable the ADC channel
8794 MAP_ADCChannelEnable (ADC_BASE , self -> channel );
8895 // enable and configure the timer
@@ -92,68 +99,34 @@ STATIC void pybadc_init (pyb_adc_obj_t *self) {
9299 MAP_ADCEnable (ADC_BASE );
93100}
94101
95- /******************************************************************************
96- DECLARE PRIVATE DATA
97- ******************************************************************************/
98- STATIC pyb_adc_obj_t pyb_adc_obj [PYB_ADC_NUM_CHANNELS ];
99-
100102/******************************************************************************/
101103/* Micro Python bindings : adc object */
102104
103105STATIC void adc_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
104106 pyb_adc_obj_t * self = self_in ;
105- mp_printf (print , "<ADC, channel=%u>" , ( self -> idx + 1 ) );
107+ mp_printf (print , "<ADC1 channel=%u on %q >" , self -> id , self -> pin -> name );
106108}
107109
108- /// \classmethod \constructor(channel )
109- /// Create an ADC object associated with the given channel .
110+ /// \classmethod \constructor(pin )
111+ /// Create an ADC object associated with the given pin .
110112/// This allows you to then read analog values on that pin.
111113STATIC mp_obj_t adc_make_new (mp_obj_t type_in , mp_uint_t n_args , mp_uint_t n_kw , const mp_obj_t * args ) {
112114 // check number of arguments
113115 mp_arg_check_num (n_args , n_kw , 1 , 1 , false);
114116
115- // the first argument is the channel number
116- int32_t idx = mp_obj_get_int (args [0 ]) - 1 ;
117- const pin_obj_t * pin ;
118- uint channel ;
119- switch (idx ) {
120- case 0 :
121- channel = ADC_CH_0 ;
122- pin = & pin_GP2 ;
123- break ;
124- case 1 :
125- channel = ADC_CH_1 ;
126- pin = & pin_GP3 ;
127- break ;
128- case 2 :
129- channel = ADC_CH_2 ;
130- pin = & pin_GP4 ;
131- break ;
132- case 3 :
133- channel = ADC_CH_3 ;
134- pin = & pin_GP5 ;
135- break ;
136- default :
137- nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
138- break ;
117+ // the argument passed is the pin
118+ const pin_obj_t * pin = (pin_obj_t * )pin_find (args [0 ]);
119+ for (int32_t idx = 0 ; idx < PYB_ADC_NUM_CHANNELS ; idx ++ ) {
120+ if (pin == pyb_adc_obj [idx ].pin ) {
121+ pyb_adc_obj_t * self = & pyb_adc_obj [idx ];
122+ self -> base .type = & pyb_adc_type ;
123+ pybadc_init (self );
124+ // register it with the sleep module
125+ pybsleep_add ((const mp_obj_t )self , (WakeUpCB_t )pybadc_init );
126+ return self ;
127+ }
139128 }
140-
141- // disable the callback before re-configuring
142- pyb_adc_obj_t * self = & pyb_adc_obj [idx ];
143- self -> base .type = & pyb_adc_type ;
144- self -> channel = channel ;
145- self -> idx = idx ;
146-
147- // configure the pin in analog mode
148- pin_config ((pin_obj_t * )pin , PIN_MODE_0 , GPIO_DIR_MODE_IN , PYBPIN_ANALOG_TYPE , PIN_STRENGTH_2MA );
149-
150- // initialize it
151- pybadc_init (self );
152-
153- // register it with the sleep module
154- pybsleep_add ((const mp_obj_t )self , (WakeUpCB_t )pybadc_init );
155-
156- return self ;
129+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
157130}
158131
159132/// \method read()
@@ -172,32 +145,32 @@ STATIC mp_obj_t adc_read(mp_obj_t self_in) {
172145}
173146STATIC MP_DEFINE_CONST_FUN_OBJ_1 (adc_read_obj , adc_read );
174147
175- /// \method enable ()
148+ /// \method init ()
176149/// Enable the adc channel
177- STATIC mp_obj_t adc_enable (mp_obj_t self_in ) {
150+ STATIC mp_obj_t adc_init (mp_obj_t self_in ) {
178151 pyb_adc_obj_t * self = self_in ;
179152
180153 pybadc_init (self );
181154 return mp_const_none ;
182155}
183- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (adc_enable_obj , adc_enable );
156+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (adc_init_obj , adc_init );
184157
185- /// \method disable ()
158+ /// \method deinit ()
186159/// Disable the adc channel
187- STATIC mp_obj_t adc_disable (mp_obj_t self_in ) {
160+ STATIC mp_obj_t adc_deinit (mp_obj_t self_in ) {
188161 pyb_adc_obj_t * self = self_in ;
189162
190163 MAP_ADCChannelDisable (ADC_BASE , self -> channel );
191164 // unregister it with the sleep module
192165 pybsleep_remove ((const mp_obj_t )self );
193166 return mp_const_none ;
194167}
195- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (adc_disable_obj , adc_disable );
168+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (adc_deinit_obj , adc_deinit );
196169
197170STATIC const mp_map_elem_t adc_locals_dict_table [] = {
198171 { MP_OBJ_NEW_QSTR (MP_QSTR_read ), (mp_obj_t )& adc_read_obj },
199- { MP_OBJ_NEW_QSTR (MP_QSTR_enable ), (mp_obj_t )& adc_enable_obj },
200- { MP_OBJ_NEW_QSTR (MP_QSTR_disable ), (mp_obj_t )& adc_disable_obj },
172+ { MP_OBJ_NEW_QSTR (MP_QSTR_init ), (mp_obj_t )& adc_init_obj },
173+ { MP_OBJ_NEW_QSTR (MP_QSTR_deinit ), (mp_obj_t )& adc_deinit_obj },
201174};
202175
203176STATIC MP_DEFINE_CONST_DICT (adc_locals_dict , adc_locals_dict_table );
0 commit comments