Skip to content

Commit 5698b8b

Browse files
authored
Merge pull request #3850 from microDev1/touch-s2
Support for Touch Alarm
2 parents 7e78af7 + 0bad611 commit 5698b8b

File tree

17 files changed

+477
-51
lines changed

17 files changed

+477
-51
lines changed

locale/circuitpython.pot

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-12-23 23:04-0500\n"
11+
"POT-Creation-Date: 2020-12-22 22:54+0530\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -1170,6 +1170,7 @@ msgstr ""
11701170

11711171
#: ports/atmel-samd/common-hal/audioio/AudioOut.c
11721172
#: ports/atmel-samd/common-hal/touchio/TouchIn.c
1173+
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
11731174
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
11741175
#: shared-module/rgbmatrix/RGBMatrix.c
11751176
msgid "Invalid pin"
@@ -1488,6 +1489,10 @@ msgstr ""
14881489
msgid "Only one alarm.time alarm can be set."
14891490
msgstr ""
14901491

1492+
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
1493+
msgid "Only one alarm.touch alarm can be set."
1494+
msgstr ""
1495+
14911496
#: shared-module/displayio/ColorConverter.c
14921497
msgid "Only one color can be transparent at a time"
14931498
msgstr ""
@@ -1858,6 +1863,10 @@ msgstr ""
18581863
msgid "Total data to write is larger than outgoing_packet_length"
18591864
msgstr ""
18601865

1866+
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
1867+
msgid "TouchAlarm not available in light sleep"
1868+
msgstr ""
1869+
18611870
#: py/obj.c
18621871
msgid "Traceback (most recent call last):\n"
18631872
msgstr ""

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ STATIC void start_mp(supervisor_allocation* heap) {
157157

158158
#if CIRCUITPY_ALARM
159159
// Record which alarm woke us up, if any. An object may be created so the heap must be functional.
160-
alarm_save_wakeup_alarm();
160+
alarm_save_wake_alarm();
161161
// Reset alarm module only after we retrieved the wakeup alarm.
162162
alarm_reset();
163163
#endif

ports/esp32s2/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ SRC_C += \
193193
lib/utils/sys_stdio_mphal.c \
194194
lib/netutils/netutils.c \
195195
peripherals/timer.c \
196+
peripherals/touch.c \
196197
peripherals/pcnt.c \
197198
peripherals/pins.c \
198199
peripherals/rmt.c \

ports/esp32s2/common-hal/alarm/__init__.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
#include "py/runtime.h"
3232

3333
#include "shared-bindings/alarm/__init__.h"
34-
#include "shared-bindings/alarm/pin/PinAlarm.h"
3534
#include "shared-bindings/alarm/SleepMemory.h"
35+
#include "shared-bindings/alarm/pin/PinAlarm.h"
3636
#include "shared-bindings/alarm/time/TimeAlarm.h"
37-
#include "shared-bindings/microcontroller/__init__.h"
37+
#include "shared-bindings/alarm/touch/TouchAlarm.h"
38+
3839
#include "shared-bindings/wifi/__init__.h"
40+
#include "shared-bindings/microcontroller/__init__.h"
3941

4042
#include "supervisor/port.h"
4143
#include "supervisor/shared/workflow.h"
@@ -53,20 +55,23 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
5355
};
5456

5557
void alarm_reset(void) {
56-
alarm_time_timealarm_reset();
57-
alarm_pin_pinalarm_reset();
5858
alarm_sleep_memory_reset();
59+
alarm_pin_pinalarm_reset();
60+
alarm_time_timealarm_reset();
61+
alarm_touch_touchalarm_reset();
5962
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
6063
}
6164

6265
STATIC esp_sleep_wakeup_cause_t _get_wakeup_cause(void) {
66+
if (alarm_pin_pinalarm_woke_us_up()) {
67+
return ESP_SLEEP_WAKEUP_GPIO;
68+
}
6369
if (alarm_time_timealarm_woke_us_up()) {
6470
return ESP_SLEEP_WAKEUP_TIMER;
6571
}
66-
if (alarm_pin_pinalarm_woke_us_up()) {
67-
return ESP_SLEEP_WAKEUP_GPIO;
72+
if (alarm_touch_touchalarm_woke_us_up()) {
73+
return ESP_SLEEP_WAKEUP_TOUCHPAD;
6874
}
69-
7075
return esp_sleep_get_wakeup_cause();
7176
}
7277

@@ -88,8 +93,7 @@ STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
8893
}
8994

