|
25 | 25 | */
|
26 | 26 |
|
27 | 27 | #include "shared-bindings/alarm/touch/TouchAlarm.h"
|
| 28 | +#include "shared-bindings/microcontroller/__init__.h" |
| 29 | + |
| 30 | +#include "peripherals/touch.h" |
28 | 31 |
|
29 | 32 | #include "esp_sleep.h"
|
30 | 33 |
|
31 | 34 | void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
|
32 |
| - |
| 35 | + if (pin->touch_channel == TOUCH_PAD_MAX) { |
| 36 | + mp_raise_ValueError(translate("Invalid pin")); |
| 37 | + } |
| 38 | + self->pin = pin; |
33 | 39 | }
|
34 | 40 |
|
35 | 41 | mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms) {
|
36 |
| - return mp_const_none; |
| 42 | + // First, check to see if we match any given alarms. |
| 43 | + for (size_t i = 0; i < n_alarms; i++) { |
| 44 | + if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) { |
| 45 | + return alarms[i]; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + gpio_num_t pin_number = esp_sleep_get_touchpad_wakeup_status(); |
| 50 | + |
| 51 | + alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t); |
| 52 | + alarm->base.type = &alarm_touch_touchalarm_type; |
| 53 | + alarm->pin = NULL; |
| 54 | + |
| 55 | + // Map the pin number back to a pin object. |
| 56 | + for (size_t i = 0; i < mcu_pin_globals.map.used; i++) { |
| 57 | + const mcu_pin_obj_t* pin_obj = MP_OBJ_TO_PTR(mcu_pin_globals.map.table[i].value); |
| 58 | + if (pin_obj->number == pin_number) { |
| 59 | + alarm->pin = mcu_pin_globals.map.table[i].value; |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return alarm; |
37 | 65 | }
|
38 | 66 |
|
| 67 | +static uint16_t sleep_touch_pin; |
| 68 | + |
39 | 69 | void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) {
|
| 70 | + sleep_touch_pin |= 1 << self->pin->number; |
| 71 | + esp_sleep_enable_touchpad_wakeup(); |
| 72 | + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); |
| 73 | +} |
| 74 | + |
| 75 | +static void configure_sleep_touch_pin(touch_pad_t touch_channel) { |
| 76 | + // intialize touchpad |
| 77 | + peripherals_touch_init(touch_channel); |
| 78 | + |
| 79 | + // configure touchpad for sleep |
| 80 | + touch_pad_sleep_channel_enable(touch_channel, true); |
| 81 | + touch_pad_sleep_channel_enable_proximity(touch_channel, false); |
| 82 | + |
| 83 | + // wait for touch data to reset |
| 84 | + mp_hal_delay_ms(10); |
40 | 85 |
|
| 86 | + uint32_t touch_value; |
| 87 | + touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value); |
| 88 | + touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10% |
| 89 | +} |
| 90 | + |
| 91 | +void alarm_touch_touchalarm_prepare_for_deep_sleep(void) { |
| 92 | + for (uint8_t i = 1; i <= 14; i++) { |
| 93 | + if ((sleep_touch_pin & 1 << i) != 0) { |
| 94 | + configure_sleep_touch_pin((touch_pad_t)i); |
| 95 | + } |
| 96 | + } |
41 | 97 | }
|
42 | 98 |
|
43 | 99 | bool alarm_touch_touchalarm_woke_us_up(void) {
|
44 | 100 | return false;
|
45 | 101 | }
|
46 | 102 |
|
47 | 103 | void alarm_touch_touchalarm_reset(void) {
|
48 |
| - |
| 104 | + sleep_touch_pin = 0; |
49 | 105 | }
|
0 commit comments