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+
126190STATIC 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
134200STATIC MP_DEFINE_CONST_DICT (touchio_touchin_locals_dict , touchio_touchin_locals_dict_table );
0 commit comments