Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. #include <Adafruit_GFX.h> // Core graphics library
  2. #include "Adafruit_ILI9341.h" // Hardware-specific library
  3. #include <SPI.h>
  4. #include <SD.h>
  5.  
  6. // Pins
  7. #define TFT_DC 9
  8. #define TFT_CS 4
  9. #define TFT_RST 8
  10.  
  11. // Rotation
  12. #define TFT_ROTATION 2 // 180 deg
  13.  
  14. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  15.  
  16. #define SD_CS 5
  17.  
  18. void setup(void) {
  19. Serial.begin(9600);
  20.  
  21. tft.begin();
  22. tft.setRotation(TFT_ROTATION);
  23. tft.fillScreen(ILI9341_BLUE);
  24.  
  25. Serial.print("Initializing SD card...");
  26. if (!SD.begin(SD_CS)) {
  27. Serial.println("failed!");
  28. }
  29. Serial.println("OK!");
  30.  
  31. bmpDraw("08.bmp", 0, 0);
  32. }
  33.  
  34. void loop() {
  35. }
  36.  
  37. // This function opens a Windows Bitmap (BMP) file and
  38. // displays it at the given coordinates. It's sped up
  39. // by reading many pixels worth of data at a time
  40. // (rather than pixel by pixel). Increasing the buffer
  41. // size takes more of the Arduino's precious RAM but
  42. // makes loading a little faster. 20 pixels seems a
  43. // good balance.
  44.  
  45. #define BUFFPIXEL 20
  46.  
  47. void bmpDraw(char *filename, uint8_t x, uint16_t y) {
  48.  
  49. File bmpFile;
  50. int bmpWidth, bmpHeight; // W+H in pixels
  51. uint8_t bmpDepth; // Bit depth (currently must be 24)
  52. uint32_t bmpImageoffset; // Start of image data in file
  53. uint32_t rowSize; // Not always = bmpWidth; may have padding
  54. uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  55. uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  56. boolean goodBmp = false; // Set to true on valid header parse
  57. boolean flip = true; // BMP is stored bottom-to-top
  58. int w, h, row, col;
  59. uint8_t r, g, b;
  60. uint32_t pos = 0, startTime = millis();
  61.  
  62. if((x >= tft.width()) || (y >= tft.height())) return;
  63.  
  64. Serial.println();
  65. Serial.print(F("Loading image '"));
  66. Serial.print(filename);
  67. Serial.println('\'');
  68.  
  69. // Open requested file on SD card
  70. if ((bmpFile = SD.open(filename)) == NULL) {
  71. Serial.print(F("File not found"));
  72. return;
  73. }
  74.  
  75. // Parse BMP header
  76. if(read16(bmpFile) == 0x4D42) { // BMP signature
  77. Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
  78. (void)read32(bmpFile); // Read & ignore creator bytes
  79. bmpImageoffset = read32(bmpFile); // Start of image data
  80. Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
  81. // Read DIB header
  82. Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
  83. bmpWidth = read32(bmpFile);
  84. bmpHeight = read32(bmpFile);
  85. if(read16(bmpFile) == 1) { // # planes -- must be '1'
  86. bmpDepth = read16(bmpFile); // bits per pixel
  87. Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
  88. if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  89.  
  90. goodBmp = true; // Supported BMP format -- proceed!
  91. Serial.print(F("Image size: "));
  92. Serial.print(bmpWidth);
  93. Serial.print('x');
  94. Serial.println(bmpHeight);
  95.  
  96. // BMP rows are padded (if needed) to 4-byte boundary
  97. rowSize = (bmpWidth * 3 + 3) & ~3;
  98.  
  99. // If bmpHeight is negative, image is in top-down order.
  100. // This is not canon but has been observed in the wild.
  101. if(bmpHeight < 0) {
  102. bmpHeight = -bmpHeight;
  103. flip = false;
  104. }
  105.  
  106. // Crop area to be loaded
  107. w = bmpWidth;
  108. h = bmpHeight;
  109. if((x+w-1) >= tft.width()) w = tft.width() - x;
  110. if((y+h-1) >= tft.height()) h = tft.height() - y;
  111.  
  112. // Set TFT address window to clipped image bounds
  113. tft.setAddrWindow(x, y, x+w-1, y+h-1);
  114.  
  115. for (row=0; row<h; row++) { // For each scanline...
  116.  
  117. // Seek to start of scan line. It might seem labor-
  118. // intensive to be doing this on every line, but this
  119. // method covers a lot of gritty details like cropping
  120. // and scanline padding. Also, the seek only takes
  121. // place if the file position actually needs to change
  122. // (avoids a lot of cluster math in SD library).
  123. if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
  124. pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  125. else // Bitmap is stored top-to-bottom
  126. pos = bmpImageoffset + row * rowSize;
  127. if(bmpFile.position() != pos) { // Need seek?
  128. bmpFile.seek(pos);
  129. buffidx = sizeof(sdbuffer); // Force buffer reload
  130. }
  131.  
  132. for (col=0; col<w; col++) { // For each pixel...
  133. // Time to read more pixel data?
  134. if (buffidx >= sizeof(sdbuffer)) { // Indeed
  135. bmpFile.read(sdbuffer, sizeof(sdbuffer));
  136. buffidx = 0; // Set index to beginning
  137. }
  138.  
  139. // Convert pixel from BMP to TFT format, push to display
  140. b = sdbuffer[buffidx++];
  141. g = sdbuffer[buffidx++];
  142. r = sdbuffer[buffidx++];
  143. tft.pushColor(tft.color565(r,g,b));
  144. } // end pixel
  145. } // end scanline
  146. Serial.print(F("Loaded in "));
  147. Serial.print(millis() - startTime);
  148. Serial.println(" ms");
  149. } // end goodBmp
  150. }
  151. }
  152.  
  153. bmpFile.close();
  154. if(!goodBmp) Serial.println(F("BMP format not recognized."));
  155. }
  156.  
  157. // These read 16- and 32-bit types from the SD card file.
  158. // BMP data is stored little-endian, Arduino is little-endian too.
  159. // May need to reverse subscript order if porting elsewhere.
  160.  
  161. uint16_t read16(File &f) {
  162. uint16_t result;
  163. ((uint8_t *)&result)[0] = f.read(); // LSB
  164. ((uint8_t *)&result)[1] = f.read(); // MSB
  165. return result;
  166. }
  167.  
  168. uint32_t read32(File &f) {
  169. uint32_t result;
  170. ((uint8_t *)&result)[0] = f.read(); // LSB
  171. ((uint8_t *)&result)[1] = f.read();
  172. ((uint8_t *)&result)[2] = f.read();
  173. ((uint8_t *)&result)[3] = f.read(); // MSB
  174. return result;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement