Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 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 | |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 27 | #include "py/runtime.h" |
| 28 | #include "py/mphal.h" |
| 29 | #include "modmachine.h" |
Damien George | 66a86a0 | 2021-02-18 21:24:34 +1100 | [diff] [blame] | 30 | #include "driver/gpio.h" |
Wind-stormger | 3a37390 | 2022-07-24 17:15:35 +0800 | [diff] [blame] | 31 | |
Angus Gratton | ed3c75a | 2024-11-06 16:48:56 +1100 | [diff] [blame] | 32 | #if SOC_TOUCH_SENSOR_SUPPORTED |
Wind-stormger | 3a37390 | 2022-07-24 17:15:35 +0800 | [diff] [blame] | 33 | |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 34 | #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) |
| 35 | #if SOC_TOUCH_VERSION_1 |
| 36 | #define SOC_TOUCH_SENSOR_VERSION (1) |
| 37 | #elif SOC_TOUCH_VERSION_2 |
| 38 | #define SOC_TOUCH_SENSOR_VERSION (2) |
| 39 | #endif |
| 40 | #endif |
| 41 | |
| 42 | #if SOC_TOUCH_SENSOR_VERSION == 1 // ESP32 only |
Damien George | 66a86a0 | 2021-02-18 21:24:34 +1100 | [diff] [blame] | 43 | #include "driver/touch_pad.h" |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 44 | #elif SOC_TOUCH_SENSOR_VERSION == 2 // All other SoCs with touch, to date |
Damien George | 5093d49 | 2021-05-06 10:58:12 +1000 | [diff] [blame] | 45 | #include "driver/touch_sensor.h" |
Angus Gratton | ed3c75a | 2024-11-06 16:48:56 +1100 | [diff] [blame] | 46 | #else |
| 47 | #error "Unknown touch hardware version" |
Damien George | 5093d49 | 2021-05-06 10:58:12 +1000 | [diff] [blame] | 48 | #endif |
Damien George | 66a86a0 | 2021-02-18 21:24:34 +1100 | [diff] [blame] | 49 | |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 50 | typedef struct _mtp_obj_t { |
| 51 | mp_obj_base_t base; |
| 52 | gpio_num_t gpio_id; |
| 53 | touch_pad_t touchpad_id; |
| 54 | } mtp_obj_t; |
| 55 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 56 | static const mtp_obj_t touchpad_obj[] = { |
Wind-stormger | 3a37390 | 2022-07-24 17:15:35 +0800 | [diff] [blame] | 57 | #if CONFIG_IDF_TARGET_ESP32 |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 58 | {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM0}, |
| 59 | {{&machine_touchpad_type}, GPIO_NUM_0, TOUCH_PAD_NUM1}, |
| 60 | {{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2}, |
| 61 | {{&machine_touchpad_type}, GPIO_NUM_15, TOUCH_PAD_NUM3}, |
| 62 | {{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM4}, |
| 63 | {{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM5}, |
| 64 | {{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM6}, |
| 65 | {{&machine_touchpad_type}, GPIO_NUM_27, TOUCH_PAD_NUM7}, |
Damien George | d4470af | 2018-03-05 14:06:45 +1100 | [diff] [blame] | 66 | {{&machine_touchpad_type}, GPIO_NUM_33, TOUCH_PAD_NUM8}, |
| 67 | {{&machine_touchpad_type}, GPIO_NUM_32, TOUCH_PAD_NUM9}, |
Wind-stormger | 3a37390 | 2022-07-24 17:15:35 +0800 | [diff] [blame] | 68 | #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 |
Damien George | 5093d49 | 2021-05-06 10:58:12 +1000 | [diff] [blame] | 69 | {{&machine_touchpad_type}, GPIO_NUM_1, TOUCH_PAD_NUM1}, |
| 70 | {{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2}, |
| 71 | {{&machine_touchpad_type}, GPIO_NUM_3, TOUCH_PAD_NUM3}, |
| 72 | {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM4}, |
| 73 | {{&machine_touchpad_type}, GPIO_NUM_5, TOUCH_PAD_NUM5}, |
| 74 | {{&machine_touchpad_type}, GPIO_NUM_6, TOUCH_PAD_NUM6}, |
| 75 | {{&machine_touchpad_type}, GPIO_NUM_7, TOUCH_PAD_NUM7}, |
| 76 | {{&machine_touchpad_type}, GPIO_NUM_8, TOUCH_PAD_NUM8}, |
| 77 | {{&machine_touchpad_type}, GPIO_NUM_9, TOUCH_PAD_NUM9}, |
| 78 | {{&machine_touchpad_type}, GPIO_NUM_10, TOUCH_PAD_NUM10}, |
| 79 | {{&machine_touchpad_type}, GPIO_NUM_11, TOUCH_PAD_NUM11}, |
| 80 | {{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM12}, |
| 81 | {{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM13}, |
| 82 | {{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM14}, |
Angus Gratton | ed3c75a | 2024-11-06 16:48:56 +1100 | [diff] [blame] | 83 | #else |
| 84 | #error "Please add GPIO mapping for this SoC" |
Wind-stormger | 3a37390 | 2022-07-24 17:15:35 +0800 | [diff] [blame] | 85 | #endif |
Damien George | 5093d49 | 2021-05-06 10:58:12 +1000 | [diff] [blame] | 86 | }; |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 87 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 88 | static mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 89 | const mp_obj_t *args) { |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 90 | mp_arg_check_num(n_args, n_kw, 1, 1, true); |
| 91 | gpio_num_t pin_id = machine_pin_get_id(args[0]); |
| 92 | const mtp_obj_t *self = NULL; |
| 93 | for (int i = 0; i < MP_ARRAY_SIZE(touchpad_obj); i++) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 94 | if (pin_id == touchpad_obj[i].gpio_id) { |
| 95 | self = &touchpad_obj[i]; |
| 96 | break; |
| 97 | } |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 98 | } |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 99 | if (!self) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 100 | mp_raise_ValueError(MP_ERROR_TEXT("invalid pin for touchpad")); |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 101 | } |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 102 | |
| 103 | static int initialized = 0; |
| 104 | if (!initialized) { |
| 105 | touch_pad_init(); |
Tom Manning | 887a671 | 2019-05-15 05:50:53 -0400 | [diff] [blame] | 106 | touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 107 | initialized = 1; |
| 108 | } |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 109 | #if SOC_TOUCH_SENSOR_VERSION == 1 |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 110 | esp_err_t err = touch_pad_config(self->touchpad_id, 0); |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 111 | #elif SOC_TOUCH_SENSOR_VERSION == 2 |
Wind-stormger | 3a37390 | 2022-07-24 17:15:35 +0800 | [diff] [blame] | 112 | esp_err_t err = touch_pad_config(self->touchpad_id); |
| 113 | #endif |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 114 | if (err == ESP_OK) { |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 115 | #if SOC_TOUCH_SENSOR_VERSION == 2 |
Angus Gratton | 66e699e | 2024-11-06 17:14:09 +1100 | [diff] [blame] | 116 | touch_pad_fsm_start(); |
| 117 | #endif |
| 118 | |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 119 | return MP_OBJ_FROM_PTR(self); |
| 120 | } |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 121 | mp_raise_ValueError(MP_ERROR_TEXT("Touch pad error")); |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 122 | } |
| 123 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 124 | static mp_obj_t mtp_config(mp_obj_t self_in, mp_obj_t value_in) { |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 125 | mtp_obj_t *self = self_in; |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 126 | #if SOC_TOUCH_SENSOR_VERSION == 1 |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 127 | uint16_t value = mp_obj_get_int(value_in); |
| 128 | esp_err_t err = touch_pad_config(self->touchpad_id, value); |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 129 | #elif SOC_TOUCH_SENSOR_VERSION == 2 |
Wind-stormger | 3a37390 | 2022-07-24 17:15:35 +0800 | [diff] [blame] | 130 | esp_err_t err = touch_pad_config(self->touchpad_id); |
| 131 | #endif |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 132 | if (err == ESP_OK) { |
| 133 | return mp_const_none; |
| 134 | } |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 135 | mp_raise_ValueError(MP_ERROR_TEXT("Touch pad error")); |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 136 | } |
| 137 | MP_DEFINE_CONST_FUN_OBJ_2(mtp_config_obj, mtp_config); |
| 138 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 139 | static mp_obj_t mtp_read(mp_obj_t self_in) { |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 140 | mtp_obj_t *self = self_in; |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 141 | #if SOC_TOUCH_SENSOR_VERSION == 1 |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 142 | uint16_t value; |
| 143 | esp_err_t err = touch_pad_read(self->touchpad_id, &value); |
Daniël van de Giessen | 5fb846d | 2024-12-03 11:08:13 +0100 | [diff] [blame] | 144 | #elif SOC_TOUCH_SENSOR_VERSION == 2 |
Wind-stormger | 3a37390 | 2022-07-24 17:15:35 +0800 | [diff] [blame] | 145 | uint32_t value; |
| 146 | esp_err_t err = touch_pad_read_raw_data(self->touchpad_id, &value); |
| 147 | #endif |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 148 | if (err == ESP_OK) { |
| 149 | return MP_OBJ_NEW_SMALL_INT(value); |
| 150 | } |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 151 | mp_raise_ValueError(MP_ERROR_TEXT("Touch pad error")); |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 152 | } |
| 153 | MP_DEFINE_CONST_FUN_OBJ_1(mtp_read_obj, mtp_read); |
| 154 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 155 | static const mp_rom_map_elem_t mtp_locals_dict_table[] = { |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 156 | // instance methods |
| 157 | { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&mtp_config_obj) }, |
| 158 | { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mtp_read_obj) }, |
| 159 | }; |
| 160 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 161 | static MP_DEFINE_CONST_DICT(mtp_locals_dict, mtp_locals_dict_table); |
Damien George | bc08c88 | 2016-12-06 12:20:10 +1100 | [diff] [blame] | 162 | |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 163 | MP_DEFINE_CONST_OBJ_TYPE( |
| 164 | machine_touchpad_type, |
| 165 | MP_QSTR_TouchPad, |
| 166 | MP_TYPE_FLAG_NONE, |
Jim Mussared | 94beeab | 2022-09-17 00:31:23 +1000 | [diff] [blame] | 167 | make_new, mtp_make_new, |
Jim Mussared | 9dce827 | 2022-06-24 16:27:46 +1000 | [diff] [blame] | 168 | locals_dict, &mtp_locals_dict |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 169 | ); |
Damien George | 66a86a0 | 2021-02-18 21:24:34 +1100 | [diff] [blame] | 170 | |
Angus Gratton | ed3c75a | 2024-11-06 16:48:56 +1100 | [diff] [blame] | 171 | #endif // SOC_TOUCH_SENSOR_SUPPORTED |