Skip to main content

Partial update with deep sleep

What this example shows​

Use partial updates together with deep sleep. A partial update compares the new frame with what the library remembers about the old one, and deep sleep erases that memory. So after waking up, the sketch first redraws the old screen, calls preloadScreen(), then draws the new values and does the partial update.

The two values that change, counter and decimal, are kept in RTC memory with RTC_DATA_ATTR, so the sketch can rebuild the old screen. Partial updates work only in 1-bit (black and white) mode. The first start does a normal full refresh, and every wake-up after that does a partial update.

Example code​

Do not use the other partial update examples together with deep sleep. Only use the pattern in this sketch, and always rebuild the screen before calling partialUpdate(). The sketch comment warns that otherwise you can damage your screen. The file header also recommends a full refresh every 5 to 10 partial updates, which this sketch does not do on its own.

In the Arduino IDE, choose the Inkplate 10 board under Tools, Board, Inkplate Boards before you upload.

Select Board
Inkplate10_Partial_Update_With_Deep_Sleep.ino
// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif

#include "Inkplate.h" // Include Inkplate library to the sketch
#include "driver/rtc_io.h" // Include ESP32 library for RTC pin I/O (needed for rtc_gpio_isolate() function)
#include <rom/rtc.h> // Include ESP32 library for RTC (needed for rtc_get_reset_reason() function)

#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 10 // Time how long ESP32 will be in deep sleep (in seconds). In this case 10 seconds.

Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1-bit mode (BW)

// Counter variable (stored in RTC RAM that stores variable even if deep sleep is used)
// Variables that are changed after each partial update has to be stored in RTC RAM in order to recreate screen before
// deep sleep
RTC_DATA_ATTR int counter = 0;
RTC_DATA_ATTR float decimal = PI;

void setup()
{
Serial.begin(115200);
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
createScreen(); // Function that contains everything that has to be written on screen

if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) // Check if ESP32 is reseted by deep sleep or power up / user manual
// reset (or some other reason)
{
display.preloadScreen(); // If is woken up by deep sleep, recreate whole screen to be same as was before deep sleep
counter++; // Update variable / variables
decimal *= 1.23;
display.clearDisplay(); // Clear everything in buffer
createScreen(); // Create new screen with new variables
display.partialUpdate(true); // Partial update of screen. (Use this only in this
// scenario, otherwise YOU CAN DAMAGE YOUR SCRREN)
}
else // If is not deep sleep reset, that must be some thing else, so use normal update procedure (full screen
// update)
{
display.display();
}
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP *
uS_TO_S_FACTOR); // Set EPS32 to be woken up in 10 seconds (in this case)

// 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_deep_sleep_start(); // Put ESP32 into deep sleep (low power mode)
}

void loop()
{
// Nothing should be here while using deep sleep!
}

void createScreen()
{
display.setFont(NULL);
display.setCursor(200, 300); // Set text cursor @ X = 200, Y = 300
display.setTextSize(3);
display.setTextColor(BLACK,WHITE); // Set font to be scaled up three times
display.print("First variable:"); // Write first variable to buffer
display.print(counter, DEC);
display.setCursor(200, 340); // Set text cursor @ X = 200, Y = 340
display.print("Second variable:"); // Write second variable to buffer (use two decimals places)
display.print(decimal, 2);
}

Trimmed from Inkplate10_Partial_Update_With_Deep_Sleep.ino: the file header comment is left out. The code is otherwise unchanged.

Functions used​

Inkplate(uint8_t mode)returns None

