Skip to content

Commit 765d877

Browse files
authored
Merge pull request #1499 from nickzoic/nickzoic/circuitpython-nrf-touchin-1048
Nickzoic/circuitpython nrf touchin 1048
2 parents 825c80a + 294b026 commit 765d877

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

ports/nrf/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ SRC_COMMON_HAL += \
169169
supervisor/Runtime.c \
170170
supervisor/__init__.c \
171171
time/__init__.c \
172+
touchio/__init__.c \
173+
touchio/TouchIn.c \
172174

173175
ifneq ($(SD), )
174176
SRC_COMMON_HAL += \
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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
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+
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_TOUCHIO_TOUCHIN_H
29+
#define MICROPY_INCLUDED_NRF_COMMON_HAL_TOUCHIO_TOUCHIN_H
30+
31+
#include "common-hal/microcontroller/Pin.h"
32+
33+
#include "py/obj.h"
34+
35+
typedef struct {
36+
mp_obj_base_t base;
37+
const mcu_pin_obj_t *pin;
38+
uint16_t threshold;
39+
} touchio_touchin_obj_t;
40+
41+
void touchin_reset(void);
42+
43+
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_TOUCHIO_TOUCHIN_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No touchio module functions.

ports/nrf/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ extern const struct _mp_obj_module_t neopixel_write_module;
178178
extern const struct _mp_obj_module_t usb_hid_module;
179179
extern const struct _mp_obj_module_t usb_midi_module;
180180
extern const struct _mp_obj_module_t bleio_module;
181+
extern const struct _mp_obj_module_t touchio_module;
182+
181183

182184
#if MICROPY_PY_BLEIO
183185
#define BLEIO_MODULE { MP_ROM_QSTR(MP_QSTR_bleio), MP_ROM_PTR(&bleio_module) },
@@ -209,6 +211,7 @@ extern const struct _mp_obj_module_t bleio_module;
209211
{ MP_OBJ_NEW_QSTR (MP_QSTR_gamepad ), (mp_obj_t)&gamepad_module }, \
210212
{ MP_OBJ_NEW_QSTR (MP_QSTR_time ), (mp_obj_t)&time_module }, \
211213
{ MP_OBJ_NEW_QSTR (MP_QSTR_json ), (mp_obj_t)&mp_module_ujson }, \
214+
{ MP_OBJ_NEW_QSTR (MP_QSTR_touchio ), (mp_obj_t)&touchio_module }, \
212215
USBHID_MODULE \
213216
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_midi),(mp_obj_t)&usb_midi_module }, \
214217
BLEIO_MODULE

0 commit comments

Comments
 (0)