Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

In previous articles we dealt with the TFT display and the resistive touch panel; to master all the capabilities the module offers, let's try connecting the memory card, namely writing an image to an SD card and displaying it. Jumping ahead a bit, I'll say that the binary of the image came out at 150K, while the Atmega16 has 16K of flash and 1K of RAM, so no matter how hard we try, we won't be able to load the image into this microcontroller.

The following pins for working with the memory card are brought out to the header:
SD_DO – data read/write.
SD_SCK – clock line.
SD_DIN – data write/read.
SD_CS – activates the SD card; the flash memory must be deactivated at the same time.
The pin names make it clear that the card is controlled via SPI (positive clock pulse, latching on the rising edge, shifting on the falling edge).
The board also has a footprint for soldering a 16Mb memory chip.
F_CS – activates the flash memory; the SD card must be deactivated at the same time.
 
To display an image on a TFT display, it must be converted into an array in which each value encodes the color of the corresponding pixel in B5G6R5 format.
For the conversion we'll use a program called lcd-image-converter, which can be downloaded here.
So let's start: after installing the program, open the image you want to convert (File→Open), then go to the Options menu and select Conversion.
In the Preset field, set the R5G6B5 format and open the Prepare tab. Image type — color, main scan direction — top to bottom, line scan direction — forward.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Open the Matrix tab, and since our format is B5G6R5, use shifts to swap R5 and B5.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Next, open the Image tab, delete everything in the prefix and delimiter fields, select 16 bit in the block size drop-down list, and select Big-Endian byte order. 
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

In fact, the byte order of 8-bit AVR is Little-Endian, but it worked this way; I'll figure out why later. If you select Little-Endian, the image comes out as if it were a negative.
The byte order determines how the data will be stored in memory, as shown in the picture below.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Open the Font tab and set the encoding.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Now if you click Preview, you'll see what our image is converted into.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)
Click "Save As" and enter a new preset name.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Then, to save the settings, click "OK" in the lower right corner and select Convert in the File tab; the program will generate a file with the .c extension that contains the array.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

If we took a microcontroller with more memory and loaded this array into it, the compiler would turn it into a binary, but since we'll be transferring the data from the card in chunks, we have to do this work instead of the compiler.
Create a file with the txt extension, copy the contents of the array into it and remove all the extra spaces; you can use Gvim for this, which can be downloaded here. Open the file and see that each line starts with 4 spaces, which we need to remove; press colon (shift+;) to switch to command mode and type
:%s/4_spaces //g
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Let's take a closer look at what this command does. The s (substitute) command performs search and replace; its general form is as follows.
:{range}s/{what to replace}/{replace with}/{options}
Now let's break down what we wrote.
% — execute the command on all lines, "from the first to the last line"
g — from the word global, to replace every occurrence in all lines
So the command :%s/4_spaces//g can be read as follows: in every line, from beginning to end, replace 4 spaces with nothing. You can remove all spaces, without guessing whether there are 4 or 5, with a special command
:%s/ $*//g
but I found it only toward the end of writing this article.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Use the wqa command to save the file and close Gvim.
Now the file is ready to be converted into a binary. We'll also convert it to binary with Gvim: open a command prompt, go to where Gvim is installed, find the xxd.exe file in its subfolders and run the following command from there
xxd.exe -r -ps (path1)picture.txt (path2)picture.bin
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Now we have a binary that needs to be written to an SD card, formatted as FAT16 beforehand. 

Not long ago lcd-image-converter gained the ability to convert hex to bin by itself, which made the task much easier. To do this, in addition to the settings we made, you need to create a text file with the code below.

$(start_block_images_table)
le
uint16 $(out_image_width)
uint16 $(out_image_height)
uint16 $(out_blocks_count)
uint$(img_data_block_size) $(out_image_data)
$(end_block_images_table)

And select this file in the Templates tab.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

You also need to change Big-Endian to Little-Endian, write 0x in the prefix field and specify a space as the delimiter.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Then everything as usual, File→Convert; the generated file will look like this.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example (Converting an Image to an Array)

Open a command prompt, go to the folder where lcd-image-converter.exe is located and run the following command from there
lcd-image-converter --mode=hex2bin --input=(path1)file.c --output=(path2)file.bin
As a result we get a binary; I should say that this method is much more convenient, since the whole conversion is done with a single program.
In the next article we'll display this image. The original image and the resulting binary can be downloaded in the archive.picture.rar.