|
| 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 "modmachine.h" |
| 38 | + |
| 39 | +typedef struct _mtp_obj_t { |
| 40 | + mp_obj_base_t base; |
| 41 | + gpio_num_t gpio_id; |
| 42 | + touch_pad_t touchpad_id; |
| 43 | +} mtp_obj_t; |
| 44 | + |
| 45 | +STATIC const mtp_obj_t touchpad_obj[] = { |
| 46 | + {{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM0}, |
| 47 | + {{&machine_touchpad_type}, GPIO_NUM_0, TOUCH_PAD_NUM1}, |
| 48 | + {{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2}, |
| 49 | + {{&machine_touchpad_type}, GPIO_NUM_15, TOUCH_PAD_NUM3}, |
| 50 | + {{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM4}, |
| 51 | + {{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM5}, |
| 52 | + {{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM6}, |
| 53 | + {{&machine_touchpad_type}, GPIO_NUM_27, TOUCH_PAD_NUM7}, |
| 54 | + {{&machine_touchpad_type}, GPIO_NUM_33, TOUCH_PAD_NUM8}, |
| 55 | + {{&machine_touchpad_type}, GPIO_NUM_32, TOUCH_PAD_NUM9}, |
| 56 | +}; |
| 57 | + |
| 58 | +STATIC mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, |
| 59 | + const mp_obj_t *args) { |
| 60 | + |
| 61 | + mp_arg_check_num(n_args, n_kw, 1, 1, true); |
| 62 | + gpio_num_t pin_id = machine_pin_get_id(args[0]); |
| 63 | + const mtp_obj_t *self = NULL; |
| 64 | + for (int i = 0; i < MP_ARRAY_SIZE(touchpad_obj); i++) { |
| 65 | + if (pin_id == touchpad_obj[i].gpio_id) { self = &touchpad_obj[i]; break; } |
| 66 | + } |
| 67 | + if (!self) nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin for touchpad")); |
| 68 | + |
| 69 | + static int initialized = 0; |
| 70 | + if (!initialized) { |
| 71 | + touch_pad_init(); |
| 72 | + initialized = 1; |
| 73 | + } |
| 74 | + esp_err_t err = touch_pad_config(self->touchpad_id, 0); |
| 75 | + if (err == ESP_OK) return MP_OBJ_FROM_PTR(self); |
| 76 | + mp_raise_ValueError("Touch pad error"); |
| 77 | +} |
| 78 | + |
| 79 | +STATIC mp_obj_t mtp_config(mp_obj_t self_in, mp_obj_t value_in) { |
| 80 | + mtp_obj_t *self = self_in; |
| 81 | + uint16_t value = mp_obj_get_int(value_in); |
| 82 | + esp_err_t err = touch_pad_config(self->touchpad_id, value); |
| 83 | + if (err == ESP_OK) return mp_const_none; |
| 84 | + mp_raise_ValueError("Touch pad error"); |
| 85 | +} |
| 86 | +MP_DEFINE_CONST_FUN_OBJ_2(mtp_config_obj, mtp_config); |
| 87 | + |
| 88 | +STATIC mp_obj_t mtp_read(mp_obj_t self_in) { |
| 89 | + mtp_obj_t *self = self_in; |
| 90 | + uint16_t value; |
| 91 | + esp_err_t err = touch_pad_read(self->touchpad_id, &value); |
| 92 | + if (err == ESP_OK) return MP_OBJ_NEW_SMALL_INT(value); |
| 93 | + mp_raise_ValueError("Touch pad error"); |
| 94 | +} |
| 95 | +MP_DEFINE_CONST_FUN_OBJ_1(mtp_read_obj, mtp_read); |
| 96 | + |
| 97 | +STATIC const mp_rom_map_elem_t mtp_locals_dict_table[] = { |
| 98 | + // instance methods |
| 99 | + { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&mtp_config_obj) }, |
| 100 | + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mtp_read_obj) }, |
| 101 | +}; |
| 102 | + |
| 103 | +STATIC MP_DEFINE_CONST_DICT(mtp_locals_dict, mtp_locals_dict_table); |
| 104 | + |
| 105 | +const mp_obj_type_t machine_touchpad_type = { |
| 106 | + { &mp_type_type }, |
| 107 | + .name = MP_QSTR_TouchPad, |
| 108 | + .make_new = mtp_make_new, |
| 109 | + .locals_dict = (mp_obj_t)&mtp_locals_dict, |
| 110 | +}; |
0 commit comments