Skip to content

Commit 95d8b5f

Browse files
committed
extmod/machine_adc: Factor ports' ADC Python bindings to common code.
No functional change, just code factoring to have the Python bindings in one location, and all the ports use those same bindings. Signed-off-by: Damien George <damien@micropython.org>
1 parent 48e0986 commit 95d8b5f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+434
-346
lines changed

extmod/extmod.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(MICROPY_SOURCE_EXTMOD
77
${MICROPY_DIR}/shared/libc/abort_.c
88
${MICROPY_DIR}/shared/libc/printf.c
99
${MICROPY_EXTMOD_DIR}/btstack/modbluetooth_btstack.c
10+
${MICROPY_EXTMOD_DIR}/machine_adc.c
1011
${MICROPY_EXTMOD_DIR}/machine_bitstream.c
1112
${MICROPY_EXTMOD_DIR}/machine_i2c.c
1213
${MICROPY_EXTMOD_DIR}/machine_i2s.c

extmod/extmod.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# and provides rules to build 3rd-party components for extmod modules.
33

44
SRC_EXTMOD_C += \
5+
extmod/machine_adc.c \
56
extmod/machine_bitstream.c \
67
extmod/machine_i2c.c \
78
extmod/machine_i2s.c \

extmod/machine_adc.c

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Damien P. George
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 "py/runtime.h"
28+
29+
#if MICROPY_PY_MACHINE_ADC
30+
31+
#include "extmod/modmachine.h"
32+
33+
// The port must provide implementations of these low-level ADC functions.
34+
35+
STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
36+
STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
37+
STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self);
38+
39+
#if MICROPY_PY_MACHINE_ADC_INIT
40+
STATIC void mp_machine_adc_init_helper(machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
41+
#endif
42+
43+
#if MICROPY_PY_MACHINE_ADC_DEINIT
44+
STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self);
45+
#endif
46+
47+
#if MICROPY_PY_MACHINE_ADC_BLOCK
48+
STATIC mp_obj_t mp_machine_adc_block(machine_adc_obj_t *self);
49+
#endif
50+
51+
#if MICROPY_PY_MACHINE_ADC_READ_UV
52+
STATIC mp_int_t mp_machine_adc_read_uv(machine_adc_obj_t *self);
53+
#endif
54+
55+
#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH
56+
STATIC void mp_machine_adc_atten_set(machine_adc_obj_t *self, mp_int_t atten);
57+
STATIC void mp_machine_adc_width_set(machine_adc_obj_t *self, mp_int_t width);
58+
#endif
59+
60+
#if MICROPY_PY_MACHINE_ADC_READ
61+
STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self);
62+
#endif
63+
64+
// The port provides implementations of the above in this file.
65+
#include MICROPY_PY_MACHINE_ADC_INCLUDEFILE
66+
67+
#if MICROPY_PY_MACHINE_ADC_INIT
68+
// ADC.init(...)
69+
STATIC mp_obj_t machine_adc_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
70+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
71+
mp_machine_adc_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args);
72+
return mp_const_none;
73+
}
74+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_adc_init_obj, 1, machine_adc_init);
75+
#endif
76+
77+
#if MICROPY_PY_MACHINE_ADC_DEINIT
78+
// ADC.deinit()
79+
STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) {
80+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
81+
mp_machine_adc_deinit(self);
82+
return mp_const_none;
83+
}
84+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit);
85+
#endif
86+
87+
#if MICROPY_PY_MACHINE_ADC_BLOCK
88+
// ADC.block()
89+
STATIC mp_obj_t machine_adc_block(mp_obj_t self_in) {
90+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
91+
return mp_machine_adc_block(self);
92+
}
93+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_block_obj, machine_adc_block);
94+
#endif
95+
96+
// ADC.read_u16()
97+
STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) {
98+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
99+
return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read_u16(self));
100+
}
101+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16);
102+
103+
#if MICROPY_PY_MACHINE_ADC_READ_UV
104+
// ADC.read_uv()
105+
STATIC mp_obj_t machine_adc_read_uv(mp_obj_t self_in) {
106+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
107+
return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read_uv(self));
108+
}
109+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_uv_obj, machine_adc_read_uv);
110+
#endif
111+
112+
#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH
113+
114+
// ADC.atten(value) -- this is a legacy method.
115+
STATIC mp_obj_t machine_adc_atten(mp_obj_t self_in, mp_obj_t atten_in) {
116+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
117+
mp_int_t atten = mp_obj_get_int(atten_in);
118+
mp_machine_adc_atten_set(self, atten);
119+
return mp_const_none;
120+
}
121+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_adc_atten_obj, machine_adc_atten);
122+
123+
// ADC.width(value) -- this is a legacy method.
124+
STATIC mp_obj_t machine_adc_width(mp_obj_t self_in, mp_obj_t width_in) {
125+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
126+
mp_int_t width = mp_obj_get_int(width_in);
127+
mp_machine_adc_width_set(self, width);
128+
return mp_const_none;
129+
}
130+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_adc_width_obj, machine_adc_width);
131+
132+
#endif
133+
134+
#if MICROPY_PY_MACHINE_ADC_READ
135+
// ADC.read() -- this is a legacy method.
136+
STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) {
137+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
138+
return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read(self));
139+
}
140+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read);
141+
#endif
142+
143+
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
144+
#if MICROPY_PY_MACHINE_ADC_INIT
145+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_adc_init_obj) },
146+
#endif
147+
#if MICROPY_PY_MACHINE_ADC_DEINIT
148+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) },
149+
#endif
150+
#if MICROPY_PY_MACHINE_ADC_BLOCK
151+
{ MP_ROM_QSTR(MP_QSTR_block), MP_ROM_PTR(&machine_adc_block_obj) },
152+
#endif
153+
154+
{ MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) },
155+
#if MICROPY_PY_MACHINE_ADC_READ_UV
156+
{ MP_ROM_QSTR(MP_QSTR_read_uv), MP_ROM_PTR(&machine_adc_read_uv_obj) },
157+
#endif
158+
159+
// Legacy methods.
160+
#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH
161+
{ MP_ROM_QSTR(MP_QSTR_atten), MP_ROM_PTR(&machine_adc_atten_obj) },
162+
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&machine_adc_width_obj) },
163+
#endif
164+
#if MICROPY_PY_MACHINE_ADC_READ
165+
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) },
166+
#endif
167+
168+
// A port must add ADC class constants defining the following macro.
169+
// It can be defined to nothing if there are no constants.
170+
MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS
171+
};
172+
STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table);
173+
174+
MP_DEFINE_CONST_OBJ_TYPE(
175+
machine_adc_type,
176+
MP_QSTR_ADC,
177+
MP_TYPE_FLAG_NONE,
178+
make_new, mp_machine_adc_make_new,
179+
print, mp_machine_adc_print,
180+
locals_dict, &machine_adc_locals_dict
181+
);
182+
183+
#endif // MICROPY_PY_MACHINE_ADC

