espruino / Espruino

The Espruino JavaScript interpreter - Official Repo
http://www.espruino.com/
Other
2.75k stars 739 forks source link

Add function deepSleepExt0 #2358

Closed cwilling closed 1 year ago

cwilling commented 1 year ago

This PR adds ability for ESP32 boards to wake from deep sleep by trigger from external interrupt (setting a GPIO pin). It may be confirmed with this example code:

const buttonPin = D4;
pinMode(buttonPin, 'input');

// Flash LED1 (while awake)
var on = false;
setInterval(function() {
  on = !on;
  LED1.write(on);
}, 500);

// Every 10secs, sleep until buttonPin is pressed
setInterval(function() {
  console.log("Time to sleep");
  ESP32.deepSleepExt0(buttonPin, 1);
}, 10000);
gfwilliams commented 1 year ago

Looks great - thanks! Very happy to have stuff like this if it makes the ESP32 port more usable.

I'm merging now, but quick question: Can esp_sleep_enable_ext0_wakeup return an error code (eg if the wrong pin is specified like esp_sleep_enable_ext0_wakeup(700, ...)?)

If so it might be worth checking it and doing jsExceptionHere(JSET_ERROR, "Invalid Arguments") rather than trying to sleep the ESP32 with no way to wake it?

cwilling commented 1 year ago

Thanks for the merge. I'll have a look at how to do the jsExceptionHere thing (still new to Espruino).