_utils.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. // Version 6.1.5
  4. // Date: 2019-12-20
  5. //
  6. // based on work done by Thomas Telkamp for Raspberry PI 1ch gateway
  7. // and many others.
  8. //
  9. // All rights reserved. This program and the accompanying materials
  10. // are made available under the terms of the MIT License
  11. // which accompanies this distribution, and is available at
  12. // https://opensource.org/licenses/mit-license.php
  13. //
  14. // NO WARRANTY OF ANY KIND IS PROVIDED
  15. //
  16. // Author: Maarten Westenberg (mw12554@hotmail.com)
  17. //
  18. // This file contains the utilities for time and other functions
  19. // ========================================================================================
  20. // ==================== STRING STRING STRING ==============================================
  21. // ----------------------------------------------------------------------------------------
  22. // Print to the monitor console.
  23. // This function is used all over the gateway code as a substitue for USB debug code.
  24. // It allows webserver users to view printed/debugging code.
  25. //
  26. // Parameters:
  27. // txt: The text to be printed.
  28. // return:
  29. // <None>
  30. // ----------------------------------------------------------------------------------------
  31. static void mPrint(String txt)
  32. {
  33. # if DUSB>=1
  34. Serial.println(F(txt));
  35. if (debug>=2) Serial.flush();
  36. # endif //_DUSB
  37. # if _MONITOR>=1
  38. time_t tt = now();
  39. for (int i=_MONITOR; i>0; i--) { // Do only for values present....
  40. monitor[i]= monitor[i-1];
  41. }
  42. monitor[0].txt = String(day(tt)) + "-";
  43. monitor[0].txt += String(month(tt)) + "-";
  44. monitor[0].txt += String(year(tt)) + " ";
  45. byte _hour = hour(tt);
  46. byte _minute = minute(tt);
  47. byte _second = second(tt);
  48. if (_hour < 10) monitor[0].txt += "0"; monitor[0].txt += String( _hour )+ ":";
  49. if (_minute < 10) monitor[0].txt += "0"; monitor[0].txt += String(_minute) + ":";
  50. if (_second < 10) monitor[0].txt += "0"; monitor[0].txt += String(_second) + "- ";
  51. monitor[0].txt += String(txt);
  52. # endif //_MONITOR
  53. return;
  54. }
  55. // ----------------------------------------------------------------------------
  56. // mStat
  57. // Print the statistics on Serial (USB) port and/or Monitor
  58. // Depending on setting of _DUSB and _MONITOR.
  59. // Note: This function does not initialise the response var, will only append.
  60. // Parameters:
  61. // Interrupt: 8-bit
  62. // Response: String
  63. // Return:
  64. // 1: If successful
  65. // 0: No Success
  66. // ----------------------------------------------------------------------------
  67. int mStat(uint8_t intr, String & response)
  68. {
  69. #if _MONITOR>=1
  70. if (debug>=0) {
  71. response += "I=";
  72. if (intr & IRQ_LORA_RXTOUT_MASK) response += String("RXTOUT "); // 0x80
  73. if (intr & IRQ_LORA_RXDONE_MASK) response += String("RXDONE "); // 0x40
  74. if (intr & IRQ_LORA_CRCERR_MASK) response += "CRCERR "; // 0x20
  75. if (intr & IRQ_LORA_HEADER_MASK) response += "HEADER "; // 0x10
  76. if (intr & IRQ_LORA_TXDONE_MASK) response += "TXDONE "; // 0x08
  77. if (intr & IRQ_LORA_CDDONE_MASK) response += String("CDDONE "); // 0x04
  78. if (intr & IRQ_LORA_FHSSCH_MASK) response += "FHSSCH "; // 0x02
  79. if (intr & IRQ_LORA_CDDETD_MASK) response += "CDDETD "; // 0x01
  80. if (intr == 0x00) response += " -- ";
  81. response += ", F=" + String(ifreq);
  82. response += ", SF=" + String(sf);
  83. response += ", E=" + String(_event);
  84. response += ", S=";
  85. switch (_state) {
  86. case S_INIT:
  87. response += "INIT ";
  88. break;
  89. case S_SCAN:
  90. response += "SCAN ";
  91. break;
  92. case S_CAD:
  93. response += "CAD ";
  94. break;
  95. case S_RX:
  96. response += String("RX ");
  97. break;
  98. case S_TX:
  99. response += "TX ";
  100. break;
  101. case S_TXDONE:
  102. response += "TXDONE";
  103. break;
  104. default:
  105. response += " -- ";
  106. }
  107. response += ", eT=";
  108. response += String( micros() - eventTime );
  109. response += ", dT=";
  110. response += String( micros() - doneTime );
  111. //mPrint(response); // Do actual printing
  112. }
  113. #endif //_MONITOR
  114. return(1);
  115. }
  116. // ----------------------------------------------------------------------------------------
  117. // Fill a HEXadecimal String from a 4-byte char array
  118. //
  119. // ----------------------------------------------------------------------------------------
  120. static void printHEX(char * hexa, const char sep, String& response)
  121. {
  122. char m;
  123. m = hexa[0]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  124. m = hexa[1]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  125. m = hexa[2]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  126. m = hexa[3]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  127. }
  128. // ----------------------------------------------------------------------------------------
  129. // stringTime
  130. // Print the time t into the String reponse. t is of type time_t in seconds.
  131. // Only when RTC is present we print real time values
  132. // t contains number of seconds since system started that the event happened.
  133. // So a value of 100 would mean that the event took place 1 minute and 40 seconds ago
  134. // ----------------------------------------------------------------------------------------
  135. static void stringTime(time_t t, String& response) {
  136. if (t==0) { response += "--"; return; }
  137. // now() gives seconds since 1970
  138. // as millis() does rotate every 50 days
  139. // So we need another timing parameter
  140. time_t eTime = t;
  141. // Rest is standard
  142. byte _hour = hour(eTime);
  143. byte _minute = minute(eTime);
  144. byte _second = second(eTime);
  145. switch(weekday(eTime)) {
  146. case 1: response += "Sunday "; break;
  147. case 2: response += "Monday "; break;
  148. case 3: response += "Tuesday "; break;
  149. case 4: response += "Wednesday "; break;
  150. case 5: response += "Thursday "; break;
  151. case 6: response += "Friday "; break;
  152. case 7: response += "Saturday "; break;
  153. }
  154. response += String(day(eTime)) + "-";
  155. response += String(month(eTime)) + "-";
  156. response += String(year(eTime)) + " ";
  157. if (_hour < 10) response += "0"; response += String(_hour) + ":";
  158. if (_minute < 10) response += "0"; response += String(_minute) + ":";
  159. if (_second < 10) response += "0"; response += String(_second);
  160. }
  161. // ============== SERIAL SERIAL SERIAL ========================================
  162. // ----------------------------------------------------------------------------
  163. // Print utin8_t values in HEX with leading 0 when necessary
  164. // ----------------------------------------------------------------------------
  165. void printHexDigit(uint8_t digit)
  166. {
  167. // utility function for printing Hex Values with leading 0
  168. if(digit < 0x10)
  169. Serial.print('0');
  170. Serial.print(digit,HEX);
  171. }
  172. // ----------------------------------------------------------------------------
  173. // Print leading '0' digits for hours(0) and second(0) when
  174. // printing values less than 10
  175. // ----------------------------------------------------------------------------
  176. void printDigits(unsigned long digits)
  177. {
  178. // utility function for digital clock display: prints leading 0
  179. if(digits < 10)
  180. Serial.print(F("0"));
  181. Serial.print(digits);
  182. }
  183. // ----------------------------------------------------------------------------
  184. // Print the current time
  185. // ----------------------------------------------------------------------------
  186. static void printTime() {
  187. switch (weekday())
  188. {
  189. case 1: Serial.print(F("Sunday")); break;
  190. case 2: Serial.print(F("Monday")); break;
  191. case 3: Serial.print(F("Tuesday")); break;
  192. case 4: Serial.print(F("Wednesday")); break;
  193. case 5: Serial.print(F("Thursday")); break;
  194. case 6: Serial.print(F("Friday")); break;
  195. case 7: Serial.print(F("Saturday")); break;
  196. default: Serial.print(F("ERROR")); break;
  197. }
  198. Serial.print(F(" "));
  199. printDigits(hour());
  200. Serial.print(F(":"));
  201. printDigits(minute());
  202. Serial.print(F(":"));
  203. printDigits(second());
  204. return;
  205. }
  206. // ----------------------------------------------------------------------------
  207. // SerialTime
  208. // Print the current time on the Serial (USB), with leading 0.
  209. // ----------------------------------------------------------------------------
  210. void SerialTime()
  211. {
  212. uint32_t thrs = hour();
  213. uint32_t tmin = minute();
  214. uint32_t tsec = second();
  215. if (thrs<10) Serial.print('0'); Serial.print(thrs);
  216. Serial.print(':');
  217. if (tmin<10) Serial.print('0'); Serial.print(tmin);
  218. Serial.print(':');
  219. if (tsec<10) Serial.print('0'); Serial.print(tsec);
  220. if (debug>=2) Serial.flush();
  221. return;
  222. }
  223. // ----------------------------------------------------------------------------
  224. // SerialStat
  225. // Print the statistics on Serial (USB) port
  226. // ----------------------------------------------------------------------------
  227. void SerialStat(uint8_t intr)
  228. {
  229. #if _DUSB>=1
  230. if (debug>=0) {
  231. Serial.print(F("I="));
  232. if (intr & IRQ_LORA_RXTOUT_MASK) Serial.print(F("RXTOUT ")); // 0x80
  233. if (intr & IRQ_LORA_RXDONE_MASK) Serial.print(F("RXDONE ")); // 0x40
  234. if (intr & IRQ_LORA_CRCERR_MASK) Serial.print(F("CRCERR ")); // 0x20
  235. if (intr & IRQ_LORA_HEADER_MASK) Serial.print(F("HEADER ")); // 0x10
  236. if (intr & IRQ_LORA_TXDONE_MASK) Serial.print(F("TXDONE ")); // 0x08
  237. if (intr & IRQ_LORA_CDDONE_MASK) Serial.print(F("CDDONE ")); // 0x04
  238. if (intr & IRQ_LORA_FHSSCH_MASK) Serial.print(F("FHSSCH ")); // 0x02
  239. if (intr & IRQ_LORA_CDDETD_MASK) Serial.print(F("CDDETD ")); // 0x01
  240. if (intr == 0x00) Serial.print(F(" -- "));
  241. Serial.print(F(", F="));
  242. Serial.print(ifreq);
  243. Serial.print(F(", SF="));
  244. Serial.print(sf);
  245. Serial.print(F(", E="));
  246. Serial.print(_event);
  247. Serial.print(F(", S="));
  248. //Serial.print(_state);
  249. switch (_state) {
  250. case S_INIT:
  251. Serial.print(F("INIT "));
  252. break;
  253. case S_SCAN:
  254. Serial.print(F("SCAN "));
  255. break;
  256. case S_CAD:
  257. Serial.print(F("CAD "));
  258. break;
  259. case S_RX:
  260. Serial.print(F("RX "));
  261. break;
  262. case S_TX:
  263. Serial.print(F("TX "));
  264. break;
  265. case S_TXDONE:
  266. Serial.print(F("TXDONE"));
  267. break;
  268. default:
  269. Serial.print(F(" -- "));
  270. }
  271. Serial.print(F(", eT="));
  272. Serial.print( micros() - eventTime );
  273. Serial.print(F(", dT="));
  274. Serial.print( micros() - doneTime );
  275. Serial.println();
  276. }
  277. #endif
  278. }
  279. // ----------------------------------------------------------------------------
  280. // SerialName(id, response)
  281. // Check whether for address a (4 bytes in Unsigned Long) there is a name
  282. // This function only works if _TRUSTED_NODES is set
  283. // ----------------------------------------------------------------------------
  284. int SerialName(char * a, String& response)
  285. {
  286. #if _TRUSTED_NODES>=1
  287. uint32_t id = ((a[0]<<24) | (a[1]<<16) | (a[2]<<8) | a[3]);
  288. int i;
  289. for ( i=0; i< (sizeof(nodes)/sizeof(nodex)); i++) {
  290. if (id == nodes[i].id) {
  291. #if _DUSB >=1
  292. if (( debug>=3 ) && ( pdebug & P_GUI )) {
  293. Serial.print(F("G Name="));
  294. Serial.print(nodes[i].nm);
  295. Serial.print(F(" for node=0x"));
  296. Serial.print(nodes[i].id,HEX);
  297. Serial.println();
  298. }
  299. #endif // _DUSB
  300. response += nodes[i].nm;
  301. return(i);
  302. }
  303. }
  304. #endif // _TRUSTED_NODES
  305. return(-1); // If no success OR is TRUSTED NODES not defined
  306. }
  307. #if _LOCALSERVER==1
  308. // ----------------------------------------------------------------------------
  309. // inDecodes(id)
  310. // Find the id in Decodes array, and return the index of the item
  311. // Parameters:
  312. // id: The first field in the array (normally DevAddr id). Must be char[4]
  313. // Returns:
  314. // The index of the ID in the Array. Returns -1 if not found
  315. // ----------------------------------------------------------------------------
  316. int inDecodes(char * id) {
  317. uint32_t ident = ((id[3]<<24) | (id[2]<<16) | (id[1]<<8) | id[0]);
  318. int i;
  319. for ( i=0; i< (sizeof(decodes)/sizeof(codex)); i++) {
  320. if (ident == decodes[i].id) {
  321. return(i);
  322. }
  323. }
  324. return(-1);
  325. }
  326. #endif