_utils.ino 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. // Version 6.1.3
  4. // Date: 2019-11-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. // Fill a HEXadecimal String from a 4-byte char array
  23. //
  24. // ----------------------------------------------------------------------------
  25. static void printHEX(char * hexa, const char sep, String& response)
  26. {
  27. char m;
  28. m = hexa[0]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  29. m = hexa[1]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  30. m = hexa[2]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  31. m = hexa[3]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
  32. }
  33. // ----------------------------------------------------------------------------
  34. // stringTime
  35. // Print the time t into the String reponse. t is of type time_t in seconds.
  36. // Only when RTC is present we print real time values
  37. // t contains number of seconds since system started that the event happened.
  38. // So a value of 100 would mean that the event took place 1 minute and 40 seconds ago
  39. // ----------------------------------------------------------------------------
  40. static void stringTime(time_t t, String& response) {
  41. if (t==0) { response += "--"; return; }
  42. // now() gives seconds since 1970
  43. // as millis() does rotate every 50 days
  44. // So we need another timing parameter
  45. time_t eTime = t;
  46. // Rest is standard
  47. byte _hour = hour(eTime);
  48. byte _minute = minute(eTime);
  49. byte _second = second(eTime);
  50. switch(weekday(eTime)) {
  51. case 1: response += "Sunday "; break;
  52. case 2: response += "Monday "; break;
  53. case 3: response += "Tuesday "; break;
  54. case 4: response += "Wednesday "; break;
  55. case 5: response += "Thursday "; break;
  56. case 6: response += "Friday "; break;
  57. case 7: response += "Saturday "; break;
  58. }
  59. response += String() + day(eTime) + "-";
  60. response += String() + month(eTime) + "-";
  61. response += String() + year(eTime) + " ";
  62. if (_hour < 10) response += "0";
  63. response += String() + _hour + ":";
  64. if (_minute < 10) response += "0";
  65. response += String() + _minute + ":";
  66. if (_second < 10) response += "0";
  67. response += String() + _second;
  68. }
  69. // ============== SERIAL SERIAL SERIAL ========================================
  70. // ----------------------------------------------------------------------------
  71. // Print utin8_t values in HEX with leading 0 when necessary
  72. // ----------------------------------------------------------------------------
  73. void printHexDigit(uint8_t digit)
  74. {
  75. // utility function for printing Hex Values with leading 0
  76. if(digit < 0x10)
  77. Serial.print('0');
  78. Serial.print(digit,HEX);
  79. }
  80. // ----------------------------------------------------------------------------
  81. // Print leading '0' digits for hours(0) and second(0) when
  82. // printing values less than 10
  83. // ----------------------------------------------------------------------------
  84. void printDigits(unsigned long digits)
  85. {
  86. // utility function for digital clock display: prints leading 0
  87. if(digits < 10)
  88. Serial.print(F("0"));
  89. Serial.print(digits);
  90. }
  91. // ----------------------------------------------------------------------------
  92. // Print the current time
  93. // ----------------------------------------------------------------------------
  94. static void printTime() {
  95. switch (weekday())
  96. {
  97. case 1: Serial.print(F("Sunday")); break;
  98. case 2: Serial.print(F("Monday")); break;
  99. case 3: Serial.print(F("Tuesday")); break;
  100. case 4: Serial.print(F("Wednesday")); break;
  101. case 5: Serial.print(F("Thursday")); break;
  102. case 6: Serial.print(F("Friday")); break;
  103. case 7: Serial.print(F("Saturday")); break;
  104. default: Serial.print(F("ERROR")); break;
  105. }
  106. Serial.print(F(" "));
  107. printDigits(hour());
  108. Serial.print(F(":"));
  109. printDigits(minute());
  110. Serial.print(F(":"));
  111. printDigits(second());
  112. return;
  113. }
  114. // ----------------------------------------------------------------------------
  115. // SerialTime
  116. // Print the current time on the Serial (USB), with leading 0.
  117. // ----------------------------------------------------------------------------
  118. void SerialTime()
  119. {
  120. uint32_t thrs = hour();
  121. uint32_t tmin = minute();
  122. uint32_t tsec = second();
  123. if (thrs<10) Serial.print('0'); Serial.print(thrs);
  124. Serial.print(':');
  125. if (tmin<10) Serial.print('0'); Serial.print(tmin);
  126. Serial.print(':');
  127. if (tsec<10) Serial.print('0'); Serial.print(tsec);
  128. if (debug>=2) Serial.flush();
  129. return;
  130. }
  131. // ----------------------------------------------------------------------------
  132. // SerialStat
  133. // Print the statistics on Serial (USB) port
  134. // ----------------------------------------------------------------------------
  135. void SerialStat(uint8_t intr)
  136. {
  137. #if _DUSB>=1
  138. if (debug>=0) {
  139. Serial.print(F("I="));
  140. if (intr & IRQ_LORA_RXTOUT_MASK) Serial.print(F("RXTOUT ")); // 0x80
  141. if (intr & IRQ_LORA_RXDONE_MASK) Serial.print(F("RXDONE ")); // 0x40
  142. if (intr & IRQ_LORA_CRCERR_MASK) Serial.print(F("CRCERR ")); // 0x20
  143. if (intr & IRQ_LORA_HEADER_MASK) Serial.print(F("HEADER ")); // 0x10
  144. if (intr & IRQ_LORA_TXDONE_MASK) Serial.print(F("TXDONE ")); // 0x08
  145. if (intr & IRQ_LORA_CDDONE_MASK) Serial.print(F("CDDONE ")); // 0x04
  146. if (intr & IRQ_LORA_FHSSCH_MASK) Serial.print(F("FHSSCH ")); // 0x02
  147. if (intr & IRQ_LORA_CDDETD_MASK) Serial.print(F("CDDETD ")); // 0x01
  148. if (intr == 0x00) Serial.print(F(" -- "));
  149. Serial.print(F(", F="));
  150. Serial.print(ifreq);
  151. Serial.print(F(", SF="));
  152. Serial.print(sf);
  153. Serial.print(F(", E="));
  154. Serial.print(_event);
  155. Serial.print(F(", S="));
  156. //Serial.print(_state);
  157. switch (_state) {
  158. case S_INIT:
  159. Serial.print(F("INIT "));
  160. break;
  161. case S_SCAN:
  162. Serial.print(F("SCAN "));
  163. break;
  164. case S_CAD:
  165. Serial.print(F("CAD "));
  166. break;
  167. case S_RX:
  168. Serial.print(F("RX "));
  169. break;
  170. case S_TX:
  171. Serial.print(F("TX "));
  172. break;
  173. case S_TXDONE:
  174. Serial.print(F("TXDONE"));
  175. break;
  176. default:
  177. Serial.print(F(" -- "));
  178. }
  179. Serial.print(F(", eT="));
  180. Serial.print( micros() - eventTime );
  181. Serial.print(F(", dT="));
  182. Serial.print( micros() - doneTime );
  183. Serial.println();
  184. }
  185. #endif
  186. }
  187. // ----------------------------------------------------------------------------
  188. // SerialName(id, response)
  189. // Check whether for address a (4 bytes in Unsigned Long) there is a name
  190. // This function only works if _TRUSTED_NODES is set
  191. // ----------------------------------------------------------------------------
  192. int SerialName(char * a, String& response)
  193. {
  194. #if _TRUSTED_NODES>=1
  195. uint32_t id = ((a[0]<<24) | (a[1]<<16) | (a[2]<<8) | a[3]);
  196. int i;
  197. for ( i=0; i< (sizeof(nodes)/sizeof(nodex)); i++) {
  198. if (id == nodes[i].id) {
  199. #if _DUSB >=1
  200. if (( debug>=3 ) && ( pdebug & P_GUI )) {
  201. Serial.print(F("G Name="));
  202. Serial.print(nodes[i].nm);
  203. Serial.print(F(" for node=0x"));
  204. Serial.print(nodes[i].id,HEX);
  205. Serial.println();
  206. }
  207. #endif // _DUSB
  208. response += nodes[i].nm;
  209. return(i);
  210. }
  211. }
  212. #endif // _TRUSTED_NODES
  213. return(-1); // If no success OR is TRUSTED NODES not defined
  214. }
  215. #if _LOCALSERVER==1
  216. // ----------------------------------------------------------------------------
  217. // inDecodes(id)
  218. // Find the id in Decodes array, and return the index of the item
  219. // Parameters:
  220. // id: The first field in the array (normally DevAddr id). Must be char[4]
  221. // Returns:
  222. // The index of the ID in the Array. Returns -1 if not found
  223. // ----------------------------------------------------------------------------
  224. int inDecodes(char * id) {
  225. uint32_t ident = ((id[3]<<24) | (id[2]<<16) | (id[1]<<8) | id[0]);
  226. int i;
  227. for ( i=0; i< (sizeof(decodes)/sizeof(codex)); i++) {
  228. if (ident == decodes[i].id) {
  229. return(i);
  230. }
  231. }
  232. return(-1);
  233. }
  234. #endif