forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTouchIn.c
178 lines (149 loc) · 6.36 KB
/
TouchIn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
//
// SPDX-License-Identifier: MIT
#include <stdint.h>
#include <string.h>
#include "shared/runtime/context_manager_helpers.h"
#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"
#include "shared-bindings/touchio/TouchIn.h"
#include "shared-bindings/util.h"
//| class TouchIn:
//| """Read the state of a capacitive touch sensor
//|
//| Usage::
//|
//| import touchio
//| from board import *
//|
//| touch = touchio.TouchIn(A1)
//| while True:
//| if touch.value:
//| print("touched!")"""
//|
//| def __init__(self, pin: microcontroller.Pin, pull: Optional[digitalio.Pull] = None) -> None:
//| """Use the TouchIn on the given pin.
//|
//| :param ~microcontroller.Pin pin: the pin to read from
//| :param Optional[digitalio.Pull] pull: specify external pull resistor type. If None, assume pull-down or chip-specific implementation that does not require a pull.
//| """
//| ...
//|
static mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_pin, ARG_pull };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj, MP_QSTR_pin);
const digitalio_pull_t pull = validate_pull(args[ARG_pull].u_obj, MP_QSTR_pull);
touchio_touchin_obj_t *self = mp_obj_malloc(touchio_touchin_obj_t, &touchio_touchin_type);
common_hal_touchio_touchin_construct(self, pin, pull);
return (mp_obj_t)self;
}
//| def deinit(self) -> None:
//| """Deinitialises the TouchIn and releases any hardware resources for reuse."""
//| ...
//|
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);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_deinit_obj, touchio_touchin_deinit);
static void check_for_deinit(touchio_touchin_obj_t *self) {
if (common_hal_touchio_touchin_deinited(self)) {
raise_deinited_error();
}
}
//| def __enter__(self) -> TouchIn:
//| """No-op used by Context Managers."""
//| ...
//|
// Provided by context manager helper.
//| def __exit__(self) -> None:
//| """Automatically deinitializes the hardware when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
//|
// Provided by context manager helper.
//| value: bool
//| """Whether the touch pad is being touched or not. (read-only)
//|
//| True when `raw_value` > `threshold`."""
static mp_obj_t touchio_touchin_obj_get_value(mp_obj_t self_in) {
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return mp_obj_new_bool(common_hal_touchio_touchin_get_value(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_value_obj, touchio_touchin_obj_get_value);
MP_PROPERTY_GETTER(touchio_touchin_value_obj,
(mp_obj_t)&touchio_touchin_get_value_obj);
//| raw_value: int
//| """The raw touch measurement as an `int`. (read-only)"""
static mp_obj_t touchio_touchin_obj_get_raw_value(mp_obj_t self_in) {
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_raw_value(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_raw_value_obj, touchio_touchin_obj_get_raw_value);
MP_PROPERTY_GETTER(touchio_touchin_raw_value_obj,
(mp_obj_t)&touchio_touchin_get_raw_value_obj);
//| threshold: Optional[int]
//| """Minimum `raw_value` needed to detect a touch (and for `value` to be `True`).
//|
//| 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.
//|
//| You can adjust `threshold` to make the pin more or less sensitive::
//|
//| import board
//| import touchio
//|
//| touch = touchio.TouchIn(board.A1)
//| touch.threshold = 7300"""
//|
//|
static mp_obj_t touchio_touchin_obj_get_threshold(mp_obj_t self_in) {
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_threshold(self));
}
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) {
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
uint32_t new_threshold = mp_obj_get_int(threshold_obj);
mp_arg_validate_int_range(new_threshold, 0, UINT16_MAX, MP_QSTR_threshold);
common_hal_touchio_touchin_set_threshold(self, new_threshold);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(touchio_touchin_set_threshold_obj, touchio_touchin_obj_set_threshold);
MP_PROPERTY_GETSET(touchio_touchin_threshold_obj,
(mp_obj_t)&touchio_touchin_get_threshold_obj,
(mp_obj_t)&touchio_touchin_set_threshold_obj);
static const mp_rom_map_elem_t touchio_touchin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&touchio_touchin_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)},
{ MP_ROM_QSTR(MP_QSTR_raw_value), MP_ROM_PTR(&touchio_touchin_raw_value_obj)},
{ MP_ROM_QSTR(MP_QSTR_threshold), MP_ROM_PTR(&touchio_touchin_threshold_obj)},
};
static MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
touchio_touchin_type,
MP_QSTR_TouchIn,
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
make_new, touchio_touchin_make_new,
locals_dict, &touchio_touchin_locals_dict
);