diff --git a/examples/Advanced/Lora/LoRaSender/LoRaSender.ino b/examples/Advanced/Lora/LoRaSender/LoRaSender.ino index f09c7ee..4bae1a0 100644 --- a/examples/Advanced/Lora/LoRaSender/LoRaSender.ino +++ b/examples/Advanced/Lora/LoRaSender/LoRaSender.ino @@ -41,5 +41,5 @@ void loop() { counter++; - delay(50); + delay(1000); } \ No newline at end of file diff --git a/examples/Basics/Hello/Hello.ino b/examples/Basics/Hello/Hello.ino index a71a586..a2e6b48 100644 --- a/examples/Basics/Hello/Hello.ino +++ b/examples/Basics/Hello/Hello.ino @@ -13,4 +13,7 @@ void setup(){ // the loop routine runs over and over again forever void loop() { + if(digitalRead(39) == 0) { + M5.powerOFF(); + } } diff --git a/examples/Basics/PowerOFF/PowerOFF.ino b/examples/Basics/PowerOFF/PowerOFF.ino new file mode 100644 index 0000000..96075a2 --- /dev/null +++ b/examples/Basics/PowerOFF/PowerOFF.ino @@ -0,0 +1,26 @@ +#include + +// the setup routine runs once when M5Stack starts up +void setup(){ + + // initialize the M5Stack object + M5.begin(); + + // Lcd display + M5.Lcd.println("This is software power off demo"); + M5.Lcd.println("Press the button A to power off."); + + // Set the wakeup button + M5.setWakeupButton(BUTTON_A_PIN); +} + +// the loop routine runs over and over again forever +void loop() { + + if(M5.BtnA.wasPressed()) { + + M5.powerOFF(); + } + + M5.update(); +} diff --git a/src/M5Stack.cpp b/src/M5Stack.cpp index 4ff4bd5..c96d11c 100644 --- a/src/M5Stack.cpp +++ b/src/M5Stack.cpp @@ -8,7 +8,11 @@ void M5Stack::begin() { // UART Serial.begin(115200); Serial.flush(); - Serial.println("M5Stack init..."); + Serial.print("M5Stack initializing..."); + + // I2C + pinMode(SCL, OUTPUT); + digitalWrite(SDA, 1); // TONE Speaker.begin(); @@ -28,6 +32,11 @@ void M5Stack::begin() { Lcd.setTextColor(WHITE); Lcd.setTextSize(1); Lcd.setBrightness(50); + + // Set wakeup button + setWakeupButton(BUTTON_A_PIN); + + Serial.println("OK."); } void M5Stack::update() { @@ -72,4 +81,26 @@ void M5Stack::startupLogo() { } } +void M5Stack::setWakeupButton(uint8_t button) { + _wakeupPin = button; +} + +void M5Stack::powerOFF() { + + // power off the Lcd + Lcd.setBrightness(0); + Lcd.sleep(); + + // ESP32 into deep sleep + uint64_t _wakeupPin_mask = 1ULL << _wakeupPin; + USE_SERIAL.printf("Enabling EXT1 wakeup on pins GPIO%d\n", _wakeupPin); + esp_sleep_enable_ext1_wakeup(_wakeupPin_mask , ESP_EXT1_WAKEUP_ALL_LOW); + while(digitalRead(_wakeupPin) == LOW) { + delay(10); + } + USE_SERIAL.println("On deep sleep mode."); + esp_deep_sleep_start(); + USE_SERIAL.println("On power OFF fail!"); +} + M5Stack m5stack; diff --git a/src/M5Stack.h b/src/M5Stack.h index ff20a22..399012c 100644 --- a/src/M5Stack.h +++ b/src/M5Stack.h @@ -94,6 +94,7 @@ extern "C" { #include "esp32-hal-dac.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "esp_sleep.h" } class M5Stack { @@ -104,6 +105,9 @@ class M5Stack { void update(); void startupLogo(); + void setWakeupButton(uint8_t button); + void powerOFF(); + // Button API #define DEBOUNCE_MS 20 Button BtnA = Button(BUTTON_A_PIN, true, DEBOUNCE_MS); @@ -122,6 +126,7 @@ class M5Stack { private: + uint8_t _wakeupPin; }; extern M5Stack m5stack; diff --git a/src/utility/Display.cpp b/src/utility/Display.cpp index 60e7d19..547f560 100644 --- a/src/utility/Display.cpp +++ b/src/utility/Display.cpp @@ -3076,6 +3076,11 @@ void ILI9341::setBrightness(uint8_t brightness) ledcWrite(2, brightness); } +void ILI9341::sleep() { + spi_begin(); + writecommand(ILI9341_SLPIN); // Software reset + spi_end(); +} /*************************************************** This library is written to be compatible with Adafruit's ILI9341 library and automatically detects the display type on ESP_WROVER_KITs @@ -3212,16 +3217,7 @@ static uint32_t jpgWrite(JDEC *decoder, void *bitmap, JRECT *rect) line = w - (oL + oR); while (line--) { - // 012 - // 021 - // 102 - // 120 - // 210 - // 201 - pixBuf[pixIndex++] = jpgColor(data); -// #define jpgColorx(c) (((uint16_t)(((uint8_t *)(c))[0] & 0xF8) << 8) | ((uint16_t)(((uint8_t *)(c))[2] & 0xFC) << 3) | ((((uint8_t *)(c))[1] & 0xF8) >> 3)) - // pixBuf[pixIndex++] = jpgColorx(data); data += 3; if (pixIndex == 32) { diff --git a/src/utility/Display.h b/src/utility/Display.h index e64109b..e662f54 100644 --- a/src/utility/Display.h +++ b/src/utility/Display.h @@ -582,6 +582,7 @@ public: textWidth(const String& string), fontHeight(int16_t font); //--- + void sleep(); void setBrightness(uint8_t brightness); void progressBar(int x, int y, int w, int h, uint8_t val); void setAddrWindow(int32_t xs, int32_t ys, int32_t xe, int32_t ye);