spitftbitmap.ino 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /***************************************************
  2. This is our Bitmap drawing example for the Adafruit ILI9341 Breakout and Shield
  3. ----> http://www.adafruit.com/products/1651
  4. Check out the links above for our tutorials and wiring diagrams
  5. These displays use SPI to communicate, 4 or 5 pins are required to
  6. interface (RST is optional)
  7. Adafruit invests time and resources providing this open source code,
  8. please support Adafruit and open-source hardware by purchasing
  9. products from Adafruit!
  10. Written by Limor Fried/Ladyada for Adafruit Industries.
  11. MIT license, all text above must be included in any redistribution
  12. ****************************************************/
  13. #include <Adafruit_GFX.h> // Core graphics library
  14. #include "Adafruit_ILI9341.h" // Hardware-specific library
  15. #include <SPI.h>
  16. #include <SD.h>
  17. // TFT display and SD card will share the hardware SPI interface.
  18. // Hardware SPI pins are specific to the Arduino board type and
  19. // cannot be remapped to alternate pins. For Arduino Uno,
  20. // Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
  21. #define TFT_DC 9
  22. #define TFT_CS 10
  23. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  24. #define SD_CS 4
  25. void setup(void) {
  26. Serial.begin(9600);
  27. tft.begin();
  28. yield();
  29. Serial.print("Initializing SD card...");
  30. if (!SD.begin(SD_CS)) {
  31. Serial.println("failed!");
  32. }
  33. Serial.println("OK!");
  34. }
  35. void loop() {
  36. for(uint8_t r=0; r<4; r++) {
  37. tft.setRotation(r);
  38. tft.fillScreen(ILI9341_BLUE);
  39. for(int8_t i=-2; i<1; i++) {
  40. bmpDraw("purple.bmp",
  41. (tft.width() / 2) + (i * 120),
  42. (tft.height() / 2) + (i * 160));
  43. }
  44. }
  45. }
  46. // This function opens a Windows Bitmap (BMP) file and
  47. // displays it at the given coordinates. It's sped up
  48. // by reading many pixels worth of data at a time
  49. // (rather than pixel by pixel). Increasing the buffer
  50. // size takes more of the Arduino's precious RAM but
  51. // makes loading a little faster. 20 pixels seems a
  52. // good balance.
  53. #define BUFFPIXEL 20
  54. void bmpDraw(char *filename, int16_t x, int16_t y) {
  55. File bmpFile;
  56. int bmpWidth, bmpHeight; // W+H in pixels
  57. uint8_t bmpDepth; // Bit depth (currently must be 24)
  58. uint32_t bmpImageoffset; // Start of image data in file
  59. uint32_t rowSize; // Not always = bmpWidth; may have padding
  60. uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  61. uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  62. boolean goodBmp = false; // Set to true on valid header parse
  63. boolean flip = true; // BMP is stored bottom-to-top
  64. int w, h, row, col, x2, y2, bx1, by1;
  65. uint8_t r, g, b;
  66. uint32_t pos = 0, startTime = millis();
  67. if((x >= tft.width()) || (y >= tft.height())) return;
  68. Serial.println();
  69. Serial.print(F("Loading image '"));
  70. Serial.print(filename);
  71. Serial.println('\'');
  72. // Open requested file on SD card
  73. if ((bmpFile = SD.open(filename)) == NULL) {
  74. Serial.print(F("File not found"));
  75. return;
  76. }
  77. // Parse BMP header
  78. if(read16(bmpFile) == 0x4D42) { // BMP signature
  79. Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
  80. (void)read32(bmpFile); // Read & ignore creator bytes
  81. bmpImageoffset = read32(bmpFile); // Start of image data
  82. Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
  83. // Read DIB header
  84. Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
  85. bmpWidth = read32(bmpFile);
  86. bmpHeight = read32(bmpFile);
  87. if(read16(bmpFile) == 1) { // # planes -- must be '1'
  88. bmpDepth = read16(bmpFile); // bits per pixel
  89. Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
  90. if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  91. goodBmp = true; // Supported BMP format -- proceed!
  92. Serial.print(F("Image size: "));
  93. Serial.print(bmpWidth);
  94. Serial.print('x');
  95. Serial.println(bmpHeight);
  96. // BMP rows are padded (if needed) to 4-byte boundary
  97. rowSize = (bmpWidth * 3 + 3) & ~3;
  98. // If bmpHeight is negative, image is in top-down order.
  99. // This is not canon but has been observed in the wild.
  100. if(bmpHeight < 0) {
  101. bmpHeight = -bmpHeight;
  102. flip = false;
  103. }
  104. // Crop area to be loaded
  105. x2 = x + bmpWidth - 1; // Lower-right corner
  106. y2 = y + bmpHeight - 1;
  107. if((x2 >= 0) && (y2 >= 0)) { // On screen?
  108. w = bmpWidth; // Width/height of section to load/display
  109. h = bmpHeight;
  110. bx1 = by1 = 0; // UL coordinate in BMP file
  111. if(x < 0) { // Clip left
  112. bx1 = -x;
  113. x = 0;
  114. w = x2 + 1;
  115. }
  116. if(y < 0) { // Clip top
  117. by1 = -y;
  118. y = 0;
  119. h = y2 + 1;
  120. }
  121. if(x2 >= tft.width()) w = tft.width() - x; // Clip right
  122. if(y2 >= tft.height()) h = tft.height() - y; // Clip bottom
  123. // Set TFT address window to clipped image bounds
  124. tft.startWrite(); // Requires start/end transaction now
  125. tft.setAddrWindow(x, y, w, h);
  126. for (row=0; row<h; row++) { // For each scanline...
  127. // Seek to start of scan line. It might seem labor-
  128. // intensive to be doing this on every line, but this
  129. // method covers a lot of gritty details like cropping
  130. // and scanline padding. Also, the seek only takes
  131. // place if the file position actually needs to change
  132. // (avoids a lot of cluster math in SD library).
  133. if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
  134. pos = bmpImageoffset + (bmpHeight - 1 - (row + by1)) * rowSize;
  135. else // Bitmap is stored top-to-bottom
  136. pos = bmpImageoffset + (row + by1) * rowSize;
  137. pos += bx1 * 3; // Factor in starting column (bx1)
  138. if(bmpFile.position() != pos) { // Need seek?
  139. tft.endWrite(); // End TFT transaction
  140. bmpFile.seek(pos);
  141. buffidx = sizeof(sdbuffer); // Force buffer reload
  142. tft.startWrite(); // Start new TFT transaction
  143. }
  144. for (col=0; col<w; col++) { // For each pixel...
  145. // Time to read more pixel data?
  146. if (buffidx >= sizeof(sdbuffer)) { // Indeed
  147. tft.endWrite(); // End TFT transaction
  148. bmpFile.read(sdbuffer, sizeof(sdbuffer));
  149. buffidx = 0; // Set index to beginning
  150. tft.startWrite(); // Start new TFT transaction
  151. }
  152. // Convert pixel from BMP to TFT format, push to display
  153. b = sdbuffer[buffidx++];
  154. g = sdbuffer[buffidx++];
  155. r = sdbuffer[buffidx++];
  156. tft.writePixel(tft.color565(r,g,b));
  157. } // end pixel
  158. } // end scanline
  159. tft.endWrite(); // End last TFT transaction
  160. } // end onscreen
  161. Serial.print(F("Loaded in "));
  162. Serial.print(millis() - startTime);
  163. Serial.println(" ms");
  164. } // end goodBmp
  165. }
  166. }
  167. bmpFile.close();
  168. if(!goodBmp) Serial.println(F("BMP format not recognized."));
  169. }
  170. // These read 16- and 32-bit types from the SD card file.
  171. // BMP data is stored little-endian, Arduino is little-endian too.
  172. // May need to reverse subscript order if porting elsewhere.
  173. uint16_t read16(File &f) {
  174. uint16_t result;
  175. ((uint8_t *)&result)[0] = f.read(); // LSB
  176. ((uint8_t *)&result)[1] = f.read(); // MSB
  177. return result;
  178. }
  179. uint32_t read32(File &f) {
  180. uint32_t result;
  181. ((uint8_t *)&result)[0] = f.read(); // LSB
  182. ((uint8_t *)&result)[1] = f.read();
  183. ((uint8_t *)&result)[2] = f.read();
  184. ((uint8_t *)&result)[3] = f.read(); // MSB
  185. return result;
  186. }