-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplay.cpp
298 lines (256 loc) · 12.9 KB
/
Display.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include "logging.h"
#include "WiFiService.h"
#include "Display.h"
#include "DoorState.h"
bool bInfoUseBestTime = false;
extern ansiVT220Logger MyLogger;
extern UDPWiFiService *pMyUDPService;
#ifdef UAP_SUPPORT
extern DoorState *pGarageDoor;
//extern DoorStatusPin *pDoorSwitchPin;
#endif
#ifdef BME280_SUPPORT
extern struct TEMP_STATS
{
float temperature;
float pressure; // at sea level
float humidity;
float dewpoint;
uint32_t ulTimeOfReadingms;
} EnvironmentResults;
#endif
extern const char * VERSION;
// Error message process used when generating messages during interrupt
constexpr uint8_t ERROR_LINE = 25;
constexpr uint8_t NWPrintStartLine = 15; // Start line for network stats
auto fgInfoErrorColour = ansiVT220Logger::FG_WHITE;
auto bgInfoErrorColour = ansiVT220Logger::BG_GREEN;
String sInfoErrorMsg;
time_t timeError;
void GetLocalTime ( String &Result )
{
if ( pMyUDPService != nullptr && pMyUDPService->IsConnected () )
{
timeError = 0;
pMyUDPService->GetLocalTime ( Result );
Result += " ";
}
}
/// @brief Logs error to error line the provided error message prepended with local date and time
/// @param s message to be logged
/// @param bInISR indicates if code being called from interrupt level code, defaults to false
void Error ( String s, bool bInISR )
{
String Result;
//Serial.println ( s );
// do not call when in ISR level code path
if ( !bInISR )
{
pMyUDPService->GetLocalTime ( Result );
}
sInfoErrorMsg = Result + s;
fgInfoErrorColour = ansiVT220Logger::FG_BRIGHTWHITE;
bgInfoErrorColour = ansiVT220Logger::BG_BRIGHTRED;
}
/// @brief Logs info to error line the provided error message prepended with local date and time
/// @param s message to be logged
/// @param bInISR indicates if code being called from interrupt level code, defaults to false
void Info ( String s, bool bInISR )
{
String Result;
//Serial.println ( s);
// do not call when in ISR level code path
if ( !bInISR )
{
bInfoUseBestTime = false;
GetLocalTime ( Result );
}
else
{
bInfoUseBestTime = true;
}
sInfoErrorMsg = Result + s;
fgInfoErrorColour = ansiVT220Logger::FG_WHITE;
bgInfoErrorColour = ansiVT220Logger::BG_BLUE;
}
void DisplaylastInfoErrorMsg ()
{
#ifdef MNDEBUG
String sTime;
if ( bInfoUseBestTime )
{
pMyUDPService->GetLocalTime( sTime );
sInfoErrorMsg = sTime + sInfoErrorMsg;
bInfoUseBestTime = false;
}
MyLogger.ClearLine ( ERROR_LINE );
MyLogger.COLOUR_AT ( fgInfoErrorColour, bgInfoErrorColour, ERROR_LINE, 1, sInfoErrorMsg );
#endif
}
/**
* @brief Display the uptime on the screen
* @param logger the logger to use
* @param line the line to display the uptime
* @param row the row to display the uptime
* @param Foreground the colour of the text
* @param Background the colour of the background
*/
void DisplayUptime ( ansiVT220Logger logger, uint8_t line, uint8_t row, ansiVT220Logger::colours Foreground, ansiVT220Logger::colours Background )
{
static uint32_t ulStartTime = 0UL;
uint32_t ulNow = millis ();
// set initial start time
if ( ulStartTime == 0 )
{
ulStartTime = ulNow;
}
else
{
// check for wrap around (when millis() overflows and resets to zero)
if ( ulNow > ulStartTime )
{
uint32_t ulTotalNumSeconds = ( ulNow-ulStartTime ) / 1000;
uint32_t ulDays = ulTotalNumSeconds / ( 60 * 60 * 24 );
uint32_t ulHours = ( ulTotalNumSeconds / ( 60 * 60 ) ) % 24;
uint32_t ulMinutes = ( ulTotalNumSeconds / 60 ) % 60;
uint32_t ulSecs = ulTotalNumSeconds % 60;
char sUpTime [ 20 ];
snprintf ( sUpTime, sizeof(sUpTime), "%02d:%02d:%02d:%02d", ulDays, ulHours, ulMinutes, ulSecs );
logger.COLOUR_AT ( Foreground, Background, line, row, sUpTime );
}
else
{
// wrapped around
ulStartTime = ulNow;
}
}
}
// Debug information for ANSI screen with cursor control
void DisplayStats ( void )
{
#ifdef MNDEBUG
// display uptime
DisplayUptime ( MyLogger, 1, 1, ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK );
static time_t LastTime = 0;
#ifdef UAP_SUPPORT
String Heading = F ( "Garage Door Control - ver " );
#else
String Heading = F ( "Temp Sensor - ver " );
#endif
Heading += String ( VERSION );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 1, 20, Heading );
String sTime;
pMyUDPService->GetLocalTime ( sTime );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 1, 60, sTime );
#ifdef UAP_SUPPORT
String result;
if ( pGarageDoor != nullptr )
{
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 4, 0, F ( "Light is " ) );
MyLogger.ClearPartofLine ( 4, 14, 3 );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, 4, 14, pGarageDoor->IsLit () ? F ( "On" ) : F ( "Off" ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 5, 0, F ( "State is " ) );
MyLogger.ClearPartofLine ( 5, 14, 8 );
if ( pGarageDoor->GetDoorState() == DoorState::Closed )
{
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, 5, 14, pGarageDoor->GetDoorDisplayState () );
}
else
{
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_RED, ansiVT220Logger::BG_BLACK, 5, 14, pGarageDoor->GetDoorDisplayState () );
}
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 6, 0, F ( "Direction is " ) );
MyLogger.ClearPartofLine ( 6, 14, 10 );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, 6, 14, pGarageDoor->GetDoorDirectionName () );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 4, 25, F ( "Light Off count " ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_GREEN, ansiVT220Logger::BG_BLACK, 4, 43, String ( pGarageDoor->GetLightOffCount () ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 5, 25, F ( "Door Opened count " ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_GREEN, ansiVT220Logger::BG_BLACK, 5, 43, String ( pGarageDoor->GetDoorOpenedCount () ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 6, 25, F ( "Door Closed count " ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_GREEN, ansiVT220Logger::BG_BLACK, 6, 43, String ( pGarageDoor->GetDoorClosedCount () ) );
}
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 9, 43, F ( "Count Called Unchngd Matched UnMtchdSpurious Duration" ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 10, 25, F ( "Switch Presssed " ) );
if ( pGarageDoor->IsSwitchConfigured() )
{
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_GREEN, ansiVT220Logger::BG_BLACK, 10, 43, String ( pGarageDoor->GetSwitchMatchCount() ) );
pGarageDoor->SwitchDebugStats ( result );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 10, 50, result );
}
#endif
#ifdef BME280_SUPPORT
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 12, 0, F ( "Temperature is " ) );
MyLogger.ClearPartofLine ( 12, 16, 6 );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_RED, ansiVT220Logger::BG_BLACK, 12, 16, String ( EnvironmentResults.temperature ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 13, 0, F ( "Humidity is " ) );
MyLogger.ClearPartofLine ( 13, 16, 6 );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, 13, 16, String ( EnvironmentResults.humidity ) );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, 14, 0, F ( "Pressure is " ) );
MyLogger.ClearPartofLine ( 14, 16, 7 );
MyLogger.COLOUR_AT ( ansiVT220Logger::FG_YELLOW, ansiVT220Logger::BG_BLACK, 14, 16, String ( EnvironmentResults.pressure ) );
#endif
DisplayNWStatus ( MyLogger );
DisplaylastInfoErrorMsg ();
#endif
}
/**
* @brief Displays the network status on the screen
* @param logger the logger to use for displaying the network status
*/
void DisplayNWStatus ( ansiVT220Logger logger )
{
// print the SSID of the network you're attached to:
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine, 0, F ( "SSID: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine, 23, WiFi.SSID () );
if ( pMyUDPService != nullptr )
{
FixedIPList *m_pMulticastDestList = pMyUDPService->GetMulticastList ();
if ( m_pMulticastDestList != nullptr )
{
uint8_t iterator = m_pMulticastDestList->GetIterator ();
IPAddress mcastDest;
while ( (mcastDest = m_pMulticastDestList->GetNext ( iterator )) != IPAddress((uint32_t)0) )
{
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + iterator - 1, 41, "Mcast #" + String ( iterator ) + ": " );
logger.ClearPartofLine ( NWPrintStartLine + iterator - 1, 61, 15 );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + iterator - 1, 61, pMyUDPService->ToIPString ( mcastDest ) );
}
}
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 1, 0, F ( "My Hostname: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 1, 23, pMyUDPService->GetHostName () );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 2, 0, F ( "IP Address: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 2, 23, pMyUDPService->ToIPString ( WiFi.localIP () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 3, 0, F ( "Subnet Mask: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 3, 23, pMyUDPService->ToIPString ( WiFi.subnetMask () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 4, 0, F ( "Local Multicast Addr: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 4, 23, pMyUDPService->ToIPString ( pMyUDPService->GetMulticastAddress () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 4, 41, F ( "WiFi connect/fail: " ) );
logger.ClearPartofLine ( NWPrintStartLine + 4, 61, 10 );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 4, 61, String ( pMyUDPService->GetBeginCount () ) + "/" + String ( pMyUDPService->GetBeginTimeOutCount () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 5, 41, F ( "Multicasts sent: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 5, 61, String ( pMyUDPService->GetMCastSentCount () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 6, 41, F ( "Requests recvd: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 6, 61, String ( pMyUDPService->GetRequestsReceivedCount () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 7, 41, F ( "Replies sent: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 7, 61, String ( pMyUDPService->GetReplySentCount () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 5, 0, F ( "Mac address: " ) );
byte bMac [ 6 ] = {0};
WiFi.macAddress ( bMac );
char s [ 18 ];
snprintf ( s, sizeof(s), "%02X:%02X:%02X:%02X:%02X:%02X", bMac [ 5 ], bMac [ 4 ], bMac [ 3 ], bMac [ 2 ], bMac [ 1 ], bMac [ 0 ] );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 5, 23, s );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 6, 0, F ( "Gateway Address: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 6, 23, pMyUDPService->ToIPString ( WiFi.gatewayIP () ) );
// print the received signal strength:
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 7, 0, F ( "Signal strength (RSSI):" ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 7, 23, String ( WiFi.RSSI () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 7, 30, F ( " dBm" ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 8, 0, F ( "WiFi Status: " ) );
logger.ClearPartofLine ( NWPrintStartLine + 8, 23, 15 );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 8, 23, pMyUDPService->WiFiStatusToString ( WiFi.status () ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_WHITE, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 8, 41, F ( "WiFi Service State: " ) );
logger.COLOUR_AT ( ansiVT220Logger::FG_CYAN, ansiVT220Logger::BG_BLACK, NWPrintStartLine + 8, 61, String ( pMyUDPService->GetState () ) );
}
}