|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries |
| 7 | + * Copyright (c) 2018 Nick Moore for Adafruit Industries |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <string.h> |
| 29 | +#include <stdio.h> |
| 30 | + |
| 31 | +#include "py/nlr.h" |
| 32 | +#include "py/mperrno.h" |
| 33 | +#include "py/runtime.h" |
| 34 | +#include "py/binary.h" |
| 35 | +#include "py/mphal.h" |
| 36 | +#include "shared-bindings/touchio/TouchIn.h" |
| 37 | +#include "supervisor/shared/translate.h" |
| 38 | + |
| 39 | +#include "nrf.h" |
| 40 | + |
| 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 | + |
| 50 | +#define N_SAMPLES 10 |
| 51 | +#define TIMEOUT_TICKS 10000 |
| 52 | + |
| 53 | +static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { |
| 54 | + |
| 55 | + uint16_t ticks = 0; |
| 56 | + |
| 57 | + for (uint16_t i = 0; i < N_SAMPLES; i++) { |
| 58 | + // set pad to digital output high for 10us to charge it |
| 59 | + |
| 60 | + nrf_gpio_cfg_output(self->pin->number); |
| 61 | + nrf_gpio_pin_set(self->pin->number); |
| 62 | + mp_hal_delay_us(10); |
| 63 | + |
| 64 | + // set pad back to an input and take some samples |
| 65 | + |
| 66 | + nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL); |
| 67 | + |
| 68 | + while(nrf_gpio_pin_read(self->pin->number)) { |
| 69 | + if (ticks >= TIMEOUT_TICKS) return TIMEOUT_TICKS; |
| 70 | + ticks++; |
| 71 | + } |
| 72 | + } |
| 73 | + return ticks; |
| 74 | +} |
| 75 | + |
| 76 | +void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, |
| 77 | + const mcu_pin_obj_t *pin) { |
| 78 | + self->pin = pin; |
| 79 | + claim_pin(pin); |
| 80 | + |
| 81 | + self->threshold = get_raw_reading(self) * 1.05 + 100; |
| 82 | +} |
| 83 | + |
| 84 | +bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) { |
| 85 | + return self->pin == NULL; |
| 86 | +} |
| 87 | + |
| 88 | +void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) { |
| 89 | + if (common_hal_touchio_touchin_deinited(self)) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + reset_pin_number(self->pin->number); |
| 94 | + self->pin = NULL; |
| 95 | +} |
| 96 | + |
| 97 | +void touchin_reset() { |
| 98 | +} |
| 99 | + |
| 100 | +bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) { |
| 101 | + uint16_t reading = get_raw_reading(self); |
| 102 | + return reading > self->threshold; |
| 103 | +} |
| 104 | + |
| 105 | +uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) { |
| 106 | + return get_raw_reading(self); |
| 107 | +} |
| 108 | + |
| 109 | +uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) { |
| 110 | + return self->threshold; |
| 111 | +} |
| 112 | + |
| 113 | +void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, |
| 114 | + uint16_t new_threshold) { |
| 115 | + self->threshold = new_threshold; |
| 116 | +} |
0 commit comments