forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTouchIn.c
More file actions
124 lines (96 loc) · 3.52 KB
/
TouchIn.c
File metadata and controls
124 lines (96 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <string.h>
#include "py/nlr.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/digitalio/Pull.h"
#include "shared-bindings/touchio/TouchIn.h"
// Native touchio only exists for SAMD21
#ifdef SAMD21
#include "hpl/pm/hpl_pm_base.h"
#include "samd/clocks.h"
#include "samd/pins.h"
#include "adafruit_ptc.h"
bool touch_enabled = false;
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
adafruit_ptc_start_conversion(PTC, &self->config);
while (!adafruit_ptc_is_conversion_finished(PTC)) {
// wait
RUN_BACKGROUND_TASKS;
}
return adafruit_ptc_get_conversion_result(PTC);
}
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
const mcu_pin_obj_t *pin, const digitalio_pull_t pull) {
if (!pin->has_touch) {
raise_ValueError_invalid_pin();
}
claim_pin(pin);
// Turn on the PTC if its not in use. We won't turn it off until reset.
if (((Ptc *)PTC)->CTRLA.bit.ENABLE == 0) {
// We run the PTC at 8mhz so divide the 48mhz clock by 6.
uint8_t gclk = find_free_gclk(6);
if (gclk > GCLK_GEN_NUM) {
mp_raise_RuntimeError(MP_ERROR_TEXT("No free GCLKs"));
}
enable_clock_generator(gclk, CLOCK_48MHZ, 6);
/* Setup and enable generic clock source for PTC module. */
connect_gclk_to_peripheral(gclk, PTC_GCLK_ID);
_pm_enable_bus_clock(PM_BUS_APBC, PTC);
}
adafruit_ptc_get_config_default(&self->config);
self->config.pin = pin->number;
self->config.yline = pin->touch_y_line;
adafruit_ptc_init(PTC, &self->config);
// Initial values for pins will vary, depending on what peripherals the pins
// share on-chip.
//
// Set a "touched" threshold not too far above the initial value.
// For simple finger touch, the values may vary as much as a factor of two,
// but for touches using fruit or other objects, the difference is much less.
self->threshold = get_raw_reading(self) + 100;
}
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self) {
return self->config.pin == NO_PIN;
}
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self) {
// TODO(tannewt): Reset the PTC.
if (common_hal_touchio_touchin_deinited(self)) {
return;
}
// We leave the clocks running because they may be in use by others.
reset_pin_number(self->config.pin);
self->config.pin = NO_PIN;
}
void touchin_reset(void) {
Ptc *ptc = ((Ptc *)PTC);
if (ptc->CTRLA.bit.ENABLE == 1) {
ptc->CTRLA.bit.ENABLE = 0;
while (ptc->CTRLA.bit.ENABLE == 1) {
}
ptc->CTRLA.bit.SWRESET = 1;
while (ptc->CTRLA.bit.SWRESET == 1) {
}
}
}
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
uint16_t reading = get_raw_reading(self);
return reading > self->threshold;
}
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {
return get_raw_reading(self);
}
uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) {
return self->threshold;
}
void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold) {
self->threshold = new_threshold;
}
#endif // SAMD21