Skip to content

Commit 232b699

Browse files
committed
Add "pull" parameter to TouchIn ports
1 parent 6c1b6a4 commit 232b699

File tree

12 files changed

+43
-161
lines changed

12 files changed

+43
-161
lines changed

locale/circuitpython.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ msgstr ""
15551555
msgid "No pulldown on pin; 1Mohm recommended"
15561556
msgstr ""
15571557

1558-
#: ports/raspberrypi/common-hal/touchio/TouchIn.c
1558+
#: shared-module/touchio/TouchIn.c
15591559
msgid "No pullup on pin; 1Mohm recommended"
15601560
msgstr ""
15611561

ports/atmel-samd/common-hal/touchio/TouchIn.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "py/binary.h"
1313
#include "py/mphal.h"
1414
#include "shared-bindings/microcontroller/Pin.h"
15+
#include "shared-bindings/digitalio/Pull.h"
1516
#include "shared-bindings/touchio/TouchIn.h"
1617

1718
// Native touchio only exists for SAMD21
@@ -38,7 +39,7 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
3839
}
3940

4041
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
41-
const mcu_pin_obj_t *pin) {
42+
const mcu_pin_obj_t *pin, const digitalio_pull_t pull) {
4243
if (!pin->has_touch) {
4344
raise_ValueError_invalid_pin();
4445
}

ports/espressif/common-hal/touchio/TouchIn.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
#include "py/runtime.h"
1010
#include "peripherals/touch.h"
1111
#include "shared-bindings/microcontroller/Pin.h"
12+
#include "shared-bindings/digitalio/Pull.h"
1213

1314
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
14-
const mcu_pin_obj_t *pin) {
15+
const mcu_pin_obj_t *pin, const digitalio_pull_t pull) {
1516
if (pin->touch_channel == NO_TOUCH_CHANNEL) {
1617
raise_ValueError_invalid_pin();
1718
}

ports/raspberrypi/common-hal/touchio/TouchIn.c

Lines changed: 0 additions & 103 deletions
This file was deleted.

ports/raspberrypi/common-hal/touchio/TouchIn.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

ports/raspberrypi/common-hal/touchio/__init__.c

Lines changed: 0 additions & 5 deletions
This file was deleted.

ports/raspberrypi/mpconfigport.mk

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ CIRCUITPY_ALARM = 0
6464
# Default PICODVI on because it doesn't require much code in RAM to talk to HSTX.
6565
CIRCUITPY_PICODVI ?= 1
6666

67-
# Our generic touchio uses a pull down and RP2350 A2 hardware doesn't work correctly.
68-
# So, turn generic touchio off because it doesn't work.
69-
# And use a "native" touchio that expects 1M pull-up resistor instead of pull-down
70-
CIRCUITPY_TOUCHIO = 1
71-
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1
72-
7367
# delay in ms before calling cyw43_arch_init_with_country
7468
CIRCUITPY_CYW43_INIT_DELAY ?= 0
7569

shared-bindings/touchio/TouchIn.c

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

34-
//| def __init__(self, pin: microcontroller.Pin) -> None:
34+
//| def __init__(self, pin: microcontroller.Pin, pull: Optional[digitalio.Pull] = None) -> None:
3535
//| """Use the TouchIn on the given pin.
3636
//|
3737
//| :param ~microcontroller.Pin pin: the pin to read from"""
38+
//| :param Optional[digitalio.Pull] pull: specify external pull resistor type. If None, assume pull-down or chip-specific implementation
3839
//| ...
3940
//|
4041
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) {
42-
// check number of arguments
43-
mp_arg_check_num(n_args, n_kw, 1, 1, false);
42+
size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
4443

45-
// 1st argument is the pin
46-
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0], MP_QSTR_pin);
44+
enum { ARG_pin, ARG_pull };
45+
static const mp_arg_t allowed_args[] = {
46+
{ MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED },
47+
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none} },
48+
};
49+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
50+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
51+
52+
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj, MP_QSTR_pin);
53+
const digitalio_pull_t pull = validate_pull(args[ARG_pull].u_obj, MP_QSTR_pull);
4754

4855
touchio_touchin_obj_t *self = mp_obj_malloc(touchio_touchin_obj_t, &touchio_touchin_type);
49-
common_hal_touchio_touchin_construct(self, pin);
56+
common_hal_touchio_touchin_construct(self, pin, pull);
5057

5158
return (mp_obj_t)self;
5259
}

shared-bindings/touchio/TouchIn.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma once
88

99
#include "common-hal/microcontroller/Pin.h"
10+
#include "shared-bindings/digitalio/Pull.h"
1011

1112
#if CIRCUITPY_TOUCHIO_USE_NATIVE
1213
#include "common-hal/touchio/TouchIn.h"
@@ -16,7 +17,7 @@
1617

