summaryrefslogtreecommitdiff
path: root/shared-bindings/touchio
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2017-10-01 14:43:42 -0400
committerScott Shawcroft <scott@tannewt.org>2017-10-02 11:15:51 -0700
commit7f744128826b8328aaa6434fdcb8f0a272c87d72 (patch)
treef83374d475e9d42e472c7a4645759a44f40c67bb /shared-bindings/touchio
parentb2dcc5bb6c50f9b6eb3c294c98df8a5880167fd8 (diff)
Make touch more sensitive. Add .raw_value and .threshold attributes.
Diffstat (limited to 'shared-bindings/touchio')
-rw-r--r--shared-bindings/touchio/TouchIn.c66
-rw-r--r--shared-bindings/touchio/TouchIn.h3
2 files changed, 69 insertions, 0 deletions
diff --git a/shared-bindings/touchio/TouchIn.c b/shared-bindings/touchio/TouchIn.c
index 894781423..213fbc669 100644
--- a/shared-bindings/touchio/TouchIn.c
+++ b/shared-bindings/touchio/TouchIn.c
@@ -24,6 +24,7 @@
* THE SOFTWARE.
*/
+#include <stdint.h>
#include <string.h>
#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
//| .. attribute:: value
//|
//| Whether the touch pad is being touched or not.
+//| True if `raw_value` > `threshold`.
//|
//| :return: True when touched, False otherwise.
//| :rtype: bool
@@ -123,12 +125,76 @@ const mp_obj_property_t touchio_touchin_value_obj = {
(mp_obj_t)&mp_const_none_obj},
};
+
+//| .. attribute:: raw_value
+//|
+//| The raw touch measurement. Not settable.
+//|
+//| :return: an integer >= 0
+//| :rtype: int
+//|
+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);
+ 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);
+
+const mp_obj_property_t touchio_touchin_raw_value_obj = {
+ .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},
+ };
+
+
+//| .. attribute:: threshold
+//|
+//| `value` will return True if `raw_value` is greater than than this threshold.
+//| 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 set the threshold to a different value to make the pin more or less sensitive.
+//|
+//| :return: an integer >= 0
+//| :rtype: int
+//|
+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);
+ 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);
+ 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.
+ mp_raise_ValueError("threshold must be in the range 0-65536");
+ }
+ 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);
+
+const mp_obj_property_t touchio_touchin_threshold_obj = {
+ .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},
+ };
+
+
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(&touchio_touchin___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&touchio_touchin_deinit_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)},
+ { 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)},
};
STATIC MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table);
diff --git a/shared-bindings/touchio/TouchIn.h b/shared-bindings/touchio/TouchIn.h
index 6ee668f38..0570afb20 100644
--- a/shared-bindings/touchio/TouchIn.h
+++ b/shared-bindings/touchio/TouchIn.h
@@ -35,5 +35,8 @@ extern const mp_obj_type_t touchio_touchin_type;
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin);
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self);
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self);
+uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self);
+uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self);
+void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H