|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2016 Scott Shawcroft for Adafruit Industries |
| 4 | +// SPDX-FileCopyrightText: Copyright (c) 2018 Nick Moore for Adafruit Industries |
| 5 | +// |
| 6 | +// SPDX-License-Identifier: MIT |
| 7 | + |
| 8 | +#include <string.h> |
| 9 | +#include <stdio.h> |
| 10 | + |
| 11 | +#include "py/runtime.h" |
| 12 | +#include "py/mphal.h" |
| 13 | +#include "shared-bindings/touchio/TouchIn.h" |
| 14 | +#include "shared-bindings/microcontroller/Pin.h" |
| 15 | + |
| 16 | +// Native touchio only for RP2350, which cannot do shared-module touchio |
| 17 | +#ifdef PICO_RP2350 |
| 18 | + |
| 19 | +// This is a capacitive touch sensing routine using a single digital |
| 20 | +// pin. The pin should be connected to the sensing pad, and to ground |
| 21 | +// via a 1Mohm or thereabout drain resistor. When a reading is taken, |
| 22 | +// the pin's capacitance is charged by setting it to a digital output |
| 23 | +// 'high' for a few microseconds, and then it is changed to a high |
| 24 | +// impedance input. We measure how long it takes to discharge through |
| 25 | +// the resistor (around 50us), using a busy-waiting loop, and average |
| 26 | +// over N_SAMPLES cycles to reduce the effects of noise. |
| 27 | + |
| 28 | +#define N_SAMPLES 10 |
| 29 | +#define TIMEOUT_TICKS 10000 |
| 30 | + |
| 31 | +static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { |
| 32 | + |
| 33 | + uint16_t ticks = 0; |
| 34 | + |
| 35 | + for (uint16_t i = 0; i < N_SAMPLES; i++) { |
| 36 | + // set pad to digital output LOW for 10us to charge it |
| 37 | + |
| 38 | + common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, false, DRIVE_MODE_PUSH_PULL); |
| 39 | + mp_hal_delay_us(10); |
| 40 | + |
| 41 | + // set pad back to an input and take some samples, waiting for it to go HIGH |
| 42 | + |
| 43 | + common_hal_digitalio_digitalinout_switch_to_input(self->digitalinout, PULL_NONE); |
| 44 | + |
| 45 | + while (!common_hal_digitalio_digitalinout_get_value(self->digitalinout)) { |
| 46 | + if (ticks >= TIMEOUT_TICKS) { |
| 47 | + return TIMEOUT_TICKS; |
| 48 | + } |
| 49 | + ticks++; |
| 50 | + } |
| 51 | + } |
| 52 | + return ticks; |
| 53 | +} |
| 54 | + |
| 55 | +void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, const mcu_pin_obj_t *pin) { |
| 56 | + common_hal_mcu_pin_claim(pin); |
| 57 | + self->digitalinout = mp_obj_malloc(digitalio_digitalinout_obj_t, &digitalio_digitalinout_type); |
| 58 | + |
| 59 | + common_hal_digitalio_digitalinout_construct(self->digitalinout, pin); |
| 60 | + |
| 61 | + uint16_t raw_reading = get_raw_reading(self); |
| 62 | + if (raw_reading == TIMEOUT_TICKS) { |
| 63 | + common_hal_touchio_touchin_deinit(self); |
| 64 | + mp_raise_ValueError(MP_ERROR_TEXT("No pullup on pin; 1Mohm recommended")); |
| 65 | + } |
| 66 | + self->threshold = raw_reading * 1.05 + 100; |
| 67 | +} |
| 68 | + |
| 69 | +bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self) { |
| 70 | + return self->digitalinout == MP_OBJ_NULL; |
| 71 | +} |
| 72 | + |
| 73 | +void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self) { |
| 74 | + if (common_hal_touchio_touchin_deinited(self)) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + common_hal_digitalio_digitalinout_deinit(self->digitalinout); |
| 79 | + self->digitalinout = MP_OBJ_NULL; |
| 80 | +} |
| 81 | + |
| 82 | +void touchin_reset() { |
| 83 | +} |
| 84 | + |
| 85 | +bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) { |
| 86 | + uint16_t reading = get_raw_reading(self); |
| 87 | + return reading > self->threshold; |
| 88 | +} |
| 89 | + |
| 90 | +uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) { |
| 91 | + return get_raw_reading(self); |
| 92 | +} |
| 93 | + |
| 94 | +uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) { |
| 95 | + return self->threshold; |
| 96 | +} |
| 97 | + |
| 98 | +void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, |
| 99 | + uint16_t new_threshold) { |
| 100 | + self->threshold = new_threshold; |
| 101 | +} |
| 102 | + |
| 103 | +#endif |
0 commit comments