Components and supplies
Arduino Mega 2560
Jumper wires (generic)
ILI9341 5V/3.3V 2.8" TFT SPI Serial Port LCD Touch Panel Display Module
Project description
Code
ILI9341 TFT SD CARD BMP
arduino
1 2 3//LCD 4#include <ILI9341_kbv.h> // This library has been modified 5ILI9341_kbv my_lcd; 6#define vali0 0 7 8 9//SD CARD 10#include <SD.h> 11#define TFT_SD 47 //SC_CS 12Sd2Card card; 13SdVolume volume; 14SdFile root; 15 16 17 18//TFT Touch Library 19#include <LCDWIKI_TOUCH.h> 20LCDWIKI_TOUCH my_tft(44,52,50,51,46); //tcs,tclk (sharing with LCD),tdout (MISO) ,tdin (MOSI) ,tirq 21 22 23 24//Folder on the SD card to keep the pictures 25String FOLDERROOT = ""; // I have no folder because all the pictures are on the root of the SD 26uint8_t max_img = 40; 27uint8_t image_counter = 0; 28unsigned long auxtimer, colortimer; 29 30 31 32 33 34void setup() 35 { 36 37 my_lcd.begin(); 38 my_lcd.setRotation(3);// Landscape 39 my_lcd.fillScreen(TFT_PINK);// set color iitial screen 40 SD.begin(TFT_SD); 41 my_tft.TP_Init(1,240,320); //Define the values for my_lcd ( rotation,With,Height) 42 } 43 44 45// Makes an Array out of a BMP 46void bmp (String filename){ 47 int len = 30; 48 char pepito[len]; 49 filename.toCharArray(pepito, len); 50 bmpDraw(pepito , 0, 0);} 51 52 53void loop(){ 54 55 my_tft.TP_Scan(0); 56 if (my_tft.TP_Get_State()&TP_PRES_DOWN) 57 { 58 bmp("roger.bmp"); 59 delay(1000); 60 my_lcd.fillScreen(TFT_PINK);// set color iitial screen 61 } 62 } 63 64 65 66 67 68uint16_t _read16(File f) { 69 uint16_t result; 70 ((uint8_t *)&result)[0] = f.read(); // LSB 71 ((uint8_t *)&result)[1] = f.read(); // MSB 72 return result; 73} 74 75uint32_t _read32(File f) { 76 uint32_t result; 77 ((uint8_t *)&result)[0] = f.read(); // LSB 78 ((uint8_t *)&result)[1] = f.read(); 79 ((uint8_t *)&result)[2] = f.read(); 80 ((uint8_t *)&result)[3] = f.read(); // MSB 81 return result; 82} 83 84#define BUFFPIXEL 20 85void bmpDraw(char *fileName, int x, int y){ 86 87 File bmpFile; 88 int bmpWidth, bmpHeight; // W+H in pixels 89 uint8_t bmpDepth; // Bit depth (currently must be 24) 90 uint32_t bmpImageoffset; // Start of image data in file 91 uint32_t rowSize; // Not always = bmpWidth; may have padding 92 uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel) 93 94 uint16_t lcdbuffer[BUFFPIXEL]; // pixel out buffer (16-bit per pixel) 95 uint8_t lcdidx = 0; 96 boolean first = true; 97 98 uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer 99 boolean goodBmp = false; // Set to true on valid header parse 100 boolean flip = true; // BMP is stored bottom-to-top 101 int w, h, row, col; 102 uint8_t r, g, b; 103 uint32_t pos = 0, startTime = millis(); 104 105 if((x >= my_lcd.width()) || (y >= my_lcd.height())) return; 106 107 // Open requested file on SD card 108 if ((bmpFile = SD.open(fileName)) == NULL) { 109 Serial.println("File Found??" ); 110 Serial.println( fileName); 111 return; 112 }else{ 113 Serial.println("File Found" ); 114 } 115 116 117 if(_read16(bmpFile) == 0x4D42) { // BMP signature 118 119 (void)_read32(bmpFile); // Read & ignore creator bytes 120 Serial.print("File size: ");Serial.println( _read32(bmpFile) ); 121 bmpImageoffset = _read32(bmpFile); // Start of image data 122 Serial.print(" Header size: ");Serial.println( _read32(bmpFile) ); 123 124 // Read DIB header 125 126 bmpWidth = _read32(bmpFile); 127 bmpHeight = _read32(bmpFile); 128 if(_read16(bmpFile) == 1) { // # planes -- must be '1' 129 bmpDepth = _read16(bmpFile); // bits per pixel 130 131 if((bmpDepth == 24) && (_read32(bmpFile) == 0)) { // 0 = uncompressed 132 133 goodBmp = true; // Supported BMP format -- proceed! 134 135 // BMP rows are padded (if needed) to 4-byte boundary 136 rowSize = (bmpWidth * 3 + 3) & ~3; 137 138 // If bmpHeight is negative, image is in top-down order. 139 // This is not canon but has been observed in the wild. 140 if(bmpHeight < 0) { 141 bmpHeight = -bmpHeight; 142 flip = false; 143 } 144 145 // Crop area to be loaded 146 w = bmpWidth; 147 h = bmpHeight; 148 if((x+w-1) >= my_lcd.width()) w = my_lcd.width() - x; 149 if((y+h-1) >= my_lcd.height()) h = my_lcd.height() - y; 150 151 // Set TFT address window to clipped image bounds 152 my_lcd.setAddrWindow(x, y, x+w-1, y+h-1); 153 154 for (row=0; row<h; row++) { // For each scanline... 155 // Seek to start of scan line. It might seem labor- 156 // intensive to be doing this on every line, but this 157 // method covers a lot of gritty details like cropping 158 // and scanline padding. Also, the seek only takes 159 // place if the file position actually needs to change 160 // (avoids a lot of cluster math in SD library). 161 if(flip) // Bitmap is stored bottom-to-top order (normal BMP) 162 pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize; 163 else // Bitmap is stored top-to-bottom 164 pos = bmpImageoffset + row * rowSize; 165 if(bmpFile.position() != pos) { // Need seek? 166 bmpFile.seek(pos); 167 buffidx = sizeof(sdbuffer); // Force buffer reload 168 } 169 170 for (col=0; col<w; col++) { // For each column... 171 // Time to read more pixel data? 172 if (buffidx >= sizeof(sdbuffer)) { // Indeed 173 // Push LCD buffer to the display first 174 175 if(lcdidx > 0) { 176 my_lcd.pushColors(lcdbuffer, lcdidx, first); 177 lcdidx = 0; 178 first = false; 179 } 180 181 bmpFile.read(sdbuffer, sizeof(sdbuffer)); 182 buffidx = 0; // Set index to beginning 183 } 184 185 // Convert pixel from BMP to TFT format 186 b = sdbuffer[buffidx++]; 187 g = sdbuffer[buffidx++]; 188 r = sdbuffer[buffidx++]; 189 190 lcdbuffer[lcdidx++] = my_lcd.color565(r,g,b); 191 } // end pixel 192 } // end scanline 193 194 if(lcdidx > 0) { 195 my_lcd.pushColors(lcdbuffer, lcdidx, first); 196 } 197 Serial.print("Loaded in: ");Serial.println( millis() - startTime); 198 } // end goodBmp 199 } 200 } 201 202 bmpFile.close(); 203 if(!goodBmp) Serial.println("BMP format not recognized."); 204 } 205 206
Downloadable files
connections ILI 9341
connections ILI 9341
connections ILI9341
connections ILI9341
Special jumper wires ;)
I welded some to make it easy to have 3 going to the same pin
Special jumper wires ;)
connections ILI9341
connections ILI9341
connections ILI 9341
connections ILI 9341
Special jumper wires ;)
I welded some to make it easy to have 3 going to the same pin
Special jumper wires ;)
Comments
Only logged in users can leave comments
roger_marin
0 Followers
•10 Projects
0
1
dimitri_lww
a year ago
With arduino uno will it be possible to do this.