Josef Gajdusek | 25a8a42 | 2015-05-18 18:35:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2015 Josef Gajdusek |
| 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 <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "py/nlr.h" |
| 31 | #include "py/obj.h" |
| 32 | #include "py/runtime.h" |
Josef Gajdusek | 25a8a42 | 2015-05-18 18:35:56 +0200 | [diff] [blame] | 33 | #include "user_interface.h" |
| 34 | |
| 35 | const mp_obj_type_t pyb_adc_type; |
| 36 | |
| 37 | typedef struct _pyb_adc_obj_t { |
| 38 | mp_obj_base_t base; |
| 39 | bool isvdd; |
| 40 | } pyb_adc_obj_t; |
| 41 | |
| 42 | STATIC pyb_adc_obj_t pyb_adc_vdd3 = {{&pyb_adc_type}, true}; |
| 43 | STATIC pyb_adc_obj_t pyb_adc_adc = {{&pyb_adc_type}, false}; |
| 44 | |
Damien George | 5b3f0b7 | 2016-01-03 15:55:55 +0000 | [diff] [blame] | 45 | STATIC mp_obj_t pyb_adc_make_new(const mp_obj_type_t *type_in, mp_uint_t n_args, mp_uint_t n_kw, |
Josef Gajdusek | 25a8a42 | 2015-05-18 18:35:56 +0200 | [diff] [blame] | 46 | const mp_obj_t *args) { |
| 47 | mp_arg_check_num(n_args, n_kw, 1, 1, false); |
| 48 | |
| 49 | mp_int_t chn = mp_obj_get_int(args[0]); |
| 50 | |
| 51 | switch (chn) { |
| 52 | case 0: |
| 53 | return &pyb_adc_adc; |
| 54 | case 1: |
| 55 | return &pyb_adc_vdd3; |
| 56 | default: |
| 57 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
| 58 | "not a valid ADC Channel: %d", chn)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | STATIC mp_obj_t pyb_adc_read(mp_obj_t self_in) { |
| 63 | pyb_adc_obj_t *adc = self_in; |
| 64 | |
| 65 | if (adc->isvdd) { |
| 66 | return mp_obj_new_int(system_get_vdd33()); |
| 67 | } else { |
| 68 | return mp_obj_new_int(system_adc_read()); |
| 69 | } |
| 70 | } |
| 71 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_adc_read_obj, pyb_adc_read); |
| 72 | |
| 73 | STATIC const mp_map_elem_t pyb_adc_locals_dict_table[] = { |
| 74 | { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&pyb_adc_read_obj } |
| 75 | }; |
| 76 | STATIC MP_DEFINE_CONST_DICT(pyb_adc_locals_dict, pyb_adc_locals_dict_table); |
| 77 | |
| 78 | const mp_obj_type_t pyb_adc_type = { |
| 79 | { &mp_type_type }, |
| 80 | .name = MP_QSTR_ADC, |
| 81 | .make_new = pyb_adc_make_new, |
| 82 | .locals_dict = (mp_obj_t)&pyb_adc_locals_dict, |
| 83 | }; |