Images from a microSD card
What this example shows
Read two JPEG pictures from a microSD card and show them one after the other. The first is drawn by file name with image.draw(), the second through an SdFile object, the SdFat way. At the end the sketch switches off the card's supply to save power.
Before you upload, format the card as FAT and copy picture1.jpg and picture2.jpg from the example folder to the root of the card. The sketch's header comment says pictures can be BMP, JPEG or PNG and must be smaller than 600 × 448 pixels to fit.
Example code
#include "Inkplate.h" // Include Inkplate library to the sketch
Inkplate display; // 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()
{
// Init serial communication
Serial.begin(115200);
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(2); // Set font size to 2
// Init SD card. Display if SD card is init propery or not.
if (display.sdCardInit())
{
Serial.println("SD Card OK! Reading image...");
// 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("picture1.jpg", 0, 0, 1))
{
display.display();
delay(5000);
}
else
{
// If is something failed (wrong filename or wrong bitmap format), write error message on the Serial
// Monitor. 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
Serial.println("Image open error");
}
// Now try to load image using SdFat library class (for more advanced users) and display image on epaper.
display.clearDisplay();
if (file.open("picture2.jpg", O_RDONLY))
{
display.image.drawJpegFromSd(&file, 0, 0);
display.display();
delay(5000);
}
else
{
Serial.println("Image open error");
}
}
else
{
// If SD card init not success, display error on screen
Serial.println("SD Card error!");
}
// Turn off the MOSFET that powers the SD card
display.sdCardSleep();
}
void loop()
{
// Nothing...
}
The comment next to the Inkplate display; line mentions a 3-bit mode; Inkplate 7SPECTRA has only its six-color mode, and the constructor takes no mode.
Functions used
Inkplate()returns NoneCreates the Inkplate object. Inkplate 7SPECTRA has a single six-color mode, so the constructor takes no mode argument.
ReturnsConstructor; no return value.
display.begin()returns voidInitializes the library, the peripherals and the frame buffer. Call it once in setup(); later calls are ignored.
ReturnsNothing.
display.clearDisplay()returns voidFills the frame buffer with white. It does not change the panel.
ReturnsNothing.
display.setTextColor(uint16_t c)returns voidSets the text color. This sketch prints no text on the screen, so the setting has no visible effect here.
ReturnsNothing.
display.setTextSize(uint8_t s)returns voidSets the text magnification. As with the color, this sketch prints no text on the screen.
ReturnsNothing.
display.sdCardInit()returns int16_tSwitches on the microSD card's supply, starts its SPI bus and mounts the card.
Returns1 if the card was initialized, 0 if it failed.
display.image.draw(const char *path, int x, int y, bool dither = 1, bool invert = 0)returns boolOpens an image file on the microSD card by name and draws it into the frame buffer with its top-left corner at (x, y).
Returnstrue if the image was drawn, false if the file could not be opened or decoded.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | path | File name on the card, here "picture1.jpg". |
int | x, y | Top-left corner in pixels. |
bool | dither | Dither the image to the six panel colors. Defaults to 1. |
bool | invert | Invert the colors. Defaults to 0. |
display.image.drawJpegFromSd(SdFile *p, int x, int y, bool dither = 0, bool invert = 0)returns boolDraws a JPEG from a file you already opened with SdFat.
Returnstrue if the image was drawn.
Parameters
| Type | Name | Description |
|---|---|---|
SdFile * | p | Pointer to the open file. |
int | x, y | Top-left corner in pixels. |
bool | dither | Defaults to 0. |
bool | invert | Defaults to 0. |
SdFile()returns NoneSdFat class, bundled with the Inkplate library: a file object on the microSD card. The sketch creates one as `file`.
ReturnsConstructor; no return value.
file.open(path, mode)returns boolSdFat function, bundled with the Inkplate library: opens a file on the card. The sketch passes O_RDONLY to open it for reading.
Returnstrue if the file was opened.
display.display(bool _leaveOn = 0)returns voidSends the whole frame buffer to the panel and refreshes it.
ReturnsNothing.
display.sdCardSleep()returns voidSets the card's SPI pins to inputs and switches off the card's supply, to save power in deep sleep.
ReturnsNothing.
What to expect
With the card in place, the Serial Monitor at 115200 baud shows:
SD Card OK! Reading image...
Then picture1.jpg appears on the screen for 5 seconds, followed by picture2.jpg. A missing or unreadable picture prints Image open error instead; a card that fails to start prints SD Card error!.
🔗Open the complete example
examples/Inkplate7SPECTRA/Advanced/SD/Inkplate7SPECTRA_SD_Pictures