PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spitftbitmap.ino 8.0KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /***************************************************
  2. This is an example sketch for the Adafruit 1.8" SPI display.
  3. This library works with the Adafruit 1.8" TFT Breakout w/SD card
  4. ----> http://www.adafruit.com/products/358
  5. The 1.8" TFT shield
  6. ----> https://www.adafruit.com/product/802
  7. The 1.44" TFT breakout
  8. ----> https://www.adafruit.com/product/2088
  9. as well as Adafruit raw 1.8" TFT display
  10. ----> http://www.adafruit.com/products/618
  11. Check out the links above for our tutorials and wiring diagrams
  12. These displays use SPI to communicate, 4 or 5 pins are required to
  13. interface (RST is optional)
  14. Adafruit invests time and resources providing this open source code,
  15. please support Adafruit and open-source hardware by purchasing
  16. products from Adafruit!
  17. Written by Limor Fried/Ladyada for Adafruit Industries.
  18. MIT license, all text above must be included in any redistribution
  19. ****************************************************/
  20. #include <Adafruit_GFX.h> // Core graphics library
  21. #include <ST7735_t3.h> // Hardware-specific library
  22. #include <SPI.h>
  23. #include <SD.h>
  24. // This Teensy3 native optimized version requires specific pins
  25. //
  26. #define TFT_SCLK 13 // SCLK can also use pin 14
  27. #define TFT_MOSI 11 // MOSI can also use pin 7
  28. #define TFT_CS 10 // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
  29. #define TFT_DC 9 // but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
  30. #define TFT_RST 8 // RST can use any pin
  31. #define SD_CS 4 // CS for SD card, can use any pin
  32. ST7735_t3 tft = ST7735_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  33. void setup(void) {
  34. pinMode(SD_CS, INPUT_PULLUP); // keep SD CS high when not using SD card
  35. Serial.begin(9600);
  36. // Use this initializer if you're using a 1.8" TFT
  37. tft.initR(INITR_BLACKTAB);
  38. // Use this initializer (uncomment) if you're using a 1.44" TFT
  39. //tft.initR(INITR_144GREENTAB);
  40. Serial.print("Initializing SD card...");
  41. if (!SD.begin(SD_CS)) {
  42. Serial.println("failed!");
  43. tft.fillScreen(ST7735_BLACK);
  44. tft.setCursor(5, tft.height()/2 - 6);
  45. tft.print("Unable to access");
  46. tft.setCursor(32, tft.height()/2 + 6);
  47. tft.print("SD card");
  48. while (1) {
  49. // do nothing if SD card not working
  50. }
  51. }
  52. Serial.println("OK!");
  53. bmpDraw("parrot.bmp", 0, 0);
  54. }
  55. void loop() {
  56. // uncomment these lines to draw bitmaps in different locations/rotations!
  57. /*
  58. tft.fillScreen(ST7735_BLACK); // Clear display
  59. for (int i=0; i<4; i++) // Draw 4 parrots
  60. bmpDraw("parrot.bmp", tft.width() / 4 * i, tft.height() / 4 * i);
  61. delay(1000);
  62. tft.setRotation(tft.getRotation() + 1); // Inc rotation 90 degrees
  63. */
  64. }
  65. // This function opens a Windows Bitmap (BMP) file and
  66. // displays it at the given coordinates. It's sped up
  67. // by reading many pixels worth of data at a time
  68. // (rather than pixel by pixel). Increasing the buffer
  69. // size takes more of the Arduino's precious RAM but
  70. // makes loading a little faster. 20 pixels seems a
  71. // good balance.
  72. #define BUFFPIXEL 20
  73. //===========================================================
  74. // Try Draw using writeRect
  75. void bmpDraw(const char *filename, uint8_t x, uint16_t y) {
  76. File bmpFile;
  77. int bmpWidth, bmpHeight; // W+H in pixels
  78. uint8_t bmpDepth; // Bit depth (currently must be 24)
  79. uint32_t bmpImageoffset; // Start of image data in file
  80. uint32_t rowSize; // Not always = bmpWidth; may have padding
  81. uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  82. uint16_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  83. boolean goodBmp = false; // Set to true on valid header parse
  84. boolean flip = true; // BMP is stored bottom-to-top
  85. int w, h, row, col;
  86. uint8_t r, g, b;
  87. uint32_t pos = 0, startTime = millis();
  88. uint16_t awColors[320]; // hold colors for one row at a time...
  89. if((x >= tft.width()) || (y >= tft.height())) return;
  90. Serial.println();
  91. Serial.print(F("Loading image '"));
  92. Serial.print(filename);
  93. Serial.println('\'');
  94. // Open requested file on SD card
  95. bmpFile = SD.open(filename);
  96. if (!bmpFile) {
  97. Serial.print("File not found");
  98. tft.fillScreen(ST7735_BLACK);
  99. tft.setCursor(12, tft.height()/2 - 12);
  100. tft.print("Unable to");
  101. tft.setCursor(12, tft.height()/2 - 0);
  102. tft.print("read file: ");
  103. tft.setCursor(12, tft.height()/2 + 12);
  104. tft.setTextColor(ST7735_YELLOW);
  105. tft.print(filename);
  106. return;
  107. }
  108. // Parse BMP header
  109. if(read16(bmpFile) == 0x4D42) { // BMP signature
  110. Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
  111. (void)read32(bmpFile); // Read & ignore creator bytes
  112. bmpImageoffset = read32(bmpFile); // Start of image data
  113. Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
  114. // Read DIB header
  115. Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
  116. bmpWidth = read32(bmpFile);
  117. bmpHeight = read32(bmpFile);
  118. if(read16(bmpFile) == 1) { // # planes -- must be '1'
  119. bmpDepth = read16(bmpFile); // bits per pixel
  120. Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
  121. if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  122. goodBmp = true; // Supported BMP format -- proceed!
  123. Serial.print(F("Image size: "));
  124. Serial.print(bmpWidth);
  125. Serial.print('x');
  126. Serial.println(bmpHeight);
  127. // BMP rows are padded (if needed) to 4-byte boundary
  128. rowSize = (bmpWidth * 3 + 3) & ~3;
  129. // If bmpHeight is negative, image is in top-down order.
  130. // This is not canon but has been observed in the wild.
  131. if(bmpHeight < 0) {
  132. bmpHeight = -bmpHeight;
  133. flip = false;
  134. }
  135. // Crop area to be loaded
  136. w = bmpWidth;
  137. h = bmpHeight;
  138. if((x+w-1) >= tft.width()) w = tft.width() - x;
  139. if((y+h-1) >= tft.height()) h = tft.height() - y;
  140. for (row=0; row<h; row++) { // For each scanline...
  141. // Seek to start of scan line. It might seem labor-
  142. // intensive to be doing this on every line, but this
  143. // method covers a lot of gritty details like cropping
  144. // and scanline padding. Also, the seek only takes
  145. // place if the file position actually needs to change
  146. // (avoids a lot of cluster math in SD library).
  147. if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
  148. pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  149. else // Bitmap is stored top-to-bottom
  150. pos = bmpImageoffset + row * rowSize;
  151. if(bmpFile.position() != pos) { // Need seek?
  152. bmpFile.seek(pos);
  153. buffidx = sizeof(sdbuffer); // Force buffer reload
  154. }
  155. for (col=0; col<w; col++) { // For each pixel...
  156. // Time to read more pixel data?
  157. if (buffidx >= sizeof(sdbuffer)) { // Indeed
  158. bmpFile.read(sdbuffer, sizeof(sdbuffer));
  159. buffidx = 0; // Set index to beginning
  160. }
  161. // Convert pixel from BMP to TFT format, push to display
  162. b = sdbuffer[buffidx++];
  163. g = sdbuffer[buffidx++];
  164. r = sdbuffer[buffidx++];
  165. awColors[col] = tft.Color565(r,g,b);
  166. } // end pixel
  167. tft.writeRect(0, row, w, 1, awColors);
  168. } // end scanline
  169. Serial.print(F("Loaded in "));
  170. Serial.print(millis() - startTime);
  171. Serial.println(" ms");
  172. } // end goodBmp
  173. }
  174. }
  175. bmpFile.close();
  176. if(!goodBmp) Serial.println(F("BMP format not recognized."));
  177. }
  178. // These read 16- and 32-bit types from the SD card file.
  179. // BMP data is stored little-endian, Arduino is little-endian too.
  180. // May need to reverse subscript order if porting elsewhere.
  181. uint16_t read16(File f) {
  182. uint16_t result;
  183. ((uint8_t *)&result)[0] = f.read(); // LSB
  184. ((uint8_t *)&result)[1] = f.read(); // MSB
  185. return result;
  186. }
  187. uint32_t read32(File f) {
  188. uint32_t result;
  189. ((uint8_t *)&result)[0] = f.read(); // LSB
  190. ((uint8_t *)&result)[1] = f.read();
  191. ((uint8_t *)&result)[2] = f.read();
  192. ((uint8_t *)&result)[3] = f.read(); // MSB
  193. return result;
  194. }