Creates the Inkplate object and chooses the display mode. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` is grayscale and only supports full updates.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_tmodeDisplay mode: INKPLATE_1BIT (0) or INKPLATE_3BIT (1).
Serial.begin(unsigned long baud)returns void

Starts the serial connection to the computer. Set the Serial Monitor to the same baud rate.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longbaudBaud rate. The sketch uses 115200.
display.begin()returns void

Initializes the library, the peripherals and the frame buffers. Call it once in `setup()`, later calls are ignored.

ReturnsNothing.

rtc_get_reset_reason(int cpu_no)returns RESET_REASON

Returns why the CPU last reset. The sketch compares it with `DEEPSLEEP_RESET` to tell a wake-up from deep sleep apart from a power-up or a manual reset. It comes from the ESP32 `rom/rtc.h` header, not from the Inkplate library.

ReturnsThe reset reason.

Parameters

TypeNameDescription
intcpu_noCPU number. The sketch passes 0.
display.preloadScreen()returns void

Copies the partial-update buffer into the buffer the library compares against on the next partial update. The deep sleep example calls it after waking up, once the screen has been drawn again, so the partial update can work out what changed.

ReturnsNothing.

display.clearDisplay()returns void

Clears the frame buffer in memory. It does not clear the panel itself, call `display()` afterwards to do that.

ReturnsNothing.

display.partialUpdate(bool _forced = false, bool leaveOn = false)returns uint32_t

Updates only the parts of the screen that changed, without a full refresh. It works in 1-bit mode only, in 3-bit mode it does nothing and returns 0. A full refresh every 5 to 10 partial updates keeps the image quality good.

ReturnsNumber of pixels that changed from black to white, which are the ones that leave visible blur.

Parameters

TypeNameDescription
bool_forcedFor advanced use with deep sleep: forces a partial update. Defaults to false.
boolleaveOnIf true, the e-paper supply stays on after the update. Defaults to false.
display.display(bool _leaveOn = 0)returns void

Refreshes the whole panel from the frame buffer (a full update).

ReturnsNothing.

Parameters

TypeNameDescription
bool_leaveOnIf true, the e-paper supply stays on after the update, which saves time on the next update. Defaults to 0.
esp_sleep_enable_timer_wakeup(uint64_t time_in_us)returns esp_err_t

Sets a timer that wakes the ESP32 from deep sleep. It comes from the ESP32 Arduino core, not from the Inkplate library.

ReturnsError code, not checked by the sketch.

Parameters

TypeNameDescription
uint64_ttime_in_usTime until wake-up, in microseconds. The sketch multiplies seconds by uS_TO_S_FACTOR (1000000).
esp_deep_sleep_start()returns void

Puts the ESP32 into deep sleep. The program stops here, and on wake-up it starts again from the beginning of `setup()`. It comes from the ESP32 Arduino core, not from the Inkplate library.

ReturnsDoes not return.

display.setFont(const GFXfont *f = NULL)returns void

Selects the font used for printed text. Passing `NULL` selects the built-in classic font. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
const GFXfont *fFont to use, or NULL for the built-in font.
display.setCursor(int16_t x, int16_t y)returns void

Sets where the next printed text starts, in pixels from the top left corner. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
int16_txX coordinate in pixels.
int16_tyY coordinate in pixels.
display.setTextSize(uint8_t s)returns void

Sets the text magnification. Size 1 is the classic 5x7 pixel font, size 5 is five times bigger. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tsText size multiplier.
display.setTextColor(uint16_t c, uint16_t bg)returns void

Sets the text color and the background color behind it. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
uint16_tcText color, for example BLACK.
uint16_tbgBackground color, for example WHITE.
display.print(value, format)returns size_t

Prints text, a character or a number at the cursor position, in the frame buffer. The display object builds on the Adafruit GFX base class, which extends the Arduino `Print` class, so it prints like `Serial` does. Call `display()` or `partialUpdate()` to show the result.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text, char or numbervalueWhat to print.
intformatOptional. Number base for integers, for example DEC.

What to expect​

The first start shows First variable:0 and Second variable:3.14 after a full refresh. Every 10 seconds the board wakes up, adds one to the first value, multiplies the second by 1.23, and updates the screen with a partial update. The sketch starts Serial at 115200 baud but does not print anything.