Skip to content

Commit 0d03ba8

Browse files
committed
add TouchIn "pull_dir" option, touchio works for RP2350
1 parent bd13492 commit 0d03ba8

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

ports/raspberrypi/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ CIRCUITPY_PICODVI ?= 1
6666

6767
# Our generic touchio uses a pull down and RP2350 A2 hardware doesn't work correctly.
6868
# So, turn touchio off because it doesn't work.
69-
CIRCUITPY_TOUCHIO = 0
69+
#CIRCUITPY_TOUCHIO = 0
7070

7171
# delay in ms before calling cyw43_arch_init_with_country
7272
CIRCUITPY_CYW43_INIT_DELAY ?= 0

shared-bindings/touchio/TouchIn.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,33 @@
3131
//| print("touched!")"""
3232
//|
3333

34-
//| def __init__(self, pin: microcontroller.Pin) -> None:
34+
//| def __init__(self, pin: microcontroller.Pin, pulldir: bool -> False) -> None:
3535
//| """Use the TouchIn on the given pin.
3636
//|
3737
//| :param ~microcontroller.Pin pin: the pin to read from"""
38+
//| :param ~bool pulldir: kind of external pull resistor, false = pulldown, true = pullup
3839
//| ...
3940
//|
40-
static mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
41-
size_t n_args, size_t n_kw, const mp_obj_t *args) {
41+
//static mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
42+
// size_t n_args, size_t n_kw, const mp_obj_t *args) {
4243
// check number of arguments
43-
mp_arg_check_num(n_args, n_kw, 1, 1, false);
44-
44+
/// mp_arg_check_num(n_args, n_kw, 1, 1, false);
45+
static mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
46+
size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
47+
enum { ARG_pin, ARG_pulldir };
48+
static const mp_arg_t allowed_args[] = {
49+
{ MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED },
50+
{ MP_QSTR_pulldir, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
51+
};
52+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
53+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
54+
4555
// 1st argument is the pin
46-
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0], MP_QSTR_pin);
56+
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj, MP_QSTR_pin);
57+
const bool pulldir = args[ARG_pulldir].u_bool;
4758

4859
touchio_touchin_obj_t *self = mp_obj_malloc(touchio_touchin_obj_t, &touchio_touchin_type);
49-
common_hal_touchio_touchin_construct(self, pin);
60+
common_hal_touchio_touchin_construct(self, pin, pulldir);
5061

5162
return (mp_obj_t)self;
5263
}

shared-bindings/touchio/TouchIn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
extern const mp_obj_type_t touchio_touchin_type;
1818

19-
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin);
19+
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin, const bool pulldir);
2020
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self);
2121
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self);
2222
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self);

shared-module/touchio/TouchIn.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@
2828
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
2929

3030
uint16_t ticks = 0;
31-
31+
const pulldir = self->pulldir;
32+
3233
for (uint16_t i = 0; i < N_SAMPLES; i++) {
33-
// set pad to digital output high for 10us to charge it
34+
// set pad to digital output "high" for 10us to charge it (dpending on pulldir)
3435

35-
common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, true, DRIVE_MODE_PUSH_PULL);
36+
common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, !pulldir, DRIVE_MODE_PUSH_PULL);
3637
mp_hal_delay_us(10);
3738

3839
// set pad back to an input and take some samples
3940

4041
common_hal_digitalio_digitalinout_switch_to_input(self->digitalinout, PULL_NONE);
4142

42-
while (common_hal_digitalio_digitalinout_get_value(self->digitalinout)) {
43+
while (common_hal_digitalio_digitalinout_get_value(self->digitalinout) != pulldir) {
4344
if (ticks >= TIMEOUT_TICKS) {
4445
return TIMEOUT_TICKS;
4546
}
@@ -49,16 +50,21 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
4950
return ticks;
5051
}
5152

52-
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin) {
53+
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin, const bool pulldir) {
5354
common_hal_mcu_pin_claim(pin);
5455
self->digitalinout = mp_obj_malloc(digitalio_digitalinout_obj_t, &digitalio_digitalinout_type);
55-
56+
self->pulldir = pulldir;
57+
5658
common_hal_digitalio_digitalinout_construct(self->digitalinout, pin);
5759

5860
uint16_t raw_reading = get_raw_reading(self);
5961
if (raw_reading == TIMEOUT_TICKS) {
6062
common_hal_touchio_touchin_deinit(self);
61-
mp_raise_ValueError(MP_ERROR_TEXT("No pulldown on pin; 1Mohm recommended"));
63+
if (self->pulldir) {
64+
mp_raise_ValueError(MP_ERROR_TEXT("No pullup on pin; 1Mohm recommended"));
65+
} else {
66+
mp_raise_ValueError(MP_ERROR_TEXT("No pulldown on pin; 1Mohm recommended"));
67+
}
6268
}
6369
self->threshold = raw_reading * 1.05 + 100;
6470
}

shared-module/touchio/TouchIn.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ typedef struct {
1616
mp_obj_base_t base;
1717
digitalio_digitalinout_obj_t *digitalinout;
1818
uint16_t threshold;
19+
bool pulldir;
1920
} touchio_touchin_obj_t;
2021

2122
void touchin_reset(void);

0 commit comments

Comments
 (0)