Display.cpp 28 KB

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