Skip to content

Commit 25a8a42

Browse files
atxpfalcon
authored andcommitted
esp8266: Add pyb.ADC class
1 parent 492fd5c commit 25a8a42

5 files changed

Lines changed: 91 additions & 0 deletions

File tree

esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ SRC_C = \
5757
modpyb.c \
5858
modpybpin.c \
5959
modpybrtc.c \
60+
modpybadc.c \
6061
modesp.c \
6162
modutime.c \
6263
moduos.c \

esp8266/modpyb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
165165
{ MP_OBJ_NEW_QSTR(MP_QSTR_hard_reset), (mp_obj_t)&pyb_hard_reset_obj },
166166

167167
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pyb_pin_type },
168+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type },
168169
{ MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_obj },
169170
};
170171

esp8266/modpyb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
extern const mp_obj_type_t pyb_pin_type;
2+
extern const mp_obj_type_t pyb_adc_type;
23
extern const mp_obj_base_t pyb_rtc_obj;

esp8266/modpybadc.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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"
33+
#include MICROPY_HAL_H
34+
#include "user_interface.h"
35+
36+
const mp_obj_type_t pyb_adc_type;
37+
38+
typedef struct _pyb_adc_obj_t {
39+
mp_obj_base_t base;
40+
bool isvdd;
41+
} pyb_adc_obj_t;
42+
43+
STATIC pyb_adc_obj_t pyb_adc_vdd3 = {{&pyb_adc_type}, true};
44+
STATIC pyb_adc_obj_t pyb_adc_adc = {{&pyb_adc_type}, false};
45+
46+
STATIC 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+
63+
STATIC 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+
}
72+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_adc_read_obj, pyb_adc_read);
73+
74+
STATIC 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+
};
77+
STATIC MP_DEFINE_CONST_DICT(pyb_adc_locals_dict, pyb_adc_locals_dict_table);
78+
79+
const 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+
};

esp8266/qstrdefsport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ Q(RTC)
101101
Q(datetime)
102102
Q(memory)
103103

104+
// ADC
105+
Q(ADC)
106+
Q(read)
107+
104108
// utime
105109
Q(utime)
106110
Q(localtime)

0 commit comments

Comments
 (0)