Display.cpp 26 KB

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