Skip to content

Support for Touch Alarm #3850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-12-23 23:04-0500\n"
"POT-Creation-Date: 2020-12-22 22:54+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -1170,6 +1170,7 @@ msgstr ""

#: ports/atmel-samd/common-hal/audioio/AudioOut.c
#: ports/atmel-samd/common-hal/touchio/TouchIn.c
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
#: shared-module/rgbmatrix/RGBMatrix.c
msgid "Invalid pin"
Expand Down Expand Up @@ -1488,6 +1489,10 @@ msgstr ""
msgid "Only one alarm.time alarm can be set."
msgstr ""

#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
msgid "Only one alarm.touch alarm can be set."
msgstr ""

#: shared-module/displayio/ColorConverter.c
msgid "Only one color can be transparent at a time"
msgstr ""
Expand Down Expand Up @@ -1858,6 +1863,10 @@ msgstr ""
msgid "Total data to write is larger than outgoing_packet_length"
msgstr ""

#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
msgid "TouchAlarm not available in light sleep"
msgstr ""

#: py/obj.c
msgid "Traceback (most recent call last):\n"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ STATIC void start_mp(supervisor_allocation* heap) {

#if CIRCUITPY_ALARM
// Record which alarm woke us up, if any. An object may be created so the heap must be functional.
alarm_save_wakeup_alarm();
alarm_save_wake_alarm();
// Reset alarm module only after we retrieved the wakeup alarm.
alarm_reset();
#endif
Expand Down
1 change: 1 addition & 0 deletions ports/esp32s2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ SRC_C += \
lib/utils/sys_stdio_mphal.c \
lib/netutils/netutils.c \
peripherals/timer.c \
peripherals/touch.c \
peripherals/pcnt.c \
peripherals/pins.c \
peripherals/rmt.c \
Expand Down
26 changes: 16 additions & 10 deletions ports/esp32s2/common-hal/alarm/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
#include "py/runtime.h"

#include "shared-bindings/alarm/__init__.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/alarm/SleepMemory.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/alarm/time/TimeAlarm.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/alarm/touch/TouchAlarm.h"

#include "shared-bindings/wifi/__init__.h"
#include "shared-bindings/microcontroller/__init__.h"

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

void alarm_reset(void) {
alarm_time_timealarm_reset();
alarm_pin_pinalarm_reset();
alarm_sleep_memory_reset();
alarm_pin_pinalarm_reset();
alarm_time_timealarm_reset();
alarm_touch_touchalarm_reset();
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
}

STATIC esp_sleep_wakeup_cause_t _get_wakeup_cause(void) {
if (alarm_pin_pinalarm_woke_us_up()) {
return ESP_SLEEP_WAKEUP_GPIO;
}
if (alarm_time_timealarm_woke_us_up()) {
return ESP_SLEEP_WAKEUP_TIMER;
}
if (alarm_pin_pinalarm_woke_us_up()) {
return ESP_SLEEP_WAKEUP_GPIO;
if (alarm_touch_touchalarm_woke_us_up()) {
return ESP_SLEEP_WAKEUP_TOUCHPAD;
}

return esp_sleep_get_wakeup_cause();
}

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

case ESP_SLEEP_WAKEUP_TOUCHPAD:
// TODO: implement TouchIO
// Wake up from touch on pad, esp_sleep_get_touchpad_wakeup_status()
return alarm_touch_touchalarm_get_wakeup_alarm(n_alarms, alarms);
break;

case ESP_SLEEP_WAKEUP_UNDEFINED:
Expand All @@ -108,6 +112,7 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) {
STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) {
alarm_pin_pinalarm_set_alarms(deep_sleep, n_alarms, alarms);
alarm_time_timealarm_set_alarms(deep_sleep, n_alarms, alarms);
alarm_touch_touchalarm_set_alarm(deep_sleep, n_alarms, alarms);
}

STATIC void _idle_until_alarm(void) {
Expand All @@ -116,9 +121,9 @@ STATIC void _idle_until_alarm(void) {
RUN_BACKGROUND_TASKS;
// Allow ctrl-C interrupt.
if (alarm_woken_from_sleep()) {
alarm_save_wake_alarm();
return;
}

port_idle_until_interrupt();
}
}
Expand All @@ -144,6 +149,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala

void NORETURN alarm_enter_deep_sleep(void) {
alarm_pin_pinalarm_prepare_for_deep_sleep();
alarm_touch_touchalarm_prepare_for_deep_sleep();
// The ESP-IDF caches the deep sleep settings and applies them before sleep.
// We don't need to worry about resetting them in the interim.
esp_deep_sleep_start();
Expand Down
153 changes: 153 additions & 0 deletions ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "shared-bindings/alarm/touch/TouchAlarm.h"
#include "shared-bindings/microcontroller/__init__.h"

#include "esp_sleep.h"
#include "peripherals/touch.h"
#include "supervisor/esp_port.h"

static volatile bool woke_up = false;
static touch_pad_t touch_channel = TOUCH_PAD_MAX;

void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
if (pin->touch_channel == TOUCH_PAD_MAX) {
mp_raise_ValueError(translate("Invalid pin"));
}
claim_pin(pin);
self->pin = pin;
}

mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp_obj_t *alarms) {
// First, check to see if we match any given alarms.
for (size_t i = 0; i < n_alarms; i++) {
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
return alarms[i];
}
}

alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t);
alarm->base.type = &alarm_touch_touchalarm_type;
alarm->pin = NULL;

// Map the pin number back to a pin object.
for (size_t i = 0; i < mcu_pin_globals.map.used; i++) {
const mcu_pin_obj_t* pin_obj = MP_OBJ_TO_PTR(mcu_pin_globals.map.table[i].value);
if (pin_obj->touch_channel == touch_channel) {
alarm->pin = mcu_pin_globals.map.table[i].value;
break;
}
}

return alarm;
}

// This is used to wake the main CircuitPython task.
void touch_interrupt(void *arg) {
(void) arg;
woke_up = true;
BaseType_t task_wakeup;
vTaskNotifyGiveFromISR(circuitpython_task, &task_wakeup);
if (task_wakeup) {
portYIELD_FROM_ISR();
}
}

void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms) {
bool touch_alarm_set = false;
alarm_touch_touchalarm_obj_t *touch_alarm = MP_OBJ_NULL;

for (size_t i = 0; i < n_alarms; i++) {
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) {
if (!touch_alarm_set) {
touch_alarm = MP_OBJ_TO_PTR(alarms[i]);
touch_alarm_set = true;
} else {
mp_raise_ValueError(translate("Only one alarm.touch alarm can be set."));
}
}
}
if (!touch_alarm_set) {
return;
}

touch_channel = touch_alarm->pin->touch_channel;

// configure interrupt for pretend to deep sleep
// this will be disabled if we actually deep sleep

// intialize touchpad
peripherals_touch_reset();
peripherals_touch_never_reset(true);
peripherals_touch_init(touch_channel);

// wait for touch data to reset
mp_hal_delay_ms(10);

// configure trigger threshold
uint32_t touch_value;
touch_pad_read_benchmark(touch_channel, &touch_value);
touch_pad_set_thresh(touch_channel, touch_value * 0.1); //10%

// configure touch interrupt
touch_pad_timeout_set(true, SOC_TOUCH_PAD_THRESHOLD_MAX);
touch_pad_isr_register(touch_interrupt, NULL, TOUCH_PAD_INTR_MASK_ALL);
touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT);
}

void alarm_touch_touchalarm_prepare_for_deep_sleep(void) {
// intialize touchpad
peripherals_touch_never_reset(false);
peripherals_touch_reset();
peripherals_touch_init(touch_channel);

// configure touchpad for sleep
touch_pad_sleep_channel_enable(touch_channel, true);
touch_pad_sleep_channel_enable_proximity(touch_channel, false);

// wait for touch data to reset
mp_hal_delay_ms(10);

// configure trigger threshold
uint32_t touch_value;
touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value);
touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10%

// enable touchpad wakeup
esp_sleep_enable_touchpad_wakeup();
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
}

bool alarm_touch_touchalarm_woke_us_up(void) {
return woke_up;
}

void alarm_touch_touchalarm_reset(void) {
woke_up = false;
touch_channel = TOUCH_PAD_MAX;
peripherals_touch_never_reset(false);
}
46 changes: 46 additions & 0 deletions ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H
#define MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H

#include "py/obj.h"
#include "common-hal/microcontroller/Pin.h"

typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t *pin;
} alarm_touch_touchalarm_obj_t;

// Find the alarm object that caused us to wake up or create an equivalent one.
mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp_obj_t *alarms);
// Check for the wake up alarm from pretend deep sleep.
void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms);
void alarm_touch_touchalarm_prepare_for_deep_sleep(void);
bool alarm_touch_touchalarm_woke_us_up(void);
void alarm_touch_touchalarm_reset(void);

#endif // MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H
28 changes: 5 additions & 23 deletions ports/esp32s2/common-hal/touchio/TouchIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,11 @@
#include "shared-bindings/touchio/TouchIn.h"

#include "py/runtime.h"
#include "driver/touch_pad.h"

bool touch_inited = false;

void touchin_reset(void) {
if (touch_inited) {
touch_pad_deinit();
touch_inited = false;
}
}
#include "peripherals/touch.h"

static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
uint32_t touch_value;
touch_pad_read_raw_data((touch_pad_t)self->pin->touch_channel, &touch_value);
touch_pad_read_raw_data(self->pin->touch_channel, &touch_value);
if (touch_value > UINT16_MAX) {
return UINT16_MAX;
}
Expand All @@ -54,21 +45,12 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
}
claim_pin(pin);

if (!touch_inited) {
touch_pad_init();
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
touch_pad_fsm_start();
touch_inited = true;
}

touch_pad_config((touch_pad_t)pin->touch_channel);
// initialize touchpad
peripherals_touch_init(pin->touch_channel);

// wait for "raw data" to reset
// wait for touch data to reset
mp_hal_delay_ms(10);

// 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.
Expand Down
Loading