-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDisplay.cpp
More file actions
171 lines (148 loc) · 3.62 KB
/
Display.cpp
File metadata and controls
171 lines (148 loc) · 3.62 KB
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
/*
* Display.cpp
*
* Created on: Mar 4, 2018
* Author: chris.l
*/
#include <stdarg.h>
#include <time.h>
#include "ESP8266WiFi.h"
#include "Display.h"
#include "Log.h"
#include "DialogInput_plain_10.h"
static const char* TAG = "Display";
Display::Display(GPS& gps, NTP& ntp, uint8_t sda, uint8_t scl) : _dsp(0x3c, sda, scl), _gps(gps), _ntp(ntp)
{
// TODO Auto-generated constructor stub
}
Display::~Display()
{
// TODO Auto-generated destructor stub
}
void Display::begin()
{
dlog.info(TAG, F("initializing display"));
if (!_dsp.init())
{
dlog.error(TAG, F("display.init() failed!"));
}
//_dsp.flipScreenVertically();
font(ArialMT_Plain_10);
}
void Display::end()
{
_dsp.end();
}
void Display::process()
{
time_t secs = _gps.getSeconds();;
struct tm tm;
gmtime_r(&secs, &tm);
clear();
font(DialogInput_plain_10);
IPAddress ip = WiFi.localIP();
const char* wifi_status_str;
int wifi_status = WiFi.status();
switch (wifi_status)
{
case WL_CONNECTED:
wifi_status_str = "READY";
break;
case WL_NO_SSID_AVAIL:
wifi_status_str = "NSSID";
break;
case WL_CONNECT_FAILED:
wifi_status_str = "FAIL";
break;
case WL_IDLE_STATUS:
wifi_status_str = "IDLE";
break;
case WL_DISCONNECTED:
wifi_status_str = "DISCON";
break;
default:
wifi_status_str = "<UNK>";
break;
}
align(TEXT_ALIGN_CENTER);
print(64, 0, "%04d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,
tm.tm_mon+1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec);
align(TEXT_ALIGN_LEFT);
print(0, 10, "Sats: %d", _gps.getSatelliteCount());
print(0, 20, "Reqs: %d", _ntp.getReqCount());
print(0, 30, "Rsps: %d", _ntp.getRspCount());
if (_gps.isValid())
{
uint32_t seconds = secs - _gps.getValidSince();
uint32_t days = seconds / 86400;
seconds -= days * 86400;
uint32_t hours = seconds /3600;
seconds -= hours * 3600;
uint32_t minutes = seconds / 60;
seconds -= minutes * 60;
align(TEXT_ALIGN_RIGHT);
print(127, 40, "%dd %02dh %02dm %02ds", days, hours, minutes, seconds);
}
else
{
if (_gps.isGPSValid())
{
align(TEXT_ALIGN_RIGHT);
print(127, 40, "%d till VALID", _gps.getValidDelay());
}
else
{
align(TEXT_ALIGN_CENTER);
print(64, 40, "GPS NOT VALID");
}
}
align(TEXT_ALIGN_RIGHT);
print(127, 50, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
align(TEXT_ALIGN_LEFT);
print(0, 50, "%s", wifi_status_str);
display();
}
void Display::message(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
clear();
align(TEXT_ALIGN_CENTER);
vprint(64, 32, fmt, ap);
display();
va_end(ap);
}
void Display::clear()
{
_dsp.clear();
}
void Display::display()
{
_dsp.display();
}
void Display::font(const uint8_t* fontData)
{
_dsp.setFont(fontData);
}
void Display::align(OLEDDISPLAY_TEXT_ALIGNMENT alignment)
{
_dsp.setTextAlignment(alignment);
}
void Display::print(int16_t x, int16_t y, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprint(x, y, fmt, ap);
va_end(ap);
}
void Display::vprint(int16_t x, int16_t y, const char* fmt, va_list ap)
{
vsnprintf(_buffer, DISPLAY_BUFFER_LEN, fmt, ap);
_buffer[DISPLAY_BUFFER_LEN-1] = '\0';
_dsp.drawString(x, y, _buffer);
}