Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Esp32 touchpad, ADC, DAC #23

Closed
wants to merge 4 commits into from

Conversation

nickzoic
Copy link
Collaborator

This provides support for machine.TouchPad, machine.ADC and machine.DAC classes, like so:

import machine

t = machine.TouchPad(machine.Pin(12))
t.read()

a = machine.ADC(machine.Pin(33))
a.width(machine.ADC.WIDTH_11Bit)
a.read()

d = machine.DAC(machine.Pin(25))
d.write(128)

I haven't included IRQ handling for TouchPad or Pin, I'll put that in as a separate PR later.

Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

Maybe I was doing something wrong, or my board is not working correctly, but the ADC seems to be inverted: if I input GND then it reads 4095, if I input 3.3v then it reads 0.

mp_arg_check_num(n_args, n_kw, 1, 1, true);
gpio_num_t pin_id = machine_pin_get_id(args[0]);
const madc_obj_t *self = NULL;
for (int i=0; i<MP_ARRAY_SIZE(madc_obj); i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note coding style: spaces around binary operators (eg i = 0, i < MP_ARRAY_SIZE).


STATIC mp_obj_t madc_width(mp_obj_t self_in, mp_obj_t width_in) {
// XXX This should be a classmethod, as there's only one width
// across all ADC channels.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this a classmethod you can wrap it in MP_DEFINE_CONST_CLASSMETHOD_OBJ; see py/objdict.c dict_fromkeys for how it's done.

{ MP_ROM_QSTR(MP_QSTR_WIDTH_9Bit), MP_ROM_INT(ADC_WIDTH_9Bit) },
{ MP_ROM_QSTR(MP_QSTR_WIDTH_10Bit), MP_ROM_INT(ADC_WIDTH_10Bit) },
{ MP_ROM_QSTR(MP_QSTR_WIDTH_11Bit), MP_ROM_INT(ADC_WIDTH_11Bit) },
{ MP_ROM_QSTR(MP_QSTR_WIDTH_12Bit), MP_ROM_INT(ADC_WIDTH_12Bit) },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be inclined to make these constants fully upper-case, eg WIDTH_12BIT.

@nickzoic
Copy link
Collaborator Author

OK, just pushed fixes for all the above, thanks for the feedback.

The ADC readings thing happens if you don't set a bit width on the ADC, it defaults to that weird behaviour because it isn't initialized properly yet (see comments in esp_idf/components/driver/rtc_module.c:adc1_config_width() ) This is sufficiently annoying that I've just added a line to set the width to 12 bits the first time a machine.ADC() is initialized.

@dpgeorge
Copy link
Member

Great, thanks, I check it and it now works for me.

I notice that the ADC inputs read voltages up to a maximum of 1 volt.

I rebase and merged this PR in c471049, ff547b4 and fbaace7

@dpgeorge dpgeorge closed this Feb 17, 2017
Peetz0r added a commit to Peetz0r/micropython-esp32 that referenced this pull request Jun 10, 2017
We also lose ugfx.print_funts() which was somehow broken. See micropython#23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
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