Display.cpp 26 KB

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