|
| 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 | +} |
0 commit comments