-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageController.cpp
216 lines (168 loc) · 4.52 KB
/
PageController.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
#include <Wire.h>
#include <avr/pgmspace.h>
#include "PageController.h"
void fmtDateTime(const RtcDateTime& dt, char &buf, int buflen)
{
snprintf_P(&buf, buflen,
PSTR("%04u-%02u-%02u %02u:%02u:%02u"),
dt.Year(), dt.Month(), dt.Day(),
dt.Hour(), dt.Minute(), dt.Second());
}
PageController::PageController(RtcDS3231 *rtc, getData_cb getData)
{
// initialize with the I2C addr 0x3C and generate
// HV for display internally.
display = new Adafruit_SSD1306(-1);
display->begin(SSD1306_SWITCHCAPVCC, 0x3C);
display->clearDisplay();
display->setTextSize(1);
display->setTextColor(WHITE);
display->setRotation(2);
this->Rtc = rtc;
this->getData = getData;
this->currentPage = 1;
}
void PageController::drawTitle(const char *text)
{
if(text != NULL) {
display->setCursor((128 - strlen_P(text) * 6) / 2, 2);
display->println(GetFString(text));
display->drawRect(0, 0, 128, 11, WHITE);
}
}
void PageController::drawMenu(const char *menuItem1, const char *menuItem2)
{
if(menuItem1 != NULL && strlen_P(menuItem1) > 0) {
//display->setCursor((64 - strlen_P(menuItem1) * 6) / 2, 54);
display->setCursor(0, 54);
display->println(GetFString(menuItem1));
}
if(menuItem2 != NULL && strlen_P((const char *)menuItem2) > 0) {
//display->setCursor(64 + (64 - strlen_P(menuItem2) * 6) / 2, 54);
display->setCursor(127 - strlen_P(menuItem2) * 6, 54);
display->println(GetFString(menuItem2));
}
}
void PageController::drawFStringAt(const char *string, int x, int y)
{
display->setCursor(x, y);
display->println(GetFString(string));
}
void PageController::drawStringAt(const char *string, int x, int y)
{
display->setCursor(x, y);
display->println(string);
}
void PageController::drawStringAt(long int s, int x, int y)
{
display->setCursor(x, y);
display->println(s);
}
void PageController::drawPage1()
{
auto data = this->getData();
currentPage = 1;
display->clearDisplay();
drawTitle((page1_title));
if(data->isBuzzing) {
drawMenu(quiet, next);
} else {
drawMenu(NULL, next);
}
drawFStringAt(lifetime, getCol(1), getRow(0));
drawFStringAt(current, getCol(2), getRow(1));
drawFStringAt(lifetime, getCol(1), getRow(2));
drawFStringAt(current, getCol(2), getRow(3));
int col = getCol(11);
drawStringAt(data->pump1->GetLifetimeCount(), col, getRow(0));
drawStringAt(data->pump1->GetCurrentCount(), col, getRow(1));
drawStringAt(data->pump2->GetLifetimeCount(), col, getRow(2));
drawStringAt(data->pump2->GetCurrentCount(), col, getRow(3));
display->display();
}
void PageController::drawPage2()
{
char datestring[20];
currentPage = 2;
Rtc->IsDateTimeValid();
RtcDateTime dt = Rtc->GetDateTime();
fmtDateTime(dt, *datestring, 20);
display->clearDisplay();
drawTitle(page2_title);
drawMenu(reset, next);
display->setCursor(getCol(1), getRow(1));
display->println(datestring);
display->setCursor(getCol(1), getRow(2));
display->println(F("2013-12-11 10:09:08"));
display->setCursor(getCol(1), getRow(3));
display->println(F("2006-05-04 03:02:01"));
display->display();
}
void PageController::drawPage3()
{
auto data = this->getData();
long s1 = data->pump1->GetShortestRun();
long a1 = data->pump1->GetAverageRun();
long l1 = data->pump1->GetLongestRun();
long s2 = data->pump2->GetShortestRun();
long a2 = data->pump2->GetAverageRun();
long l2 = data->pump2->GetLongestRun();
currentPage = 3;
display->clearDisplay();
drawTitle(page3_title);
drawMenu(reset, next);
drawFStringAt(min_avg_max, getCol(1), getRow(1));
drawStringAt(s1, getCol(0x1), getRow(2));
drawStringAt(a1, getCol(0x8), getRow(2));
drawStringAt(l1, getCol(0xF), getRow(2));
drawStringAt(s2, getCol(0x1), getRow(3));
drawStringAt(a2, getCol(0x8), getRow(3));
drawStringAt(l2, getCol(0xF), getRow(3));
display->display();
}
void PageController::drawResetWindow(int num)
{
display->clearDisplay();
drawTitle(reset);
display->setTextSize(4);
display->setCursor(54, 20);
display->println(num);
display->setTextSize(1);
display->display();
}
void PageController::drawCurrentPage()
{
switch(currentPage) {
case 1:
drawPage1();
break;
case 2:
drawPage2();
break;
case 3:
drawPage3();
break;
}
}
void PageController::drawNextPage()
{
switch(currentPage) {
case 1:
drawPage2();
break;
case 2:
drawPage3();
break;
case 3:
drawPage1();
break;
}
}
int PageController::getRow(int r)
{
return 12 + r * 9;
}
int PageController::getCol(int c)
{
return 1 + c * 6;
}