|
26 | 26 | // you can use any GPIO for WAKE_UP_PIN except for D0/GPIO16 as it doesn't support interrupts
|
27 | 27 |
|
28 | 28 | void IRAM_ATTR wakeupPinIsr() {
|
29 |
| - // For edge-triggered IRQ. |
30 |
| - detachInterrupt(WAKE_UP_PIN); |
31 |
| - schedule_function([]() { |
32 |
| - Serial.println("GPIO went from HI to LO"); |
33 |
| - }); |
| 29 | + // For edge-triggered IRQ. |
| 30 | + detachInterrupt(WAKE_UP_PIN); |
| 31 | + schedule_function([]() { |
| 32 | + Serial.println("GPIO went from HI to LO"); |
| 33 | + }); |
34 | 34 | }
|
35 | 35 |
|
36 | 36 | void IRAM_ATTR wakeupPinIsrWE() {
|
37 |
| - // Wakeup IRQs are available as level-triggered only. |
38 |
| - detachInterrupt(WAKE_UP_PIN); |
39 |
| - schedule_function([]() { |
40 |
| - Serial.println("GPIO wakeup IRQ"); |
41 |
| - }); |
42 |
| - wakeupPinIsr(); |
43 |
| - // reattach falling edge IRQ in loop |
| 37 | + // Wakeup IRQs are available as level-triggered only. |
| 38 | + detachInterrupt(WAKE_UP_PIN); |
| 39 | + schedule_function([]() { |
| 40 | + Serial.println("GPIO wakeup IRQ"); |
| 41 | + }); |
| 42 | + wakeupPinIsr(); |
| 43 | + // reattach falling edge IRQ in loop |
44 | 44 | }
|
45 | 45 |
|
46 | 46 | void wakeupCallback() {
|
47 |
| - schedule_function([]() { |
48 |
| - Serial.println("wakeup callback was performed"); |
49 |
| - }); |
50 |
| - // return to falling edge IRQ, otherwise level-triggered IRQ with wakeup |
51 |
| - // would get called unexpectedly while awake. |
52 |
| - attachInterrupt(WAKE_UP_PIN, wakeupPinIsr, FALLING); |
| 47 | + schedule_function([]() { |
| 48 | + Serial.println("wakeup callback was performed"); |
| 49 | + }); |
| 50 | + // return to falling edge IRQ, otherwise level-triggered IRQ with wakeup |
| 51 | + // would get called unexpectedly while awake. |
| 52 | + attachInterrupt(WAKE_UP_PIN, wakeupPinIsr, FALLING); |
53 | 53 | }
|
54 | 54 |
|
55 | 55 | void setup() {
|
56 |
| - Serial.begin(74880); |
57 |
| - while (!Serial) |
58 |
| - ; |
59 |
| - delay(100); |
60 |
| - pinMode(LED_BUILTIN, OUTPUT); // activity and status indicator |
61 |
| - digitalWrite(LED_BUILTIN, LOW); // turn on the LED |
62 |
| - pinMode(WAKE_UP_PIN, INPUT_PULLUP); // polled to advance tests, interrupt for Forced Light Sleep |
63 |
| - attachInterrupt(WAKE_UP_PIN, wakeupPinIsr, FALLING); |
| 56 | + Serial.begin(74880); |
| 57 | + while (!Serial) |
| 58 | + ; |
| 59 | + delay(100); |
| 60 | + pinMode(LED_BUILTIN, OUTPUT); // activity and status indicator |
| 61 | + digitalWrite(LED_BUILTIN, LOW); // turn on the LED |
| 62 | + pinMode(WAKE_UP_PIN, INPUT_PULLUP); // polled to advance tests, interrupt for Forced Light Sleep |
| 63 | + attachInterrupt(WAKE_UP_PIN, wakeupPinIsr, FALLING); |
64 | 64 | }
|
65 | 65 |
|
66 | 66 | using oneShotYieldMs = esp8266::polledTimeout::timeoutTemplate<false, esp8266::polledTimeout::YieldPolicy::YieldOrSkip>;
|
67 | 67 | oneShotYieldMs gotoSleep(2000);
|
68 | 68 |
|
69 | 69 | void loop() {
|
70 |
| - if (gotoSleep) { |
71 |
| - // No new timers, no delay(), while RAII ForcedLightSleepToken exists. |
72 |
| - // Only ONLOW_WE or ONHIGH_WE interrupts work, no edge, that's an SDK or CPU limitation. |
73 |
| - // If the GPIO is in the state that will cause a wakeup on attaching the interrupt, |
74 |
| - // it cannot trigger a wakeup later, but any sleep duration will be honored. |
75 |
| - bool wakeupPinIsHigh = digitalRead(WAKE_UP_PIN); |
76 |
| - { |
77 |
| - ESPForcedLightSleepToken token(10 * 1000 * 1000, wakeupCallback); |
78 |
| - if (token) { // if true, run user code to set up forced light sleep details |
79 |
| - // debouncing the wake up pin |
80 |
| - delayMicroseconds(5000); |
81 |
| - wakeupPinIsHigh &= digitalRead(WAKE_UP_PIN); |
82 |
| - delayMicroseconds(5000); |
83 |
| - wakeupPinIsHigh &= digitalRead(WAKE_UP_PIN); |
84 |
| - // the GPIO might still bounce to LOW after this but before sleep is full engaged, |
85 |
| - // disabling wakeup after all |
86 |
| - if (wakeupPinIsHigh) { |
87 |
| - attachInterrupt(WAKE_UP_PIN, wakeupPinIsrWE, ONLOW_WE); |
88 |
| - } |
89 |
| - digitalWrite(LED_BUILTIN, HIGH); // turn the LED off so they know the CPU isn't running |
90 |
| - if (!wakeupPinIsHigh) token.cancel(); |
91 |
| - } |
92 |
| - // RAII token gets destructed, going to sleep if all went well |
93 |
| - } |
94 |
| - digitalWrite(LED_BUILTIN, LOW); // turn on the LED |
95 |
| - // retry immediately if the GPIO was found not ready for entering sleep |
| 70 | + if (gotoSleep) { |
| 71 | + // No new timers, no delay(), while RAII ForcedLightSleepToken exists. |
| 72 | + // Only ONLOW_WE or ONHIGH_WE interrupts work, no edge, that's an SDK or CPU limitation. |
| 73 | + // If the GPIO is in the state that will cause a wakeup on attaching the interrupt, |
| 74 | + // it cannot trigger a wakeup later, but any sleep duration will be honored. |
| 75 | + bool wakeupPinIsHigh = digitalRead(WAKE_UP_PIN); |
| 76 | + { |
| 77 | + ESPForcedLightSleepToken token(10 * 1000 * 1000, wakeupCallback); |
| 78 | + if (token) { // if true, run user code to set up forced light sleep details |
| 79 | + // debouncing the wake up pin |
| 80 | + delayMicroseconds(5000); |
| 81 | + wakeupPinIsHigh &= digitalRead(WAKE_UP_PIN); |
| 82 | + delayMicroseconds(5000); |
| 83 | + wakeupPinIsHigh &= digitalRead(WAKE_UP_PIN); |
| 84 | + // the GPIO might still bounce to LOW after this but before sleep is full engaged, |
| 85 | + // disabling wakeup after all |
96 | 86 | if (wakeupPinIsHigh) {
|
97 |
| - gotoSleep.reset(); |
| 87 | + attachInterrupt(WAKE_UP_PIN, wakeupPinIsrWE, ONLOW_WE); |
98 | 88 | }
|
99 |
| - // restore falling edge IRQ |
100 |
| - attachInterrupt(WAKE_UP_PIN, wakeupPinIsr, FALLING); |
| 89 | + digitalWrite(LED_BUILTIN, HIGH); // turn the LED off so they know the CPU isn't running |
| 90 | + if (!wakeupPinIsHigh) token.cancel(); |
| 91 | + } |
| 92 | + // RAII token gets destructed, going to sleep if all went well |
101 | 93 | }
|
| 94 | + digitalWrite(LED_BUILTIN, LOW); // turn on the LED |
| 95 | + // retry immediately if the GPIO was found not ready for entering sleep |
| 96 | + if (wakeupPinIsHigh) { |
| 97 | + gotoSleep.reset(); |
| 98 | + } |
| 99 | + // restore falling edge IRQ |
| 100 | + attachInterrupt(WAKE_UP_PIN, wakeupPinIsr, FALLING); |
| 101 | + } |
102 | 102 | }
|
0 commit comments