Skip to content

Commit 7f74412

Browse files
dhalberttannewt
authored andcommitted
Make touch more sensitive. Add .raw_value and .threshold attributes.
1 parent b2dcc5b commit 7f74412

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

atmel-samd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ CFLAGS += -Os -ggdb -DNDEBUG -DENABLE_MICRO_TRACE_BUFFER -DMICROPY_DEBUG_MODULES
137137
else
138138
# -finline-limit can shrink the image size. -finline-limit=80 or so is similar to not having it on.
139139
# There is no simple default value, though.
140-
CFLAGS += -Os -DNDEBUG -flto -finline-limit=49
140+
CFLAGS += -Os -DNDEBUG -flto -finline-limit=39
141141
endif
142142

143143
ifneq ($(FROZEN_DIR),)

atmel-samd/common-hal/touchio/TouchIn.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
7171

7272
adafruit_ptc_init(PTC, &self->config);
7373

74-
self->threshold = 2 * get_raw_reading(self);
74+
// Initial values for pins will vary, depending on what peripherals the pins
75+
// share on-chip.
76+
//
77+
// Set a "touched" threshold not too far above the initial value.
78+
// For simple finger touch, the values may vary as much as a factor of two,
79+
// but for touches using fruit or other objects, the difference is much less.
80+
81+
self->threshold = get_raw_reading(self) + 100;
7582
}
7683

7784
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) {
@@ -87,3 +94,15 @@ bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
8794
uint16_t reading = get_raw_reading(self);
8895
return reading > self->threshold;
8996
}
97+
98+
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {
99+
return get_raw_reading(self);
100+
}
101+
102+
uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) {
103+
return self->threshold;
104+
}
105+
106+
void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold) {
107+
self->threshold = new_threshold;
108+
}

shared-bindings/touchio/TouchIn.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdint.h>
2728
#include <string.h>
2829

2930
#include "lib/utils/context_manager_helpers.h"
@@ -106,6 +107,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(touchio_touchin___exit___obj, 4, 4, t
106107
//| .. attribute:: value
107108
//|
108109
//| Whether the touch pad is being touched or not.
110+
//| True if `raw_value` > `threshold`.
109111
//|
110112
//| :return: True when touched, False otherwise.
111113
//| :rtype: bool
@@ -123,12 +125,76 @@ const mp_obj_property_t touchio_touchin_value_obj = {
123125
(mp_obj_t)&mp_const_none_obj},
124126
};
125127

128+
129+
//| .. attribute:: raw_value
130+
//|
131+
//| The raw touch measurement. Not settable.
132+
//|
133+
//| :return: an integer >= 0
134+
//| :rtype: int
135+
//|
136+
STATIC mp_obj_t touchio_touchin_obj_get_raw_value(mp_obj_t self_in) {
137+
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
138+
return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_raw_value(self));
139+
}
140+
141+
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_raw_value_obj, touchio_touchin_obj_get_raw_value);
142+
143+
const mp_obj_property_t touchio_touchin_raw_value_obj = {
144+
.base.type = &mp_type_property,
145+
.proxy = {(mp_obj_t)&touchio_touchin_get_raw_value_obj,
146+
(mp_obj_t)&mp_const_none_obj,
147+
(mp_obj_t)&mp_const_none_obj},
148+
};
149+
150+
151+
//| .. attribute:: threshold
152+
//|
153+
//| `value` will return True if `raw_value` is greater than than this threshold.
154+
//| When the **TouchIn** object is created, an initial `raw_value` is read from the pin,
155+
//| and then `threshold` is set to be 100 + that value.
156+
//|
157+
//| You can set the threshold to a different value to make the pin more or less sensitive.
158+
//|
159+
//| :return: an integer >= 0
160+
//| :rtype: int
161+
//|
162+
STATIC mp_obj_t touchio_touchin_obj_get_threshold(mp_obj_t self_in) {
163+
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
164+
return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_threshold(self));
165+
}
166+
167+
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_threshold_obj, touchio_touchin_obj_get_threshold);
168+
169+
STATIC mp_obj_t touchio_touchin_obj_set_threshold(mp_obj_t self_in, mp_obj_t threshold_obj) {
170+
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
171+
uint32_t new_threshold = mp_obj_get_int(threshold_obj);
172+
if (new_threshold < 0 || new_threshold > UINT16_MAX) {
173+
// I would use MP_STRINGIFY(UINT16_MAX), but that prints "0xffff" instead of 65536.
174+
mp_raise_ValueError("threshold must be in the range 0-65536");
175+
}
176+
common_hal_touchio_touchin_set_threshold(self, new_threshold);
177+
return mp_const_none;
178+
}
179+
180+
MP_DEFINE_CONST_FUN_OBJ_2(touchio_touchin_set_threshold_obj, touchio_touchin_obj_set_threshold);
181+
182+
const mp_obj_property_t touchio_touchin_threshold_obj = {
183+
.base.type = &mp_type_property,
184+
.proxy = {(mp_obj_t)&touchio_touchin_get_threshold_obj,
185+
(mp_obj_t)&touchio_touchin_set_threshold_obj,
186+
(mp_obj_t)&mp_const_none_obj},
187+
};
188+
189+
126190
STATIC const mp_rom_map_elem_t touchio_touchin_locals_dict_table[] = {
127191
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
128192
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&touchio_touchin___exit___obj) },
129193
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&touchio_touchin_deinit_obj) },
130194

131195
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)},
196+
{ MP_OBJ_NEW_QSTR(MP_QSTR_raw_value), MP_ROM_PTR(&touchio_touchin_raw_value_obj)},
197+
{ MP_OBJ_NEW_QSTR(MP_QSTR_threshold), MP_ROM_PTR(&touchio_touchin_threshold_obj)},
132198
};
133199

134200
STATIC MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table);

shared-bindings/touchio/TouchIn.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ extern const mp_obj_type_t touchio_touchin_type;
3535
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin);
3636
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self);
3737
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self);
38+
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self);
39+
uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self);
40+
void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold);
3841

3942
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H

0 commit comments

Comments
 (0)