Skip to content

Commit cccbbd9

Browse files
committed
Detect lack of pulldown; check for pin in use
1 parent e2a4c76 commit cccbbd9

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

shared-bindings/touchio/TouchIn.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ STATIC mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
6868
// 1st argument is the pin
6969
mp_obj_t pin_obj = args[0];
7070
assert_pin(pin_obj, false);
71+
const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj);
72+
assert_pin_free(pin);
7173

7274
touchio_touchin_obj_t *self = m_new_obj(touchio_touchin_obj_t);
7375
self->base.type = &touchio_touchin_type;
74-
const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj);
7576
common_hal_touchio_touchin_construct(self, pin);
7677

7778
return (mp_obj_t) self;

shared-module/touchio/TouchIn.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
6767
}
6868

6969
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin) {
70+
claim_pin(pin);
7071
self->digitalinout = m_new_obj(digitalio_digitalinout_obj_t);
7172
self->digitalinout->base.type = &digitalio_digitalinout_type;
7273

7374
common_hal_digitalio_digitalinout_construct(self->digitalinout, pin);
7475

75-
self->threshold = get_raw_reading(self) * 1.05 + 100;
76+
uint16_t raw_reading = get_raw_reading(self);
77+
if (raw_reading == TIMEOUT_TICKS) {
78+
mp_raise_ValueError(translate("No pulldown on pin; 1Mohm recommended"));
79+
}
80+
self->threshold = raw_reading * 1.05 + 100;
7681
}
7782

7883
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) {

0 commit comments

Comments
 (0)