|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2023 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/runtime.h" |
| 28 | + |
| 29 | +#if MICROPY_PY_MACHINE_ADC |
| 30 | + |
| 31 | +#include "extmod/modmachine.h" |
| 32 | + |
| 33 | +// The port must provide implementations of these low-level ADC functions. |
| 34 | + |
| 35 | +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind); |
| 36 | +STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args); |
| 37 | +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self); |
| 38 | + |
| 39 | +#if MICROPY_PY_MACHINE_ADC_INIT |
| 40 | +STATIC void mp_machine_adc_init_helper(machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args); |
| 41 | +#endif |
| 42 | + |
| 43 | +#if MICROPY_PY_MACHINE_ADC_DEINIT |
| 44 | +STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self); |
| 45 | +#endif |
| 46 | + |
| 47 | +#if MICROPY_PY_MACHINE_ADC_BLOCK |
| 48 | +STATIC mp_obj_t mp_machine_adc_block(machine_adc_obj_t *self); |
| 49 | +#endif |
| 50 | + |
| 51 | +#if MICROPY_PY_MACHINE_ADC_READ_UV |
| 52 | +STATIC mp_int_t mp_machine_adc_read_uv(machine_adc_obj_t *self); |
| 53 | +#endif |
| 54 | + |
| 55 | +#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH |
| 56 | +STATIC void mp_machine_adc_atten_set(machine_adc_obj_t *self, mp_int_t atten); |
| 57 | +STATIC void mp_machine_adc_width_set(machine_adc_obj_t *self, mp_int_t width); |
| 58 | +#endif |
| 59 | + |
| 60 | +#if MICROPY_PY_MACHINE_ADC_READ |
| 61 | +STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self); |
| 62 | +#endif |
| 63 | + |
| 64 | +// The port provides implementations of the above in this file. |
| 65 | +#include MICROPY_PY_MACHINE_ADC_INCLUDEFILE |
| 66 | + |
| 67 | +#if MICROPY_PY_MACHINE_ADC_INIT |
| 68 | +// ADC.init(...) |
| 69 | +STATIC mp_obj_t machine_adc_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 70 | + machine_adc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
| 71 | + mp_machine_adc_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args); |
| 72 | + return mp_const_none; |
| 73 | +} |
| 74 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_adc_init_obj, 1, machine_adc_init); |
| 75 | +#endif |
| 76 | + |
| 77 | +#if MICROPY_PY_MACHINE_ADC_DEINIT |
| 78 | +// ADC.deinit() |
| 79 | +STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) { |
| 80 | + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 81 | + mp_machine_adc_deinit(self); |
| 82 | + return mp_const_none; |
| 83 | +} |
| 84 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit); |
| 85 | +#endif |
| 86 | + |
| 87 | +#if MICROPY_PY_MACHINE_ADC_BLOCK |
| 88 | +// ADC.block() |
| 89 | +STATIC mp_obj_t machine_adc_block(mp_obj_t self_in) { |
| 90 | + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 91 | + return mp_machine_adc_block(self); |
| 92 | +} |
| 93 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_block_obj, machine_adc_block); |
| 94 | +#endif |
| 95 | + |
| 96 | +// ADC.read_u16() |
| 97 | +STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { |
| 98 | + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 99 | + return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read_u16(self)); |
| 100 | +} |
| 101 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); |
| 102 | + |
| 103 | +#if MICROPY_PY_MACHINE_ADC_READ_UV |
| 104 | +// ADC.read_uv() |
| 105 | +STATIC mp_obj_t machine_adc_read_uv(mp_obj_t self_in) { |
| 106 | + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 107 | + return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read_uv(self)); |
| 108 | +} |
| 109 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_uv_obj, machine_adc_read_uv); |
| 110 | +#endif |
| 111 | + |
| 112 | +#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH |
| 113 | + |
| 114 | +// ADC.atten(value) -- this is a legacy method. |
| 115 | +STATIC mp_obj_t machine_adc_atten(mp_obj_t self_in, mp_obj_t atten_in) { |
| 116 | + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 117 | + mp_int_t atten = mp_obj_get_int(atten_in); |
| 118 | + mp_machine_adc_atten_set(self, atten); |
| 119 | + return mp_const_none; |
| 120 | +} |
| 121 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_adc_atten_obj, machine_adc_atten); |
| 122 | + |
| 123 | +// ADC.width(value) -- this is a legacy method. |
| 124 | +STATIC mp_obj_t machine_adc_width(mp_obj_t self_in, mp_obj_t width_in) { |
| 125 | + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 126 | + mp_int_t width = mp_obj_get_int(width_in); |
| 127 | + mp_machine_adc_width_set(self, width); |
| 128 | + return mp_const_none; |
| 129 | +} |
| 130 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_adc_width_obj, machine_adc_width); |
| 131 | + |
| 132 | +#endif |
| 133 | + |
| 134 | +#if MICROPY_PY_MACHINE_ADC_READ |
| 135 | +// ADC.read() -- this is a legacy method. |
| 136 | +STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) { |
| 137 | + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 138 | + return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read(self)); |
| 139 | +} |
| 140 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read); |
| 141 | +#endif |
| 142 | + |
| 143 | +STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { |
| 144 | + #if MICROPY_PY_MACHINE_ADC_INIT |
| 145 | + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_adc_init_obj) }, |
| 146 | + #endif |
| 147 | + #if MICROPY_PY_MACHINE_ADC_DEINIT |
| 148 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) }, |
| 149 | + #endif |
| 150 | + #if MICROPY_PY_MACHINE_ADC_BLOCK |
| 151 | + { MP_ROM_QSTR(MP_QSTR_block), MP_ROM_PTR(&machine_adc_block_obj) }, |
| 152 | + #endif |
| 153 | + |
| 154 | + { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, |
| 155 | + #if MICROPY_PY_MACHINE_ADC_READ_UV |
| 156 | + { MP_ROM_QSTR(MP_QSTR_read_uv), MP_ROM_PTR(&machine_adc_read_uv_obj) }, |
| 157 | + #endif |
| 158 | + |
| 159 | + // Legacy methods. |
| 160 | + #if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH |
| 161 | + { MP_ROM_QSTR(MP_QSTR_atten), MP_ROM_PTR(&machine_adc_atten_obj) }, |
| 162 | + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&machine_adc_width_obj) }, |
| 163 | + #endif |
| 164 | + #if MICROPY_PY_MACHINE_ADC_READ |
| 165 | + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) }, |
| 166 | + #endif |
| 167 | + |
| 168 | + // A port must add ADC class constants defining the following macro. |
| 169 | + // It can be defined to nothing if there are no constants. |
| 170 | + MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS |
| 171 | +}; |
| 172 | +STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); |
| 173 | + |
| 174 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 175 | + machine_adc_type, |
| 176 | + MP_QSTR_ADC, |
| 177 | + MP_TYPE_FLAG_NONE, |
| 178 | + make_new, mp_machine_adc_make_new, |
| 179 | + print, mp_machine_adc_print, |
| 180 | + locals_dict, &machine_adc_locals_dict |
| 181 | + ); |
| 182 | + |
| 183 | +#endif // MICROPY_PY_MACHINE_ADC |
0 commit comments