9095
case ESP_SLEEP_WAKEUP_TOUCHPAD:
91-
// TODO: implement TouchIO
92-
// Wake up from touch on pad, esp_sleep_get_touchpad_wakeup_status()
96+
return alarm_touch_touchalarm_get_wakeup_alarm(n_alarms, alarms);
9397
break;
9498

9599
case ESP_SLEEP_WAKEUP_UNDEFINED:
@@ -108,6 +112,7 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) {
108112
STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) {
109113
alarm_pin_pinalarm_set_alarms(deep_sleep, n_alarms, alarms);
110114
alarm_time_timealarm_set_alarms(deep_sleep, n_alarms, alarms);
115+
alarm_touch_touchalarm_set_alarm(deep_sleep, n_alarms, alarms);
111116
}
112117

113118
STATIC void _idle_until_alarm(void) {
@@ -116,9 +121,9 @@ STATIC void _idle_until_alarm(void) {
116121
RUN_BACKGROUND_TASKS;
117122
// Allow ctrl-C interrupt.
118123
if (alarm_woken_from_sleep()) {
124+
alarm_save_wake_alarm();
119125
return;
120126
}
121-
122127
port_idle_until_interrupt();
123128
}
124129
}
@@ -144,6 +149,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
144149

145150
void NORETURN alarm_enter_deep_sleep(void) {
146151
alarm_pin_pinalarm_prepare_for_deep_sleep();
152+
alarm_touch_touchalarm_prepare_for_deep_sleep();
147153
// The ESP-IDF caches the deep sleep settings and applies them before sleep.
148154
// We don't need to worry about resetting them in the interim.
149155
esp_deep_sleep_start();
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/alarm/touch/TouchAlarm.h"
28+
#include "shared-bindings/microcontroller/__init__.h"
29+
30+
#include "esp_sleep.h"
31+
#include "peripherals/touch.h"
32+
#include "supervisor/esp_port.h"
33+
34+
static volatile bool woke_up = false;
35+
static touch_pad_t touch_channel = TOUCH_PAD_MAX;
36+
37+
void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
38+
if (pin->touch_channel == TOUCH_PAD_MAX) {
39+
mp_raise_ValueError(translate("Invalid pin"));
40+
}
41+
claim_pin(pin);
42+
self->pin = pin;
43+
}
44+
45+
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp_obj_t *alarms) {
46+
// First, check to see if we match any given alarms.
47+
for (size_t i = 0; i < n_alarms; i++) {
48+
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
49+
return alarms[i];
50+
}
51+
}
52+
53+
alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t);
54+
alarm->base.type = &alarm_touch_touchalarm_type;
55+
alarm->pin = NULL;
56+
57+
// Map the pin number back to a pin object.
58+
for (size_t i = 0; i < mcu_pin_globals.map.used; i++) {
59+
const mcu_pin_obj_t* pin_obj = MP_OBJ_TO_PTR(mcu_pin_globals.map.table[i].value);
60+
if (pin_obj->touch_channel == touch_channel) {
61+
alarm->pin = mcu_pin_globals.map.table[i].value;
62+
break;
63+
}
64+
}
65+
66+
return alarm;
67+
}
68+
69+
// This is used to wake the main CircuitPython task.
70+
void touch_interrupt(void *arg) {
71+
(void) arg;
72+
woke_up = true;
73+
BaseType_t task_wakeup;
74+
vTaskNotifyGiveFromISR(circuitpython_task, &task_wakeup);
75+
if (task_wakeup) {
76+
portYIELD_FROM_ISR();
77+
}
78+
}
79+
80+
void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms) {
81+
bool touch_alarm_set = false;
82+
alarm_touch_touchalarm_obj_t *touch_alarm = MP_OBJ_NULL;
83+
84+
for (size_t i = 0; i < n_alarms; i++) {
85+
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
86+
if (!touch_alarm_set) {
87+
touch_alarm = MP_OBJ_TO_PTR(alarms[i]);
88+
touch_alarm_set = true;
89+
} else {
90+
mp_raise_ValueError(translate("Only one alarm.touch alarm can be set."));
91+
}
92+
}
93+
}
94+
if (!touch_alarm_set) {
95+
return;
96+
}
97+
98+
touch_channel = touch_alarm->pin->touch_channel;
99+
100+
// configure interrupt for pretend to deep sleep
101+
// this will be disabled if we actually deep sleep
102+
103+
// intialize touchpad
104+
peripherals_touch_reset();
105+
peripherals_touch_never_reset(true);
106+
peripherals_touch_init(touch_channel);
107+
108+
// wait for touch data to reset
109+
mp_hal_delay_ms(10);
110+
111+
// configure trigger threshold
112+
uint32_t touch_value;
113+
touch_pad_read_benchmark(touch_channel, &touch_value);
114+
touch_pad_set_thresh(touch_channel, touch_value * 0.1); //10%
115+
116+
// configure touch interrupt
117+
touch_pad_timeout_set(true, SOC_TOUCH_PAD_THRESHOLD_MAX);
118+
touch_pad_isr_register(touch_interrupt, NULL, TOUCH_PAD_INTR_MASK_ALL);
119+
touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT);
120+
}
121+
122+
void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
123+
// intialize touchpad
124+
peripherals_touch_never_reset(false);
125+
peripherals_touch_reset();
126+
peripherals_touch_init(touch_channel);
127+
128+
// configure touchpad for sleep
129+
touch_pad_sleep_channel_enable(touch_channel, true);
130+
touch_pad_sleep_channel_enable_proximity(touch_channel, false);
131+
132+
// wait for touch data to reset
133+
mp_hal_delay_ms(10);
134+
135+
// configure trigger threshold
136+
uint32_t touch_value;
137+
touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value);
138+
touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10%
139+
140+
// enable touchpad wakeup
141+
esp_sleep_enable_touchpad_wakeup();
142+
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
143+
}
144+
145+
bool alarm_touch_touchalarm_woke_us_up(void) {
146+
return woke_up;
147+
}
148+
149+
void alarm_touch_touchalarm_reset(void) {
150+
woke_up = false;
151+
touch_channel = TOUCH_PAD_MAX;
152+
peripherals_touch_never_reset(false);
153+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H
28+
#define MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H
29+
30+
#include "py/obj.h"
31+
#include "common-hal/microcontroller/Pin.h"
32+
33+
typedef struct {
34+
mp_obj_base_t base;
35+
const mcu_pin_obj_t *pin;
36+
} alarm_touch_touchalarm_obj_t;
37+
38+
// Find the alarm object that caused us to wake up or create an equivalent one.
39+
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp_obj_t *alarms);
40+
// Check for the wake up alarm from pretend deep sleep.
41+
void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms);
42+
void alarm_touch_touchalarm_prepare_for_deep_sleep(void);
43+
bool alarm_touch_touchalarm_woke_us_up(void);
44+
void alarm_touch_touchalarm_reset(void);
45+
46+
#endif // MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,11 @@
2727
#include "shared-bindings/touchio/TouchIn.h"
2828

