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.3KB

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