Thank you for your response.
I don't quit know how I could start. This is the code I am using ATM. It has the sleep function in it:
#include <GxEPD2_BW.h>
#include <GxEPD2_3C.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#if defined(ESP32)
// has support for FAT32 support with long filenames
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#define SdFile File
#define seekSet seek
#else
// include SdFat for FAT32 support with long filenames; available through Library Manager
#include <SdFat.h>
SdFat SD;
#endif
// select the display driver class (only one) for your panel
//#define GxEPD2_DRIVER_CLASS GxEPD2_154 // GDEP015OC1 200x200, no longer available
//#define GxEPD2_DRIVER_CLASS GxEPD2_154_D67 // GDEH0154D67 200x200
//#define GxEPD2_DRIVER_CLASS GxEPD2_154_T8 // GDEW0154T8 152x152
//#define GxEPD2_DRIVER_CLASS GxEPD2_154_M09 // GDEW0154M09 200x200
//#define GxEPD2_DRIVER_CLASS GxEPD2_154_M10 // GDEW0154M10 152x152
//#define GxEPD2_DRIVER_CLASS GxEPD2_213 // GDE0213B1 128x250, phased out
//#define GxEPD2_DRIVER_CLASS GxEPD2_213_B72 // GDEH0213B72 128x250
//#define GxEPD2_DRIVER_CLASS GxEPD2_213_B73 // GDEH0213B73 128x250
//#define GxEPD2_DRIVER_CLASS GxEPD2_213_flex // GDEW0213I5F 104x212
//#define GxEPD2_DRIVER_CLASS GxEPD2_213_M21 // GDEW0213M21 104x212
//#define GxEPD2_DRIVER_CLASS GxEPD2_213_T5D // GDEW0213T5D 104x212
//#define GxEPD2_DRIVER_CLASS GxEPD2_290 // GDEH029A1 128x296
//#define GxEPD2_DRIVER_CLASS GxEPD2_290_T5 // GDEW029T5 128x296
//#define GxEPD2_DRIVER_CLASS GxEPD2_290_T5D // GDEW029T5D 128x296
//#define GxEPD2_DRIVER_CLASS GxEPD2_290_M06 // GDEW029M06 128x296
//#define GxEPD2_DRIVER_CLASS GxEPD2_290_T94 // GDEM029T94 128x296
//#define GxEPD2_DRIVER_CLASS GxEPD2_260 // GDEW026T0 152x296
//#define GxEPD2_DRIVER_CLASS GxEPD2_270 // GDEW027W3 176x264
//#define GxEPD2_DRIVER_CLASS GxEPD2_371 // GDEW0371W7 240x416
//#define GxEPD2_DRIVER_CLASS GxEPD2_420 // GDEW042T2 400x300
//#define GxEPD2_DRIVER_CLASS GxEPD2_420_M01 // GDEW042M01 400x300
//#define GxEPD2_DRIVER_CLASS GxEPD2_583 // GDEW0583T7 600x448
//#define GxEPD2_DRIVER_CLASS GxEPD2_583_T8 // GDEW0583T8 648x480
//#define GxEPD2_DRIVER_CLASS GxEPD2_750 // GDEW075T8 640x384
//#define GxEPD2_DRIVER_CLASS GxEPD2_750_T7 // GDEW075T7 800x480
// 3-color e-papers
//#define GxEPD2_DRIVER_CLASS GxEPD2_154c // GDEW0154Z04 200x200, no longer available
//#define GxEPD2_DRIVER_CLASS GxEPD2_154_Z90c // GDEH0154Z90 200x200
//#define GxEPD2_DRIVER_CLASS GxEPD2_213c // GDEW0213Z16 104x212
//#define GxEPD2_DRIVER_CLASS GxEPD2_213_Z19c // GDEW0213Z19 104x212
//#define GxEPD2_DRIVER_CLASS GxEPD2_290c // GDEW029Z10 128x296
//#define GxEPD2_DRIVER_CLASS GxEPD2_290_Z13c // GDEH029Z13 128x296
//#define GxEPD2_DRIVER_CLASS GxEPD2_270c // GDEW027C44 176x264
#define GxEPD2_DRIVER_CLASS GxEPD2_420c // GDEW042Z15 400x300 The 4.2 inch color screen I use.
//#define GxEPD2_DRIVER_CLASS GxEPD2_583c // GDEW0583Z21 600x448
//#define GxEPD2_DRIVER_CLASS GxEPD2_750c // GDEW075Z09 600x384
//#define GxEPD2_DRIVER_CLASS GxEPD2_750c_Z08 // GDEW075Z08 800x480
//#define GxEPD2_DRIVER_CLASS GxEPD2_750c_Z90 // GDEH075Z90 880x528
// 7-color e-paper
//#define GxEPD2_DRIVER_CLASS GxEPD2_565c // Waveshare 5.65" 7-color (3C graphics)
#if defined(__AVR)
#define SD_CS 6 // adapt to your wiring
#define EPD_CS SS // adapt to your wiring
GxEPD2_DRIVER_CLASS display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
#endif
// SD card can be connected as:
//sck 13, miso 12, mosi 11, cs (6 in this case)
// non-AVR board can also be used with GxEPD2 base display classes, e.g. for SD bitmap drawing
// uncomment your display for your board in the following included header file and adapt to your mapping
#include "GxEPD2_SD_AVR_boards_added.h"
// function declaration with default parameter
void drawBitmapFromSD(const char *filename, int16_t x, int16_t y, bool with_color = true);
static const uint16_t input_buffer_pixels = 20; // may affect performance
static const uint16_t max_row_width = 640; // for up to 7.5" display
static const uint16_t max_palette_pixels = 256; // for depth <= 8
uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
void setup()
{
Serial.begin(115200);
Serial.println();
delay(10);
Serial.println(F("setup"));
display.init(115200);
Serial.print(F("Initializing SD card..."));
if (!SD.begin(SD_CS))
{
Serial.println(F("SD failed!"));
return;
}
Serial.println(F("SD OK!"));
Serial.println(F("entering loop"));
}
void loop(void)
{
//drawBitmaps_200x200(); //I don't want to draw such small pictures
drawBitmaps_other();
}
void myWatchdogEnable(const byte interval) //code to enable watchdog (for low power)
{
MCUSR = 0; // reset various flags
WDTCSR |= 0b00011000; // see docs, set WDCE, WDE
WDTCSR = 0b01000000 | interval; // set WDIE, and appropriate delay
wdt_reset();
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_mode(); // now goes to Sleep and waits for the interrupt
}
ISR(WDT_vect)
{
wdt_disable(); // disable watchdog
}
void sleepfunction(void) //the function that putes the arduino in deep sleep to save a lot of power
{
delay(50);
Serial.println("Putting Arduino in Low power mode");
delay(10);
for (int i = 0; i < 720; i++) //1 time loop is about 60 seconds. 1440 times this loop is about one day of sleep
{
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100000);
myWatchdogEnable (0b000111);
}
delay(100);
Serial.println("Waking Arduino from low power mode");
delay(10);
}
void drawBitmaps_200x200()
{
int16_t x = (int16_t(display.WIDTH) - 200) / 2;
int16_t y = (int16_t(display.HEIGHT) - 200) / 2;
drawBitmapFromSD("photo(1).bmp", x, y);
delay(2000);
}
void drawBitmaps_other() //this is where you put your pictures in BMP format.
{
int16_t w2 = display.WIDTH / 2;
int16_t h2 = display.HEIGHT / 2;
drawBitmapFromSD("painttest.bmp", w2 - 200, h2 - 150);
delay(1000);
sleepfunction();
drawBitmapFromSD("astro.bmp", w2 - 200, h2 - 150);
delay(1000);
sleepfunction();
drawBitmapFromSD("snake.bmp", w2 - 200, h2 - 150);
delay(1000);
sleepfunction();
drawBitmapFromSD("BPS.bmp", w2 - 200, h2 - 150);
delay(1000);
sleepfunction();
drawBitmapFromSD("snoopy.bmp", w2 - 200, h2 - 150);
delay(1000);
}
void drawBitmapFromSD(const char *filename, int16_t x, int16_t y, bool with_color) //function that draws the picture.
{
SdFile file;
bool valid = false; // valid format to be handled
bool flip = true; // bitmap is stored bottom-to-top
uint32_t startTime = millis();
//Serial.println(); Serial.print(x); Serial.print(" "); Serial.print(y); Serial.print(" "); Serial.println(filename);
if ((x >= int16_t(display.WIDTH)) || (y >= int16_t(display.HEIGHT))) return;
Serial.println();
Serial.println(F("--------PICTURE DATA--------"));
Serial.print(F("Loading image '"));
Serial.print(filename);
Serial.println('\'');
#if defined(ESP32)
file = SD.open(String("/") + filename, FILE_READ);
if (!file)
{
Serial.print(F("File not found"));
return;
}
#else
if (!file.open(filename, FILE_READ))
{
Serial.print(F("File not found"));
return;
}
#endif
// Parse BMP header
if (read16(file) == 0x4D42) // BMP signature
{
uint32_t fileSize = read32(file);
uint32_t creatorBytes = read32(file);
uint32_t imageOffset = read32(file); // Start of image data
uint32_t headerSize = read32(file);
uint32_t width = read32(file);
uint32_t height = read32(file);
uint16_t planes = read16(file);
uint16_t depth = read16(file); // bits per pixel
uint32_t format = read32(file);
if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
{
Serial.print(F("File size: ")); Serial.println(fileSize);
Serial.print(F("Image Offset: ")); Serial.println(imageOffset);
Serial.print(F("Header size: ")); Serial.println(headerSize);
Serial.print(F("Bit Depth: ")); Serial.println(depth);
Serial.print(F("Image size: "));
Serial.print(width);
Serial.print('x');
Serial.println(height);
// BMP rows are padded (if needed) to 4-byte boundary
uint32_t rowSize = (width * depth / 8 + 3) & ~3;
if (height < 0)
{
height = -height;
flip = false;
}
uint16_t w = width;
uint16_t h = height;
if ((x + w - 1) >= int16_t(display.WIDTH)) w = int16_t(display.WIDTH) - x;
if ((y + h - 1) >= int16_t(display.HEIGHT)) h = int16_t(display.HEIGHT) - y;
if (w <= max_row_width) // handle with direct drawing
{
valid = true;
uint8_t bitmask = 0xFF;
uint8_t bitshift = 8 - depth;
uint16_t red, green, blue;
bool whitish, colored;
if (depth == 1) with_color = false;
if (depth <= 8)
{
if (depth < 8) bitmask >>= depth;
//file.seekSet(54); //palette is always @ 54
file.seekSet(imageOffset - (4 << depth)); // 54 for regular, diff for colorsimportant
for (uint16_t pn = 0; pn < (1 << depth); pn++)
{
blue = file.read();
green = file.read();
red = file.read();
file.read();
whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
mono_palette_buffer[pn / 8] |= whitish << pn % 8;
if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
color_palette_buffer[pn / 8] |= colored << pn % 8;
}
}
display.clearScreen();
uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
{
uint32_t in_remain = rowSize;
uint32_t in_idx = 0;
uint32_t in_bytes = 0;
uint8_t in_byte = 0; // for depth <= 8
uint8_t in_bits = 0; // for depth <= 8
uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
uint32_t out_idx = 0;
file.seekSet(rowPosition);
for (uint16_t col = 0; col < w; col++) // for each pixel
{
// Time to read more pixel data?
if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
{
in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
in_remain -= in_bytes;
in_idx = 0;
}
switch (depth)
{
case 24:
blue = input_buffer[in_idx++];
green = input_buffer[in_idx++];
red = input_buffer[in_idx++];
whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
break;
case 16:
{
uint8_t lsb = input_buffer[in_idx++];
uint8_t msb = input_buffer[in_idx++];
if (format == 0) // 555
{
blue = (lsb & 0x1F) << 3;
green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
red = (msb & 0x7C) << 1;
}
else // 565
{
blue = (lsb & 0x1F) << 3;
green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
red = (msb & 0xF8);
}
whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
}
break;
case 1:
case 4:
case 8:
{
if (0 == in_bits)
{
in_byte = input_buffer[in_idx++];
in_bits = 8;
}
uint16_t pn = (in_byte >> bitshift) & bitmask;
whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
in_byte <<= depth;
in_bits -= depth;
}
break;
}
if (whitish)
{
//out_byte |= 0x80 >> col % 8; // not black
//out_color_byte |= 0x80 >> col % 8; // not colored
// keep white
}
else if (colored && with_color)
{
//out_byte |= 0x80 >> col % 8; // not black
out_color_byte &= ~(0x80 >> col % 8); // colored
}
else
{
//out_color_byte |= 0x80 >> col % 8; // not colored
out_byte &= ~(0x80 >> col % 8); // black
}
if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
{
output_row_color_buffer[out_idx] = out_color_byte;
output_row_mono_buffer[out_idx++] = out_byte;
out_byte = 0xFF; // white (for w%8!=0 border)
out_color_byte = 0xFF; // white (for w%8!=0 border)
}
} // end pixel
uint16_t yrow = y + (flip ? h - row - 1 : row);
display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
} // end line
Serial.print(F("loaded in ")); Serial.print(millis() - startTime); Serial.println(F(" ms"));
display.refresh();
Serial.println(F("---------------------------"));
Serial.println();
}
}
}
//Serial.print(F("end curPosition ")); Serial.println(file.curPosition());
file.close();
if (!valid)
{
Serial.println(F("bitmap format not handled."));
}
}
uint16_t read16(SdFile& f)
{
// BMP data is stored little-endian, same as Arduino.
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(SdFile& f)
{
// BMP data is stored little-endian, same as Arduino.
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
Also, could you tell me how to use int0 or int0 (or was this a typo?).