1718
extern const mp_obj_type_t touchio_touchin_type;
1819

19-
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin);
20+
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin, digitalio_pull_t pull);
2021
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self);
2122
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self);
2223
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self);

shared-bindings/touchio/__init__.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
//| For more information about working with the `touchio` module in CircuitPython,
2929
//| see `this Learn guide page <https://learn.adafruit.com/circuitpython-essentials/circuitpython-cap-touch>`_.
3030
//|
31-
//| **Limitations**: `touchio` is available on Raspberry Pi RP2040 builds,
32-
//| but not on RP2350, due to GPIO hardware limitations.
31+
//| **Limitations**: `touchio` on RP2350 must have a pull-up resistor to 3.3V
32+
//| instead of ground and set the `pull=Pull.UP` parameter when constructing
33+
//| a `TouchIn` object, due to GPIO hardware limitations.
3334
//|
3435
//| Example::
3536
//|

shared-module/touchio/TouchIn.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,38 @@
1212
#include "py/mphal.h"
1313
#include "shared-bindings/touchio/TouchIn.h"
1414
#include "shared-bindings/microcontroller/Pin.h"
15+
#include "shared-bindings/digitalio/Pull.h"
1516

16-
// This is a capacitive touch sensing routine using a single digital
17-
// pin. The pin should be connected to the sensing pad, and to ground
17+
// This is a capacitive touch sensing routine using a single digital pin.
18+
// For pull==PULL_DOWN, the pin should be connected to the sensing pad and to ground
1819
// via a 1Mohm or thereabout drain resistor. When a reading is taken,
1920
// the pin's capacitance is charged by setting it to a digital output
2021
// 'high' for a few microseconds, and then it is changed to a high
2122
// impedance input. We measure how long it takes to discharge through
2223
// the resistor (around 50us), using a busy-waiting loop, and average
2324
// over N_SAMPLES cycles to reduce the effects of noise.
25+
// For the pull=PULL_UP case, the 1M resistor is connected to 3v3, the pin is
26+
// driven 'low' then measure how long it takes to go 'high'.
2427

2528
#define N_SAMPLES 10
2629
#define TIMEOUT_TICKS 10000
2730

2831
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
2932

3033
uint16_t ticks = 0;
31-
34+
// state to charge pin to: if pull-down or None, pull HIGH, if pull-up, pull LOW
35+
bool pincharge = !(self->pull == PULL_UP);
3236
for (uint16_t i = 0; i < N_SAMPLES; i++) {
33-
// set pad to digital output high for 10us to charge it
37+
// set pad to digital output 'pincharge' for 10us to charge it
3438

35-
common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, true, DRIVE_MODE_PUSH_PULL);
39+
common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, pincharge, DRIVE_MODE_PUSH_PULL);
3640
mp_hal_delay_us(10);
3741

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

4044
common_hal_digitalio_digitalinout_switch_to_input(self->digitalinout, PULL_NONE);
4145

42-
while (common_hal_digitalio_digitalinout_get_value(self->digitalinout)) {
46+
while (common_hal_digitalio_digitalinout_get_value(self->digitalinout) == pincharge) {
4347
if (ticks >= TIMEOUT_TICKS) {
4448
return TIMEOUT_TICKS;
4549
}
@@ -49,16 +53,22 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
4953
return ticks;
5054
}
5155

52-
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin) {
56+
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin, const digitalio_pull_t pull) {
5357
common_hal_mcu_pin_claim(pin);
5458
self->digitalinout = mp_obj_malloc(digitalio_digitalinout_obj_t, &digitalio_digitalinout_type);
5559

5660
common_hal_digitalio_digitalinout_construct(self->digitalinout, pin);
5761

62+
self->pull = pull;
63+
5864
uint16_t raw_reading = get_raw_reading(self);
5965
if (raw_reading == TIMEOUT_TICKS) {
6066
common_hal_touchio_touchin_deinit(self);
61-
mp_raise_ValueError(MP_ERROR_TEXT("No pulldown on pin; 1Mohm recommended"));
67+
if (self->pull == PULL_UP) {
68+
mp_raise_ValueError(MP_ERROR_TEXT("No pullup on pin; 1Mohm recommended"));
69+
} else {
70+
mp_raise_ValueError(MP_ERROR_TEXT("No pulldown on pin; 1Mohm recommended"));
71+
}
6272
}
6373
self->threshold = raw_reading * 1.05 + 100;
6474
}

shared-module/touchio/TouchIn.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
typedef struct {
1616
mp_obj_base_t base;
1717
digitalio_digitalinout_obj_t *digitalinout;
18+
digitalio_pull_t pull;
1819
uint16_t threshold;
1920
} touchio_touchin_obj_t;
2021

0 commit comments

Comments
 (0)