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.

SDfatTest.ino 6.1KB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 //see below...
  24. /*
  25. Teensy 3.x can use: 2,6,9,10,15,20,21,22,23
  26. */
  27. #define RA8875_RESET 9//any pin or nothing!
  28. #define BUFFPIXEL 30
  29. RA8875 tft = RA8875(RA8875_CS, RA8875_RESET); //Teensy3/arduino's
  30. SdFat SD;
  31. File bmpFile;
  32. void setup()
  33. {
  34. Serial.begin(38400);
  35. long unsigned debug_start = millis ();
  36. while (!Serial && ((millis () - debug_start) <= 5000)) ;
  37. Serial.println("RA8875 start");
  38. tft.begin(RA8875_480x272);
  39. if (!SD.begin(SDCSPIN, SPI_FULL_SPEED)) {
  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++], sdbuffer[buffidx++]);
  126. } // end pixel
  127. tft.setY(y + row);
  128. tft.drawPixels(rowBuffer, w, x, y + row);
  129. } // end scanline
  130. Serial.print("Loaded in ");
  131. Serial.print(millis() - startTime);
  132. Serial.println(" ms");
  133. } // end goodBmp
  134. }
  135. }
  136. bmpFile.close();
  137. if (!goodBmp) {
  138. Serial.println("BMP format not recognized.");
  139. } else {
  140. Serial.println("end...");
  141. }
  142. }
  143. // These read 16- and 32-bit types from the SD card file.
  144. // BMP data is stored little-endian, Arduino is little-endian too.
  145. // May need to reverse subscript order if porting elsewhere.
  146. void writePixb(int16_t x, uint16_t color) {
  147. tft.setX(x);
  148. tft.writeCommand(RA8875_MRWC);
  149. tft.writeData16(color);
  150. }
  151. uint16_t read16(File &f) {
  152. uint16_t result = 0;
  153. ((uint8_t *)&result)[0] = f.read(); // LSB
  154. ((uint8_t *)&result)[1] = f.read(); // MSB
  155. return result;
  156. }
  157. uint32_t read32(File &f) {
  158. uint32_t result = 0;
  159. ((uint8_t *)&result)[0] = f.read(); // LSB
  160. ((uint8_t *)&result)[1] = f.read();
  161. ((uint8_t *)&result)[2] = f.read();
  162. ((uint8_t *)&result)[3] = f.read(); // MSB
  163. return result;
  164. }