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