8000 esp32/machine_dac.c: implement DAC pins as well · nickzoic/micropython-esp32@fbaace7 · GitHub
[go: up one dir, main page]

Skip to content

Commit fbaace7

Browse files
nickzoicdpgeorge
authored andcommitted
esp32/machine_dac.c: implement DAC pins as well
1 parent ff547b4 commit fbaace7

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

esp32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ SRC_C = \
121121
machine_pin.c \
122122
machine_touchpad.c \
123123
machine_adc.c \
124+
machine_dac.c \
124125
modmachine.c \
125126
modnetwork.c \
126127
modsocket.c \

esp32/machine_dac.c

Lines changed: 93 additions & 0 deletions
D743
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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/dac.h"
34+
35+
#include "py/runtime.h"
36+
#include "py/mphal.h"
37+
#include "modmachine.h"
38+
39+
typedef struct _mdac_obj_t {
40+
mp_obj_base_t base;
41+
gpio_num_t gpio_id;
42+
dac_channel_t dac_id;
43+
} mdac_obj_t;
44+
45+
STATIC const mdac_obj_t mdac_obj[] = {
46+
{{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1},
47+
{{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2},
48+
};
49+
50+
STATIC mp_obj_t mdac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
51+
const mp_obj_t *args) {
52+
53+
mp_arg_check_num(n_args, n_kw, 1, 1, true);
54+
gpio_num_t pin_id = machine_pin_get_id(args[0]);
55+
const mdac_obj_t *self = NULL;
56+
for (int i = 0; i < MP_ARRAY_SIZE(mdac_obj); i++) {
57+
if (pin_id == mdac_obj[i].gpio_id) { self = &mdac_obj[i]; break; }
58+
}
59+
if (!self) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid Pin for DAC"));
60+
esp_err_t err = dac_out_voltage(self->dac_id, 0);
61+
if (err == ESP_OK) return MP_OBJ_FROM_PTR(self);
62+
mp_raise_ValueError("Parameter Error");
63+
}
64+
65+
STATIC void mdac_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
66+
mdac_obj_t *self = self_in;
67+
mp_printf(print, "DAC(Pin(%u))", self->gpio_id);
68+
}
69+
70+
STATIC mp_obj_t mdac_write(mp_obj_t self_in, mp_obj_t value_in) {
71+
mdac_obj_t *self = self_in;
72+
int value = mp_obj_get_int(value_in);
73+
if (value < 0 || value > 255) mp_raise_ValueError("Value out of range");
74+
75+
esp_err_t err = dac_out_voltage(self->dac_id, value);
76+
if (err == ESP_OK) return mp_const_none;
77+
mp_raise_ValueError("Parameter Error");
78+
}
79+
MP_DEFINE_CONST_FUN_OBJ_2(mdac_write_obj, mdac_write);
80+
81+
STATIC const mp_rom_map_elem_t mdac_locals_dict_table[] = {
82+
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mdac_write_obj) },
83+
};
84+
85+
STATIC MP_DEFINE_CONST_DICT(mdac_locals_dict, mdac_locals_dict_table);
86+
87+
const mp_obj_type_t machine_dac_type = {
88+
{ &mp_type_type },
89+
.name = MP_QSTR_DAC,
90+
.print = mdac_print,
91+
.make_new = mdac_make_new,
92+
.locals_dict = (mp_obj_t)&mdac_locals_dict,
93+
};

esp32/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
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) },
9494
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
95+
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
9596
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
9697
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
9798
};

0 commit comments

Comments
 (0)
0