Thursday, May 12, 2022

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

This is an extension for the ST7735 display driver library made for displaying Bitmap images stored on an 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, include:

#include "DIS_ST7735_Flash.h"

which will include the following files

  • sd.h - low level sd card interface
  • fat.h - the FAT16/32 file system driver
  • DIS_ST7735.h - TFT display driver
     

    Displaying the Bitmap images on the display

    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.

    DIS_ST7735_drawImage()
     
    DIR* dir_p

    pointer to an open directory containing the images

    char *file_name

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

    uint16_t idx

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

    int16_t x, y

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

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

    • 20 - IMG_OK
    • 21 - IMG_UNSUPPORTED_FORMAT
    • 22 - IMG_NOT_FOUND
    • 23 - IMG_DEVICE_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()

    DIR* dir_p

    pointer to an open directory containing the images

    char *file_name

    name of the file including file extension

    uint16_t idx

    if the file name is null like this "" then this index will be used instead, which represents the order in which the file has been written to the storage device. Set it to 0 if the file name is known.

    uint16_t *width_buf, uint16_t *height_buf

    pointer to variables where to store the image width and height

    Usage:

        uint16_t width = 0, height = 0;
    
        DIR imgDir;
        DIS_ST7735_imageDimensions(&imgDir, "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){
    	DIR imgDir;	// active directory object
    	
    	// Initialize the display
    	DIS_ST7735_displayInit();
    	
    	// Clear the display
    	DIS_ST7735_fillScreen(ST77XX_WHITE);
    	
    	uint8_t return_code;
    	
    	// Mount the memory card
    	return_code = FAT_mountVolume();
    	
    	if(return_code == FR_OK){
    		// Open the folder containing the pictures if no error occurred
    		return_code = FAT_openDir(&imgDir, "Pictures");
    		
    		// Draw the image at 0,0 position (top left corner)
    		DIS_ST7735_drawImage(&imgDir, "img1.bmp", 0, 0, 0);
    		
    		_delay_ms(5000);
    		
    		// Draw another image at 0,10 position (top left corner)
    		DIS_ST7735_drawImage(&imgDir, "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){
    	DIR imgDir;	// active directory object
    	
    	// Initialize the display
    	DIS_ST7735_displayInit();
    	
    	// Clear the display
    	DIS_ST7735_fillScreen(ST77XX_WHITE);
    	
    	uint8_t return_code;
    	
    	// Mount the memory card
    	return_code = FAT_mountVolume();
    	
    	if(return_code == 0){
    		// Open the folder containing the pictures if no error occurred
    		return_code = FAT_openDir(&imgDir, "Pictures");
    		
    		// Display all Bitmap images
    		// i = 1 because index starts from 1
    		for(uint8_t i = 1; i < FAT_dirCountItems(&imgDir) + 1; i++){
    			// Clear display
    			DIS_ST7735_fillScreen(ST77XX_WHITE);
    
    			// Draw the image at 0,0 position (top left corner)
    			DIS_ST7735_drawImage(&imgDir, "", i, 0, 0);
    			
    			// Wait before showing the next image
    			_delay_ms(5000);
    		}
    	}
    	
    
    	while(1){
    	
    	}
    }
    


    Download

    Changelog and license can be found at the beginning of the files.
    v1.2 DIS_ST7735_Flash
    DIS_ST7735
    Folders can be downloaded as zip files. Apart from ST7735 library, there are also libraries for reading the SD card.

    BMP Pictures 128x24-bits.7z Bitmap image samples used in the video.
    External Links

    Bits to Bitmaps - A simple walkthrough of BMP Image Format

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

    https://formats.kaitai.io/bmp
    Changelog
    v1.2 06-02-2024:
    - Included support for newer UPDI AVR devices.

    No comments:

    Post a Comment