Snorktracker
 All Data Structures Files Functions Variables Macros Pages
Utils.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 SFini
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
27 class SerialOut
28 {
29 protected:
30  String message;
31 
32 public:
34  SerialOut(String msg)
35  : message(msg)
36  {
37  String m = ":" + String(millis()) + "[" + message;
38  Serial.println(m.c_str());
39  }
42  {
43  String m = message + ":" + String(millis()) + "]";
44  Serial.println(m.c_str());
45  }
46 };
47 
49 long secondsSincePowerOn();
50 
52 bool secondsElapsed(long &lastCheckSec, const long &intervalSec)
53 {
54  long currentSec = secondsSincePowerOn();
55 
56  if (lastCheckSec == 0 || (currentSec - lastCheckSec > intervalSec)) {
57  return true;
58  }
59  return false;
60 }
61 
63 bool secondsElapsedAndUpdate(long &lastCheckSec, const long &intervalSec)
64 {
65  long currentSec = secondsSincePowerOn();
66 
67  if (lastCheckSec == 0 || (currentSec - lastCheckSec > intervalSec)) {
68  lastCheckSec = currentSec;
69  return true;
70  }
71  return false;
72 }
73 
74 #define POLY 0xedb88320
75 
76 
77 long crc32(long crc, unsigned char *buf, size_t len)
78 {
79  crc = ~crc;
80  while (len--) {
81  crc ^= *buf++;
82  for (int k = 0; k < 8; k++)
83  crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
84  }
85  return ~crc;
86 }
87 
89 void myDebugInfo(String info, bool isWebServer, bool newline);
90 
94 void MyDbg(String info, bool fromWebServer = false, bool newline = true)
95 {
96  myDebugInfo(info, fromWebServer, newline);
97 }
98 
99 
101 void myDelayLoop();
102 
104 void MyDelay(long millisDelay)
105 {
106  long m = millis();
107 
108  while (millis() - m < millisDelay) {
109  myDelayLoop();
110  yield();
111  delay(1);
112  }
113 }
114 
115 
119 String WifiGetRssiAsQuality(int rssi)
120 {
121  int quality = 0;
122 
123  if (rssi <= -100) {
124  quality = 0;
125  } else if (rssi >= -50) {
126  quality = 100;
127  } else {
128  quality = 2 * (rssi + 100);
129  }
130  return String(quality);
131 }
132 
137 String TextToUrl(String data)
138 {
139  data.replace(F("%"), F("%25"));
140  data.replace(F("&"), F("%26"));
141  data.replace(F("<"), F("%3C"));
142  data.replace(F(">"), F("%3E"));
143 
144  for (int i = 0; i < data.length(); i++) {
145  bool validChar = (data[i] == 0x09 || data[i] == 0x0A || data[i] == 0x0D || (data[i] >= 0x20 && data[i] <= 0xFF));
146 
147  if (!validChar) {
148  data[i] = '?';
149  }
150  }
151  return data;
152 }
153 
156 String TextToXml(String data)
157 {
158  data.replace(F("&"), F("&amp;"));
159  data.replace(F("<"), F("&lt;"));
160  data.replace(F(">"), F("&gt;"));
161  data.replace(F("\""), F("&quot;"));
162  return data;
163 }
164 
168 String Trim(const String &data, const String &chars)
169 {
170  String ret = data;
171 
172  for (int i = 0; i < ret.length(); i++) {
173  if (chars.indexOf(ret[i]) != -1) {
174  ret.remove(i, 1);
175  i--;
176  continue;
177  }
178  break;
179  }
180  for (int i = data.length() - 1; i >= 0; i--) {
181  if (chars.indexOf(ret[i]) != -1) {
182  ret.remove(i, 1);
183  continue;
184  }
185  break;
186  }
187  return ret;
188 }
189 
191 String formatInterval(long secs)
192 {
193  char buff[255];
194 
195  int days = secs / 60 / 60 / 24;
196  int hours = (secs / 60 / 60) % 24;
197  int minutes = (secs / 60) % 60;
198  int seconds = secs % 60;
199 
200  buff[0] = '\0';
201  if (days <= 0) {
202  sprintf(buff, "%02d:%02d:%02d", hours, minutes, seconds);
203  } else {
204  sprintf(buff, "%d %02d:%02d:%02d", days, hours, minutes, seconds);
205  }
206  return buff;
207 }
208 
210 bool scanInterval(String interval, long &secs)
211 {
212  int first = -1;
213 
214  interval = Trim(interval, F(" "));
215  first = interval.indexOf(F(":"));
216  if (first != -1) {
217  String daysString;
218  String hoursString;
219  String minutesString;
220  String secondsString;
221  long days = 0;
222  long hours = 0;
223  long minutes = 0;
224  long seconds = 0;
225 
226  int second = interval.indexOf(F(":"), first + 1);
227 
228  if (second != -1) {
229  int space = interval.indexOf(F(" "));
230 
231  if (space != -1 && space < first) {
232  daysString = interval.substring(0, space);
233  hoursString = interval.substring(space + 1, first);
234  } else {
235  hoursString = interval.substring(0, first);
236  }
237  minutesString = interval.substring(first + 1, second);
238  secondsString = interval.substring(second + 1);
239 
240  days = atol(daysString.c_str());
241  hours = atol(hoursString.c_str());
242  minutes = atol(minutesString.c_str());
243  seconds = atol(secondsString.c_str());
244 
245  if (days >= 0 &&
246  hours >= 0 && hours <= 23 &&
247  minutes >= 0 && minutes <= 59 &&
248  seconds >= 0 && seconds <= 59) {
249  secs = 0;
250  secs += days * 24 * 60 * 60;
251  secs += hours * 60 * 60;
252  secs += minutes * 60;
253  secs += seconds;
254  return true;
255  }
256  }
257  }
258  return false;
259 }
260 
264 void SetupOTA()
265 {
266  MyDbg(F("StartOTA"));
267 
268  ArduinoOTA.setHostname("SnorkTracker");
269 
270  ArduinoOTA.setPort(8266);
271 
272  ArduinoOTA.onStart([]() {
273  MyDbg(F("OTA Start"));
274  });
275  ArduinoOTA.onEnd([]() {
276  MyDbg(F("\nOTA End"));
277  });
278  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
279  MyDbg("OTA Progress: " + String(progress / (total / 100)));
280  });
281  ArduinoOTA.onError([](ota_error_t error) {
282  MyDbg("OTA Error[%u]: " + error);
283  if (error == OTA_AUTH_ERROR) MyDbg(F("OTA Auth Failed"));
284  else if (error == OTA_BEGIN_ERROR) MyDbg(F("OTA Begin Failed"));
285  else if (error == OTA_CONNECT_ERROR) MyDbg(F("OTA Connect Failed"));
286  else if (error == OTA_RECEIVE_ERROR) MyDbg(F("OTA Receive Failed"));
287  else if (error == OTA_END_ERROR) MyDbg(F("OTA End Failed"));
288  });
289 }
String formatInterval(long secs)
Definition: Utils.h:191
bool scanInterval(String interval, long &secs)
Definition: Utils.h:210
String TextToXml(String data)
Definition: Utils.h:156
void SetupOTA()
Definition: Utils.h:264
void myDebugInfo(String info, bool isWebServer, bool newline)
Definition: tracker.ino:92
long crc32(long crc, unsigned char *buf, size_t len)
Definition: Utils.h:77
String TextToUrl(String data)
Definition: Utils.h:137
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
Definition: Utils.h:94
SerialOut(String msg)
Definition: Utils.h:34
String WifiGetRssiAsQuality(int rssi)
Definition: Utils.h:119
void MyDelay(long millisDelay)
Definition: Utils.h:104
~SerialOut()
Definition: Utils.h:41
void yield(void)
Definition: tracker.ino:83
long secondsSincePowerOn()
Definition: tracker.ino:132
#define POLY
CRC-32 (Ethernet, ZIP, etc.) polynomial in reversed bit order.
Definition: Utils.h:74
String message
Mesage on contructor and destructor.
Definition: Utils.h:30
bool secondsElapsed(long &lastCheckSec, const long &intervalSec)
Definition: Utils.h:52
bool secondsElapsedAndUpdate(long &lastCheckSec, const long &intervalSec)
Definition: Utils.h:63
void myDelayLoop()
Definition: tracker.ino:122
String Trim(const String &data, const String &chars)
Definition: Utils.h:168