Created
January 21, 2020 15:51
-
-
Save joeycastillo/c4d00a4a0379207dcf3fcc4460ec5b2d to your computer and use it in GitHub Desktop.
Display grayscale images on a GDEW042T2 e-paper display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// library here: https://github.com/joeycastillo/The-Open-Book/tree/master/src | |
// convert images with this tool: https://littlevgl.com/image-to-c-array | |
// options: Indexed 4 colors, output format binary. Do the dithering on your own, | |
// and make sure your four colors include pure white and pure black. | |
#include <OpenBook.h> | |
#include <SD.h> | |
OpenBook *book; | |
// or for using your own 4.2" B&W display (substitute your pin assignments): | |
// OpenBook_IL0398 *display = new OpenBook_IL0398(300, 400, DC, RST, CS, SRCS, BUSY); | |
void drawBitmap(OpenBook_IL0398 *display, int16_t x, int16_t y, char *filename, int16_t w, int16_t h) { | |
File file = SD.open(filename); | |
file.seek(4); | |
uint32_t colors[4]; | |
file.read(&colors, 16); | |
colors[0] &= 0xFF; | |
colors[1] &= 0xFF; | |
colors[2] &= 0xFF; | |
colors[3] &= 0xFF; | |
uint8_t INDEX_BLACK = 255; | |
uint8_t INDEX_DARK = 255; | |
uint8_t INDEX_LIGHT = 255; | |
uint8_t INDEX_WHITE = 255; | |
for(int i = 0; i < 4; i++) { | |
switch (colors[i]) { | |
case 0x00: | |
INDEX_BLACK = i; | |
break; | |
case 0xFF: | |
INDEX_WHITE = i; | |
break; | |
default: | |
if (colors[i] < 128) INDEX_DARK = i; | |
else INDEX_LIGHT = i; | |
} | |
} | |
int16_t byteWidth = (w*2 + 7) / 8; | |
uint8_t byte = 0; | |
uint16_t color; | |
display->setDisplayMode(OPEN_BOOK_DISPLAY_MODE_GRAYSCALE); | |
display->startWrite(); | |
for(int16_t j=0; j<h; j++, y++) { | |
for(int16_t i=0; i < w * 2; i += 2 ) { | |
if(i & 7) byte <<= 2; | |
else file.read(&byte, 1); // byte = bitmap[j * byteWidth + i / 8] | |
uint8_t val = (byte & 0xC0) >> 6; | |
if (val == INDEX_BLACK) { color = EPD_BLACK; Serial.print("#"); } | |
else if (val == INDEX_DARK) { color = EPD_DARK; Serial.print("+"); } | |
else if (val == INDEX_LIGHT) { color = EPD_LIGHT; Serial.print("-"); } | |
else if (val == INDEX_WHITE) { color = EPD_WHITE; Serial.print(" "); } | |
display->writePixel(x+i/2, y, color); | |
} | |
Serial.println(); | |
} | |
display->endWrite(); | |
} | |
void setup() { | |
Serial.begin(115200); | |
while(!Serial && millis() < 5000); | |
if (!SD.begin(OPENBOOK_SDCS)) { | |
Serial.println("No SD?"); | |
} | |
book = new OpenBook(); | |
book->configureScreen(); | |
OpenBook_IL0398 *display = book->getDisplay(); | |
display->setRotation(0); | |
drawBitmap(display, 0, 0, "image.bin", 300, 400); | |
display->display(); | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment