PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

218 lines
7.4KB

  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 <ILI9488_t3.h> // Hardware-specific library
  14. #include <SPI.h>
  15. #include <SD.h>
  16. // TFT display and SD card will share the hardware SPI interface.
  17. // Hardware SPI pins are specific to the Arduino board type and
  18. // cannot be remapped to alternate pins. For Arduino Uno,
  19. // Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
  20. #define TFT_DC 9
  21. #define TFT_CS 10
  22. ILI9488_t3 tft = ILI9488_t3(&SPI, TFT_CS, TFT_DC);
  23. #define SD_CS 4
  24. void setup(void) {
  25. // Keep the SD card inactive while working the display.
  26. pinMode(SD_CS, INPUT_PULLUP);
  27. delay(200);
  28. tft.begin();
  29. tft.fillScreen(ILI9488_BLUE);
  30. Serial.begin(9600);
  31. tft.setTextColor(ILI9488_WHITE);
  32. tft.setTextSize(2);
  33. tft.println(F("Waiting for Arduino Serial Monitor..."));
  34. while (!Serial) {
  35. if (millis() > 8000) break;
  36. }
  37. Serial.print(F("Initializing SD card..."));
  38. tft.println(F("Init SD card..."));
  39. while (!SD.begin(SD_CS)) {
  40. Serial.println(F("failed to access SD card!"));
  41. tft.println(F("failed to access SD card!"));
  42. delay(2000);
  43. }
  44. Serial.println("OK!");
  45. }
  46. void loop() {
  47. tft.fillScreen(ILI9488_GREEN);
  48. bmpDraw("purple.bmp", 0, 0);
  49. delay(5000);
  50. bmpDraw("flowers.bmp", 0, 0);
  51. delay(5000);
  52. bmpDraw("cat.bmp", 0, 0);
  53. delay(5000);
  54. }
  55. // This function opens a Windows Bitmap (BMP) file and
  56. // displays it at the given coordinates. It's sped up
  57. // by reading many pixels worth of data at a time
  58. // (rather than pixel by pixel). Increasing the buffer
  59. // size takes more of the Arduino's precious RAM but
  60. // makes loading a little faster. 20 pixels seems a
  61. // good balance for tiny AVR chips.
  62. // Larger buffers are slightly more efficient, but if
  63. // the buffer is too large, extra data is read unnecessarily.
  64. // For example, if the image is 240 pixels wide, a 100
  65. // pixel buffer will read 3 groups of 100 pixels. The
  66. // last 60 pixels from the 3rd read may not be used.
  67. #define BUFFPIXEL 80
  68. //===========================================================
  69. // Try Draw using writeRect
  70. void bmpDraw(const char *filename, uint8_t x, uint16_t y) {
  71. File bmpFile;
  72. int bmpWidth, bmpHeight; // W+H in pixels
  73. uint8_t bmpDepth; // Bit depth (currently must be 24)
  74. uint32_t bmpImageoffset; // Start of image data in file
  75. uint32_t rowSize; // Not always = bmpWidth; may have padding
  76. uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  77. uint16_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  78. boolean goodBmp = false; // Set to true on valid header parse
  79. boolean flip = true; // BMP is stored bottom-to-top
  80. int w, h, row, col;
  81. uint8_t r, g, b;
  82. uint32_t pos = 0, startTime = millis();
  83. uint16_t awColors[320]; // hold colors for one row at a time...
  84. if((x >= tft.width()) || (y >= tft.height())) return;
  85. Serial.println();
  86. Serial.print(F("Loading image '"));
  87. Serial.print(filename);
  88. Serial.println('\'');
  89. // Open requested file on SD card
  90. if (!(bmpFile = SD.open(filename))) {
  91. Serial.print(F("File not found"));
  92. return;
  93. }
  94. // Parse BMP header
  95. if(read16(bmpFile) == 0x4D42) { // BMP signature
  96. Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
  97. (void)read32(bmpFile); // Read & ignore creator bytes
  98. bmpImageoffset = read32(bmpFile); // Start of image data
  99. Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
  100. // Read DIB header
  101. Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
  102. bmpWidth = read32(bmpFile);
  103. bmpHeight = read32(bmpFile);
  104. if(read16(bmpFile) == 1) { // # planes -- must be '1'
  105. bmpDepth = read16(bmpFile); // bits per pixel
  106. Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
  107. if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  108. goodBmp = true; // Supported BMP format -- proceed!
  109. Serial.print(F("Image size: "));
  110. Serial.print(bmpWidth);
  111. Serial.print('x');
  112. Serial.println(bmpHeight);
  113. // BMP rows are padded (if needed) to 4-byte boundary
  114. rowSize = (bmpWidth * 3 + 3) & ~3;
  115. // If bmpHeight is negative, image is in top-down order.
  116. // This is not canon but has been observed in the wild.
  117. if(bmpHeight < 0) {
  118. bmpHeight = -bmpHeight;
  119. flip = false;
  120. }
  121. // Crop area to be loaded
  122. w = bmpWidth;
  123. h = bmpHeight;
  124. if((x+w-1) >= tft.width()) w = tft.width() - x;
  125. if((y+h-1) >= tft.height()) h = tft.height() - y;
  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) * rowSize;
  135. else // Bitmap is stored top-to-bottom
  136. pos = bmpImageoffset + row * rowSize;
  137. if(bmpFile.position() != pos) { // Need seek?
  138. bmpFile.seek(pos);
  139. buffidx = sizeof(sdbuffer); // Force buffer reload
  140. }
  141. for (col=0; col<w; col++) { // For each pixel...
  142. // Time to read more pixel data?
  143. if (buffidx >= sizeof(sdbuffer)) { // Indeed
  144. bmpFile.read(sdbuffer, sizeof(sdbuffer));
  145. buffidx = 0; // Set index to beginning
  146. }
  147. // Convert pixel from BMP to TFT format, push to display
  148. b = sdbuffer[buffidx++];
  149. g = sdbuffer[buffidx++];
  150. r = sdbuffer[buffidx++];
  151. awColors[col] = tft.color565(r,g,b);
  152. } // end pixel
  153. tft.writeRect(0, row, w, 1, awColors);
  154. } // end scanline
  155. Serial.print(F("Loaded in "));
  156. Serial.print(millis() - startTime);
  157. Serial.println(" ms");
  158. } // end goodBmp
  159. }
  160. }
  161. bmpFile.close();
  162. if(!goodBmp) Serial.println(F("BMP format not recognized."));
  163. }
  164. // These read 16- and 32-bit types from the SD card file.
  165. // BMP data is stored little-endian, Arduino is little-endian too.
  166. // May need to reverse subscript order if porting elsewhere.
  167. uint16_t read16(File &f) {
  168. uint16_t result;
  169. ((uint8_t *)&result)[0] = f.read(); // LSB
  170. ((uint8_t *)&result)[1] = f.read(); // MSB
  171. return result;
  172. }
  173. uint32_t read32(File &f) {
  174. uint32_t result;
  175. ((uint8_t *)&result)[0] = f.read(); // LSB
  176. ((uint8_t *)&result)[1] = f.read();
  177. ((uint8_t *)&result)[2] = f.read();
  178. ((uint8_t *)&result)[3] = f.read(); // MSB
  179. return result;
  180. }