Thursday, May 12, 2022

Displaying Bitmap images on ST7735 display from a memory card using an AVR microcontroller

This library is an extension for the ST7735 display driver library and can be used for displaying Bitmap images stored on a SD memory card on the ST7735 TFT display using an AVR microcontroller. The Bitmaps can be of 16-bit or 24-bit colors. Since the ST7735 display driver only accepts 16-bit colors, the 24-bit images will be converted to 16-bits.

Here is a short demo video:


The images must be resized first to fit the size of your display. A free and popular software for this task is Irfanview. Open the image using Irfanview then press CTRL+R, specify the new image size then save it as a .bmp and place the images on the memory card in a folder of your choice.

To use this library you will need 3 more files placed in the same folder. First include this library:

#include "DIS_ST7735_Flash.h"

that depends on the "DIS_ST7735.h" which is used to interface with the ST7735 display and "fat16.h" that drives the SD memory card. "fat16.h" includes the "sd_mmc.h".

So the order of dependency is this:

  • sd_mmc.h - low level memory card interface
  • fat16.h - the FAT file system driver
  • DIS_ST7735.h - display driver
  • DIS_ST7735_Flash.h - Bitmap decoder

Notice: the "fat16" driver is still in development so this library cannot be used at the moment. The card driver will be released in a few weeks hopefully.

Displaying the Bitmap images on the display

DIS_ST7735_drawImage(char *file_name, uint16_t idx, int16_t x, int16_t y);

The images can be accessed by name or by index. The index is the order number in which they appear on the SD card and can change if a file is deleted and another added.If the image is larger than the screen then it will be clipped.

file_name - the filename including the extension. If omitted like this "", the index will be used

idx - the file index from 0 to the number of items inside the opened folder. If the file_name is set the index is ignored

x and y - x and y coordinates from which the image will be printed in the range: 0 - display width/height - 1

The function can return the following codes found in the IMG_RESULT enum:

  • 0 - IMG_OK
  • 1 - Reserved
  • 2 - IMG_UNSUPPORTED_FORMAT
  • 3 - IMG_NOT_FOUND
  • 4 - IMG_LOW_ERR (low level error with card interface)

It is recommended the use of the error names instead of numbers because these can change if the enum is modified.

Getting the image dimensions

void DIS_ST7735_imageDimensions(char *file_name, uint16_t idx, uint16_t *width, uint16_t *height);

width and height - pointer to variables where to store the image width and height

Usage: 

uint16_t width = 0, height = 0; DIS_ST7735_imageDimensions("img.bmp", 0, &width, &height);

Code examples

Display images in a folder by their name. If the folder is the root folder then use "/" as the folder name.

#include "DIS_ST7735_Flash.h"


int main(void){
	
	// Initialize the display
	DIS_ST7735_displayInit();
	
	// Clear the display
	DIS_ST7735_fillScreen(ST77XX_WHITE);
	
	uint8_t return_code;
	
	// Mount the memory card
	return_code = FAT16_Mount();
	
	if(return_code == 0){
		// Open the folder containing the pictures if no error occurred
		return_code = FAT16_OpenDir("Pictures", 0);
		
		// Draw the image at 0,0 position (top left corner)
		DIS_ST7735_drawImage("img1.bmp", 0, 0, 0);
		
		_delay_ms(5000);
		
		// Draw the image at 0,10 position (top left corner)
		DIS_ST7735_drawImage("img2.bmp", 0, 0, 10);
	}
	

	while(1){
	
	}
}


Display all .bmp images in a folder by index. If there are other folders or other file formats they will be skipped.

#include "DIS_ST7735_Flash.h"


int main(void){
	
	// Initialize the display
	DIS_ST7735_displayInit();
	
	// Clear the display
	DIS_ST7735_fillScreen(ST77XX_WHITE);
	
	uint8_t return_code;
	
	// Mount the memory card
	return_code = FAT16_Mount();
	
	if(return_code == 0){
		// Open the folder containing the pictures if no error occurred
		return_code = FAT16_OpenDir("Pictures", 0);
		
		// Display all Bitmap images
		for(uint16_t i = 0; i < FAT16_getDirItemsCount(); i++){
			
			// Get file proprieties
			FAT16_ListFile(i);
			
			// Check if it is a file to skip folders
			if(!FAT16_attrIsFile()) continue;
			
			// Clear display
			DIS_ST7735_fillScreen(ST77XX_WHITE);

			// Draw the image at 0,0 position (top left corner)
			DIS_ST7735_drawImage("", i, 0, 0);	
			
			// Wait before showing the next image
			_delay_ms(5000);
		}
	}
	

	while(1){
	
	}
}


Download

Version 1.0

DIS_ST7735_Flash.h

Bitmap image samples used in the video

BMP Pictures 128x24-bits.7z

Other external resources

Bits to Bitmaps - A simple walkthrough of BMP Image Format

https://en.wikipedia.org/wiki/BMP_file_format

https://formats.kaitai.io/bmp/


No comments:

Post a Comment