Display.cpp 23 KB

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