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.

SDfatTest.ino 6.2KB

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