Display.cpp 27 KB

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