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.

SDTest.ino 6.2KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Grab bmp image from an sd card.
  3. It reads column by column and send each RA instead of pixels
  4. Look inside the folder RA8875/examples/SDTest_800x480
  5. there's a folder, copy the content in a formatted FAT32 SD card
  6. Note: Fixed the garbage introduced with Teensy3 optimized SD library
  7. You can use USE_TEENSY3_OPTIMIZED_CODE now.
  8. */
  9. #include <SPI.h>
  10. #include <RA8875.h>
  11. #include <SD.h>
  12. /*
  13. Teensy3.x
  14. You are using 4 wire SPI here, so:
  15. MOSI: 11//Teensy3.x
  16. MISO: 12//Teensy3.x
  17. SCK: 13//Teensy3.x
  18. the rest of pin below:
  19. */
  20. #define SDCSPIN 6//for SD
  21. #define RA8875_CS 10 //see below...
  22. /*
  23. Teensy 3.x can use: 2,6,9,10,15,20,21,22,23
  24. */
  25. #define RA8875_RESET 9//any pin or nothing!
  26. #if defined USE_TEENSY3_OPTIMIZED_CODE
  27. #define BUFFPIXEL 120//do not change this! It's a workaround to SD caching artifact introduced in SD optimized
  28. #else
  29. #define BUFFPIXEL 20//I've experiment a little with this but not so many differences
  30. #endif
  31. //instances
  32. RA8875 tft = RA8875(RA8875_CS, RA8875_RESET); //Teensy3/arduino's
  33. File bmpFile;
  34. void setup()
  35. {
  36. Serial.begin(38400);
  37. long unsigned debug_start = millis ();
  38. while (!Serial && ((millis () - debug_start) <= 5000)) ;
  39. Serial.println("RA8875 start");
  40. tft.begin(RA8875_480x272);
  41. if (!SD.begin(SDCSPIN)) {
  42. Serial.println("SD failed!");
  43. return;
  44. }
  45. Serial.println("OK!");
  46. bmpDraw("alert.bmp", 0, 0);//copy the enclosed image in a SD card (check the folder!!!)
  47. }
  48. void loop()
  49. {
  50. }
  51. void bmpDraw(const char *filename, uint16_t x, uint16_t y) {
  52. uint16_t bmpWidth, bmpHeight; // W+H in pixels
  53. uint8_t bmpDepth; // Bit depth (currently must be 24)
  54. uint32_t bmpImageoffset; // Start of image data in file
  55. uint32_t rowSize; // Not always = bmpWidth; may have padding
  56. uint8_t sdbuffer[3 * BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  57. uint16_t buffidx = 0;
  58. boolean goodBmp = false; // Set to true on valid header parse
  59. boolean flip = true; // BMP is stored bottom-to-top
  60. int16_t w, h, row, col;
  61. uint32_t pos = 0, startTime = millis();
  62. buffidx = sizeof(sdbuffer);// Current position in sdbuffer
  63. if ((x >= tft.width()) || (y >= tft.height())) return;
  64. Serial.println();
  65. Serial.print("Loading image '");
  66. Serial.print(filename);
  67. Serial.println('\'');
  68. // Open requested file on SD card
  69. if ((bmpFile = SD.open(filename)) == 0) {
  70. Serial.print("File not found");
  71. return;
  72. }
  73. // Parse BMP header
  74. if (read16(bmpFile) == 0x4D42) { // BMP signature
  75. Serial.print("File size: "); Serial.println(read32(bmpFile));
  76. (void)read32(bmpFile); // Read & ignore creator bytes
  77. bmpImageoffset = read32(bmpFile); // Start of image data
  78. Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
  79. // Read DIB header
  80. Serial.print("Header size: "); Serial.println(read32(bmpFile));
  81. bmpWidth = read32(bmpFile);
  82. bmpHeight = read32(bmpFile);
  83. if (read16(bmpFile) == 1) { // # planes -- must be '1'
  84. bmpDepth = read16(bmpFile); // bits per pixel
  85. Serial.print("Bit Depth: "); Serial.println(bmpDepth);
  86. if ((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  87. goodBmp = true; // Supported BMP format -- proceed!
  88. Serial.print("Image size: ");
  89. Serial.print(bmpWidth);
  90. Serial.print('x');
  91. Serial.println(bmpHeight);
  92. // BMP rows are padded (if needed) to 4-byte boundary
  93. rowSize = (bmpWidth * 3 + 3) & ~3;
  94. // If bmpHeight is negative, image is in top-down order.
  95. // This is not canon but has been observed in the wild.
  96. if (bmpHeight < 0) {
  97. bmpHeight = -bmpHeight;
  98. flip = false;
  99. }
  100. // Crop area to be loaded
  101. w = bmpWidth;
  102. h = bmpHeight;
  103. uint16_t rowBuffer[w];
  104. if (((w - 1)+x) >= tft.width()) w = tft.width() - x;
  105. if (((h - 1)+y) >= tft.height()) h = tft.height() - y;
  106. for (row = 0; row < h; row++) { // For each scanline...
  107. // Seek to start of scan line. It might seem labor-
  108. // intensive to be doing this on every line, but this
  109. // method covers a lot of gritty details like cropping
  110. // and scanline padding. Also, the seek only takes
  111. // place if the file position actually needs to change
  112. // (avoids a lot of cluster math in SD library).
  113. if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
  114. pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  115. else // Bitmap is stored top-to-bottom
  116. pos = bmpImageoffset + row * rowSize;
  117. if (bmpFile.position() != pos) { // Need seek?
  118. bmpFile.seek(pos);
  119. buffidx = sizeof(sdbuffer); // Force buffer reload
  120. }
  121. for (col = 0; col < w; col++) { // For each pixel...
  122. // Time to read more pixel data?
  123. if (buffidx >= sizeof(sdbuffer)) { // Indeed
  124. bmpFile.read(sdbuffer, sizeof(sdbuffer));
  125. buffidx = 0; // Set index to beginning
  126. }
  127. rowBuffer[col] = tft.Color565(sdbuffer[buffidx++], sdbuffer[buffidx++], sdbuffer[buffidx++]);
  128. } // end pixel
  129. tft.setY(y + row);
  130. tft.drawPixels(rowBuffer, w, x, y + row);
  131. } // end scanline
  132. Serial.print("Loaded in ");
  133. Serial.print(millis() - startTime);
  134. Serial.println(" ms");
  135. } // end goodBmp
  136. }
  137. }
  138. bmpFile.close();
  139. if (!goodBmp) {
  140. Serial.println("BMP format not recognized.");
  141. } else {
  142. Serial.println("end...");
  143. }
  144. }
  145. // These read 16- and 32-bit types from the SD card file.
  146. // BMP data is stored little-endian, Arduino is little-endian too.
  147. // May need to reverse subscript order if porting elsewhere.
  148. void writePixb(int16_t x, uint16_t color) {
  149. tft.setX(x);
  150. tft.writeCommand(RA8875_MRWC);
  151. tft.writeData16(color);
  152. }
  153. uint16_t read16(File &f) {
  154. uint16_t result = 0;
  155. ((uint8_t *)&result)[0] = f.read(); // LSB
  156. ((uint8_t *)&result)[1] = f.read(); // MSB
  157. return result;
  158. }
  159. uint32_t read32(File &f) {
  160. uint32_t result = 0;
  161. ((uint8_t *)&result)[0] = f.read(); // LSB
  162. ((uint8_t *)&result)[1] = f.read();
  163. ((uint8_t *)&result)[2] = f.read();
  164. ((uint8_t *)&result)[3] = f.read(); // MSB
  165. return result;
  166. }