/**
 **************************************************
 * @file        Inkplate6PLUS_Simple_Deep_Sleep.ino
 * @brief       Simple deep sleep slideshow example for Soldered Inkplate 6PLUS.
 *
 * @details     Demonstrates low-power operation on Inkplate 6PLUS using ESP32 deep
 *              sleep. On each wake-up (timer-based), the board redraws the screen
 *              with the next image in a small slideshow, performs a full display
 *              refresh, and then returns to deep sleep.
 *
 * Requirements:
 * - Board:      Soldered Inkplate 6PLUS
 * - Hardware:   Inkplate 6PLUS, USB cable (or battery for low-power testing)
 * - Extra:      Converted image header files (picture1.h, picture2.h, picture3.h)
 *
 * Configuration:
 * - Boards Manager -> Inkplate Boards -> Soldered Inkplate6PLUS
 * - Serial settings: Not required
 *
 * Don't have Inkplate Boards in Arduino Boards Manager?
 * See https://docs.soldered.com/inkplate/6PLUS/quick-start-guide/
 *
 * How to use:
 * 1) Convert 3 images using the Soldered Image Converter and include them as
 *    picture1.h, picture2.h, and picture3.h.
 * 2) Upload the sketch to Inkplate 6PLUS.
 * 3) The board will show an image, go to deep sleep, and wake up every 20 seconds.
 * 4) After each wake-up, the next image is shown (loops through 3 images).
 *
 * Expected output:
 * - Inkplate display shows a new image every 20 seconds.
 * - The slideshow loops through all provided images.
 *
 * Notes:
 * - Deep sleep restarts the program from the beginning on every wake-up.
 * - RAM contents are lost during deep sleep, so standard partial updates cannot be used.
 * - This example uses 3-bit (grayscale) mode, which requires full refresh updates.
 *
 * Docs:         https://docs.soldered.com/inkplate
 * Image tool:   https://tools.soldered.com/tools/image-converter/
 * Support:      https://forum.soldered.com/
 *
 * @author      Soldered
 * @date        2021-02-11
 * @license     GNU GPL V3
 **************************************************/


// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#if !defined(ARDUINO_INKPLATE6PLUS) && !defined(ARDUINO_INKPLATE6PLUSV2)
#error                                                                                                                 \
    "Wrong board selection for this example, please select e-radionica Inkplate 6Plus or Soldered Inkplate 6Plus in the boards menu."
#endif

#include "Inkplate.h"      //Include Inkplate library to the sketch
#include "driver/rtc_io.h" //ESP32 library used for deep sleep and RTC wake up pins
#include "picture1.h" //Include .h files of 3 pictures. All three pictures were converted using Inkplate Image Converter
#include "picture2.h"
#include "picture3.h"
const uint8_t *pictures[] = {picture1, picture2,
                             picture3}; // This array of pinters holds address of every picture in the memory,
                                        // so we can easly select it by selecting index in array

#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP  20      // How long ESP32 will be in deep sleep (in seconds)
RTC_DATA_ATTR int slide = 0;

Inkplate display(INKPLATE_3BIT); // Create an object on Inkplate library and also set library into 3 Bit mode (gray)

void setup()
{
    display.begin();        // Init Inkplate library (you should call this function ONLY ONCE)
    display.clearDisplay(); // Clear frame buffer of display
    display.image.draw(
        pictures[slide], 0, 0, 1100,
        825); // Display selected picture at location X=0, Y=0. All three pictures have resolution of 1100x825 pixels
    display.display(); // Refresh the screen with new picture
    slide++; // Update counter for pictures. With this variable, we choose what picture is going to be displayed on
             // screen
    if (slide > 2)
        slide = 0; // We do not have more than 3 images, so roll back to zero

    // Uncomment this line if your Inkplate is older than Aug 2021 as older Inkplates have ESP32 wrover-e chips
    // rtc_gpio_isolate(GPIO_NUM_12); // Isolate/disable GPIO12 on ESP32 (only to reduce power consumption in sleep)
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); // Activate wake-up timer -- wake up after 20s here
    esp_deep_sleep_start();                                        // Put ESP32 into deep sleep. Program stops here.
}

void loop()
{
    // Nothing! If you use deep sleep, whole program should be in setup() because each time the board restarts, not in a
    // loop()! loop() must be empty!
}
