Display.cpp 13 KB

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