blob: 48250280badafb8b3e14544f77437ad8f08777a3 [file] [log] [blame]
Damien Georgebc08c882016-12-06 12:20:10 +11001/*
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
Damien Georgebc08c882016-12-06 12:20:10 +110027#include "py/runtime.h"
28#include "py/mphal.h"
29#include "modmachine.h"
Damien George66a86a02021-02-18 21:24:34 +110030#include "driver/gpio.h"
Wind-stormger3a373902022-07-24 17:15:35 +080031
Angus Grattoned3c75a2024-11-06 16:48:56 +110032#if SOC_TOUCH_SENSOR_SUPPORTED
Wind-stormger3a373902022-07-24 17:15:35 +080033
Angus Grattoned3c75a2024-11-06 16:48:56 +110034#if SOC_TOUCH_VERSION_1 // ESP32 only
Damien George66a86a02021-02-18 21:24:34 +110035#include "driver/touch_pad.h"
Angus Grattoned3c75a2024-11-06 16:48:56 +110036#elif SOC_TOUCH_VERSION_2 // All other SoCs with touch, to date
Damien George5093d492021-05-06 10:58:12 +100037#include "driver/touch_sensor.h"
Angus Grattoned3c75a2024-11-06 16:48:56 +110038#else
39#error "Unknown touch hardware version"
Damien George5093d492021-05-06 10:58:12 +100040#endif
Damien George66a86a02021-02-18 21:24:34 +110041
Damien Georgebc08c882016-12-06 12:20:10 +110042typedef struct _mtp_obj_t {
43 mp_obj_base_t base;
44 gpio_num_t gpio_id;
45 touch_pad_t touchpad_id;
46} mtp_obj_t;
47
Angus Grattondecf8e62024-02-27 15:32:29 +110048static const mtp_obj_t touchpad_obj[] = {
Wind-stormger3a373902022-07-24 17:15:35 +080049 #if CONFIG_IDF_TARGET_ESP32
Damien Georgebc08c882016-12-06 12:20:10 +110050 {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM0},
51 {{&machine_touchpad_type}, GPIO_NUM_0, TOUCH_PAD_NUM1},
52 {{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2},
53 {{&machine_touchpad_type}, GPIO_NUM_15, TOUCH_PAD_NUM3},
54 {{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM4},
55 {{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM5},
56 {{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM6},
57 {{&machine_touchpad_type}, GPIO_NUM_27, TOUCH_PAD_NUM7},
Damien Georged4470af2018-03-05 14:06:45 +110058 {{&machine_touchpad_type}, GPIO_NUM_33, TOUCH_PAD_NUM8},
59 {{&machine_touchpad_type}, GPIO_NUM_32, TOUCH_PAD_NUM9},
Wind-stormger3a373902022-07-24 17:15:35 +080060 #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
Damien George5093d492021-05-06 10:58:12 +100061 {{&machine_touchpad_type}, GPIO_NUM_1, TOUCH_PAD_NUM1},
62 {{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2},
63 {{&machine_touchpad_type}, GPIO_NUM_3, TOUCH_PAD_NUM3},
64 {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM4},
65 {{&machine_touchpad_type}, GPIO_NUM_5, TOUCH_PAD_NUM5},
66 {{&machine_touchpad_type}, GPIO_NUM_6, TOUCH_PAD_NUM6},
67 {{&machine_touchpad_type}, GPIO_NUM_7, TOUCH_PAD_NUM7},
68 {{&machine_touchpad_type}, GPIO_NUM_8, TOUCH_PAD_NUM8},
69 {{&machine_touchpad_type}, GPIO_NUM_9, TOUCH_PAD_NUM9},
70 {{&machine_touchpad_type}, GPIO_NUM_10, TOUCH_PAD_NUM10},
71 {{&machine_touchpad_type}, GPIO_NUM_11, TOUCH_PAD_NUM11},
72 {{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM12},
73 {{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM13},
74 {{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM14},
Angus Grattoned3c75a2024-11-06 16:48:56 +110075 #else
76 #error "Please add GPIO mapping for this SoC"
Wind-stormger3a373902022-07-24 17:15:35 +080077 #endif
Damien George5093d492021-05-06 10:58:12 +100078};
Damien Georgebc08c882016-12-06 12:20:10 +110079
Angus Grattondecf8e62024-02-27 15:32:29 +110080static mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
Damien George69661f32020-02-27 15:36:53 +110081 const mp_obj_t *args) {
Damien Georgebc08c882016-12-06 12:20:10 +110082 mp_arg_check_num(n_args, n_kw, 1, 1, true);
83 gpio_num_t pin_id = machine_pin_get_id(args[0]);
84 const mtp_obj_t *self = NULL;
85 for (int i = 0; i < MP_ARRAY_SIZE(touchpad_obj); i++) {
Damien George69661f32020-02-27 15:36:53 +110086 if (pin_id == touchpad_obj[i].gpio_id) {
87 self = &touchpad_obj[i];
88 break;
89 }
Damien Georgebc08c882016-12-06 12:20:10 +110090 }
Damien George69661f32020-02-27 15:36:53 +110091 if (!self) {
Jim Mussareddef76fe2020-03-02 22:35:22 +110092 mp_raise_ValueError(MP_ERROR_TEXT("invalid pin for touchpad"));
Damien George69661f32020-02-27 15:36:53 +110093 }
Damien Georgebc08c882016-12-06 12:20:10 +110094
95 static int initialized = 0;
96 if (!initialized) {
97 touch_pad_init();
Tom Manning887a6712019-05-15 05:50:53 -040098 touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
Damien Georgebc08c882016-12-06 12:20:10 +110099 initialized = 1;
100 }
Angus Grattoned3c75a2024-11-06 16:48:56 +1100101 #if SOC_TOUCH_VERSION_1
Damien Georgebc08c882016-12-06 12:20:10 +1100102 esp_err_t err = touch_pad_config(self->touchpad_id, 0);
Angus Grattoned3c75a2024-11-06 16:48:56 +1100103 #elif SOC_TOUCH_VERSION_2
Wind-stormger3a373902022-07-24 17:15:35 +0800104 esp_err_t err = touch_pad_config(self->touchpad_id);
105 #endif
Damien George69661f32020-02-27 15:36:53 +1100106 if (err == ESP_OK) {
Angus Gratton66e699e2024-11-06 17:14:09 +1100107 #if SOC_TOUCH_VERSION_2
108 touch_pad_fsm_start();
109 #endif
110
Damien George69661f32020-02-27 15:36:53 +1100111 return MP_OBJ_FROM_PTR(self);
112 }
Jim Mussareddef76fe2020-03-02 22:35:22 +1100113 mp_raise_ValueError(MP_ERROR_TEXT("Touch pad error"));
Damien Georgebc08c882016-12-06 12:20:10 +1100114}
115
Angus Grattondecf8e62024-02-27 15:32:29 +1100116static mp_obj_t mtp_config(mp_obj_t self_in, mp_obj_t value_in) {
Damien Georgebc08c882016-12-06 12:20:10 +1100117 mtp_obj_t *self = self_in;
Angus Grattoned3c75a2024-11-06 16:48:56 +1100118 #if SOC_TOUCH_VERSION_1
Damien Georgebc08c882016-12-06 12:20:10 +1100119 uint16_t value = mp_obj_get_int(value_in);
120 esp_err_t err = touch_pad_config(self->touchpad_id, value);
Angus Grattoned3c75a2024-11-06 16:48:56 +1100121 #elif SOC_TOUCH_VERSION_2
Wind-stormger3a373902022-07-24 17:15:35 +0800122 esp_err_t err = touch_pad_config(self->touchpad_id);
123 #endif
Damien George69661f32020-02-27 15:36:53 +1100124 if (err == ESP_OK) {
125 return mp_const_none;
126 }
Jim Mussareddef76fe2020-03-02 22:35:22 +1100127 mp_raise_ValueError(MP_ERROR_TEXT("Touch pad error"));
Damien Georgebc08c882016-12-06 12:20:10 +1100128}
129MP_DEFINE_CONST_FUN_OBJ_2(mtp_config_obj, mtp_config);
130
Angus Grattondecf8e62024-02-27 15:32:29 +1100131static mp_obj_t mtp_read(mp_obj_t self_in) {
Damien Georgebc08c882016-12-06 12:20:10 +1100132 mtp_obj_t *self = self_in;
Angus Grattoned3c75a2024-11-06 16:48:56 +1100133 #if SOC_TOUCH_VERSION_1
Damien Georgebc08c882016-12-06 12:20:10 +1100134 uint16_t value;
135 esp_err_t err = touch_pad_read(self->touchpad_id, &value);
Angus Grattoned3c75a2024-11-06 16:48:56 +1100136 #elif SOC_TOUCH_VERSION_2
Wind-stormger3a373902022-07-24 17:15:35 +0800137 uint32_t value;
138 esp_err_t err = touch_pad_read_raw_data(self->touchpad_id, &value);
139 #endif
Damien George69661f32020-02-27 15:36:53 +1100140 if (err == ESP_OK) {
141 return MP_OBJ_NEW_SMALL_INT(value);
142 }
Jim Mussareddef76fe2020-03-02 22:35:22 +1100143 mp_raise_ValueError(MP_ERROR_TEXT("Touch pad error"));
Damien Georgebc08c882016-12-06 12:20:10 +1100144}
145MP_DEFINE_CONST_FUN_OBJ_1(mtp_read_obj, mtp_read);
146
Angus Grattondecf8e62024-02-27 15:32:29 +1100147static const mp_rom_map_elem_t mtp_locals_dict_table[] = {
Damien Georgebc08c882016-12-06 12:20:10 +1100148 // instance methods
149 { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&mtp_config_obj) },
150 { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mtp_read_obj) },
151};
152
Angus Grattondecf8e62024-02-27 15:32:29 +1100153static MP_DEFINE_CONST_DICT(mtp_locals_dict, mtp_locals_dict_table);
Damien Georgebc08c882016-12-06 12:20:10 +1100154
Jim Mussared662b9762021-07-14 14:38:38 +1000155MP_DEFINE_CONST_OBJ_TYPE(
156 machine_touchpad_type,
157 MP_QSTR_TouchPad,
158 MP_TYPE_FLAG_NONE,
Jim Mussared94beeab2022-09-17 00:31:23 +1000159 make_new, mtp_make_new,
Jim Mussared9dce8272022-06-24 16:27:46 +1000160 locals_dict, &mtp_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000161 );
Damien George66a86a02021-02-18 21:24:34 +1100162
Angus Grattoned3c75a2024-11-06 16:48:56 +1100163#endif // SOC_TOUCH_SENSOR_SUPPORTED