diff options
author | Damien George <damien.p.george@gmail.com> | 2019-07-25 18:22:54 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-09-05 22:13:04 +1000 |
commit | 9cad134a2f861a0f8864b70ab80c9d6cc61c2932 (patch) | |
tree | 72c6fd23045be117cd4d93997e61e0b4554b6487 | |
parent | 983283a8cd504e2f9438a836effa9e862f19ec97 (diff) |
nrf/machine/adc: Add ADC.read_u16() method.
-rw-r--r-- | ports/nrf/modules/machine/adc.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/ports/nrf/modules/machine/adc.c b/ports/nrf/modules/machine/adc.c index 55b68c6b8..b543c94f3 100644 --- a/ports/nrf/modules/machine/adc.c +++ b/ports/nrf/modules/machine/adc.c @@ -169,6 +169,20 @@ int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj) { return value; } +// read_u16() +STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { + machine_adc_obj_t *self = self_in; + int16_t raw = machine_adc_value_read(self); + #if defined(NRF52_SERIES) + // raw is signed but the channel is in single-ended mode and this method cannot return negative values + if (raw < 0) { + raw = 0; + } + #endif + // raw is an 8-bit value + return MP_OBJ_NEW_SMALL_INT(raw << 8 | raw); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_adc_read_u16_obj, machine_adc_read_u16); /// \method value() /// Read adc level. @@ -263,6 +277,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_machine_adc_battery_level_obj, machine_adc_b STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { // instance methods + { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&mp_machine_adc_read_u16_obj) }, { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&mp_machine_adc_value_obj) }, // class methods |