Advertisement
DoHNotGiveUp

Card Mount Failed

Aug 30th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | Source Code | 0 0
  1. // This sketch if for an ESP32, it draws Jpeg images pulled from an SD Card onto the TFT.
  2. // As well as the TFT_eSPI library you will need the JPEG Decoder library.
  3. // A copy can be downloaded here, it is based on the library by Makoto Kurauchi.
  4. // https://github.com/Bodmer/JPEGDecoder
  5. // Images on SD Card must be put in the root folder (top level) to be found
  6. // Use the SD library examples to verify your SD Card interface works
  7. // The example images used to test this sketch can be found in the library
  8. // JPEGDecoder/extras folder
  9.  
  10. #include <FS.h>
  11. #include <SD.h>
  12. #include <TFT_eSPI.h>
  13. TFT_eSPI tft = TFT_eSPI();
  14. // JPEG decoder library
  15. #include <JPEGDecoder.h>
  16. #define SD_MOSI 23//added per Elecrow's Tech Support
  17. #define SD_MISO 19
  18. #define SD_SCK 18
  19. #define SD_CS 5
  20.  
  21. //####################################################################################################
  22. // void setup
  23. //####################################################################################################
  24. void setup() {
  25. Serial.begin(115200);
  26.  
  27. // Set all chip selects high to avoid bus contention during initialisation of each peripheral
  28. // digitalWrite(22, HIGH); // Touch controller chip select (if used)
  29. digitalWrite(33, HIGH); // Touch controller chip select (if used)- Elecrow's info
  30. digitalWrite(15, HIGH); // TFT screen chip select
  31. digitalWrite( 5, HIGH); // SD card chips select, must use GPIO 5 (ESP32 SS)
  32.  
  33. tft.begin();
  34.  
  35. if (!SD.begin(5, tft.getSPIinstance())) {
  36. Serial.println("Card Mount Failed");
  37. return;
  38. }
  39. uint8_t cardType = SD.cardType();
  40.  
  41. if (cardType == CARD_NONE) {
  42. Serial.println("No SD card attached");
  43. return;
  44. }
  45.  
  46. Serial.print("SD Card Type: ");
  47. if (cardType == CARD_MMC) {
  48. Serial.println("MMC");
  49. } else if (cardType == CARD_SD) {
  50. Serial.println("SDSC");
  51. } else if (cardType == CARD_SDHC) {
  52. Serial.println("SDHC");
  53. } else {
  54. Serial.println("UNKNOWN");
  55. }
  56.  
  57. uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  58. Serial.printf("SD Card Size: %lluMB\n", cardSize);
  59.  
  60. Serial.println("initialisation done.");
  61. }
  62.  
  63. //####################################################################################################
  64. // Main loop
  65. //####################################################################################################
  66. void loop() {
  67.  
  68. tft.setRotation(2); // portrait
  69. tft.fillScreen(random(0xFFFF));
  70.  
  71. // The image is 300 x 300 pixels so we do some sums to position image in the middle of the screen!
  72. // Doing this by reading the image width and height from the jpeg info is left as an exercise!
  73. int x = (tft.width() - 300) / 2 - 1;
  74. int y = (tft.height() - 300) / 2 - 1;
  75.  
  76. drawSdJpeg("/EagleEye.jpg", x, y); // This draws a jpeg pulled off the SD Card
  77. delay(2000);
  78.  
  79. tft.setRotation(2); // portrait
  80. tft.fillScreen(random(0xFFFF));
  81. drawSdJpeg("/Baboon40.jpg", 0, 0); // This draws a jpeg pulled off the SD Card
  82. delay(2000);
  83.  
  84. tft.setRotation(2); // portrait
  85. tft.fillScreen(random(0xFFFF));
  86. drawSdJpeg("/lena20k.jpg", 0, 0); // This draws a jpeg pulled off the SD Card
  87. delay(2000);
  88.  
  89. tft.setRotation(1); // landscape
  90. tft.fillScreen(random(0xFFFF));
  91. drawSdJpeg("/Mouse480.jpg", 0, 0); // This draws a jpeg pulled off the SD Card
  92.  
  93. delay(2000);
  94.  
  95. while(1); // Wait here
  96. }
  97.  
  98. //####################################################################################################
  99. // Draw a JPEG on the TFT pulled from SD Card
  100. //####################################################################################################
  101. // xpos, ypos is top left corner of plotted image
  102. void drawSdJpeg(const char *filename, int xpos, int ypos) {
  103.  
  104. // Open the named file (the Jpeg decoder library will close it)
  105. File jpegFile = SD.open( filename, FILE_READ); // or, file handle reference for SD library
  106.  
  107. if ( !jpegFile ) {
  108. Serial.print("ERROR: File \""); Serial.print(filename); Serial.println ("\" not found!");
  109. return;
  110. }
  111.  
  112. Serial.println("===========================");
  113. Serial.print("Drawing file: "); Serial.println(filename);
  114. Serial.println("===========================");
  115.  
  116. // Use one of the following methods to initialise the decoder:
  117. bool decoded = JpegDec.decodeSdFile(jpegFile); // Pass the SD file handle to the decoder,
  118. //bool decoded = JpegDec.decodeSdFile(filename); // or pass the filename (String or character array)
  119.  
  120. if (decoded) {
  121. // print information about the image to the serial port
  122. jpegInfo();
  123. // render the image onto the screen at given coordinates
  124. jpegRender(xpos, ypos);
  125. }
  126. else {
  127. Serial.println("Jpeg file format not supported!");
  128. }
  129. }
  130.  
  131. //####################################################################################################
  132. // Draw a JPEG on the TFT, images will be cropped on the right/bottom sides if they do not fit
  133. //####################################################################################################
  134. // This function assumes xpos,ypos is a valid screen coordinate. For convenience images that do not
  135. // fit totally on the screen are cropped to the nearest MCU size and may leave right/bottom borders.
  136. void jpegRender(int xpos, int ypos) {
  137.  
  138. //jpegInfo(); // Print information from the JPEG file (could comment this line out)
  139.  
  140. uint16_t *pImg;
  141. uint16_t mcu_w = JpegDec.MCUWidth;
  142. uint16_t mcu_h = JpegDec.MCUHeight;
  143. uint32_t max_x = JpegDec.width;
  144. uint32_t max_y = JpegDec.height;
  145.  
  146. bool swapBytes = tft.getSwapBytes();
  147. tft.setSwapBytes(true);
  148.  
  149. // Jpeg images are draw as a set of image block (tiles) called Minimum Coding Units (MCUs)
  150. // Typically these MCUs are 16x16 pixel blocks
  151. // Determine the width and height of the right and bottom edge image blocks
  152. uint32_t min_w = jpg_min(mcu_w, max_x % mcu_w);
  153. uint32_t min_h = jpg_min(mcu_h, max_y % mcu_h);
  154.  
  155. // save the current image block size
  156. uint32_t win_w = mcu_w;
  157. uint32_t win_h = mcu_h;
  158.  
  159. // record the current time so we can measure how long it takes to draw an image
  160. uint32_t drawTime = millis();
  161.  
  162. // save the coordinate of the right and bottom edges to assist image cropping
  163. // to the screen size
  164. max_x += xpos;
  165. max_y += ypos;
  166.  
  167. // Fetch data from the file, decode and display
  168. while (JpegDec.read()) { // While there is more data in the file
  169. pImg = JpegDec.pImage ; // Decode a MCU (Minimum Coding Unit, typically a 8x8 or 16x16 pixel block)
  170.  
  171. // Calculate coordinates of top left corner of current MCU
  172. int mcu_x = JpegDec.MCUx * mcu_w + xpos;
  173. int mcu_y = JpegDec.MCUy * mcu_h + ypos;
  174.  
  175. // check if the image block size needs to be changed for the right edge
  176. if (mcu_x + mcu_w <= max_x) win_w = mcu_w;
  177. else win_w = min_w;
  178.  
  179. // check if the image block size needs to be changed for the bottom edge
  180. if (mcu_y + mcu_h <= max_y) win_h = mcu_h;
  181. else win_h = min_h;
  182.  
  183. // copy pixels into a contiguous block
  184. if (win_w != mcu_w)
  185. {
  186. uint16_t *cImg;
  187. int p = 0;
  188. cImg = pImg + win_w;
  189. for (int h = 1; h < win_h; h++)
  190. {
  191. p += mcu_w;
  192. for (int w = 0; w < win_w; w++)
  193. {
  194. *cImg = *(pImg + w + p);
  195. cImg++;
  196. }
  197. }
  198. }
  199.  
  200. // calculate how many pixels must be drawn
  201. uint32_t mcu_pixels = win_w * win_h;
  202.  
  203. // draw image MCU block only if it will fit on the screen
  204. if (( mcu_x + win_w ) <= tft.width() && ( mcu_y + win_h ) <= tft.height())
  205. tft.pushImage(mcu_x, mcu_y, win_w, win_h, pImg);
  206. else if ( (mcu_y + win_h) >= tft.height())
  207. JpegDec.abort(); // Image has run off bottom of screen so abort decoding
  208. }
  209.  
  210. tft.setSwapBytes(swapBytes);
  211.  
  212. showTime(millis() - drawTime); // These lines are for sketch testing only
  213. }
  214.  
  215. //####################################################################################################
  216. // Print image information to the serial port (optional)
  217. //####################################################################################################
  218. // JpegDec.decodeFile(...) or JpegDec.decodeArray(...) must be called before this info is available!
  219. void jpegInfo() {
  220.  
  221. // Print information extracted from the JPEG file
  222. Serial.println("JPEG image info");
  223. Serial.println("===============");
  224. Serial.print("Width :");
  225. Serial.println(JpegDec.width);
  226. Serial.print("Height :");
  227. Serial.println(JpegDec.height);
  228. Serial.print("Components :");
  229. Serial.println(JpegDec.comps);
  230. Serial.print("MCU / row :");
  231. Serial.println(JpegDec.MCUSPerRow);
  232. Serial.print("MCU / col :");
  233. Serial.println(JpegDec.MCUSPerCol);
  234. Serial.print("Scan type :");
  235. Serial.println(JpegDec.scanType);
  236. Serial.print("MCU width :");
  237. Serial.println(JpegDec.MCUWidth);
  238. Serial.print("MCU height :");
  239. Serial.println(JpegDec.MCUHeight);
  240. Serial.println("===============");
  241. Serial.println("");
  242. }
  243.  
  244. //####################################################################################################
  245. // Show the execution time (optional)
  246. //####################################################################################################
  247. // WARNING: for UNO/AVR legacy reasons printing text to the screen with the Mega might not work for
  248. // sketch sizes greater than ~70KBytes because 16-bit address pointers are used in some libraries.
  249.  
  250. // The Due will work fine with the HX8357_Due library.
  251.  
  252. void showTime(uint32_t msTime) {
  253. //tft.setCursor(0, 0);
  254. //tft.setTextFont(1);
  255. //tft.setTextSize(2);
  256. //tft.setTextColor(TFT_WHITE, TFT_BLACK);
  257. //tft.print(F(" JPEG drawn in "));
  258. //tft.print(msTime);
  259. //tft.println(F(" ms "));
  260. Serial.print(F(" JPEG drawn in "));
  261. Serial.print(msTime);
  262. Serial.println(F(" ms "));
  263. }
  264.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement