-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWHD_Util.h
141 lines (114 loc) · 5.23 KB
/
WHD_Util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// WHD_Util.h
// DNOCR_LIB
//
// Created by Simon Hewitt on 07/01/2015.
// Copyright (c) 2015 Simon Hewitt. All rights reserved.
//
// Utility library , mostly string manupulation and other utilities not present in Arduino/LinkIt libraries.
// Note the code is intended for the LinkitOne development board, a dialect of Arduino
// Can be compiled in Xcode (and maybe other C++ IDEs) using the LFlash emulator library.
// NOTE the STL is avaiulable for Arduino, may use this more in the future https://github.com/rpavlik/StandardCplusplus
//
// Contact [email protected]
//
#ifndef __DNOCR_LIB__WHD_Util__
#define __DNOCR_LIB__WHD_Util__
#include "LFlash.h"
// define XCODE to dev and test under XCODE, standard C++. Comment out for normal Arduino use
#define XCODE 1
class DNOCR_Config;
/*
** Global constants
*/
#define CRLF "\r\n" // Windows line ending
// configuration files in Flash memory
#define CONFIGFILENAME "RMAC"
#define CONFIGFILEEXT ".DAT"
#define CONFIGBACKUPEXT ".BAK"
#define CONFIGDEFAULTEXT ".CFG"
#define LOG_ALARM 'A' // Logs relating to alarms, (not code faults)
#define LOG_WARN 'W'
#define LOG_INFO 'I'
#define LOG_ERROR 'E'
#define LOG_VERBOSE 'V' // These are normally sent to serial port but no logged
const bool VERBOSE_LOG_ON = true ;// unless this is true - warning log files can get big!
/*
** Global constants
*/
const int IDLength = 6; // 5 chars plus \0
const int ShortNameLength = 9; // 8 chars plus \0
const int LongNameLength = 33; // 32 chars plus \0
const int MaxDigitalAlarms = 8; // thats the number of digital channels built!
const int MaxAnalogueChannels = 3; // ditto 3 analogue channels
const int MaxCertifiedPhones = 32; // arbitrary limit
const int MaxPhoneLength = 16; // ITU says 15 max, plus \0
const int PINlength = 5; // Security PIN
/*
** WHD_Util - mostly static utility routines
*/
class WHD_Util
{
public:
// ================ String manupulation and clean up ===============
static int safeCopy (char *dest, char *source, int l);
// copies source to dest , l is length of dest.
// Ensures no overflow and is null terminated, so at most l-1 characters copied
static int cleanTelephone (char *tnum);
// Cleans up telephone number in place to a string of contiguous digits, anything else discarded,
// and ensures it startes with 0044 if no intl code specified
// eg "07584 658227" -> "00447584658227", "(01792)-455806" -> 00441792455806
// +34 123 4567 -> 00341234567, 0034 123 4567 -> 00341234567 etc
// truncates to 15 chars if necessary
static void cleanStr (char * s);
// removes leading and trailing spaces, multiple spaces reduced to one, removes any nonprinting chars
// MUST be null terminated
static void cleanNumStr (char *s);
// modifies str in place to a nice clean string, leading +- and . and digits only
// not THAT clever 22.22.22 probably OK?
// ================ error handling ===============
static void writeLog (char logType, const char *s); // /log/RMAC000001.LOG etc
static void writeLog (char logType, const char *s, const int n); // /log/RMAC000001.LOG etc
static void writeLog (char logType, const char *s, const char *s2);
static void fatalError (const char *); // if program really cannot progress - just sits there flashing LEDs!
static void fatalError (const char *, const char *);
static void fatalError (const char *, int);
// ================ Date & Time ===============
static void timeStamp (char *dt );
// buff must be 22 chars. Returns yyyy-mm-dd hh:mm:ss
static void timeStamp (char *dt, const char *seperators );
// as above but 3-char separators string gives :
// (if s[0] is A, s[1] is B, s[2] is C -> yyyyAmmAddBhhCmmCss
// eg (datebuff, "/-:") -> yyyy/mm/dd-hh:mm:ss
static void timeStamp (char *dt, const char *seperators , const datetimeInfo *theDate);
// as above but using the date supplied
static long secondsDifference(const datetimeInfo *startDate, const datetimeInfo *endDate);
static long minutesDifference (const datetimeInfo *firstDate, const datetimeInfo *secondDate);
// returns minutes by which second date is after first date
// negative if second date is BEFORE first date
static long secondsSince(const datetimeInfo *startDate);
static unsigned long millisSince (unsigned long now, unsigned long then);
static unsigned long millisSince (unsigned long then);
};
class ardTime
{
// Use Arduino time constructs to hold and maniuplate times.
// Basic holding structure is DatetimeInfo
public:
ardTime (char *str);
ardTime( bool now); // inits to NOW if now is true
ardTime();
void getArdTimeStr (char *str);
void getArdTimeStr ( char *str, const char *separators);
void setArdTimeNow();
void setArdTimeZero();
bool setArdTimeFromStr(const char *tmStr);
long secondsDifference();
long secondsDifference (const ardTime *bTime);
long minutesDifference (const ardTime *bTime);
static int dayOfEpoch (); // Returns number of days since 1 Jan 2015
private:
datetimeInfo _dt;
void getArdTimeCPPTime (std::tm *stdtm);
};
#endif /* defined(__DNOCR_LIB__WHD_Util__) */