Skip to content

Commit 9cad134

Browse files
committed
nrf/machine/adc: Add ADC.read_u16() method.
1 parent 983283a commit 9cad134

File tree

1 file changed

+15
-0
lines changed
  • ports/nrf/modules/machine

1 file changed

+15
-0
lines changed

ports/nrf/modules/machine/adc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj) {
169169
return value;
170170
}
171171

172+
// read_u16()
173+
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
174+
machine_adc_obj_t *self = self_in;
175+
int16_t raw = machine_adc_value_read(self);
176+
#if defined(NRF52_SERIES)
177+
// raw is signed but the channel is in single-ended mode and this method cannot return negative values
178+
if (raw < 0) {
179+
raw = 0;
180+
}
181+
#endif
182+
// raw is an 8-bit value
183+
return MP_OBJ_NEW_SMALL_INT(raw << 8 | raw);
184+
}
185+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_adc_read_u16_obj, machine_adc_read_u16);
172186

173187
/// \method value()
174188
/// Read adc level.
@@ -263,6 +277,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_machine_adc_battery_level_obj, machine_adc_b
263277

264278
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
265279
// instance methods
280+
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&mp_machine_adc_read_u16_obj) },
266281
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&mp_machine_adc_value_obj) },
267282

268283
// class methods

0 commit comments

Comments
 (0)