Advertisement
Guest User

Arduino Code Display

a guest
Sep 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. // BMP-loading example specifically for the TFTLCD breakout board.
  2. // If using the Arduino shield, use the tftbmp_shield.pde sketch instead!
  3. // If using an Arduino Mega make sure to use its hardware SPI pins, OR make
  4. // sure the SD library is configured for 'soft' SPI in the file Sd2Card.h.
  5.  
  6. #include <Adafruit_GFX.h> // Core graphics library
  7. #include <TftSpfd5408.h> // Hardware-specific library
  8. #include <SD.h>
  9. #include <SPI.h>
  10.  
  11. // The control pins for the LCD can be assigned to any digital or
  12. // analog pins...but we'll use the analog pins as this allows us to
  13. // double up the pins with the touch screen (see the TFT paint example).
  14. #define LCD_CS A3 // Chip Select goes to Analog 3
  15. #define LCD_CD A2 // Command/Data goes to Analog 2
  16. #define LCD_WR A1 // LCD Write goes to Analog 1
  17. #define LCD_RD A0 // LCD Read goes to Analog 0
  18.  
  19. // When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
  20. // For the Arduino Uno, Duemilanove, Diecimila, etc.:
  21. // D0 connects to digital pin 8 (Notice these are
  22. // D1 connects to digital pin 9 NOT in order!)
  23. // D2 connects to digital pin 2
  24. // D3 connects to digital pin 3
  25. // D4 connects to digital pin 4
  26. // D5 connects to digital pin 5
  27. // D6 connects to digital pin 6
  28. // D7 connects to digital pin 7
  29. // For the Arduino Mega, use digital pins 22 through 29
  30. // (on the 2-row header at the end of the board).
  31.  
  32. // For Arduino Uno/Duemilanove, etc
  33. // connect the SD card with DI going to pin 11, DO going to pin 12 and SCK going to pin 13 (standard)
  34. // Then pin 10 goes to CS (or whatever you have set up)
  35. #define SD_CS 10 // Set the chip select line to whatever you use (10 doesnt conflict with the library)
  36.  
  37. // In the SD card, place 24 bit color BMP files (be sure they are 24-bit!)
  38. // There are examples in the sketch folder
  39.  
  40. // our TFT wiring
  41. TftSpfd5408 tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, A4);
  42.  
  43. void setup()
  44. {
  45. Serial.begin(9600);
  46.  
  47. tft.reset();
  48.  
  49. tft.begin(0x9341);
  50.  
  51. Serial.print(F("Initializing SD card..."));
  52. if (!SD.begin(SD_CS)) {
  53. Serial.println(F("failed!"));
  54. return;
  55. }
  56. Serial.println(F("OK!"));
  57.  
  58. bmpDraw("alien.bmp", 0, 0);
  59. delay(1000);
  60. }
  61.  
  62. void loop()
  63. {
  64. for(int i = 0; i<4; i++) {
  65. tft.setRotation(0);
  66. tft.fillScreen(0);
  67. for(int j=0; j <= 200; j += 50) {
  68. bmpDraw("alien.bmp", 0, 0);
  69. }
  70.  
  71. }
  72. }
  73.  
  74. // This function opens a Windows Bitmap (BMP) file and
  75. // displays it at the given coordinates. It's sped up
  76. // by reading many pixels worth of data at a time
  77. // (rather than pixel by pixel). Increasing the buffer
  78. // size takes more of the Arduino's precious RAM but
  79. // makes loading a little faster. 20 pixels seems a
  80. // good balance.
  81.  
  82. #define BUFFPIXEL 20
  83.  
  84. void bmpDraw(char *filename, int x, int y) {
  85.  
  86. File bmpFile;
  87. int bmpWidth, bmpHeight; // W+H in pixels
  88. uint8_t bmpDepth; // Bit depth (currently must be 24)
  89. uint32_t bmpImageoffset; // Start of image data in file
  90. uint32_t rowSize; // Not always = bmpWidth; may have padding
  91. uint8_t sdbuffer[3*BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
  92. uint16_t lcdbuffer[BUFFPIXEL]; // pixel out buffer (16-bit per pixel)
  93. uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  94. boolean goodBmp = false; // Set to true on valid header parse
  95. boolean flip = true; // BMP is stored bottom-to-top
  96. int w, h, row, col;
  97. uint8_t r, g, b;
  98. uint32_t pos = 0, startTime = millis();
  99. uint8_t lcdidx = 0;
  100. boolean first = true;
  101.  
  102. if((x >= tft.width()) || (y >= tft.height())) return;
  103.  
  104. Serial.println();
  105. Serial.print(F("Loading image '"));
  106. Serial.print(filename);
  107. Serial.println('\'');
  108. // Open requested file on SD card
  109. if ((bmpFile = SD.open(filename)) == NULL) {
  110. Serial.println(F("File not found"));
  111. return;
  112. }
  113.  
  114. // Parse BMP header
  115. if(read16(bmpFile) == 0x4D42) { // BMP signature
  116. Serial.println(F("File size: ")); Serial.println(read32(bmpFile));
  117. (void)read32(bmpFile); // Read & ignore creator bytes
  118. bmpImageoffset = read32(bmpFile); // Start of image data
  119. Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
  120. // Read DIB header
  121. Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
  122. bmpWidth = read32(bmpFile);
  123. bmpHeight = read32(bmpFile);
  124. if(read16(bmpFile) == 1) { // # planes -- must be '1'
  125. bmpDepth = read16(bmpFile); // bits per pixel
  126. Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
  127. if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  128.  
  129. goodBmp = true; // Supported BMP format -- proceed!
  130. Serial.print(F("Image size: "));
  131. Serial.print(bmpWidth);
  132. Serial.print('x');
  133. Serial.println(bmpHeight);
  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) >= tft.width()) w = tft.width() - x;
  149. if((y+h-1) >= tft.height()) h = tft.height() - y;
  150.  
  151. // Set TFT address window to clipped image bounds
  152. tft.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. if(lcdidx > 0) {
  175. tft.pushColors(lcdbuffer, lcdidx, first);
  176. lcdidx = 0;
  177. first = false;
  178. }
  179. bmpFile.read(sdbuffer, sizeof(sdbuffer));
  180. buffidx = 0; // Set index to beginning
  181. }
  182.  
  183. // Convert pixel from BMP to TFT format
  184. b = sdbuffer[buffidx++];
  185. g = sdbuffer[buffidx++];
  186. r = sdbuffer[buffidx++];
  187. lcdbuffer[lcdidx++] = tft.color565(r,g,b);
  188. } // end pixel
  189. } // end scanline
  190. // Write any remaining data to LCD
  191. if(lcdidx > 0) {
  192. tft.pushColors(lcdbuffer, lcdidx, first);
  193. }
  194. Serial.print(F("Loaded in "));
  195. Serial.print(millis() - startTime);
  196. Serial.println(" ms");
  197. } // end goodBmp
  198. }
  199. }
  200.  
  201. bmpFile.close();
  202. if(!goodBmp) Serial.println(F("BMP format not recognized."));
  203. }
  204.  
  205. // These read 16- and 32-bit types from the SD card file.
  206. // BMP data is stored little-endian, Arduino is little-endian too.
  207. // May need to reverse subscript order if porting elsewhere.
  208.  
  209. uint16_t read16(File f) {
  210. uint16_t result;
  211. ((uint8_t *)&result)[0] = f.read(); // LSB
  212. ((uint8_t *)&result)[1] = f.read(); // MSB
  213. return result;
  214. }
  215.  
  216. uint32_t read32(File f) {
  217. uint32_t result;
  218. ((uint8_t *)&result)[0] = f.read(); // LSB
  219. ((uint8_t *)&result)[1] = f.read();
  220. ((uint8_t *)&result)[2] = f.read();
  221. ((uint8_t *)&result)[3] = f.read(); // MSB
  222. return result;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement