Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add TouchIn "pull_dir" option, touchio works for RP2350 #10223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ports/raspberrypi/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CIRCUITPY_PICODVI ?= 1

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

# delay in ms before calling cyw43_arch_init_with_country
CIRCUITPY_CYW43_INIT_DELAY ?= 0
Expand Down
25 changes: 18 additions & 7 deletions shared-bindings/touchio/TouchIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,33 @@
//| print("touched!")"""
//|

//| def __init__(self, pin: microcontroller.Pin) -> None:
//| def __init__(self, pin: microcontroller.Pin, pulldir: bool -> False) -> None:
//| """Use the TouchIn on the given pin.
//|
//| :param ~microcontroller.Pin pin: the pin to read from"""
//| :param ~bool pulldir: kind of external pull resistor, false = pulldown, true = pullup
//| ...
//|
static mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
size_t n_args, size_t n_kw, const mp_obj_t *args) {
//static mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
// size_t n_args, size_t n_kw, const mp_obj_t *args) {
// check number of arguments
mp_arg_check_num(n_args, n_kw, 1, 1, false);

/// mp_arg_check_num(n_args, n_kw, 1, 1, false);
static mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type,
size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_pin, ARG_pulldir };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_pulldir, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

// 1st argument is the pin
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0], MP_QSTR_pin);
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj, MP_QSTR_pin);
const bool pulldir = args[ARG_pulldir].u_bool;

touchio_touchin_obj_t *self = mp_obj_malloc(touchio_touchin_obj_t, &touchio_touchin_type);
common_hal_touchio_touchin_construct(self, pin);
common_hal_touchio_touchin_construct(self, pin, pulldir);

return (mp_obj_t)self;
}
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/touchio/TouchIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin, const bool pulldir);
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self);
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self);
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self);
Expand Down
20 changes: 13 additions & 7 deletions shared-module/touchio/TouchIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {

uint16_t ticks = 0;

const bool pulldir = self->pulldir;

for (uint16_t i = 0; i < N_SAMPLES; i++) {
// set pad to digital output high for 10us to charge it
// set pad to digital output "high" for 10us to charge it (dpending on pulldir)

common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, true, DRIVE_MODE_PUSH_PULL);
common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, !pulldir, DRIVE_MODE_PUSH_PULL);
mp_hal_delay_us(10);

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

common_hal_digitalio_digitalinout_switch_to_input(self->digitalinout, PULL_NONE);

while (common_hal_digitalio_digitalinout_get_value(self->digitalinout)) {
while (common_hal_digitalio_digitalinout_get_value(self->digitalinout) != pulldir) {
if (ticks >= TIMEOUT_TICKS) {
return TIMEOUT_TICKS;
}
Expand All @@ -49,16 +50,21 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
return ticks;
}

void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin) {
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin, const bool pulldir) {
common_hal_mcu_pin_claim(pin);
self->digitalinout = mp_obj_malloc(digitalio_digitalinout_obj_t, &digitalio_digitalinout_type);

self->pulldir = pulldir;

common_hal_digitalio_digitalinout_construct(self->digitalinout, pin);

uint16_t raw_reading = get_raw_reading(self);
if (raw_reading == TIMEOUT_TICKS) {
common_hal_touchio_touchin_deinit(self);
mp_raise_ValueError(MP_ERROR_TEXT("No pulldown on pin; 1Mohm recommended"));
if (self->pulldir) {
mp_raise_ValueError(MP_ERROR_TEXT("No pullup on pin; 1Mohm recommended"));
} else {
mp_raise_ValueError(MP_ERROR_TEXT("No pulldown on pin; 1Mohm recommended"));
}
}
self->threshold = raw_reading * 1.05 + 100;
}
Expand Down
1 change: 1 addition & 0 deletions shared-module/touchio/TouchIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct {
mp_obj_base_t base;
digitalio_digitalinout_obj_t *digitalinout;
uint16_t threshold;
bool pulldir;
} touchio_touchin_obj_t;

void touchin_reset(void);
Loading