Show pictures from a microSD card
What this example shows
Reading image files from a microSD card and drawing them on the display: a BMP by file name, a second BMP through an opened SdFile, and a JPEG. The card must be FAT, FAT32 or exFAT formatted.
Example code
Trimmed from the pinned example: only the file header comment is removed, the code is unchanged.
#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
Inkplate display(INKPLATE_3BIT); // Create an object on Inkplate library and also set library into 3 Bit mode
SdFile file; // Create SdFile object used for accessing files on SD card
void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); // Clear frame buffer of display
display.setTextColor(BLACK); // Set text color to black
display.setTextSize(3); // Set font size to 3
// Init SD card. Display if SD card is init propery or not.
if (display.sdCardInit())
{
display.println("SD Card OK! Reading image...");
display.display();
// If card is properly init, try to load image and display it on e-paper at position X=0, Y=0
// NOTE: Both drawImage methods allow for an optional fifth "invert" parameter. Setting this parameter
// to true will flip all colors on the image, making black white and white black. This may be necessary when
// exporting bitmaps from certain softwares.
if (!display.image.draw("image1.bmp", 0, 0, 1))
{
// If is something failed (wrong filename or wrong bitmap format), write error message on the screen.
// REMEMBER! You can only use Windows Bitmap file with color depth of 1, 4, 8 or 24 bits with no
// compression! You can turn of dithering for somewhat faster image load by changing the last 1 to 0, or
// removing the 1 argument completely
display.println("Image open error");
}
display.display();
delay(5000);
// Now try to load image using SdFat library class (for more advanced users) and display image on epaper.
display.clearDisplay();
if (file.open("image2.bmp", O_RDONLY))
{
display.image.drawBitmapFromSd(&file, 0, 0);
}
else
{
display.println("Image open error");
}
display.display();
delay(5000);
// Now draw a JPEG
display.clearDisplay();
if (!display.image.draw("pyramid.jpg", 100, 0, true, false))
{
// If is something failed (wrong filename or wrong format), write error message on the screen.
// You can turn off dithering for somewhat faster image load by changing the fifth parameter to false, or
// removing the parameter completely
display.println("Image open error");
}
display.display();
}
else
{
// If SD card init not success, display error on screen
display.println("SD Card error!");
display.display();
}
// Turn off the MOSFET that powers the SD card
display.sdCardSleep();
}
void loop()
{
// Nothing...
}
Functions used
Inkplate(uint8_t mode)returns NoneCreates the display object. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` gives 8 levels of grey but only full updates.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | mode | INKPLATE_1BIT or INKPLATE_3BIT. |
display.begin()returns voidInitializes the library and the display. Call it once, at the start of `setup()`.
ReturnsNothing.
display.clearDisplay()returns voidClears the frame buffer in memory. It does not change what the panel shows until you call `display()`.
ReturnsNothing.
display.setTextColor(uint16_t c)returns voidSets the text color.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t | c | Text color, for example BLACK. |
display.setTextSize(uint8_t s)returns voidScales text by an integer factor. The base font is 5 x 7 pixels.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | s | Scale factor. |
display.sdCardInit()returns int16_tPowers the microSD card and starts the SD interface.
Returns`1` if the card initialized, `0` if it failed.
display.println(value)returns size_tSame as `print()`, then moves the cursor to the start of the next line. It comes from the Arduino `Print` class that `Adafruit_GFX` builds on.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text or number | value | What to print. |
display.display(bool _leaveOn = 0)returns voidDoes a full refresh: sends the frame buffer to the panel. If `_leaveOn` is true, the panel supply stays on after the refresh, which saves time on the next update.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | _leaveOn | Keeps the panel supply on after the update when true. Defaults to 0. |
delay(unsigned long ms)returns voidArduino function that pauses the sketch.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
unsigned long | ms | Time to wait in milliseconds. |
display.image.draw(const char *path, int x, int y, bool dither = 1, bool invert = 0)returns boolDraws an image file from the SD card. The format (BMP, JPEG or PNG) is picked from the file.
Returns`true` if the image drew, `false` if it failed (for example a wrong file name or an unsupported format).
Parameters
| Type | Name | Description |
|---|---|---|
const char * | path | File name on the card. |
int | x | Left position in pixels. |
int | y | Top position in pixels. |
bool | dither | Dithers the image when true. Defaults to 1. |
bool | invert | Swaps black and white when true. Defaults to 0. |
SdFile()returns NoneCreates a file object for reading and writing files on the card. `SdFile` is part of the SdFat library bundled with the Inkplate library.
ReturnsConstructor; no return value.
file.open(const char *path, oflag_t oflag = O_RDONLY)returns boolOpens a file on the card.
Returns`true` if the file opened.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | path | File name or path on the card. |
oflag_t | oflag | How to open the file, for example O_RDONLY. Defaults to O_RDONLY. |
display.image.drawBitmapFromSd(SdFile *p, int x, int y, bool dither = 0, bool invert = 0)returns boolDraws a BMP from an already opened `SdFile`.
Returns`true` if the bitmap drew.
Parameters
| Type | Name | Description |
|---|---|---|
SdFile * | p | Pointer to the opened file. |
int | x | Left position in pixels. |
int | y | Top position in pixels. |
bool | dither | Dithers the image when true. Defaults to 0. |
bool | invert | Swaps black and white when true. Defaults to 0. |
display.sdCardSleep()returns voidTurns off the power to the microSD card and sets the SD interface pins to inputs to save energy.
ReturnsNothing.
What to expect
The display shows SD Card OK! Reading image..., then image1.bmp drawn at the top left corner. After 5 seconds it shows image2.bmp, and after another 5 seconds it shows pyramid.jpg drawn 100 pixels from the left edge. Put image1.bmp, image2.bmp and pyramid.jpg in the root of the card. If an image fails to open, the display shows Image open error. If the card does not start, it shows SD Card error!.
The example comments say the BMP files must be uncompressed Windows bitmaps with a color depth of 1, 4, 8 or 24 bits. The example header lists BMP, JPEG and PNG as supported formats, with a maximum size of 1200 x 825 pixels, which is the size of the Inkplate 10 display. Every example in this group asks for a FAT, FAT32 or exFAT card.