Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.89 KB | None | 0 0
  1. //<File !Start!>
  2. // FILE: [Basis1.ino]
  3. // Created by GUIslice Builder version: [0.13.b013]
  4. //
  5. // GUIslice Builder Generated File
  6. //
  7. // For the latest guides, updates and support view:
  8. // https://github.com/ImpulseAdventure/GUIslice
  9. //
  10. //<File !End!>
  11. //
  12. // ARDUINO NOTES:
  13. // - GUIslice_config.h must be edited to match the pinout connections
  14. // between the Arduino CPU and the display controller (see ADAGFX_PIN_*).
  15. //
  16.  
  17. // ------------------------------------------------
  18. // SD Card Includes
  19. // ------------------------------------------------
  20. #include <SPI.h>
  21. #include <SD.h>
  22.  
  23. // ------------------------------------------------
  24. // GPS Includes
  25. // ------------------------------------------------
  26. //#include <SoftwareSerial.h>
  27. #include "HardwareSerial.h"
  28. #include <TinyGPS++.h>
  29.  
  30. // ------------------------------------------------
  31. // ESP Now Includes
  32. // ------------------------------------------------
  33. #include <esp_now.h>
  34. #include <WiFi.h>
  35.  
  36. // ------------------------------------------------
  37. // Headers to include
  38. // ------------------------------------------------
  39. #include "GUIslice.h"
  40. #include "GUIslice_drv.h"
  41.  
  42. // Include any extended elements
  43. //<Includes !Start!>
  44. // Include extended elements
  45. #include "elem/XKeyPad_Num.h"
  46. #include "elem/XSlider.h"
  47. #include "elem/XTextbox.h"
  48.  
  49. // Ensure optional features are enabled in the configuration
  50. #if !(GSLC_FEATURE_COMPOUND)
  51. #error "Config: GSLC_FEATURE_COMPOUND required for this program but not enabled. Please update GUIslice/config."
  52. #endif
  53. //<Includes !End!>
  54.  
  55. // ------------------------------------------------
  56. // Headers and Defines for fonts
  57. // Note that font files are located within the Adafruit-GFX library folder:
  58. // ------------------------------------------------
  59. //<Fonts !Start!>
  60. #if !defined(DRV_DISP_TFT_ESPI)
  61. #error Builder config "Edit->Options->General->Target Platform" should be "arduino"
  62. #endif
  63. #include <TFT_eSPI.h>
  64. //<Fonts !End!>
  65.  
  66. // ------------------------------------------------
  67. // Defines for resources
  68. // ------------------------------------------------
  69. //<Resources !Start!>
  70. //<Resources !End!>
  71.  
  72. // ------------------------------------------------
  73. // Enumerations for pages, elements, fonts, images
  74. // ------------------------------------------------
  75. //<Enum !Start!>
  76. enum {E_PG_MAIN,E_PG_BLANK,E_POP_KEYPAD};
  77. enum {E_ELEM_BTN_BACKLIGHT,E_ELEM_DATUMINFO,E_ELEM_GPSINFO
  78. ,E_ELEM_TBSENSOREN,E_ELEM_TEXTDATUM,E_ELEM_TEXTGPS
  79. ,E_ELEM_TEXTTRID,E_ELEM_TITLE,E_ELEM_TRID,E_TXTSCROLL1
  80. ,E_ELEM_KEYPAD};
  81. // Must use separate enum for fonts with MAX_FONT at end to use gslc_FontSet.
  82. enum {E_FONT_MONOB12,E_FONT_TXT5,MAX_FONT};
  83. //<Enum !End!>
  84.  
  85. // ------------------------------------------------
  86. // Instantiate the GUI
  87. // ------------------------------------------------
  88.  
  89. // ------------------------------------------------
  90. // Define the maximum number of elements and pages
  91. // ------------------------------------------------
  92. //<ElementDefines !Start!>
  93. #define MAX_PAGE 3
  94.  
  95. #define MAX_ELEM_PG_MAIN 10 // # Elems total on page
  96. #define MAX_ELEM_PG_MAIN_RAM MAX_ELEM_PG_MAIN // # Elems in RAM
  97.  
  98. #define MAX_ELEM_PG_BLANK 1 // # Elems total on page
  99. #define MAX_ELEM_PG_BLANK_RAM MAX_ELEM_PG_BLANK // # Elems in RAM
  100. //<ElementDefines !End!>
  101.  
  102. // ------------------------------------------------
  103. // Create element storage
  104. // ------------------------------------------------
  105. gslc_tsGui m_gui;
  106. gslc_tsDriver m_drv;
  107. gslc_tsFont m_asFont[MAX_FONT];
  108. gslc_tsPage m_asPage[MAX_PAGE];
  109.  
  110. //<GUI_Extra_Elements !Start!>
  111. gslc_tsElem m_asPage1Elem[MAX_ELEM_PG_MAIN_RAM];
  112. gslc_tsElemRef m_asPage1ElemRef[MAX_ELEM_PG_MAIN];
  113. gslc_tsElem m_asPage2Elem[MAX_ELEM_PG_BLANK_RAM];
  114. gslc_tsElemRef m_asPage2ElemRef[MAX_ELEM_PG_BLANK];
  115. gslc_tsElem m_asKeypadElem[1];
  116. gslc_tsElemRef m_asKeypadElemRef[1];
  117. gslc_tsXKeyPad_Num m_sKeyPad;
  118. gslc_tsXTextbox m_sTextbox1;
  119. char m_acTextboxBuf1[616]; // NRows=14 NCols=44
  120. gslc_tsXSlider m_sTextScroll1;
  121.  
  122. #define MAX_STR 100
  123.  
  124. //<GUI_Extra_Elements !End!>
  125.  
  126. // ------------------------------------------------
  127. // Program Globals
  128. // ------------------------------------------------
  129.  
  130. // Save some element references for direct access
  131. //<Save_References !Start!>
  132. gslc_tsElemRef* m_pElemOutTxt4 = NULL;
  133. gslc_tsElemRef* m_pElemOutTxt6 = NULL;
  134. gslc_tsElemRef* m_pElemTextbox1 = NULL;
  135. gslc_tsElemRef* m_pElemVal1 = NULL;
  136. gslc_tsElemRef* m_pTextSlider1 = NULL;
  137. gslc_tsElemRef* m_pElemKeyPad = NULL;
  138. //<Save_References !End!>
  139.  
  140. // Define debug message function
  141. static int16_t DebugOut(char ch) { if (ch == (char)'\n') Serial.println(""); else Serial.write(ch); return 0; }
  142.  
  143. //-------------------------------------------------
  144. // SD Globals
  145. //-------------------------------------------------
  146. File myFile;
  147. File root;
  148.  
  149. //-------------------------------------------------
  150. // GPS Globals
  151. //-------------------------------------------------
  152. static const int RX2Pin = 17, TX2Pin = 16;
  153. TinyGPSPlus gps;
  154. //SoftwareSerial gpsSerial(RX2Pin, TX2Pin);
  155. HardwareSerial gpsSerial(2);
  156.  
  157. //-------------------------------------------------
  158. // ESP Now Globals
  159. //-------------------------------------------------
  160. const char* ssid = "PTB_Station";
  161. const char* password = "ptb_station_2020";
  162.  
  163. #define CHANNEL 1
  164.  
  165. // keep in sync with sensor struct
  166. struct SENSOR_DATA {
  167. float temp;
  168. float hum;
  169. float t;
  170. float pressure;
  171. };
  172.  
  173. // ------------------------------------------------
  174. // Callback Methods
  175. // ------------------------------------------------
  176. // Common Button callback
  177. bool CbBtnCommon(void* pvGui,void *pvElemRef,gslc_teTouch eTouch,int16_t nX,int16_t nY)
  178. {
  179. gslc_tsElemRef* pElemRef = (gslc_tsElemRef*)(pvElemRef);
  180. gslc_tsElem* pElem = pElemRef->pElem;
  181.  
  182. if ( eTouch == GSLC_TOUCH_UP_IN ) {
  183. // From the element's ID we can determine which button was pressed.
  184. switch (pElem->nId) {
  185. //<Button Enums !Start!>
  186. case E_ELEM_TRID:
  187. //TODO- Check the code to see what else you may need to add
  188. // Clicked on edit field, so show popup box and associate with this text field
  189. gslc_ElemXKeyPadTargetIdSet(&m_gui, m_pElemKeyPad, E_ELEM_TRID);
  190. gslc_PopupShow(&m_gui, E_POP_KEYPAD, true);
  191. // Preload current value
  192. gslc_ElemXKeyPadValSet(&m_gui, m_pElemKeyPad, gslc_ElemGetTxtStr(&m_gui, m_pElemVal1));
  193. break;
  194. case E_ELEM_BTN_BACKLIGHT:
  195. //TODO- Replace with button handling code
  196. break;
  197.  
  198. //<Button Enums !End!>
  199. default:
  200. break;
  201. }
  202. }
  203. return true;
  204. }
  205. //<Checkbox Callback !Start!>
  206. //<Checkbox Callback !End!>
  207. // KeyPad Input Ready callback
  208. bool CbKeypad(void* pvGui, void *pvElemRef, int16_t nState, void* pvData)
  209. {
  210. gslc_tsElemRef* pElemRef = (gslc_tsElemRef*)(pvElemRef);
  211. gslc_tsElem* pElem = pElemRef->pElem;
  212. gslc_tsGui* pGui = (gslc_tsGui*)pvGui;
  213.  
  214. // From the pvData we can get the ID element that is ready.
  215. int16_t nTargetElemId = gslc_ElemXKeyPadDataTargetIdGet(pGui, pvData);
  216. if (nState == XKEYPAD_CB_STATE_DONE) {
  217. // User clicked on Enter to leave popup
  218. // - If we have a popup active, pass the return value directly to
  219. // the corresponding value field
  220. switch (nTargetElemId) {
  221. //<Keypad Enums !Start!>
  222. case E_ELEM_TRID:
  223. gslc_ElemSetTxtStr(pGui, m_pElemVal1, gslc_ElemXKeyPadDataValGet(pGui, pvData));
  224. gslc_PopupHide(&m_gui);
  225. break;
  226.  
  227. //<Keypad Enums !End!>
  228. default:
  229. break;
  230. }
  231. } else if (nState == XKEYPAD_CB_STATE_CANCEL) {
  232. // User escaped from popup, so don't update values
  233. gslc_PopupHide(&m_gui);
  234. }
  235. return true;
  236. }
  237. //<Spinner Callback !Start!>
  238. //<Spinner Callback !End!>
  239. //<Listbox Callback !Start!>
  240. //<Listbox Callback !End!>
  241. //<Draw Callback !Start!>
  242. //<Draw Callback !End!>
  243.  
  244. // Callback function for when a slider's position has been updated
  245. bool CbSlidePos(void* pvGui,void* pvElemRef,int16_t nPos)
  246. {
  247. gslc_tsGui* pGui = (gslc_tsGui*)(pvGui);
  248. gslc_tsElemRef* pElemRef = (gslc_tsElemRef*)(pvElemRef);
  249. gslc_tsElem* pElem = pElemRef->pElem;
  250. int16_t nVal;
  251.  
  252. // From the element's ID we can determine which slider was updated.
  253. switch (pElem->nId) {
  254. //<Slider Enums !Start!>
  255. case E_TXTSCROLL1:
  256. // Fetch the slider position
  257. nVal = gslc_ElemXSliderGetPos(pGui,m_pTextSlider1);
  258. //TODO- Replace with slider handling code
  259. break;
  260.  
  261. //<Slider Enums !End!>
  262. default:
  263. break;
  264. }
  265.  
  266. return true;
  267. }
  268. //<Tick Callback !Start!>
  269. //<Tick Callback !End!>
  270.  
  271. // ------------------------------------------------
  272. // Create page elements
  273. // ------------------------------------------------
  274. bool InitGUI()
  275. {
  276. gslc_tsElemRef* pElemRef = NULL;
  277.  
  278. //<InitGUI !Start!>
  279. gslc_PageAdd(&m_gui,E_PG_MAIN,m_asPage1Elem,MAX_ELEM_PG_MAIN_RAM,m_asPage1ElemRef,MAX_ELEM_PG_MAIN);
  280. gslc_PageAdd(&m_gui,E_PG_BLANK,m_asPage2Elem,MAX_ELEM_PG_BLANK_RAM,m_asPage2ElemRef,MAX_ELEM_PG_BLANK);
  281. gslc_PageAdd(&m_gui,E_POP_KEYPAD,m_asKeypadElem,1,m_asKeypadElemRef,1); // KeyPad
  282.  
  283. // NOTE: The current page defaults to the first page added. Here we explicitly
  284. // ensure that the main page is the correct page no matter the add order.
  285. gslc_SetPageCur(&m_gui,E_PG_MAIN);
  286.  
  287. // Set Background to a flat color
  288. gslc_SetBkgndColor(&m_gui,GSLC_COL_BLACK);
  289.  
  290. // -----------------------------------
  291. // PAGE: E_PG_MAIN
  292.  
  293.  
  294. // Create E_ELEM_TITLE text label
  295. pElemRef = gslc_ElemCreateTxt(&m_gui,E_ELEM_TITLE,E_PG_MAIN,(gslc_tsRect){10,10,168,33},
  296. (char*)"PTB Sensoren",0,E_FONT_MONOB12);
  297. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_BLUE_LT4);
  298.  
  299. // Create E_ELEM_TRID numeric input field
  300. static char m_sInputNumber1[11] = "";
  301. pElemRef = gslc_ElemCreateTxt(&m_gui,E_ELEM_TRID,E_PG_MAIN,(gslc_tsRect){250,80,60,10},
  302. (char*)m_sInputNumber1,11,E_FONT_TXT5);
  303. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_WHITE);
  304. gslc_ElemSetCol(&m_gui,pElemRef,GSLC_COL_GRAY,GSLC_COL_BLACK,GSLC_COL_BLACK);
  305. gslc_ElemSetFrameEn(&m_gui,pElemRef,true);
  306. gslc_ElemSetClickEn(&m_gui, pElemRef, true);
  307. gslc_ElemSetTouchFunc(&m_gui, pElemRef, &CbBtnCommon);
  308. m_pElemVal1 = pElemRef;
  309.  
  310. // Create E_ELEM_TEXTTRID text label
  311. pElemRef = gslc_ElemCreateTxt(&m_gui,E_ELEM_TEXTTRID,E_PG_MAIN,(gslc_tsRect){170,80,72,10},
  312. (char*)"Transport ID",0,E_FONT_TXT5);
  313. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_BLUE_LT4);
  314.  
  315. // Create wrapping box for textbox E_ELEM_TBSENSOREN and scrollbar
  316. pElemRef = gslc_ElemCreateBox(&m_gui,GSLC_ID_AUTO,E_PG_MAIN,(gslc_tsRect){10,100,297,127});
  317. gslc_ElemSetCol(&m_gui,pElemRef,GSLC_COL_GRAY,GSLC_COL_BLACK,GSLC_COL_BLACK);
  318.  
  319. // Create textbox
  320. pElemRef = gslc_ElemXTextboxCreate(&m_gui,E_ELEM_TBSENSOREN,E_PG_MAIN,&m_sTextbox1,
  321. (gslc_tsRect){10+2,100+4,297-23,127-7},E_FONT_TXT5,
  322. (char*)&m_acTextboxBuf1,14,44);
  323. gslc_ElemXTextboxWrapSet(&m_gui,pElemRef,false);
  324. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_WHITE);
  325. gslc_ElemSetCol(&m_gui,pElemRef,GSLC_COL_GRAY,GSLC_COL_BLACK,GSLC_COL_BLACK);
  326. m_pElemTextbox1 = pElemRef;
  327.  
  328. // Create vertical scrollbar for textbox
  329. pElemRef = gslc_ElemXSliderCreate(&m_gui,E_TXTSCROLL1,E_PG_MAIN,&m_sTextScroll1,
  330. (gslc_tsRect){10+297-21,100+4,20,127-8},0,100,0,5,true);
  331. gslc_ElemSetCol(&m_gui,pElemRef,GSLC_COL_GRAY,GSLC_COL_BLACK,GSLC_COL_GRAY);
  332. gslc_ElemXSliderSetPosFunc(&m_gui,pElemRef,&CbSlidePos);
  333. m_pTextSlider1 = pElemRef;
  334.  
  335. // Create E_ELEM_TEXTGPS text label
  336. pElemRef = gslc_ElemCreateTxt(&m_gui,E_ELEM_TEXTGPS,E_PG_MAIN,(gslc_tsRect){10,40,54,15},
  337. (char*)"Position:",0,E_FONT_TXT5);
  338. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_BLUE_LT4);
  339.  
  340. // Create E_ELEM_GPSINFO runtime modifiable text
  341. static char m_sDisplayText4[31] = "...";
  342. pElemRef = gslc_ElemCreateTxt(&m_gui,E_ELEM_GPSINFO,E_PG_MAIN,(gslc_tsRect){100,40,174,10},
  343. (char*)m_sDisplayText4,31,E_FONT_TXT5);
  344. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_WHITE);
  345. m_pElemOutTxt4 = pElemRef;
  346.  
  347. // Create E_ELEM_TEXTDATUM text label
  348. pElemRef = gslc_ElemCreateTxt(&m_gui,E_ELEM_TEXTDATUM,E_PG_MAIN,(gslc_tsRect){10,60,84,15},
  349. (char*)"Datum / Zeit: ",0,E_FONT_TXT5);
  350. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_BLUE_LT4);
  351.  
  352. // Create E_ELEM_DATUMINFO runtime modifiable text
  353. static char m_sDisplayText6[31] = "...";
  354. pElemRef = gslc_ElemCreateTxt(&m_gui,E_ELEM_DATUMINFO,E_PG_MAIN,(gslc_tsRect){100,60,204,10},
  355. (char*)m_sDisplayText6,31,E_FONT_TXT5);
  356. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_WHITE);
  357. m_pElemOutTxt6 = pElemRef;
  358.  
  359. // -----------------------------------
  360. // PAGE: E_PG_BLANK
  361.  
  362.  
  363. // create E_ELEM_BTN_BACKLIGHT button with text label
  364. pElemRef = gslc_ElemCreateBtnTxt(&m_gui,E_ELEM_BTN_BACKLIGHT,E_PG_BLANK,
  365. (gslc_tsRect){10,10,300,215},(char*)"An",0,E_FONT_TXT5,&CbBtnCommon);
  366. gslc_ElemSetTxtCol(&m_gui,pElemRef,GSLC_COL_WHITE);
  367. gslc_ElemSetCol(&m_gui,pElemRef,GSLC_COL_BLACK,GSLC_COL_BLACK,GSLC_COL_BLACK);
  368.  
  369. // -----------------------------------
  370. // PAGE: E_POP_KEYPAD
  371.  
  372. gslc_tsXKeyPadCfg sCfg = gslc_ElemXKeyPadCfgInit_Num();
  373. gslc_ElemXKeyPadCfgSetFloatEn(&sCfg, true);
  374. gslc_ElemXKeyPadCfgSetSignEn(&sCfg, true);
  375. gslc_ElemXKeyPadCfgSetButtonSz(&sCfg, 20, 20);
  376. gslc_ElemXKeyPadCfgSetRoundEn(&sCfg, false);
  377. m_pElemKeyPad = gslc_ElemXKeyPadCreate_Num(&m_gui, E_ELEM_KEYPAD, E_POP_KEYPAD,
  378. &m_sKeyPad, 65, 80, E_FONT_TXT5, &sCfg);
  379. gslc_ElemXKeyPadValSetCb(&m_gui, m_pElemKeyPad, &CbKeypad);
  380.  
  381. //<InitGUI !End!>
  382.  
  383. return true;
  384. }
  385.  
  386. //---------------------------------------------------
  387. // SD Funktions
  388. //---------------------------------------------------
  389. void initSDCard()
  390. {
  391. // Update Textbox
  392. gslc_tsElemRef* pElemTextbox = gslc_PageFindElemById(&m_gui,E_PG_MAIN,E_ELEM_TBSENSOREN);
  393.  
  394. if (!SD.begin(5)) {
  395. Serial.println("Card Mount Failed");
  396. gslc_ElemXTextboxAdd(&m_gui,pElemTextbox,(char*)"SD Kartenslot konnte nicht initialisiert\n werden!\n");
  397. return;
  398. }
  399. uint8_t cardType = SD.cardType();
  400.  
  401. if (cardType == CARD_NONE) {
  402. Serial.println("No SD card attached");
  403. gslc_ElemXTextboxAdd(&m_gui,pElemTextbox,(char*)"Es wurde keine SD Karte gefunden!\n");
  404. return;
  405. }
  406. gslc_ElemXTextboxAdd(&m_gui,pElemTextbox,(char*)"SD Karte wurde eingelesen\n");
  407. if (!SD.exists("/tid.txt")) {
  408. myFile = SD.open("/tid.txt", FILE_WRITE);
  409. gslc_ElemXTextboxAdd(&m_gui,pElemTextbox,(char*)"tid.txt erstellt\n");
  410. myFile.close();
  411. }
  412.  
  413. char tid[15] = {0};
  414. // re-open the file for reading:
  415. myFile = SD.open("/tid.txt");
  416. if (myFile) {
  417. Serial.println("tid.txt:");
  418. // read from the file until there's nothing else in it:
  419. int counter = 0;
  420. while (myFile.available()) {
  421. char ltr = myFile.read();
  422. if (ltr != '\n') {
  423. tid[counter] = ltr;
  424. }
  425. counter ++;
  426. }
  427. Serial.println(tid);
  428. gslc_ElemXTextboxAdd(&m_gui,pElemTextbox,(char*)"Transport-ID: ");
  429. gslc_ElemXTextboxAdd(&m_gui,pElemTextbox,tid);
  430. gslc_ElemXTextboxAdd(&m_gui,pElemTextbox,(char*)"\n");
  431. gslc_ElemSetTxtStr(&m_gui, m_pElemVal1, tid);
  432.  
  433. // close the file:
  434. myFile.close();
  435. } else {
  436. // if the file didn't open, print an error:
  437. Serial.println("tid.txt konnte nicht geöffnet werden!");
  438. }
  439.  
  440. root = SD.open("/");
  441. printDirectory(root, 0);
  442. }
  443.  
  444. void printDirectory(File dir, int numTabs) {
  445. while (true) {
  446.  
  447. File entry = dir.openNextFile();
  448. if (! entry) {
  449. // no more files
  450. break;
  451. }
  452. for (uint8_t i = 0; i < numTabs; i++) {
  453. Serial.print('\t');
  454. }
  455. Serial.print(entry.name());
  456. if (entry.isDirectory()) {
  457. Serial.println("/");
  458. printDirectory(entry, numTabs + 1);
  459. } else {
  460. // files have sizes, directories do not
  461. Serial.print("\t\t");
  462. Serial.println(entry.size(), DEC);
  463. }
  464. entry.close();
  465. }
  466. }
  467.  
  468. //---------------------------------------------------
  469. // GPS Functions
  470. //---------------------------------------------------
  471. char* stringToCharArray(String str){
  472. // Length (with one extra character for the null terminator)
  473. int str_len = str.length() + 1;
  474.  
  475. // Prepare the character array (the buffer)
  476. char char_array[str_len];
  477.  
  478. // Copy it over
  479. str.toCharArray(char_array, str_len);
  480.  
  481. return char_array;
  482. }
  483.  
  484. void checkGPS() {
  485. while (gpsSerial.available() > 0)
  486. if (gps.encode(gpsSerial.read())){
  487. //showGPSData();
  488. serialGPSData();
  489. }
  490.  
  491. if (millis() > 5000 && gps.charsProcessed() < 10)
  492. {
  493. Serial.println(F("No GPS detected: check wiring."));
  494. while(true);
  495. }
  496. }
  497.  
  498. void showGPSData()
  499. {
  500. if (gps.location.isValid()){
  501. //GPS Coords
  502. String serialData = "";
  503. serialData = String(gps.location.lat())+","+String(gps.location.lng());
  504. gslc_ElemSetTxtStr(&m_gui, m_pElemOutTxt4, stringToCharArray(serialData));
  505. } else {
  506. gslc_ElemSetTxtStr(&m_gui, m_pElemOutTxt4, (char*)"GPS Koordinaten nicht gefunden");
  507. //gslc_ElemXTextboxAdd(&m_gui,pElemTextbox, (char*)"kein GPS Signal gefunden\n");
  508. }
  509.  
  510. //Date / Time
  511. String serialData2 = "";
  512. if (gps.date.isValid())
  513. {
  514. serialData2 = String(gps.date.day())+"."+String(gps.date.month())+"."+String(gps.date.year())+" / ";
  515. }
  516. else
  517. {
  518. serialData2 = String(millis())+" / ";
  519. }
  520.  
  521. if (gps.time.isValid())
  522. {
  523. if (gps.time.hour() < 10) serialData2 += "0";
  524. serialData2 += String(gps.time.hour());
  525. serialData2 += ":";
  526. if (gps.time.minute() < 10) serialData2 += "0";
  527. serialData2 += String(gps.time.minute());
  528. serialData2 += ":";
  529. if (gps.time.second() < 10) serialData2 += "0";
  530. serialData2 += String(gps.time.second());
  531. } else {
  532. serialData2 += String(millis());
  533. }
  534. //Serial.println(serialData2);
  535. char __serialdata2[31];
  536. serialData2.toCharArray(__serialdata2, sizeof(__serialdata2));
  537. gslc_ElemSetTxtStr(&m_gui, m_pElemOutTxt6, __serialdata2);
  538. }
  539.  
  540. void serialGPSData()
  541. {
  542. Serial.print(F("Location: "));
  543. if (gps.location.isValid())
  544. {
  545. Serial.print(gps.location.lat(), 6);
  546. Serial.print(F(","));
  547. Serial.print(gps.location.lng(), 6);
  548. }
  549. else
  550. {
  551. Serial.print(F("INVALID"));
  552. }
  553.  
  554. Serial.print(F(" Date/Time: "));
  555. if (gps.date.isValid())
  556. {
  557. Serial.print(gps.date.month());
  558. Serial.print(F("/"));
  559. Serial.print(gps.date.day());
  560. Serial.print(F("/"));
  561. Serial.print(gps.date.year());
  562. }
  563. else
  564. {
  565. Serial.print(F("INVALID"));
  566. }
  567.  
  568. Serial.print(F(" "));
  569. if (gps.time.isValid())
  570. {
  571. if (gps.time.hour() < 10) Serial.print(F("0"));
  572. Serial.print(gps.time.hour());
  573. Serial.print(F(":"));
  574. if (gps.time.minute() < 10) Serial.print(F("0"));
  575. Serial.print(gps.time.minute());
  576. Serial.print(F(":"));
  577. if (gps.time.second() < 10) Serial.print(F("0"));
  578. Serial.print(gps.time.second());
  579. Serial.print(F("."));
  580. if (gps.time.centisecond() < 10) Serial.print(F("0"));
  581. Serial.print(gps.time.centisecond());
  582. }
  583. else
  584. {
  585. Serial.print(F("INVALID"));
  586. }
  587.  
  588. Serial.println();
  589. }
  590.  
  591. //---------------------------------------------------
  592. // ESP Now Functions
  593. //---------------------------------------------------
  594. // Init ESP Now with fallback
  595. void InitESPNow() {
  596. WiFi.disconnect();
  597. if (esp_now_init() == ESP_OK) {
  598. Serial.println("ESPNow Init Success");
  599. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"Netzwerk initialisiert\n");
  600. }
  601. else {
  602. Serial.println("ESPNow Failed");
  603. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"Netzwerk konnte nicht initalisiert werden!\n");
  604. // Retry InitESPNow, add a counte and then restart?
  605. // InitESPNow();
  606. // or Simply Restart
  607. ESP.restart();
  608. }
  609. }
  610.  
  611. void configDeviceAP() {
  612. String Prefix = "Slave:";
  613. String Mac = WiFi.macAddress();
  614. String SSID = Prefix + Mac;
  615. String Password = "123456789";
  616. bool result = WiFi.softAP(SSID.c_str(), Password.c_str(), CHANNEL, 0);
  617. if (!result) {
  618. Serial.println("AP Config failed.");
  619. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"AP Konfiguration fehlgeschlagen\n");
  620. } else {
  621. Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
  622. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"AP Konfiguration erfolgreich\n");
  623. char __ca[sizeof(SSID)];
  624. SSID.toCharArray(__ca, sizeof(__ca));
  625. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,__ca);
  626. }
  627. }
  628.  
  629. // callback when data is recv from Master
  630. void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  631. char macStr[18];
  632. snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  633. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  634. Serial.print("Last Packet Recv from: "); Serial.println(macStr);
  635. Serial.print("Last Packet Recv Data: "); Serial.println(*data);
  636. Serial.println("");
  637. }
  638.  
  639.  
  640. //---------------------------------------------------
  641. // SETUP
  642.  
  643. void setup()
  644. {
  645. // ------------------------------------------------
  646. // Initialize
  647. // ------------------------------------------------
  648. Serial.begin(9600);
  649. gpsSerial.begin(9600, SERIAL_8N1, RX2Pin, TX2Pin);
  650. //LED Background
  651. pinMode(25, OUTPUT);
  652. digitalWrite(25, HIGH);
  653.  
  654. //WiFi
  655. Serial.println("Wifi Config");
  656. WiFi.mode(WIFI_AP);
  657. configDeviceAP();
  658. String MacAd = WiFi.softAPmacAddress();
  659. char __ca[sizeof(MacAd)];
  660. MacAd.toCharArray(__ca, sizeof(__ca));
  661. Serial.println ("MacAd "+MacAd);
  662. /*gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"AP MAC: ");
  663. gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,__ca);
  664. gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"\n");
  665. InitESPNow();
  666. // Once ESPNow is successfully Init, we will register for recv CB to
  667. // get recv packer info.
  668. esp_now_register_recv_cb(OnDataRecv);*/
  669. // Wait for USB Serial
  670. //delay(1000); // NOTE: Some devices require a delay after Serial.begin() before serial port can be used
  671.  
  672. gslc_InitDebug(&DebugOut);
  673.  
  674. if (!gslc_Init(&m_gui,&m_drv,m_asPage,MAX_PAGE,m_asFont,MAX_FONT)) { return; }
  675.  
  676. // ------------------------------------------------
  677. // Load Fonts
  678. // ------------------------------------------------
  679. //<Load_Fonts !Start!>
  680. if (!gslc_FontSet(&m_gui,E_FONT_MONOB12,GSLC_FONTREF_PTR,&FreeMonoBold12pt7b,1)) { return; }
  681. if (!gslc_FontSet(&m_gui,E_FONT_TXT5,GSLC_FONTREF_PTR,NULL,1)) { return; }
  682. //<Load_Fonts !End!>
  683.  
  684. // ------------------------------------------------
  685. // Create graphic elements
  686. // ------------------------------------------------
  687. InitGUI();
  688.  
  689. //non GuiSlice Inits
  690. initSDCard();
  691.  
  692. //<Startup !Start!>
  693. //<Startup !End!>
  694.  
  695. }
  696.  
  697. // -----------------------------------
  698. // Main event loop
  699. // -----------------------------------
  700. void loop()
  701. {
  702. checkGPS();
  703. // ------------------------------------------------
  704. // Update GUI Elements
  705. // ------------------------------------------------
  706.  
  707. //TODO - Add update code for any text, gauges, or sliders
  708. // - Transport ID speichern
  709. // - Speicherplatz für ESP32 überprüfen in den Config Dateien für Arduino IDE
  710. // - ESP Now als Slave
  711. // - ESP Now in den Sensoren als Master
  712. // - Daten ausgeben in Textbox
  713. // - Daten speichern in File
  714. // - Bildschirm ausschalten (switch zu anderen Bildschirm)
  715. // - Button wieder zurück zu eigentlichem Bildschirm
  716. // - Checkbox für konstant an
  717.  
  718. // ------------------------------------------------
  719. // Periodically call GUIslice update function
  720. // ------------------------------------------------
  721. gslc_Update(&m_gui);
  722. delay(5000);
  723. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement