|
| 1 | +/** |
| 2 | + * Example of esp32 deep sleep when playback is stopped. |
| 3 | + * To connect the plugin, copy its folder to the src/plugins directory. |
| 4 | + */ |
| 5 | + |
| 6 | +#include "deepsleep.h" |
| 7 | +#include <Arduino.h> |
| 8 | +#include <Ticker.h> |
| 9 | +#include "../../core/options.h" |
| 10 | +#include "../../core/display.h" |
| 11 | + |
| 12 | +#define SLEEP_DELAY 60 /* 1 min deep sleep delay */ |
| 13 | +#define WAKEUP_PIN ENC_BTNB /* wakeup pin (one of: BTN_XXXX, ENC_BTNB, ENC2_BTNB) */ |
| 14 | + /* must be one of: 0,2,4,12,13,14,15,25,26,27,32,33,34,35,36,39 */ |
| 15 | +#define WAKEUP_LEVEL LOW /* wakeup level (usually LOW) */ |
| 16 | + |
| 17 | +Ticker deepSleepTicker; |
| 18 | +deepSleep dsleep; |
| 19 | + |
| 20 | +deepSleep::deepSleep() { |
| 21 | + registerPlugin(); |
| 22 | + log_i("Plugin is registered"); |
| 23 | +} |
| 24 | + |
| 25 | +void goToSleep(){ |
| 26 | + if(BRIGHTNESS_PIN!=255) analogWrite(BRIGHTNESS_PIN, 0); /* BRIGHTNESS_PIN added in v0.7.330 */ |
| 27 | + if(display.deepsleep()) { /* if deep sleep is possible */ |
| 28 | + esp_deep_sleep_start(); /* go to sleep */ |
| 29 | + }else{ /* else */ |
| 30 | + deepSleepTicker.detach(); /* detach the timer */ |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void deepSleep::on_setup(){ /* occurs during loading */ |
| 35 | + log_i("%s called", __func__ ); |
| 36 | + if(WAKEUP_PIN!=255){ |
| 37 | + esp_sleep_enable_ext0_wakeup((gpio_num_t)WAKEUP_PIN, WAKEUP_LEVEL); /* enable wakeup pin */ |
| 38 | + deepSleepTicker.attach(SLEEP_DELAY, goToSleep); /* attach to delay */ |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void deepSleep::on_start_play(){ /* occurs during player is start playing */ |
| 43 | + log_i("%s called", __func__ ); |
| 44 | + if(WAKEUP_PIN!=255){ |
| 45 | + deepSleepTicker.detach(); /* detach the timer */ |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +void deepSleep::on_stop_play(){ /* occurs during player is stop playing */ |
| 50 | + log_i("%s called", __func__ ); |
| 51 | + if(WAKEUP_PIN!=255){ |
| 52 | + deepSleepTicker.attach(SLEEP_DELAY, goToSleep); /* attach to delay */ |
| 53 | + } |
| 54 | +} |
0 commit comments