Display.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. #include "Display.h"
  2. #include "lang_var.h"
  3. #ifdef HAS_SCREEN
  4. Display::Display()
  5. {
  6. }
  7. // Function to prepare the display and the menus
  8. void Display::RunSetup()
  9. {
  10. run_setup = false;
  11. // Need to declare new
  12. display_buffer = new LinkedList<String>();
  13. #ifdef SCREEN_BUFFER
  14. screen_buffer = new LinkedList<String>();
  15. #endif
  16. tft.init();
  17. #ifndef MARAUDER_M5STICKC
  18. tft.setRotation(0); // Portrait
  19. #endif
  20. #ifdef MARAUDER_M5STICKC
  21. tft.setRotation(3);
  22. #endif
  23. tft.setCursor(0, 0);
  24. #ifdef HAS_ILI9341
  25. #ifdef TFT_SHIELD
  26. uint16_t calData[5] = { 275, 3494, 361, 3528, 4 }; // tft.setRotation(0); // Portrait with TFT Shield
  27. //Serial.println(F("Using TFT Shield"));
  28. #else if defined(TFT_DIY)
  29. uint16_t calData[5] = { 339, 3470, 237, 3438, 2 }; // tft.setRotation(0); // Portrait with DIY TFT
  30. //Serial.println(F("Using TFT DIY"));
  31. #endif
  32. tft.setTouch(calData);
  33. #endif
  34. //tft.fillScreen(TFT_BLACK);
  35. clearScreen();
  36. //Serial.println("SPI_FREQUENCY: " + (String)SPI_FREQUENCY);
  37. //Serial.println("SPI_READ_FREQUENCY:" + (String)SPI_READ_FREQUENCY);
  38. //Serial.println("SPI_TOUCH_FREQUENCY: " + (String)SPI_TOUCH_FREQUENCY);
  39. #ifdef KIT
  40. pinMode(KIT_LED_BUILTIN, OUTPUT);
  41. #endif
  42. }
  43. void Display::drawFrame()
  44. {
  45. tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, TFT_BLACK);
  46. }
  47. void Display::tftDrawRedOnOffButton() {
  48. tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_RED);
  49. tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_DARKGREY);
  50. drawFrame();
  51. tft.setTextColor(TFT_WHITE);
  52. tft.setTextSize(2);
  53. tft.setTextDatum(MC_DATUM);
  54. tft.drawString(text03, GREENBUTTON_X + (GREENBUTTON_W / 2), GREENBUTTON_Y + (GREENBUTTON_H / 2));
  55. this->SwitchOn = false;
  56. }
  57. void Display::tftDrawGreenOnOffButton() {
  58. tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_GREEN);
  59. tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_DARKGREY);
  60. drawFrame();
  61. tft.setTextColor(TFT_WHITE);
  62. tft.setTextSize(2);
  63. tft.setTextDatum(MC_DATUM);
  64. tft.drawString(text04, REDBUTTON_X + (REDBUTTON_W / 2) + 1, REDBUTTON_Y + (REDBUTTON_H / 2));
  65. this->SwitchOn = true;
  66. }
  67. void Display::tftDrawGraphObjects(byte x_scale)
  68. {
  69. //draw the graph objects
  70. tft.fillRect(11, 5, x_scale+1, 120, TFT_BLACK); // positive start point
  71. tft.fillRect(11, 121, x_scale+1, 119, TFT_BLACK); // negative start point
  72. tft.drawFastVLine(10, 5, 230, TFT_WHITE); // y axis
  73. tft.drawFastHLine(10, HEIGHT_1 - 1, 310, TFT_WHITE); // x axis
  74. tft.setTextColor(TFT_YELLOW); tft.setTextSize(1); // set parameters for y axis labels
  75. //tft.setCursor(3, 116); tft.print(midway); // "0" at center of ya axis
  76. tft.setCursor(3, 6); tft.print("+"); // "+' at top of y axis
  77. tft.setCursor(3, 228); tft.print("0"); // "-" at bottom of y axis
  78. }
  79. void Display::tftDrawEapolColorKey()
  80. {
  81. //Display color key
  82. tft.setTextSize(1); tft.setTextColor(TFT_WHITE);
  83. tft.fillRect(14, 0, 15, 8, TFT_CYAN); tft.setCursor(30, 0); tft.print(" - EAPOL");
  84. }
  85. void Display::tftDrawColorKey()
  86. {
  87. //Display color key
  88. tft.setTextSize(1); tft.setTextColor(TFT_WHITE);
  89. tft.fillRect(14, 0, 15, 8, TFT_GREEN); tft.setCursor(30, 0); tft.print(" - Beacons");
  90. tft.fillRect(14, 8, 15, 8, TFT_RED); tft.setCursor(30, 8); tft.print(" - Deauths");
  91. tft.fillRect(14, 16, 15, 8, TFT_BLUE); tft.setCursor(30, 16); tft.print(" - Probes");
  92. }
  93. void Display::tftDrawXScaleButtons(byte x_scale)
  94. {
  95. tft.drawFastVLine(234, 0, 20, TFT_WHITE);
  96. tft.setCursor(208, 21); tft.setTextColor(TFT_WHITE); tft.setTextSize(1); tft.print("X Scale:"); tft.print(x_scale);
  97. key[0].initButton(&tft, // x - box
  98. 220,
  99. 10, // x, y, w, h, outline, fill, text
  100. 20,
  101. 20,
  102. TFT_BLACK, // Outline
  103. TFT_CYAN, // Fill
  104. TFT_BLACK, // Text
  105. "-",
  106. 2);
  107. key[1].initButton(&tft, // x + box
  108. 249,
  109. 10, // x, y, w, h, outline, fill, text
  110. 20,
  111. 20,
  112. TFT_BLACK, // Outline
  113. TFT_CYAN, // Fill
  114. TFT_BLACK, // Text
  115. "+",
  116. 2);
  117. key[0].setLabelDatum(1, 5, MC_DATUM);
  118. key[1].setLabelDatum(1, 5, MC_DATUM);
  119. key[0].drawButton();
  120. key[1].drawButton();
  121. }
  122. void Display::tftDrawYScaleButtons(byte y_scale)
  123. {
  124. tft.drawFastVLine(290, 0, 20, TFT_WHITE);
  125. tft.setCursor(265, 21); tft.setTextColor(TFT_WHITE); tft.setTextSize(1); tft.print("Y Scale:"); tft.print(y_scale);
  126. key[2].initButton(&tft, // y - box
  127. 276,
  128. 10, // x, y, w, h, outline, fill, text
  129. 20,
  130. 20,
  131. TFT_BLACK, // Outline
  132. TFT_MAGENTA, // Fill
  133. TFT_BLACK, // Text
  134. "-",
  135. 2);
  136. key[3].initButton(&tft, // y + box
  137. 305,
  138. 10, // x, y, w, h, outline, fill, text
  139. 20,
  140. 20,
  141. TFT_BLACK, // Outline
  142. TFT_MAGENTA, // Fill
  143. TFT_BLACK, // Text
  144. "+",
  145. 2);
  146. key[2].setLabelDatum(1, 5, MC_DATUM);
  147. key[3].setLabelDatum(1, 5, MC_DATUM);
  148. key[2].drawButton();
  149. key[3].drawButton();
  150. }
  151. void Display::tftDrawChannelScaleButtons(int set_channel)
  152. {
  153. tft.drawFastVLine(178, 0, 20, TFT_WHITE);
  154. tft.setCursor(145, 21); tft.setTextColor(TFT_WHITE); tft.setTextSize(1); tft.print(text10); tft.print(set_channel);
  155. key[4].initButton(&tft, // channel - box
  156. 164,
  157. 10, // x, y, w, h, outline, fill, text
  158. 20,
  159. 20,
  160. TFT_BLACK, // Outline
  161. TFT_BLUE, // Fill
  162. TFT_BLACK, // Text
  163. "-",
  164. 2);
  165. key[5].initButton(&tft, // channel + box
  166. 193,
  167. 10, // x, y, w, h, outline, fill, text
  168. 20,
  169. 20,
  170. TFT_BLACK, // Outline
  171. TFT_BLUE, // Fill
  172. TFT_BLACK, // Text
  173. "+",
  174. 2);
  175. key[4].setLabelDatum(1, 5, MC_DATUM);
  176. key[5].setLabelDatum(1, 5, MC_DATUM);
  177. key[4].drawButton();
  178. key[5].drawButton();
  179. }
  180. void Display::tftDrawExitScaleButtons()
  181. {
  182. //tft.drawFastVLine(178, 0, 20, TFT_WHITE);
  183. //tft.setCursor(145, 21); tft.setTextColor(TFT_WHITE); tft.setTextSize(1); tft.print("Channel:"); tft.print(set_channel);
  184. key[6].initButton(&tft, // Exit box
  185. 137,
  186. 10, // x, y, w, h, outline, fill, text
  187. 20,
  188. 20,
  189. TFT_ORANGE, // Outline
  190. TFT_RED, // Fill
  191. TFT_BLACK, // Text
  192. "X",
  193. 2);
  194. key[6].setLabelDatum(1, 5, MC_DATUM);
  195. key[6].drawButton();
  196. }
  197. void Display::twoPartDisplay(String center_text)
  198. {
  199. tft.setTextColor(TFT_BLACK, TFT_YELLOW);
  200. tft.fillRect(0,16,HEIGHT_1,144, TFT_YELLOW);
  201. //tft.drawCentreString(center_text,120,82,1);
  202. tft.setTextWrap(true);
  203. tft.setFreeFont(NULL);
  204. //showCenterText(center_text, 82);
  205. //tft.drawCentreString(center_text,120,82,1);
  206. tft.setCursor(0, 82);
  207. tft.println(center_text);
  208. tft.setFreeFont(MENU_FONT);
  209. tft.setTextWrap(false);
  210. }
  211. void Display::touchToExit()
  212. {
  213. tft.setTextColor(TFT_BLACK, TFT_LIGHTGREY);
  214. tft.fillRect(0,32,HEIGHT_1,16, TFT_LIGHTGREY);
  215. tft.drawCentreString(text11,120,32,2);
  216. }
  217. // Function to just draw the screen black
  218. void Display::clearScreen()
  219. {
  220. //Serial.println(F("clearScreen()"));
  221. tft.fillScreen(TFT_BLACK);
  222. tft.setCursor(0, 0);
  223. }
  224. #ifdef SCREEN_BUFFER
  225. void Display::scrollScreenBuffer(bool down) {
  226. // Scroll screen normal direction (Up)
  227. if (!down) {
  228. this->screen_buffer->shift();
  229. }
  230. }
  231. #endif
  232. void Display::displayBuffer(bool do_clear)
  233. {
  234. if (this->display_buffer->size() > 0)
  235. {
  236. delay(1);
  237. while (display_buffer->size() > 0)
  238. {
  239. #ifndef SCREEN_BUFFER
  240. xPos = 0;
  241. if ((display_buffer->size() > 0) && (!loading))
  242. {
  243. printing = true;
  244. delay(print_delay_1);
  245. yDraw = scroll_line(TFT_RED);
  246. tft.setCursor(xPos, yDraw);
  247. tft.setTextColor(TFT_GREEN, TFT_BLACK);
  248. tft.print(display_buffer->shift());
  249. printing = false;
  250. delay(print_delay_2);
  251. }
  252. if (!tteBar)
  253. blank[(18+(yStart - TOP_FIXED_AREA) / TEXT_HEIGHT)%19] = xPos;
  254. else
  255. blank[(18+(yStart - TOP_FIXED_AREA_2) / TEXT_HEIGHT)%19] = xPos;
  256. #else
  257. xPos = 0;
  258. if (this->screen_buffer->size() >= MAX_SCREEN_BUFFER)
  259. this->scrollScreenBuffer();
  260. screen_buffer->add(display_buffer->shift());
  261. for (int i = 0; i < this->screen_buffer->size(); i++) {
  262. tft.setCursor(xPos, (i * 12) + (SCREEN_HEIGHT / 6));
  263. for (int x = 0; x < TFT_WIDTH / CHAR_WIDTH; x++)
  264. tft.print(" ");
  265. tft.setCursor(xPos, (i * 12) + (SCREEN_HEIGHT / 6));
  266. tft.setTextColor(TFT_GREEN, TFT_BLACK);
  267. tft.print(this->screen_buffer->get(i));
  268. }
  269. #endif
  270. }
  271. }
  272. }
  273. void Display::showCenterText(String text, int y)
  274. {
  275. tft.setCursor((SCREEN_WIDTH - (text.length() * (6 * BANNER_TEXT_SIZE))) / 2, y);
  276. tft.println(text);
  277. }
  278. void Display::initScrollValues(bool tte)
  279. {
  280. //Serial.println(F("initScrollValues()"));
  281. yDraw = YMAX - BOT_FIXED_AREA - TEXT_HEIGHT;
  282. xPos = 0;
  283. if (!tte)
  284. {
  285. yStart = TOP_FIXED_AREA;
  286. yArea = YMAX - TOP_FIXED_AREA - BOT_FIXED_AREA;
  287. }
  288. else
  289. {
  290. yStart = TOP_FIXED_AREA_2;
  291. yArea = YMAX - TOP_FIXED_AREA_2 - BOT_FIXED_AREA;
  292. }
  293. for(uint8_t i = 0; i < 18; i++) blank[i] = 0;
  294. }
  295. // Function to execute hardware scroll for TFT screen
  296. int Display::scroll_line(uint32_t color) {
  297. //Serial.println("scroll_line()");
  298. int yTemp = yStart; // Store the old yStart, this is where we draw the next line
  299. // Use the record of line lengths to optimise the rectangle size we need to erase the top line
  300. // Check if we have the "touch to exit bar"
  301. if (!tteBar)
  302. {
  303. tft.fillRect(0,yStart,blank[(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT],TEXT_HEIGHT, color);
  304. // Change the top of the scroll area
  305. yStart+=TEXT_HEIGHT;
  306. // The value must wrap around as the screen memory is a circular buffer
  307. if (yStart >= YMAX - BOT_FIXED_AREA) yStart = TOP_FIXED_AREA + (yStart - YMAX + BOT_FIXED_AREA);
  308. }
  309. else
  310. {
  311. tft.fillRect(0,yStart,blank[(yStart-TOP_FIXED_AREA_2)/TEXT_HEIGHT],TEXT_HEIGHT, color);
  312. // Change the top of the scroll area
  313. yStart+=TEXT_HEIGHT;
  314. // The value must wrap around as the screen memory is a circular buffer
  315. if (yStart >= YMAX - BOT_FIXED_AREA) yStart = TOP_FIXED_AREA_2 + (yStart - YMAX + BOT_FIXED_AREA);
  316. }
  317. // Now we can scroll the display
  318. scrollAddress(yStart);
  319. return yTemp;
  320. }
  321. // Function to setup hardware scroll for TFT screen
  322. void Display::setupScrollArea(uint16_t tfa, uint16_t bfa) {
  323. //Serial.println(F("setupScrollArea()"));
  324. //Serial.println(" tfa: " + (String)tfa);
  325. //Serial.println(" bfa: " + (String)bfa);
  326. //Serial.println("yStart: " + (String)this->yStart);
  327. #ifdef HAS_ILI9341
  328. tft.writecommand(ILI9341_VSCRDEF); // Vertical scroll definition
  329. tft.writedata(tfa >> 8); // Top Fixed Area line count
  330. tft.writedata(tfa);
  331. tft.writedata((YMAX-tfa-bfa)>>8); // Vertical Scrolling Area line count
  332. tft.writedata(YMAX-tfa-bfa);
  333. tft.writedata(bfa >> 8); // Bottom Fixed Area line count
  334. tft.writedata(bfa);
  335. #endif
  336. }
  337. void Display::scrollAddress(uint16_t vsp) {
  338. #ifdef HAS_ILI9341
  339. tft.writecommand(ILI9341_VSCRSADD); // Vertical scrolling pointer
  340. tft.writedata(vsp>>8);
  341. tft.writedata(vsp);
  342. #endif
  343. }
  344. // JPEG_functions
  345. void Display::drawJpeg(const char *filename, int xpos, int ypos) {
  346. // Open the named file (the Jpeg decoder library will close it after rendering image)
  347. //fs::File jpegFile = SPIFFS.open( filename, "r"); // File handle reference for SPIFFS
  348. //jpegFile.close();
  349. //ESP32 always seems to return 1 for jpegFile so this null trap does not work
  350. //if ( !jpegFile ) {
  351. // Serial.print("ERROR: File \""); Serial.print(filename); Serial.println ("\" not found!");
  352. // return;
  353. //}
  354. // Use one of the three following methods to initialise the decoder,
  355. // the filename can be a String or character array type:
  356. //boolean decoded = JpegDec.decodeFsFile(filename); // or pass the filename (leading / distinguishes SPIFFS files)
  357. boolean decoded = JpegDec.decodeArray(MarauderTitle, MARAUDER_TITLE_BYTES);
  358. if (decoded) {
  359. // print information about the image to the serial port
  360. jpegInfo();
  361. // render the image onto the screen at given coordinates
  362. jpegRender(xpos, ypos);
  363. }
  364. //else {
  365. // Serial.println(F("Jpeg file format not supported!"));
  366. //}
  367. }
  368. /*void Display::setupDraw() {
  369. this->tft.drawLine(0, 0, 10, 0, TFT_MAGENTA);
  370. this->tft.drawLine(0, 0, 0, 10, TFT_GREEN);
  371. this->tft.drawLine(0, 0, 0, 0, TFT_CYAN);
  372. }*/
  373. /*uint16_t xlast;
  374. uint16_t ylast;
  375. uint32_t AH;
  376. void Display::drawStylus()
  377. {
  378. uint16_t x = 0, y = 0; // To store the touch coordinates
  379. // Pressed will be set true is there is a valid touch on the screen
  380. boolean pressed = tft.getTouch(&x, &y);
  381. if ((x <= 10) && (y <= 10) && (pressed)) {
  382. //Serial.println(F("Exit draw function"));
  383. this->draw_tft = false;
  384. this->exit_draw = true;
  385. return;
  386. }
  387. // Draw a white spot at the detected coordinates
  388. if (pressed) {
  389. // tft.fillCircle(x, y, 2, TFT_WHITE);
  390. if ( xlast > 0 && ylast > 0 ) {
  391. uint16_t the_color = TFT_WHITE;
  392. uint16_t wd = 1;
  393. int xlast2;
  394. int ylast2;
  395. int x2;
  396. int y2;
  397. int n;
  398. int n2 = -wd;
  399. xlast2 = xlast - wd;
  400. x2 = x - wd;
  401. for (n = -wd; n <= wd; n++) {
  402. ylast2 = ylast + n;
  403. y2 = y + n;
  404. tft.drawLine(xlast2, ylast2, x2, y2, the_color);
  405. }
  406. for (n2 = -wd; n2 <= wd; n2++) {
  407. xlast2 = xlast + n2;
  408. x2 = x + n2;
  409. tft.drawLine(xlast2, ylast2, x2, y2, the_color);
  410. }
  411. for (n = wd; n >= -wd; n--) {
  412. ylast2 = ylast + n;
  413. y2 = y + n;
  414. tft.drawLine(xlast2, ylast2, x2, y2, the_color);
  415. }
  416. for (n2 = wd; n2 >= -wd; n2--) {
  417. xlast2 = xlast + n2;
  418. x2 = x + n2;
  419. tft.drawLine(xlast2, ylast2, x2, y2, the_color);
  420. }
  421. // tft.drawLine(xlast, ylast, x, y, TFT_WHITE);
  422. }
  423. xlast = x;
  424. ylast = y;
  425. AH = 0;
  426. //Serial.print("x,y = ");
  427. //Serial.print(x);
  428. //Serial.print(",");
  429. //Serial.println(y);
  430. } else if ( AH < 5 ) {
  431. AH++;
  432. } else if ( AH == 5 ) {
  433. xlast = 0;
  434. ylast = 0;
  435. }
  436. }*/
  437. //====================================================================================
  438. // Decode and render the Jpeg image onto the TFT screen
  439. //====================================================================================
  440. void Display::jpegRender(int xpos, int ypos) {
  441. // retrieve infomration about the image
  442. uint16_t *pImg;
  443. int16_t mcu_w = JpegDec.MCUWidth;
  444. int16_t mcu_h = JpegDec.MCUHeight;
  445. int32_t max_x = JpegDec.width;
  446. int32_t max_y = JpegDec.height;
  447. // Jpeg images are draw as a set of image block (tiles) called Minimum Coding Units (MCUs)
  448. // Typically these MCUs are 16x16 pixel blocks
  449. // Determine the width and height of the right and bottom edge image blocks
  450. int32_t min_w = minimum(mcu_w, max_x % mcu_w);
  451. int32_t min_h = minimum(mcu_h, max_y % mcu_h);
  452. // save the current image block size
  453. int32_t win_w = mcu_w;
  454. int32_t win_h = mcu_h;
  455. // record the current time so we can measure how long it takes to draw an image
  456. uint32_t drawTime = millis();
  457. // save the coordinate of the right and bottom edges to assist image cropping
  458. // to the screen size
  459. max_x += xpos;
  460. max_y += ypos;
  461. // read each MCU block until there are no more
  462. while ( JpegDec.readSwappedBytes()) { // Swapped byte order read
  463. // save a pointer to the image block
  464. pImg = JpegDec.pImage;
  465. // calculate where the image block should be drawn on the screen
  466. int mcu_x = JpegDec.MCUx * mcu_w + xpos; // Calculate coordinates of top left corner of current MCU
  467. int mcu_y = JpegDec.MCUy * mcu_h + ypos;
  468. // check if the image block size needs to be changed for the right edge
  469. if (mcu_x + mcu_w <= max_x) win_w = mcu_w;
  470. else win_w = min_w;
  471. // check if the image block size needs to be changed for the bottom edge
  472. if (mcu_y + mcu_h <= max_y) win_h = mcu_h;
  473. else win_h = min_h;
  474. // copy pixels into a contiguous block
  475. if (win_w != mcu_w)
  476. {
  477. for (int h = 1; h < win_h-1; h++)
  478. {
  479. memcpy(pImg + h * win_w, pImg + (h + 1) * mcu_w, win_w << 1);
  480. }
  481. }
  482. // draw image MCU block only if it will fit on the screen
  483. if ( mcu_x < tft.width() && mcu_y < tft.height())
  484. {
  485. // Now push the image block to the screen
  486. tft.pushImage(mcu_x, mcu_y, win_w, win_h, pImg);
  487. }
  488. else if ( ( mcu_y + win_h) >= tft.height()) JpegDec.abort();
  489. }
  490. // calculate how long it took to draw the image
  491. drawTime = millis() - drawTime; // Calculate the time it took
  492. }
  493. //====================================================================================
  494. // Print information decoded from the Jpeg image
  495. //====================================================================================
  496. void Display::jpegInfo() {
  497. /*
  498. Serial.println("===============");
  499. Serial.println("JPEG image info");
  500. Serial.println("===============");
  501. Serial.print ("Width :"); Serial.println(JpegDec.width);
  502. Serial.print ("Height :"); Serial.println(JpegDec.height);
  503. Serial.print ("Components :"); Serial.println(JpegDec.comps);
  504. Serial.print ("MCU / row :"); Serial.println(JpegDec.MCUSPerRow);
  505. Serial.print ("MCU / col :"); Serial.println(JpegDec.MCUSPerCol);
  506. Serial.print ("Scan type :"); Serial.println(JpegDec.scanType);
  507. Serial.print ("MCU width :"); Serial.println(JpegDec.MCUWidth);
  508. Serial.print ("MCU height :"); Serial.println(JpegDec.MCUHeight);
  509. Serial.println("===============");
  510. Serial.println("");
  511. */
  512. }
  513. //====================================================================================
  514. // Open a Jpeg file and send it to the Serial port in a C array compatible format
  515. //====================================================================================
  516. void createArray(const char *filename) {
  517. // Open the named file
  518. fs::File jpgFile = SPIFFS.open( filename, "r"); // File handle reference for SPIFFS
  519. // File jpgFile = SD.open( filename, FILE_READ); // or, file handle reference for SD library
  520. if ( !jpgFile ) {
  521. Serial.print("ERROR: File \""); Serial.print(filename); Serial.println ("\" not found!");
  522. return;
  523. }
  524. uint8_t data;
  525. byte line_len = 0;
  526. Serial.println("");
  527. Serial.println(F("// Generated by a JPEGDecoder library example sketch:"));
  528. Serial.println(F("// https://github.com/Bodmer/JPEGDecoder"));
  529. Serial.println("");
  530. Serial.println(F("#if defined(__AVR__)"));
  531. Serial.println(F(" #include <avr/pgmspace.h>"));
  532. Serial.println(F("#endif"));
  533. Serial.println("");
  534. Serial.print (F("const uint8_t "));
  535. while (*filename != '.') Serial.print(*filename++);
  536. Serial.println(F("[] PROGMEM = {")); // PROGMEM added for AVR processors, it is ignored by Due
  537. while ( jpgFile.available()) {
  538. data = jpgFile.read();
  539. Serial.print("0x"); if (abs(data) < 16) Serial.print("0");
  540. Serial.print(data, HEX); Serial.print(",");// Add value and comma
  541. line_len++;
  542. if ( line_len >= 32) {
  543. line_len = 0;
  544. Serial.println();
  545. }
  546. }
  547. Serial.println("};\r\n");
  548. jpgFile.close();
  549. }
  550. // End JPEG_functions
  551. // SPIFFS_functions
  552. #ifdef ESP8266
  553. void Display::listFiles(void) {
  554. Serial.println();
  555. Serial.println(F("SPIFFS files found:"));
  556. fs::Dir dir = SPIFFS.openDir("/"); // Root directory
  557. String line = "=====================================";
  558. Serial.println(line);
  559. Serial.println(F(" File name Size"));
  560. Serial.println(line);
  561. while (dir.next()) {
  562. String fileName = dir.fileName();
  563. Serial.print(fileName);
  564. int spaces = 21 - fileName.length(); // Tabulate nicely
  565. while (spaces--) Serial.print(" ");
  566. fs::File f = dir.openFile("r");
  567. String fileSize = (String) f.size();
  568. spaces = 10 - fileSize.length(); // Tabulate nicely
  569. while (spaces--) Serial.print(" ");
  570. Serial.println(fileSize + " bytes");
  571. }
  572. Serial.println(line);
  573. Serial.println();
  574. delay(1000);
  575. }
  576. #endif
  577. //====================================================================================
  578. #ifdef ESP32
  579. void Display::listFiles(void) {
  580. listDir(SPIFFS, "/", 0);
  581. }
  582. void Display::listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  583. Serial.println();
  584. Serial.println(F("SPIFFS files found:"));
  585. Serial.printf("Listing directory: %s\n", "/");
  586. String line = "=====================================";
  587. Serial.println(line);
  588. Serial.println(F(" File name Size"));
  589. Serial.println(line);
  590. fs::File root = fs.open(dirname);
  591. if (!root) {
  592. Serial.println(F("Failed to open directory"));
  593. return;
  594. }
  595. if (!root.isDirectory()) {
  596. Serial.println(F("Not a directory"));
  597. return;
  598. }
  599. fs::File file = root.openNextFile();
  600. while (file) {
  601. if (file.isDirectory()) {
  602. Serial.print("DIR : ");
  603. String fileName = file.name();
  604. Serial.print(fileName);
  605. if (levels) {
  606. listDir(fs, file.name(), levels - 1);
  607. }
  608. } else {
  609. String fileName = file.name();
  610. Serial.print(" " + fileName);
  611. int spaces = 20 - fileName.length(); // Tabulate nicely
  612. while (spaces--) Serial.print(" ");
  613. String fileSize = (String) file.size();
  614. spaces = 10 - fileSize.length(); // Tabulate nicely
  615. while (spaces--) Serial.print(" ");
  616. Serial.println(fileSize + " bytes");
  617. }
  618. file = root.openNextFile();
  619. }
  620. Serial.println(line);
  621. Serial.println();
  622. delay(1000);
  623. }
  624. #endif
  625. void Display::updateBanner(String msg)
  626. {
  627. //this->img.deleteSprite();
  628. //this->img.setColorDepth(8);
  629. //this->img.createSprite(SCREEN_WIDTH, TEXT_HEIGHT);
  630. this->buildBanner(msg, current_banner_pos);
  631. //this->img.pushSprite(0, STATUS_BAR_WIDTH);
  632. //current_banner_pos--;
  633. //if (current_banner_pos <= 0)
  634. // current_banner_pos = SCREEN_WIDTH + 2;
  635. }
  636. void Display::buildBanner(String msg, int xpos)
  637. {
  638. int h = TEXT_HEIGHT;
  639. this->tft.fillRect(0, STATUS_BAR_WIDTH, SCREEN_WIDTH, TEXT_HEIGHT, TFT_BLACK);
  640. this->tft.setFreeFont(NULL); // Font 4 selected
  641. this->tft.setTextSize(BANNER_TEXT_SIZE); // Font size scaling is x1
  642. this->tft.setTextColor(TFT_WHITE, TFT_BLACK); // Black text, no background colour
  643. this->showCenterText(msg, STATUS_BAR_WIDTH);
  644. /*
  645. // We could just use fillSprite(color) but lets be a bit more creative...
  646. // Fill with rainbow stripes
  647. //while (h--) img.drawFastHLine(0, h, SCREEN_WIDTH, 255);
  648. // Draw some graphics, the text will apear to scroll over these
  649. //img.fillRect (SCREEN_WIDTH / 2 - 20, TEXT_HEIGHT / 2 - 10, 40, 20, TFT_YELLOW);
  650. //img.fillCircle(SCREEN_WIDTH / 2, TEXT_HEIGHT / 2, 10, TFT_ORANGE);
  651. // Now print text on top of the graphics
  652. img.setTextSize(BANNER_TEXT_SIZE); // Font size scaling is x1
  653. img.setTextFont(0); // Font 4 selected
  654. img.setTextColor(TFT_WHITE); // Black text, no background colour
  655. img.setTextWrap(false); // Turn of wrap so we can print past end of sprite
  656. // Need to print twice so text appears to wrap around at left and right edges
  657. img.setCursor(xpos, 2); // Print text at xpos
  658. img.print(msg);
  659. img.setCursor(xpos - SCREEN_WIDTH, 2); // Print text at xpos - sprite width
  660. img.print(msg);
  661. */
  662. }
  663. void Display::main(uint8_t scan_mode)
  664. {
  665. if ((scan_mode == LV_JOIN_WIFI) ||
  666. (scan_mode == LV_ADD_SSID))
  667. lv_task_handler();
  668. return;
  669. }
  670. // End SPIFFS_functions
  671. #endif