Display.cpp 25 KB

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