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

Connections
Connections
Connections
Connections

LCD + SD
LCD + SD
Comments
Only logged in users can leave comments