extmod/modmachine.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,52 @@
2929

3030
#include "py/obj.h"
3131

32+
// Whether to enable the ADC.init() method.
33+
// Requires a port to implement mp_machine_adc_init_helper().
34+
#ifndef MICROPY_PY_MACHINE_ADC_INIT
35+
#define MICROPY_PY_MACHINE_ADC_INIT (0)
36+
#endif
37+
38+
// Whether to enable the ADC.deinit() method.
39+
// Requires a port to implement mp_machine_adc_deinit().
40+
#ifndef MICROPY_PY_MACHINE_ADC_DEINIT
41+
#define MICROPY_PY_MACHINE_ADC_DEINIT (0)
42+
#endif
43+
44+
// Whether to enable the ADC.block() method.
45+
// Requires a port to implement mp_machine_adc_block().
46+
#ifndef MICROPY_PY_MACHINE_ADC_BLOCK
47+
#define MICROPY_PY_MACHINE_ADC_BLOCK (0)
48+
#endif
49+
50+
// Whether to enable the ADC.read_uv() method.
51+
// Requires a port to implement mp_machine_adc_read_uv().
52+
#ifndef MICROPY_PY_MACHINE_ADC_READ_UV
53+
#define MICROPY_PY_MACHINE_ADC_READ_UV (0)
54+
#endif
55+
56+
// Whether to enable the ADC.atten() and ADC.width() methods.
57+
// Note: these are legacy and should not be used on new ports.
58+
#ifndef MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH
59+
#define MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH (0)
60+
#endif
61+
62+
// Whether to enable the ADC.read() method.
63+
// Note: this is legacy and should not be used on new ports.
64+
#ifndef MICROPY_PY_MACHINE_ADC_READ
65+
#define MICROPY_PY_MACHINE_ADC_READ (0)
66+
#endif
67+
3268
// A port must provide these types, but they are otherwise opaque.
69+
typedef struct _machine_adc_obj_t machine_adc_obj_t;
3370
typedef struct _machine_i2s_obj_t machine_i2s_obj_t;
3471
typedef struct _machine_pwm_obj_t machine_pwm_obj_t;
3572
typedef struct _machine_wdt_obj_t machine_wdt_obj_t;
3673

3774
// These classes correspond to machine.Type entries in the machine module.
3875
// Their Python bindings are implemented in extmod, and their implementation
3976
// is provided by a port.
77+
extern const mp_obj_type_t machine_adc_type;
4078
extern const mp_obj_type_t machine_i2c_type;
4179
extern const mp_obj_type_t machine_i2s_type;
4280
extern const mp_obj_type_t machine_pwm_type;

ports/esp32/esp32_common.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ list(APPEND MICROPY_SOURCE_PORT
6666
machine_timer.c
6767
machine_pin.c
6868
machine_touchpad.c
69-
machine_adc.c
7069
machine_adcblock.c
7170
machine_dac.c
7271
machine_i2c.c

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy