October 2

Keepsake: Programming

With everything connected, it’s time to test the code that will load the pictures on the microSD, and display them on the 2.8″ TFT display. If any issues arise, I’ll be able to trace everything with ease, and can use the Arduino IDE to view the Serial Monitor for errors.

Programming with FTDI

From Wikipedia:

Future Technology Devices International, commonly known by its acronym FTDI, is a Scottish privately held semiconductor device company, specializing in Universal Serial Bus (USB) technology. It develops, manufactures, and supports devices and their related software drivers for converting RS-232 or TTL serial transmissions to USB signals, in order to allow support for legacy devices with modern computers.

Using the FTDI mini-USB to TTL serial converter adapter module, I can program the Arduino Pro Mini microcontroller. I won’t need to solder the header pins to the microcontroller, or use cables, if I don’t want to. Given the pinout on the FTDI, it’s in line with the microcontroller, with the switched RX-TX / TX-RX pins for communication. The header pins from the FTDI can go into the microcontroller’s programming pins, but may need to lean a bit to ensure contact.

 

Preparing BMP Images

The pictures have been adjusted to fit a portrait orientation, cropping sides of landscape images, and closing in on portraits, where necessary. Those images are JPG, however, and will need to be converted to BMP. Additionally, they’ll need to be reduced in size to fit the display size of the TFT display. Looping images through the following command, the pictures were converted to BMP, ready to be loaded into the root directory of the FAT32 microSD card:

for f in *.jpg ; do convert -resize 240x320 -depth 24 -compress none -extent 240x320 -gravity center -background black "${f}" "${f/%jpg/bmp}" ; done

The convert command (from imagemagick) will ensure the following:

  • resize 240×320 – resize the image to 240×320 (as best as possible)
  • depth 24 – set a 24-bit depth of the image
  • compress none – do not compress the image
  • extent 240×320 – set the canvas size to 240×320
  • gravity center – place the image in the center of the 240×320 canvas
  • background black – where necessary, use black as filler of the background

 

Sketch in Arduino IDE

Slightly modifying the Listfiles example sketch, the file list is looped, to then iterate through the bmpDraw() function.

#include <Adafruit_GFX.h> 
#include "Adafruit_ILI9341.h" 
#include <SD.h> 
#include <SPI.h> 
#include <Wire.h> 

File root; 

#define SD_CS 4 
#define TFT_DC 9 
#define TFT_CS 10 
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); 

// TFT Display Code 
#define BUFFPIXEL 20 


void setup() { 
 Serial.begin(9600); 

 // setup display 
 tft.begin(); 
 tft.fillScreen(ILI9341_BLUE); 

 // setup the microSD card with bitmap pictures 
 Serial.print("Initializing SD card..."); 
 if (!SD.begin(SD_CS)) { 
   Serial.println("failed!"); 
 } 
 else { 
   Serial.println("OK!"); 
 } 

 // once microSD is setup, load the root directory 
 root = SD.open("/"); 
} 


void loop() { 
 // display the bitmap image on the TFT display 
 showImage(root); 
} 


// Image File Listing 
void showImage(File dir) { 
 while (true) { 
   File entry = dir.openNextFile(); 
   if (!entry) { 
     dir.rewindDirectory(); 
     break; 
   } 
   bmpDraw(entry.name(), 0, 0); 
   delay(4000); 
   entry.close(); 
 } 
}

 

The bmpDraw() function is from the example sketch in the Arduino IDE. As each filename is obtained from the directory listing, it is processed through bmpDraw() to show the image on the TFT display. Once drawn, it counts 4000ms (4 seconds) before moving on to the next image. To restart cycling through the listing again, dir.rewindDirectory() resets to the first file in the directory.

Tags: ,
Copyright 2025. All rights reserved.

Posted 2019-10-02 by Draik in category "Arduino", "Personal Keepsake", "Project