2929
#include "py/runtime.h"
30-
#include "driver/touch_pad.h"
31-
32-
bool touch_inited = false;
33-
34-
void touchin_reset(void) {
35-
if (touch_inited) {
36-
touch_pad_deinit();
37-
touch_inited = false;
38-
}
39-
}
30+
#include "peripherals/touch.h"
4031

4132
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
4233
uint32_t touch_value;
43-
touch_pad_read_raw_data((touch_pad_t)self->pin->touch_channel, &touch_value);
34+
touch_pad_read_raw_data(self->pin->touch_channel, &touch_value);
4435
if (touch_value > UINT16_MAX) {
4536
return UINT16_MAX;
4637
}
@@ -54,21 +45,12 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
5445
}
5546
claim_pin(pin);
5647

57-
if (!touch_inited) {
58-
touch_pad_init();
59-
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
60-
touch_pad_fsm_start();
61-
touch_inited = true;
62-
}
63-
64-
touch_pad_config((touch_pad_t)pin->touch_channel);
48+
// initialize touchpad
49+
peripherals_touch_init(pin->touch_channel);
6550

66-
// wait for "raw data" to reset
51+
// wait for touch data to reset
6752
mp_hal_delay_ms(10);
6853

69-
// Initial values for pins will vary, depending on what peripherals the pins
70-
// share on-chip.
71-
7254
// Set a "touched" threshold not too far above the initial value.
7355
// For simple finger touch, the values may vary as much as a factor of two,
7456
// but for touches using fruit or other objects, the difference is much less.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy