Display.cpp 13 KB

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