2016-12-06 21:39:18 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2016-12-06 21:39:18 -05: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.
|
|
|
|
*/
|
|
|
|
|
2017-10-01 14:43:42 -04:00
|
|
|
#include <stdint.h>
|
2016-12-06 21:39:18 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-02-19 10:11:33 -05:00
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
2016-12-06 21:39:18 -05:00
|
|
|
#include "py/binary.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/touchio/TouchIn.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
#include "shared-bindings/util.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2016-12-06 21:39:18 -05:00
|
|
|
|
2020-05-12 11:43:24 -04:00
|
|
|
//| class TouchIn:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Read the state of a capacitive touch sensor
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2020-05-12 11:43:24 -04:00
|
|
|
//| Usage::
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2020-05-12 11:43:24 -04:00
|
|
|
//| import touchio
|
|
|
|
//| from board import *
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2020-05-12 11:43:24 -04:00
|
|
|
//| touch = touchio.TouchIn(A1)
|
|
|
|
//| while True:
|
|
|
|
//| if touch.value:
|
|
|
|
//| print("touched!")"""
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
|
|
|
|
2020-07-03 14:23:34 -04:00
|
|
|
//| def __init__(self, pin: microcontroller.Pin) -> None:
|
2020-05-12 11:43:24 -04:00
|
|
|
//| """Use the TouchIn on the given pin.
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2020-05-12 11:43:24 -04:00
|
|
|
//| :param ~microcontroller.Pin pin: the pin to read from"""
|
|
|
|
//| ...
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
|
2019-01-14 20:26:36 -05:00
|
|
|
mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
2016-12-06 21:39:18 -05:00
|
|
|
// check number of arguments
|
2019-01-14 20:26:36 -05:00
|
|
|
mp_arg_check_num(n_args, kw_args, 1, 1, false);
|
2016-12-06 21:39:18 -05:00
|
|
|
|
|
|
|
// 1st argument is the pin
|
2020-03-05 16:35:31 -05:00
|
|
|
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0]);
|
2016-12-06 21:39:18 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
touchio_touchin_obj_t *self = m_new_obj(touchio_touchin_obj_t);
|
|
|
|
self->base.type = &touchio_touchin_type;
|
|
|
|
common_hal_touchio_touchin_construct(self, pin);
|
2016-12-06 21:39:18 -05:00
|
|
|
|
|
|
|
return (mp_obj_t) self;
|
|
|
|
}
|
|
|
|
|
2020-07-03 11:34:45 -04:00
|
|
|
//| def deinit(self) -> None:
|
2020-05-12 11:43:24 -04:00
|
|
|
//| """Deinitialises the TouchIn and releases any hardware resources for reuse."""
|
|
|
|
//| ...
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t touchio_touchin_deinit(mp_obj_t self_in) {
|
|
|
|
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_touchio_touchin_deinit(self);
|
2016-12-06 21:39:18 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_deinit_obj, touchio_touchin_deinit);
|
2016-12-06 21:39:18 -05:00
|
|
|
|
2019-06-12 04:00:59 -04:00
|
|
|
STATIC void check_for_deinit(touchio_touchin_obj_t *self) {
|
|
|
|
if (common_hal_touchio_touchin_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 11:34:45 -04:00
|
|
|
//| def __enter__(self) -> TouchIn:
|
2020-05-12 11:43:24 -04:00
|
|
|
//| """No-op used by Context Managers."""
|
|
|
|
//| ...
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2017-02-19 10:11:33 -05:00
|
|
|
// Provided by context manager helper.
|
2016-12-06 21:39:18 -05:00
|
|
|
|
2020-07-03 11:34:45 -04:00
|
|
|
//| def __exit__(self) -> None:
|
2020-05-12 11:43:24 -04:00
|
|
|
//| """Automatically deinitializes the hardware when exiting a context. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
|
|
|
//| ...
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t touchio_touchin_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
2016-12-06 21:39:18 -05:00
|
|
|
(void)n_args;
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_touchio_touchin_deinit(args[0]);
|
2016-12-06 21:39:18 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(touchio_touchin___exit___obj, 4, 4, touchio_touchin_obj___exit__);
|
2016-12-06 21:39:18 -05:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| value: bool
|
2020-05-12 11:43:24 -04:00
|
|
|
//| """Whether the touch pad is being touched or not. (read-only)
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2020-05-12 11:43:24 -04:00
|
|
|
//| True when `raw_value` > `threshold`."""
|
2016-12-06 21:39:18 -05:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t touchio_touchin_obj_get_value(mp_obj_t self_in) {
|
2017-10-02 20:49:40 -04:00
|
|
|
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-10-02 20:49:40 -04:00
|
|
|
return mp_obj_new_bool(common_hal_touchio_touchin_get_value(self));
|
2016-12-06 21:39:18 -05:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_value_obj, touchio_touchin_obj_get_value);
|
2016-12-06 21:39:18 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_property_t touchio_touchin_value_obj = {
|
2016-12-06 21:39:18 -05:00
|
|
|
.base.type = &mp_type_property,
|
2017-04-10 16:32:19 -04:00
|
|
|
.proxy = {(mp_obj_t)&touchio_touchin_get_value_obj,
|
2016-12-06 21:39:18 -05:00
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2017-10-01 14:43:42 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| raw_value: int
|
2020-05-12 11:43:24 -04:00
|
|
|
//| """The raw touch measurement as an `int`. (read-only)"""
|
2017-10-01 14:43:42 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t touchio_touchin_obj_get_raw_value(mp_obj_t self_in) {
|
2017-10-02 20:49:40 -04:00
|
|
|
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-10-02 20:49:40 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_raw_value(self));
|
2017-10-01 14:43:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_raw_value_obj, touchio_touchin_obj_get_raw_value);
|
|
|
|
|
|
|
|
const mp_obj_property_t touchio_touchin_raw_value_obj = {
|
2017-10-02 20:49:40 -04:00
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&touchio_touchin_get_raw_value_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
2017-10-01 14:43:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| threshold: Optional[int]
|
2020-05-12 11:43:24 -04:00
|
|
|
//| """Minimum `raw_value` needed to detect a touch (and for `value` to be `True`).
|
2018-02-07 16:08:42 -05:00
|
|
|
//|
|
2017-10-01 14:43:42 -04:00
|
|
|
//| When the **TouchIn** object is created, an initial `raw_value` is read from the pin,
|
|
|
|
//| and then `threshold` is set to be 100 + that value.
|
|
|
|
//|
|
2021-03-14 16:20:40 -04:00
|
|
|
//| You can adjust `threshold` to make the pin more or less sensitive::
|
|
|
|
//|
|
|
|
|
//| import board
|
|
|
|
//| import touchio
|
|
|
|
//|
|
|
|
|
//| touch = touchio.TouchIn(board.A1)
|
|
|
|
//| touch.threshold = 7300"""
|
2017-10-01 14:43:42 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t touchio_touchin_obj_get_threshold(mp_obj_t self_in) {
|
2017-10-02 20:49:40 -04:00
|
|
|
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-10-02 20:49:40 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_threshold(self));
|
2017-10-01 14:43:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_threshold_obj, touchio_touchin_obj_get_threshold);
|
|
|
|
|
|
|
|
STATIC mp_obj_t touchio_touchin_obj_set_threshold(mp_obj_t self_in, mp_obj_t threshold_obj) {
|
2017-10-02 20:49:40 -04:00
|
|
|
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-10-02 20:49:40 -04:00
|
|
|
uint32_t new_threshold = mp_obj_get_int(threshold_obj);
|
|
|
|
if (new_threshold < 0 || new_threshold > UINT16_MAX) {
|
|
|
|
// I would use MP_STRINGIFY(UINT16_MAX), but that prints "0xffff" instead of 65536.
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("threshold must be in the range 0-65536"));
|
2017-10-02 20:49:40 -04:00
|
|
|
}
|
|
|
|
common_hal_touchio_touchin_set_threshold(self, new_threshold);
|
|
|
|
return mp_const_none;
|
2017-10-01 14:43:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(touchio_touchin_set_threshold_obj, touchio_touchin_obj_set_threshold);
|
|
|
|
|
|
|
|
const mp_obj_property_t touchio_touchin_threshold_obj = {
|
2017-10-02 20:49:40 -04:00
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&touchio_touchin_get_threshold_obj,
|
|
|
|
(mp_obj_t)&touchio_touchin_set_threshold_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
2017-10-01 14:43:42 -04:00
|
|
|
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC const mp_rom_map_elem_t touchio_touchin_locals_dict_table[] = {
|
2017-02-19 10:11:33 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&touchio_touchin___exit___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&touchio_touchin_deinit_obj) },
|
2016-12-06 21:39:18 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)},
|
2017-10-01 14:43:42 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_raw_value), MP_ROM_PTR(&touchio_touchin_raw_value_obj)},
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_threshold), MP_ROM_PTR(&touchio_touchin_threshold_obj)},
|
2016-12-06 21:39:18 -05:00
|
|
|
};
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table);
|
2016-12-06 21:39:18 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_type_t touchio_touchin_type = {
|
2016-12-06 21:39:18 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_TouchIn,
|
2017-04-10 16:32:19 -04:00
|
|
|
.make_new = touchio_touchin_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&touchio_touchin_locals_dict,
|
2016-12-06 21:39:18 -05:00
|
|
|
};
|