blob: b4dfa67283894809fe3024566ff9b24c8b880e0a [file] [log] [blame]
Josef Gajdusek25a8a422015-05-18 18:35:56 +02001/*
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"
33#include MICROPY_HAL_H
34#include "user_interface.h"
35
36const mp_obj_type_t pyb_adc_type;
37
38typedef struct _pyb_adc_obj_t {
39 mp_obj_base_t base;
40 bool isvdd;
41} pyb_adc_obj_t;
42
43STATIC pyb_adc_obj_t pyb_adc_vdd3 = {{&pyb_adc_type}, true};
44STATIC pyb_adc_obj_t pyb_adc_adc = {{&pyb_adc_type}, false};
45
46STATIC mp_obj_t pyb_adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
47 const mp_obj_t *args) {
48 mp_arg_check_num(n_args, n_kw, 1, 1, false);
49
50 mp_int_t chn = mp_obj_get_int(args[0]);
51
52 switch (chn) {
53 case 0:
54 return &pyb_adc_adc;
55 case 1:
56 return &pyb_adc_vdd3;
57 default:
58 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
59 "not a valid ADC Channel: %d", chn));
60 }
61}
62
63STATIC mp_obj_t pyb_adc_read(mp_obj_t self_in) {
64 pyb_adc_obj_t *adc = self_in;
65
66 if (adc->isvdd) {
67 return mp_obj_new_int(system_get_vdd33());
68 } else {
69 return mp_obj_new_int(system_adc_read());
70 }
71}
72STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_adc_read_obj, pyb_adc_read);
73
74STATIC const mp_map_elem_t pyb_adc_locals_dict_table[] = {
75 { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&pyb_adc_read_obj }
76};
77STATIC MP_DEFINE_CONST_DICT(pyb_adc_locals_dict, pyb_adc_locals_dict_table);
78
79const mp_obj_type_t pyb_adc_type = {
80 { &mp_type_type },
81 .name = MP_QSTR_ADC,
82 .make_new = pyb_adc_make_new,
83 .locals_dict = (mp_obj_t)&pyb_adc_locals_dict,
84};