Display.cpp 23 KB

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