Display.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #include "Display.h"
  2. /*
  3. Big thanks to bodmer for having great TFT and JPEG libraries
  4. https://github.com/bodmer
  5. */
  6. Display::Display()
  7. {
  8. //Serial.println("Initializing Display Object...");
  9. }
  10. void Display::main()
  11. {
  12. return;
  13. }
  14. // Function to prepare the display and the menus
  15. void Display::RunSetup()
  16. {
  17. run_setup = false;
  18. // Need to declare new
  19. display_buffer = new SimpleList<String>();
  20. tft.init();
  21. tft.setRotation(0); // Portrait
  22. tft.setCursor(0, 0);
  23. //tft.setFreeFont(&FreeMonoBold9pt7b);
  24. // Calibration data
  25. //uint16_t calData[5] = { 390, 3516, 253, 3520, 7 }; tft.setRotation(1); // Portrait
  26. uint16_t calData[5] = { 275, 3494, 361, 3528, 4 }; // tft.setRotation(0); // Portrait
  27. tft.setTouch(calData);
  28. //tft.fillScreen(TFT_BLACK);
  29. clearScreen();
  30. // Initialize file system
  31. // This should probably have its own class
  32. if (!SPIFFS.begin()) {
  33. Serial.println("SPIFFS initialisation failed!");
  34. while (1) yield(); // Stay here twiddling thumbs waiting
  35. }
  36. //Serial.println("\r\nInitialisation done.");
  37. //listFiles(); // Lists the files so you can see what is in the SPIFFS
  38. // Draw the title screen
  39. drawJpeg("/marauder3L.jpg", 0 , 0); // 240 x 320 image
  40. //showCenterText(version_number, 250);
  41. tft.drawCentreString(version_number, 120, 250, 2);
  42. digitalWrite(TFT_BL, HIGH);
  43. delay(5000);
  44. }
  45. // Function to just draw the screen black
  46. void Display::clearScreen()
  47. {
  48. Serial.println("clearScreen()");
  49. tft.fillScreen(TFT_BLACK);
  50. tft.setCursor(0, 0);
  51. }
  52. void Display::displayBuffer(bool do_clear)
  53. {
  54. if (this->display_buffer->size() > 0)
  55. {
  56. delay(1);
  57. while (display_buffer->size() > 0)
  58. {
  59. xPos = 0;
  60. //yDraw = scroll_line(TFT_RED);
  61. //tft.setCursor(xPos, yDraw);
  62. //tft.print(display_buffer->shift());
  63. //tft.print(display_buffer->get(0));
  64. if ((display_buffer->size() > 0) && (!loading))
  65. {
  66. //tft.print(display_buffer->pop());
  67. printing = true;
  68. delay(print_delay_1);
  69. yDraw = scroll_line(TFT_RED);
  70. tft.setCursor(xPos, yDraw);
  71. tft.print(display_buffer->shift());
  72. printing = false;
  73. delay(print_delay_2);
  74. //display_buffer->remove(0);
  75. }
  76. blank[(18+(yStart - TOP_FIXED_AREA) / TEXT_HEIGHT)%19] = xPos;
  77. }
  78. /*
  79. while (display_buffer->get(0) != "")
  80. {
  81. Serial.println("Printing");
  82. delay(10);
  83. xPos = 0;
  84. yDraw = scroll_line(TFT_RED);
  85. tft.setCursor(xPos, yDraw);
  86. if (display_buffer->get(0) != "")
  87. {
  88. tft.print(display_buffer->get(0));
  89. display_buffer->replace(0, "");
  90. }
  91. blank[(18+(yStart - TOP_FIXED_AREA) / TEXT_HEIGHT)%19] = xPos;
  92. }
  93. */
  94. }
  95. }
  96. void Display::showCenterText(String text, int y)
  97. {
  98. tft.setCursor((SCREEN_WIDTH - (text.length() * 6)) / 2, y);
  99. tft.println(text);
  100. }
  101. void Display::initScrollValues()
  102. {
  103. Serial.println("initScrollValues()");
  104. yDraw = YMAX - BOT_FIXED_AREA - TEXT_HEIGHT;
  105. xPos = 0;
  106. yStart = TOP_FIXED_AREA;
  107. yArea = YMAX - TOP_FIXED_AREA - BOT_FIXED_AREA;
  108. for(int i = 0; i < 18; i++) blank[i] = 0;
  109. }
  110. // Function to execute hardware scroll for TFT screen
  111. int Display::scroll_line(uint32_t color) {
  112. //Serial.println("scroll_line()");
  113. int yTemp = yStart; // Store the old yStart, this is where we draw the next line
  114. // Use the record of line lengths to optimise the rectangle size we need to erase the top line
  115. tft.fillRect(0,yStart,blank[(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT],TEXT_HEIGHT, color);
  116. // Change the top of the scroll area
  117. yStart+=TEXT_HEIGHT;
  118. // The value must wrap around as the screen memory is a circular buffer
  119. if (yStart >= YMAX - BOT_FIXED_AREA) yStart = TOP_FIXED_AREA + (yStart - YMAX + BOT_FIXED_AREA);
  120. // Now we can scroll the display
  121. scrollAddress(yStart);
  122. return yTemp;
  123. }
  124. // Function to setup hardware scroll for TFT screen
  125. void Display::setupScrollArea(uint16_t tfa, uint16_t bfa) {
  126. Serial.println("setupScrollAread()");
  127. tft.writecommand(ILI9341_VSCRDEF); // Vertical scroll definition
  128. tft.writedata(tfa >> 8); // Top Fixed Area line count
  129. tft.writedata(tfa);
  130. tft.writedata((YMAX-tfa-bfa)>>8); // Vertical Scrolling Area line count
  131. tft.writedata(YMAX-tfa-bfa);
  132. tft.writedata(bfa >> 8); // Bottom Fixed Area line count
  133. tft.writedata(bfa);
  134. }
  135. void Display::scrollAddress(uint16_t vsp) {
  136. tft.writecommand(ILI9341_VSCRSADD); // Vertical scrolling pointer
  137. tft.writedata(vsp>>8);
  138. tft.writedata(vsp);
  139. }
  140. // JPEG_functions
  141. void Display::drawJpeg(const char *filename, int xpos, int ypos) {
  142. //Serial.println("===========================");
  143. //Serial.print("Drawing file: "); Serial.println(filename);
  144. //Serial.println("===========================");
  145. // Open the named file (the Jpeg decoder library will close it after rendering image)
  146. fs::File jpegFile = SPIFFS.open( filename, "r"); // File handle reference for SPIFFS
  147. // File jpegFile = SD.open( filename, FILE_READ); // or, file handle reference for SD library
  148. //ESP32 always seems to return 1 for jpegFile so this null trap does not work
  149. if ( !jpegFile ) {
  150. Serial.print("ERROR: File \""); Serial.print(filename); Serial.println ("\" not found!");
  151. return;
  152. }
  153. // Use one of the three following methods to initialise the decoder,
  154. // the filename can be a String or character array type:
  155. //boolean decoded = JpegDec.decodeFsFile(jpegFile); // Pass a SPIFFS file handle to the decoder,
  156. //boolean decoded = JpegDec.decodeSdFile(jpegFile); // or pass the SD file handle to the decoder,
  157. boolean decoded = JpegDec.decodeFsFile(filename); // or pass the filename (leading / distinguishes SPIFFS files)
  158. if (decoded) {
  159. // print information about the image to the serial port
  160. jpegInfo();
  161. // render the image onto the screen at given coordinates
  162. jpegRender(xpos, ypos);
  163. }
  164. else {
  165. Serial.println("Jpeg file format not supported!");
  166. }
  167. }
  168. //====================================================================================
  169. // Decode and render the Jpeg image onto the TFT screen
  170. //====================================================================================
  171. void Display::jpegRender(int xpos, int ypos) {
  172. // retrieve infomration about the image
  173. uint16_t *pImg;
  174. int16_t mcu_w = JpegDec.MCUWidth;
  175. int16_t mcu_h = JpegDec.MCUHeight;
  176. int32_t max_x = JpegDec.width;
  177. int32_t max_y = JpegDec.height;
  178. // Jpeg images are draw as a set of image block (tiles) called Minimum Coding Units (MCUs)
  179. // Typically these MCUs are 16x16 pixel blocks
  180. // Determine the width and height of the right and bottom edge image blocks
  181. int32_t min_w = minimum(mcu_w, max_x % mcu_w);
  182. int32_t min_h = minimum(mcu_h, max_y % mcu_h);
  183. // save the current image block size
  184. int32_t win_w = mcu_w;
  185. int32_t win_h = mcu_h;
  186. // record the current time so we can measure how long it takes to draw an image
  187. uint32_t drawTime = millis();
  188. // save the coordinate of the right and bottom edges to assist image cropping
  189. // to the screen size
  190. max_x += xpos;
  191. max_y += ypos;
  192. // read each MCU block until there are no more
  193. while ( JpegDec.readSwappedBytes()) { // Swapped byte order read
  194. // save a pointer to the image block
  195. pImg = JpegDec.pImage;
  196. // calculate where the image block should be drawn on the screen
  197. int mcu_x = JpegDec.MCUx * mcu_w + xpos; // Calculate coordinates of top left corner of current MCU
  198. int mcu_y = JpegDec.MCUy * mcu_h + ypos;
  199. // check if the image block size needs to be changed for the right edge
  200. if (mcu_x + mcu_w <= max_x) win_w = mcu_w;
  201. else win_w = min_w;
  202. // check if the image block size needs to be changed for the bottom edge
  203. if (mcu_y + mcu_h <= max_y) win_h = mcu_h;
  204. else win_h = min_h;
  205. // copy pixels into a contiguous block
  206. if (win_w != mcu_w)
  207. {
  208. for (int h = 1; h < win_h-1; h++)
  209. {
  210. memcpy(pImg + h * win_w, pImg + (h + 1) * mcu_w, win_w << 1);
  211. }
  212. }
  213. // draw image MCU block only if it will fit on the screen
  214. if ( mcu_x < tft.width() && mcu_y < tft.height())
  215. {
  216. // Now push the image block to the screen
  217. tft.pushImage(mcu_x, mcu_y, win_w, win_h, pImg);
  218. }
  219. else if ( ( mcu_y + win_h) >= tft.height()) JpegDec.abort();
  220. }
  221. // calculate how long it took to draw the image
  222. drawTime = millis() - drawTime; // Calculate the time it took
  223. // print the results to the serial port
  224. //Serial.print ("Total render time was : "); Serial.print(drawTime); Serial.println(" ms");
  225. //Serial.println("=====================================");
  226. }
  227. //====================================================================================
  228. // Print information decoded from the Jpeg image
  229. //====================================================================================
  230. void Display::jpegInfo() {
  231. /*
  232. Serial.println("===============");
  233. Serial.println("JPEG image info");
  234. Serial.println("===============");
  235. Serial.print ("Width :"); Serial.println(JpegDec.width);
  236. Serial.print ("Height :"); Serial.println(JpegDec.height);
  237. Serial.print ("Components :"); Serial.println(JpegDec.comps);
  238. Serial.print ("MCU / row :"); Serial.println(JpegDec.MCUSPerRow);
  239. Serial.print ("MCU / col :"); Serial.println(JpegDec.MCUSPerCol);
  240. Serial.print ("Scan type :"); Serial.println(JpegDec.scanType);
  241. Serial.print ("MCU width :"); Serial.println(JpegDec.MCUWidth);
  242. Serial.print ("MCU height :"); Serial.println(JpegDec.MCUHeight);
  243. Serial.println("===============");
  244. Serial.println("");
  245. */
  246. }
  247. //====================================================================================
  248. // Open a Jpeg file and send it to the Serial port in a C array compatible format
  249. //====================================================================================
  250. void createArray(const char *filename) {
  251. // Open the named file
  252. fs::File jpgFile = SPIFFS.open( filename, "r"); // File handle reference for SPIFFS
  253. // File jpgFile = SD.open( filename, FILE_READ); // or, file handle reference for SD library
  254. if ( !jpgFile ) {
  255. Serial.print("ERROR: File \""); Serial.print(filename); Serial.println ("\" not found!");
  256. return;
  257. }
  258. uint8_t data;
  259. byte line_len = 0;
  260. Serial.println("");
  261. Serial.println("// Generated by a JPEGDecoder library example sketch:");
  262. Serial.println("// https://github.com/Bodmer/JPEGDecoder");
  263. Serial.println("");
  264. Serial.println("#if defined(__AVR__)");
  265. Serial.println(" #include <avr/pgmspace.h>");
  266. Serial.println("#endif");
  267. Serial.println("");
  268. Serial.print ("const uint8_t ");
  269. while (*filename != '.') Serial.print(*filename++);
  270. Serial.println("[] PROGMEM = {"); // PROGMEM added for AVR processors, it is ignored by Due
  271. while ( jpgFile.available()) {
  272. data = jpgFile.read();
  273. Serial.print("0x"); if (abs(data) < 16) Serial.print("0");
  274. Serial.print(data, HEX); Serial.print(",");// Add value and comma
  275. line_len++;
  276. if ( line_len >= 32) {
  277. line_len = 0;
  278. Serial.println();
  279. }
  280. }
  281. Serial.println("};\r\n");
  282. jpgFile.close();
  283. }
  284. // End JPEG_functions
  285. // SPIFFS_functions
  286. #ifdef ESP8266
  287. void Display::listFiles(void) {
  288. Serial.println();
  289. Serial.println("SPIFFS files found:");
  290. fs::Dir dir = SPIFFS.openDir("/"); // Root directory
  291. String line = "=====================================";
  292. Serial.println(line);
  293. Serial.println(" File name Size");
  294. Serial.println(line);
  295. while (dir.next()) {
  296. String fileName = dir.fileName();
  297. Serial.print(fileName);
  298. int spaces = 21 - fileName.length(); // Tabulate nicely
  299. while (spaces--) Serial.print(" ");
  300. fs::File f = dir.openFile("r");
  301. String fileSize = (String) f.size();
  302. spaces = 10 - fileSize.length(); // Tabulate nicely
  303. while (spaces--) Serial.print(" ");
  304. Serial.println(fileSize + " bytes");
  305. }
  306. Serial.println(line);
  307. Serial.println();
  308. delay(1000);
  309. }
  310. #endif
  311. //====================================================================================
  312. #ifdef ESP32
  313. void Display::listFiles(void) {
  314. listDir(SPIFFS, "/", 0);
  315. }
  316. void Display::listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  317. Serial.println();
  318. Serial.println("SPIFFS files found:");
  319. Serial.printf("Listing directory: %s\n", "/");
  320. String line = "=====================================";
  321. Serial.println(line);
  322. Serial.println(" File name Size");
  323. Serial.println(line);
  324. fs::File root = fs.open(dirname);
  325. if (!root) {
  326. Serial.println("Failed to open directory");
  327. return;
  328. }
  329. if (!root.isDirectory()) {
  330. Serial.println("Not a directory");
  331. return;
  332. }
  333. fs::File file = root.openNextFile();
  334. while (file) {
  335. if (file.isDirectory()) {
  336. Serial.print("DIR : ");
  337. String fileName = file.name();
  338. Serial.print(fileName);
  339. if (levels) {
  340. listDir(fs, file.name(), levels - 1);
  341. }
  342. } else {
  343. String fileName = file.name();
  344. Serial.print(" " + fileName);
  345. int spaces = 20 - fileName.length(); // Tabulate nicely
  346. while (spaces--) Serial.print(" ");
  347. String fileSize = (String) file.size();
  348. spaces = 10 - fileSize.length(); // Tabulate nicely
  349. while (spaces--) Serial.print(" ");
  350. Serial.println(fileSize + " bytes");
  351. }
  352. file = root.openNextFile();
  353. }
  354. Serial.println(line);
  355. Serial.println();
  356. delay(1000);
  357. }
  358. #endif
  359. // End SPIFFS_functions