Files

145 lines
4.9 KiB
C
Raw Permalink Normal View History

2018-01-03 14:18:06 +01:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nick Moore
2018-12-04 14:06:15 +08:00
* Copyright (c) 2018 LoBo (https://github.com/loboris)
2018-01-03 14:18:06 +01:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/touch_pad.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "modmachine.h"
2018-12-04 14:06:15 +08:00
#include "modmachine.h"
2018-01-03 14:18:06 +01:00
typedef struct _mtp_obj_t {
mp_obj_base_t base;
gpio_num_t gpio_id;
touch_pad_t touchpad_id;
} mtp_obj_t;
2018-12-04 14:06:15 +08:00
STATIC mtp_obj_t touchpad_obj[] = {
2018-01-03 14:18:06 +01:00
{{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM0},
{{&machine_touchpad_type}, GPIO_NUM_0, TOUCH_PAD_NUM1},
{{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2},
{{&machine_touchpad_type}, GPIO_NUM_15, TOUCH_PAD_NUM3},
{{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM4},
{{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM5},
{{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM6},
{{&machine_touchpad_type}, GPIO_NUM_27, TOUCH_PAD_NUM7},
{{&machine_touchpad_type}, GPIO_NUM_33, TOUCH_PAD_NUM8},
{{&machine_touchpad_type}, GPIO_NUM_32, TOUCH_PAD_NUM9},
};
2018-12-04 14:06:15 +08:00
static int initialized = 0;
static uint16_t threshold[10] = {0};
2018-01-03 14:18:06 +01:00
2018-12-04 14:06:15 +08:00
//------------------------------------------------------------------------------------
STATIC void mtp_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
{
mtp_obj_t *self = self_in;
mp_printf(print, "Touchpad (Pin=%u, TP=%d, Threshold=%u)", self->gpio_id, self->touchpad_id, threshold[self->touchpad_id]);
}
//-------------------------------------------------------------------------------------------------------
STATIC mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
{
2018-01-03 14:18:06 +01:00
mp_arg_check_num(n_args, n_kw, 1, 1, true);
2018-12-04 14:06:15 +08:00
gpio_num_t pin_id = machine_pin_get_gpio(args[0]);
2018-01-03 14:18:06 +01:00
const mtp_obj_t *self = NULL;
2018-12-04 14:06:15 +08:00
2018-01-03 14:18:06 +01:00
for (int i = 0; i < MP_ARRAY_SIZE(touchpad_obj); i++) {
2018-12-04 14:06:15 +08:00
if (pin_id == touchpad_obj[i].gpio_id) {
self = &touchpad_obj[i];
break;
}
2018-01-03 14:18:06 +01:00
}
if (!self) mp_raise_ValueError("invalid pin for touchpad");
if (!initialized) {
touch_pad_init();
initialized = 1;
}
2018-12-04 14:06:15 +08:00
threshold[self->touchpad_id] = 0;
2018-01-03 14:18:06 +01:00
esp_err_t err = touch_pad_config(self->touchpad_id, 0);
2018-12-04 14:06:15 +08:00
if (err != ESP_OK) {
mp_raise_ValueError("Touch pad config error");
}
return MP_OBJ_FROM_PTR(self);
2018-01-03 14:18:06 +01:00
}
2018-12-04 14:06:15 +08:00
//-------------------------------------------------------------
STATIC mp_obj_t mtp_config(mp_obj_t self_in, mp_obj_t value_in)
{
2018-01-03 14:18:06 +01:00
mtp_obj_t *self = self_in;
uint16_t value = mp_obj_get_int(value_in);
2018-12-04 14:06:15 +08:00
2018-01-03 14:18:06 +01:00
esp_err_t err = touch_pad_config(self->touchpad_id, value);
2018-12-04 14:06:15 +08:00
if (err != ESP_OK) {
mp_raise_ValueError("Touch pad config error");
}
threshold[self->touchpad_id] = value;
return mp_const_none;
2018-01-03 14:18:06 +01:00
}
MP_DEFINE_CONST_FUN_OBJ_2(mtp_config_obj, mtp_config);
2018-12-04 14:06:15 +08:00
//----------------------------------------
STATIC mp_obj_t mtp_read(mp_obj_t self_in)
{
2018-01-03 14:18:06 +01:00
mtp_obj_t *self = self_in;
uint16_t value;
2018-12-04 14:06:15 +08:00
2018-01-03 14:18:06 +01:00
esp_err_t err = touch_pad_read(self->touchpad_id, &value);
2018-12-04 14:06:15 +08:00
if (err != ESP_OK) {
mp_raise_ValueError("Touch pad read error");
}
return MP_OBJ_NEW_SMALL_INT(value);
2018-01-03 14:18:06 +01:00
}
MP_DEFINE_CONST_FUN_OBJ_1(mtp_read_obj, mtp_read);
2018-12-04 14:06:15 +08:00
//========================================================
2018-01-03 14:18:06 +01:00
STATIC const mp_rom_map_elem_t mtp_locals_dict_table[] = {
// instance methods
2018-12-04 14:06:15 +08:00
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&mtp_config_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mtp_read_obj) },
2018-01-03 14:18:06 +01:00
};
STATIC MP_DEFINE_CONST_DICT(mtp_locals_dict, mtp_locals_dict_table);
2018-12-04 14:06:15 +08:00
//-------------------------------------------
2018-01-03 14:18:06 +01:00
const mp_obj_type_t machine_touchpad_type = {
{ &mp_type_type },
2018-12-04 14:06:15 +08:00
.print = mtp_print,
2018-01-03 14:18:06 +01:00
.name = MP_QSTR_TouchPad,
.make_new = mtp_make_new,
.locals_dict = (mp_obj_t)&mtp_locals_dict,
};