Display.cpp 12 KB

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