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

Commit 00ae942

Browse files
geza-pycomiwahdan88
authored andcommitted
PYFW-318: Integrate Touch Esp32 functionality exposed to micropy
1 parent 4d3322a commit 00ae942

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

esp32/application.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ APP_MODS_SRC_C = $(addprefix mods/,\
140140
machwdt.c \
141141
machrmt.c \
142142
lwipsocket.c \
143+
machtouch.c \
143144
)
144145

145146
APP_MODS_LORA_SRC_C = $(addprefix mods/,\

esp32/mods/machtouch.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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/touch_pad.h"
34+
35+
#include "py/runtime.h"
36+
#include "py/mphal.h"
37+
#include "machtouch.h"
38+
#include "machpin.h"
39+
40+
#include "freertos/FreeRTOS.h"
41+
#include "freertos/task.h"
42+
43+
44+
#define TOUCHPAD_FILTER_TOUCH_PERIOD_MS (10)
45+
46+
47+
typedef struct _mtp_obj_t {
48+
mp_obj_base_t base;
49+
gpio_num_t gpio_id;
50+
touch_pad_t touchpad_id;
51+
uint16_t init_value;
52+
} mtp_obj_t;
53+
54+
STATIC mtp_obj_t touchpad_obj[] = {
55+
{{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM0},
56+
{{&machine_touchpad_type}, GPIO_NUM_0, TOUCH_PAD_NUM1},
57+
{{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2},
58+
{{&machine_touchpad_type}, GPIO_NUM_15, TOUCH_PAD_NUM3},
59+
{{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM4},
60+
{{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM5},
61+
{{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM6},
62+
{{&machine_touchpad_type}, GPIO_NUM_27, TOUCH_PAD_NUM7},
63+
{{&machine_touchpad_type}, GPIO_NUM_33, TOUCH_PAD_NUM8},
64+
{{&machine_touchpad_type}, GPIO_NUM_32, TOUCH_PAD_NUM9},
65+
};
66+
67+
STATIC mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
68+
const mp_obj_t *args) {
69+
70+
mp_arg_check_num(n_args, n_kw, 1, 1, true);
71+
pin_obj_t *pin = pin_find(args[0]);
72+
mtp_obj_t *self = NULL;
73+
for (int i = 0; i < MP_ARRAY_SIZE(touchpad_obj); i++) {
74+
if (pin->pin_number == touchpad_obj[i].gpio_id) { self = &touchpad_obj[i]; break; }
75+
}
76+
if (!self) mp_raise_ValueError("invalid pin for touchpad");
77+
78+
static int initialized = 0;
79+
if (!initialized) {
80+
touch_pad_init();
81+
// If use interrupt trigger mode, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
82+
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
83+
// Set reference voltage for charging/discharging
84+
// For most usage scenarios, we recommend using the following combination:
85+
// the high reference valtage will be 2.7V - 1V = 1.7V, The low reference voltage will be 0.5V.
86+
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
87+
// initialize and start a software filter to detect slight changes in capacitance
88+
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD_MS);
89+
touch_pad_intr_disable();
90+
touch_pad_clear_status();
91+
initialized = 1;
92+
}
93+
esp_err_t err = touch_pad_config(self->touchpad_id, 0);
94+
mp_hal_delay_ms(TOUCHPAD_FILTER_TOUCH_PERIOD_MS * 2);
95+
touch_pad_read_filtered(self->touchpad_id, &self->init_value);
96+
97+
if (err == ESP_OK) return MP_OBJ_FROM_PTR(self);
98+
mp_raise_ValueError("Touch pad error");
99+
}
100+
101+
STATIC mp_obj_t mtp_config(mp_obj_t self_in, mp_obj_t value_in) {
102+
mtp_obj_t *self = self_in;
103+
uint16_t value = mp_obj_get_int(value_in);
104+
esp_err_t err = touch_pad_config(self->touchpad_id, value);
105+
if (err == ESP_OK) return mp_const_none;
106+
mp_raise_ValueError("Touch pad error");
107+
}
108+
MP_DEFINE_CONST_FUN_OBJ_2(mtp_config_obj, mtp_config);
109+
110+
STATIC mp_obj_t mtp_read(mp_obj_t self_in) {
111+
mtp_obj_t *self = self_in;
112+
uint16_t value;
113+
esp_err_t err = touch_pad_read_filtered(self->touchpad_id, &value);
114+
if (err == ESP_OK) return MP_OBJ_NEW_SMALL_INT(value);
115+
mp_raise_ValueError("Touch pad error");
116+
}
117+
MP_DEFINE_CONST_FUN_OBJ_1(mtp_read_obj, mtp_read);
118+
119+
STATIC mp_obj_t mtp_init_value(uint n_args, const mp_obj_t *arg) {
120+
mtp_obj_t *self = arg[0];
121+
if (n_args > 1) {
122+
self->init_value = mp_obj_get_int(arg[1]);
123+
return mp_const_none;
124+
} else {
125+
return MP_OBJ_NEW_SMALL_INT(self->init_value);
126+
}
127+
}
128+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mtp_init_value_obj, 1, 2, mtp_init_value);
129+
130+
STATIC const mp_rom_map_elem_t mtp_locals_dict_table[] = {
131+
// instance methods
132+
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&mtp_config_obj) },
133+
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mtp_read_obj) },
134+
{ MP_ROM_QSTR(MP_QSTR_init_value), MP_ROM_PTR(&mtp_init_value_obj) },
135+
};
136+
137+
STATIC MP_DEFINE_CONST_DICT(mtp_locals_dict, mtp_locals_dict_table);
138+
139+
const mp_obj_type_t machine_touchpad_type = {
140+
{ &mp_type_type },
141+
.name = MP_QSTR_TouchPad,
142+
.make_new = mtp_make_new,
143+
.locals_dict = (mp_obj_t)&mtp_locals_dict,
144+
};

esp32/mods/machtouch.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2016, Pycom Limited.
3+
*
4+
* This software is licensed under the GNU GPL version 3 or any
5+
* later version, with permitted additional terms. For more information
6+
* see the Pycom Licence v1.0 document supplied with this file, or
7+
* available at https://www.pycom.io/opensource/licensing
8+
*/
9+
10+
#ifndef MACHTOUCH_H_
11+
#define MACHTOUCH_H_
12+
13+
extern const mp_obj_type_t machine_touchpad_type;
14+
15+
#endif // MACHTOUCH_H_

esp32/mods/modmachine.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "machwdt.h"
7676
#include "machcan.h"
7777
#include "machrmt.h"
78+
#include "machtouch.h"
7879
#include "pycom_config.h"
7980
#if defined (GPY) || defined (FIPY)
8081
#include "lteppp.h"
@@ -364,6 +365,8 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
364365
{ MP_OBJ_NEW_QSTR(MP_QSTR_WDT), (mp_obj_t)&mach_wdt_type },
365366
{ MP_OBJ_NEW_QSTR(MP_QSTR_CAN), (mp_obj_t)&mach_can_type },
366367
{ MP_OBJ_NEW_QSTR(MP_QSTR_RMT), (mp_obj_t)&mach_rmt_type },
368+
{ MP_OBJ_NEW_QSTR(MP_QSTR_Touch), (mp_obj_t)&machine_touchpad_type },
369+
367370

368371
// constants
369372
{ MP_OBJ_NEW_QSTR(MP_QSTR_PWRON_RESET), MP_OBJ_NEW_SMALL_INT(MPSLEEP_PWRON_RESET) },

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