1 /*
2  * This file is part of the MicroPython 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/runtime.h"
31 #include "py/mphal.h"
32 #include "user_interface.h"
33 
34 typedef struct _machine_adc_obj_t {
35     mp_obj_base_t base;
36     bool isvdd;
37 } machine_adc_obj_t;
38 
39 extern const mp_obj_type_t machine_adc_type;
40 
41 STATIC machine_adc_obj_t machine_adc_vdd3 = {{&machine_adc_type}, true};
42 STATIC machine_adc_obj_t machine_adc_adc = {{&machine_adc_type}, false};
43 
adc_read(machine_adc_obj_t * self)44 STATIC uint16_t adc_read(machine_adc_obj_t *self) {
45     if (self->isvdd) {
46         return system_get_vdd33();
47     } else {
48         return system_adc_read();
49     }
50 }
machine_adc_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)51 void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
52     machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
53     mp_printf(print, "ADC(%u)", self->isvdd);
54 }
55 
machine_adc_make_new(const mp_obj_type_t * type_in,size_t n_args,size_t n_kw,const mp_obj_t * args)56 mp_obj_t machine_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
57     mp_arg_check_num(n_args, n_kw, 1, 1, false);
58 
59     mp_int_t chn = mp_obj_get_int(args[0]);
60 
61     switch (chn) {
62         case 0:
63             return &machine_adc_adc;
64         case 1:
65             return &machine_adc_vdd3;
66         default:
67             mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("ADC(%d) doesn't exist"), chn);
68     }
69 }
70 
71 // read_u16()
machine_adc_read_u16(mp_obj_t self_in)72 STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
73     machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
74     uint32_t value = adc_read(self);
75     return MP_OBJ_NEW_SMALL_INT(value * 65535 / 1024);
76 }
77 STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
78 
79 // Legacy method
machine_adc_read(mp_obj_t self_in)80 STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) {
81     machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
82     return mp_obj_new_int(adc_read(self));
83 }
84 STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read);
85 
86 STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
87     { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
88     { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) }
89 };
90 STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table);
91 
92 const mp_obj_type_t machine_adc_type = {
93     { &mp_type_type },
94     .name = MP_QSTR_ADC,
95     .print = machine_adc_print,
96     .make_new = machine_adc_make_new,
97     .locals_dict = (mp_obj_dict_t *)&machine_adc_locals_dict,
98 };
99