|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Jonathan Hogg |
| 7 | + * Copyright (c) 2023 Damien P. George |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "py/runtime.h" |
| 29 | + |
| 30 | +#if MICROPY_PY_MACHINE_ADC_BLOCK |
| 31 | + |
| 32 | +#include "py/mphal.h" |
| 33 | +#include "extmod/modmachine.h" |
| 34 | + |
| 35 | +// The port must provide implementations of these low-level ADCBlock functions. |
| 36 | +STATIC void mp_machine_adc_block_print(const mp_print_t *print, machine_adc_block_obj_t *self); |
| 37 | +STATIC machine_adc_block_obj_t *mp_machine_adc_block_get(mp_int_t unit); |
| 38 | +STATIC void mp_machine_adc_block_bits_set(machine_adc_block_obj_t *self, mp_int_t bits); |
| 39 | +STATIC machine_adc_obj_t *mp_machine_adc_block_connect(machine_adc_block_obj_t *self, mp_int_t channel_id, mp_hal_pin_obj_t pin, mp_map_t *kw_args); |
| 40 | + |
| 41 | +// The port provides implementations of the above in this file. |
| 42 | +#include MICROPY_PY_MACHINE_ADC_BLOCK_INCLUDEFILE |
| 43 | + |
| 44 | +STATIC void machine_adc_block_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 45 | + machine_adc_block_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 46 | + mp_machine_adc_block_print(print, self); |
| 47 | +} |
| 48 | + |
| 49 | +STATIC void machine_adc_block_init_helper(machine_adc_block_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 50 | + enum { |
| 51 | + ARG_bits, |
| 52 | + }; |
| 53 | + |
| 54 | + static const mp_arg_t allowed_args[] = { |
| 55 | + { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
| 56 | + }; |
| 57 | + |
| 58 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 59 | + mp_arg_parse_all(n_pos_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 60 | + |
| 61 | + mp_int_t bits = args[ARG_bits].u_int; |
| 62 | + mp_machine_adc_block_bits_set(self, bits); |
| 63 | +} |
| 64 | + |
| 65 | +STATIC mp_obj_t machine_adc_block_make_new(const mp_obj_type_t *type, size_t n_pos_args, size_t n_kw_args, const mp_obj_t *args) { |
| 66 | + mp_arg_check_num(n_pos_args, n_kw_args, 1, MP_OBJ_FUN_ARGS_MAX, true); |
| 67 | + mp_int_t unit = mp_obj_get_int(args[0]); |
| 68 | + machine_adc_block_obj_t *self = mp_machine_adc_block_get(unit); |
| 69 | + if (self == NULL) { |
| 70 | + mp_raise_ValueError(MP_ERROR_TEXT("invalid block id")); |
| 71 | + } |
| 72 | + |
| 73 | + mp_map_t kw_args; |
| 74 | + mp_map_init_fixed_table(&kw_args, n_kw_args, args + n_pos_args); |
| 75 | + machine_adc_block_init_helper(self, n_pos_args - 1, args + 1, &kw_args); |
| 76 | + |
| 77 | + return MP_OBJ_FROM_PTR(self); |
| 78 | +} |
| 79 | + |
| 80 | +STATIC mp_obj_t machine_adc_block_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 81 | + machine_adc_block_obj_t *self = pos_args[0]; |
| 82 | + machine_adc_block_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args); |
| 83 | + return mp_const_none; |
| 84 | +} |
| 85 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_adc_block_init_obj, 1, machine_adc_block_init); |
| 86 | + |
| 87 | +STATIC mp_obj_t machine_adc_block_connect(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 88 | + machine_adc_block_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
| 89 | + mp_int_t channel_id = -1; |
| 90 | + mp_hal_pin_obj_t pin = -1; |
| 91 | + if (n_pos_args == 2) { |
| 92 | + if (mp_obj_is_int(pos_args[1])) { |
| 93 | + channel_id = mp_obj_get_int(pos_args[1]); |
| 94 | + } else { |
| 95 | + pin = mp_hal_get_pin_obj(pos_args[1]); |
| 96 | + } |
| 97 | + } else if (n_pos_args == 3) { |
| 98 | + channel_id = mp_obj_get_int(pos_args[1]); |
| 99 | + pin = mp_hal_get_pin_obj(pos_args[2]); |
| 100 | + } else { |
| 101 | + mp_raise_TypeError(MP_ERROR_TEXT("too many positional args")); |
| 102 | + } |
| 103 | + |
| 104 | + machine_adc_obj_t *adc = mp_machine_adc_block_connect(self, channel_id, pin, kw_args); |
| 105 | + if (adc == NULL) { |
| 106 | + mp_raise_ValueError(MP_ERROR_TEXT("no matching ADC")); |
| 107 | + } |
| 108 | + |
| 109 | + return MP_OBJ_FROM_PTR(adc); |
| 110 | +} |
| 111 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_adc_block_connect_obj, 2, machine_adc_block_connect); |
| 112 | + |
| 113 | +STATIC const mp_rom_map_elem_t machine_adc_block_locals_dict_table[] = { |
| 114 | + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_adc_block_init_obj) }, |
| 115 | + { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&machine_adc_block_connect_obj) }, |
| 116 | +}; |
| 117 | +STATIC MP_DEFINE_CONST_DICT(machine_adc_block_locals_dict, machine_adc_block_locals_dict_table); |
| 118 | + |
| 119 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 120 | + machine_adc_block_type, |
| 121 | + MP_QSTR_ADCBlock, |
| 122 | + MP_TYPE_FLAG_NONE, |
| 123 | + make_new, machine_adc_block_make_new, |
| 124 | + print, machine_adc_block_print, |
| 125 | + locals_dict, &machine_adc_block_locals_dict |
| 126 | + ); |
| 127 | + |
| 128 | +#endif // MICROPY_PY_MACHINE_ADC_BLOCK |
0 commit comments