Skip to content

Commit 539aaf0

Browse files
committed
speed up measurement loop
1 parent 4a09329 commit 539aaf0

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,17 @@
3838

3939
#include "nrf.h"
4040

41+
// This is a capacitive touch sensing routine using a single digital
42+
// pin. The pin should be connected to the sensing pad, and to ground
43+
// via a 1Mohm or thereabout drain resistor. When a reading is taken,
44+
// the pin's capacitance is charged by setting it to a digital output
45+
// 'high' for a few microseconds, and then it is changed to a high
46+
// impedance input. We measure how long it takes to discharge through
47+
// the resistor (around 50us), using a busy-waiting loop, and average
48+
// over N_SAMPLES cycles to reduce the effects of noise.
49+
4150
#define N_SAMPLES 10
42-
#define TIMEOUT_US 10000
51+
#define TIMEOUT_TICKS 10000
4352

4453
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
4554

@@ -57,9 +66,8 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
5766
nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL);
5867

5968
while(nrf_gpio_pin_read(self->pin->number)) {
60-
if (ticks >= TIMEOUT_US) return TIMEOUT_US;
69+
if (ticks >= TIMEOUT_TICKS) return TIMEOUT_TICKS;
6170
ticks++;
62-
mp_hal_delay_us(1);
6371
}
6472
}
6573
return ticks;

0 commit comments

Comments
 (0)