Skip to content

Commit ff547b4

Browse files
nickzoicdpgeorge
authored andcommitted
esp32/machine_adc.c: also machine.ADC
1 parent c471049 commit ff547b4

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

esp32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ SRC_C = \
120120
moduos.c \
121121
machine_pin.c \
122122
machine_touchpad.c \
123+
machine_adc.c \
123124
modmachine.c \
124125
modnetwork.c \
125126
modsocket.c \

esp32/machine_adc.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Nick Moore
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+
28+
#include <stdio.h>
29+
30+
#include "esp_log.h"
31+
32+
#include "driver/gpio.h"
33+
#include "driver/adc.h"
34+
35+
#include "py/runtime.h"
36+
#include "py/mphal.h"
37+
#include "modmachine.h"
38+
39+
typedef struct _madc_obj_t {
40+
mp_obj_base_t base;
41+
gpio_num_t gpio_id;
42+
adc1_channel_t adc1_id;
43+
} madc_obj_t;
44+
45+
STATIC const madc_obj_t madc_obj[] = {
46+
{{&machine_adc_type}, GPIO_NUM_36, ADC1_CHANNEL_0},
47+
{{&machine_adc_type}, GPIO_NUM_37, ADC1_CHANNEL_1},
48+
{{&machine_adc_type}, GPIO_NUM_38, ADC1_CHANNEL_2},
49+
{{&machine_adc_type}, GPIO_NUM_39, ADC1_CHANNEL_3},
50+
{{&machine_adc_type}, GPIO_NUM_32, ADC1_CHANNEL_4},
51+
{{&machine_adc_type}, GPIO_NUM_33, ADC1_CHANNEL_5},
52+
{{&machine_adc_type}, GPIO_NUM_34, ADC1_CHANNEL_6},
53+
{{&machine_adc_type}, GPIO_NUM_35, ADC1_CHANNEL_7},
54+
};
55+
56+
STATIC mp_obj_t madc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
57+
const mp_obj_t *args) {
58+
59+
static int initialized = 0;
60+
if (!initialized) {
61+
adc1_config_width(ADC_WIDTH_12Bit);
62+
initialized = 1;
63+
}
64+
65+
mp_arg_check_num(n_args, n_kw, 1, 1, true);
66+
gpio_num_t pin_id = machine_pin_get_id(args[0]);
67+
const madc_obj_t *self = NULL;
68+
for (int i = 0; i < MP_ARRAY_SIZE(madc_obj); i++) {
69+
if (pin_id == madc_obj[i].gpio_id) { self = &madc_obj[i]; break; }
70+
}
71+
if (!self) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid Pin for ADC"));
72+
esp_err_t err = adc1_config_channel_atten(self->adc1_id, ADC_ATTEN_0db);
73+
if (err == ESP_OK) return MP_OBJ_FROM_PTR(self);
74+
mp_raise_ValueError("Parameter Error");
75+
}
76+
77+
STATIC void madc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
78+
madc_obj_t *self = self_in;
79+
mp_printf(print, "ADC(Pin(%u))", self->gpio_id);
80+
}
81+
82+
STATIC mp_obj_t madc_read(mp_obj_t self_in) {
83+
madc_obj_t *self = self_in;
84+
int val = adc1_get_voltage(self->adc1_id);
85+
if (val == -1) mp_raise_ValueError("Parameter Error");
86+
return MP_OBJ_NEW_SMALL_INT(val);
87+
}
88+
MP_DEFINE_CONST_FUN_OBJ_1(madc_read_obj, madc_read);
89+
90+
STATIC mp_obj_t madc_atten(mp_obj_t self_in, mp_obj_t atten_in) {
91+
madc_obj_t *self = self_in;
92+
adc_atten_t atten = mp_obj_get_int(atten_in);
93+
esp_err_t err = adc1_config_channel_atten(self->adc1_id, atten);
94+
if (err == ESP_OK) return mp_const_none;
95+
mp_raise_ValueError("Parameter Error");
96+
}
97+
MP_DEFINE_CONST_FUN_OBJ_2(madc_atten_obj, madc_atten);
98+
99+
STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) {
100+
adc_bits_width_t width = mp_obj_get_int(width_in);
101+
esp_err_t err = adc1_config_width(width);
102+
if (err == ESP_OK) return mp_const_none;
103+
mp_raise_ValueError("Parameter Error");
104+
}
105+
MP_DEFINE_CONST_FUN_OBJ_2(madc_width_fun_obj, madc_width);
106+
MP_DEFINE_CONST_CLASSMETHOD_OBJ(madc_width_obj, MP_ROM_PTR(&madc_width_fun_obj));
107+
108+
STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = {
109+
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&madc_read_obj) },
110+
{ MP_ROM_QSTR(MP_QSTR_atten), MP_ROM_PTR(&madc_atten_obj) },
111+
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&madc_width_obj) },
112+
113+
{ MP_ROM_QSTR(MP_QSTR_ATTN_0DB), MP_ROM_INT(ADC_ATTEN_0db) },
114+
{ MP_ROM_QSTR(MP_QSTR_ATTN_2_5DB), MP_ROM_INT(ADC_ATTEN_2_5db) },
115+
{ MP_ROM_QSTR(MP_QSTR_ATTN_6DB), MP_ROM_INT(ADC_ATTEN_6db) },
116+
{ MP_ROM_QSTR(MP_QSTR_ATTN_11DB), MP_ROM_INT(ADC_ATTEN_11db) },
117+
118+
{ MP_ROM_QSTR(MP_QSTR_WIDTH_9BIT), MP_ROM_INT(ADC_WIDTH_9Bit) },
119+
{ MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(ADC_WIDTH_10Bit) },
120+
{ MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(ADC_WIDTH_11Bit) },
121+
{ MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(ADC_WIDTH_12Bit) },
122+
};
123+
124+
STATIC MP_DEFINE_CONST_DICT(madc_locals_dict, madc_locals_dict_table);
125+
126+
const mp_obj_type_t machine_adc_type = {
127+
{ &mp_type_type },
128+
.name = MP_QSTR_ADC,
129+
.print = madc_print,
130+
.make_new = madc_make_new,
131+
.locals_dict = (mp_obj_t)&madc_locals_dict,
132+
};

esp32/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
9191

9292
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
9393
{ MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) },
94+
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
9495
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
9596
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
9697
};

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