Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
357 lines (295 loc) · 8.65 KB
/
Copy pathmain.cpp
File metadata and controls
357 lines (295 loc) · 8.65 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "Digits.h"
// Always include ST7735.h for display constants/prototypes
#include "ST7735.h"
#ifndef WITH_QTWIN
#include <pigpio.h>
#else
#include <QtWidgets>
#include <QApplication>
#endif
// SPI handle - declared in both modes for consistency; 0 in QTWIN mode
int spi = 0;
#include <iostream>
#include <chrono>
#include <numeric>
#include <unistd.h>
#ifndef WITH_QTWIN
#define PIN_BTNA 24 // Change Theme
#define PIN_BTNB 5 // Switch weather mode / time mode
#define PIN_BTNC -1 // Not used
#define PIN_BTND -1 // Not used
#endif
// Global screen array for Qt emulator mode
#ifdef WITH_QTWIN
QLabel* screens[6] = { nullptr };
#endif
// Somme globals states
bool mode24h = true;
bool weatherForecast = false;
Theme currentTheme = Theme::VFD;
// Optionnal Weather
#ifdef WITH_QWEATHER
#include "QWeather/DisplayImageGenerator.h"
#include "QWeather/OpenWeatherGrabber.h"
#include <QtGui>
#include <QtNetwork>
time_t lastUpdatedWeather = 0;
const int NBDAYS = 7;
QImage weatherImages[NBDAYS]; // 0 is current weather 1-6 is J+X
bool redrawWeather = true;
#endif
#ifdef WITH_HTTP
#include "HttpServer.h"
#include <QCoreApplication>
bool requestCB(const HttpRequest& req)
{
if(req.target == "/weather")
{
weatherForecast = !weatherForecast;
// Force redraw and update
lastUpdatedWeather = 0;
redrawWeather = true;
return true;
}
else if (req.target.startsWith("/lcd"))
{
currentTheme = (Theme)((int)currentTheme + 1);
if (currentTheme == Theme::Theme_Number)
currentTheme = (Theme) 0;
return true;
}
return false;
}
#endif
#ifndef WITH_QTWIN
// call aFunction whenever GPIO changes state
void gpio_callback(int gpio, int level, uint32_t tick)
{
if (gpio == PIN_BTNA && level == 0) // Button A cycling all themes
{
currentTheme = (Theme)((int)currentTheme + 1);
if (currentTheme == Theme::Theme_Number)
currentTheme = (Theme) 0;
}
#ifdef WITH_QWEATHER
else if (gpio == PIN_BTNB && level == 0) // Button B switch weather/time mode
{
weatherForecast = !weatherForecast;
// Force redraw and update
lastUpdatedWeather = 0;
redrawWeather = true;
}
#endif
}
#endif
// Optionnal weather display on display 6
#ifdef WITH_QWEATHER
//! Pre-allocated display buffer (80x160 pixels RGB565 = 25,600 bytes)
// Reuse this buffer instead of allocating on every frame to reduce heap churn.
static uint16_t displayBuffer[SCREEN_WIDTH * SCREEN_HEIGHT];
//! Convert a Qimage to RGB 565 format, reusing static displayBuffer
//! Caller does NOT need to free the returned pointer - buffer is statically allocated
//! The buffer content is valid until the next call to convertToRGB565
uint16_t * convertToRGB565(const QImage& image)
{
// Convert to RGB 565 (Byte swaped) and store in static buffer
for ( int row = 0; row < image.height(); ++row )
{
for ( int col = 0; col < image.width(); ++col )
{
QColor clr( image.pixel( col , row ) );
uint16_t * pixel = &displayBuffer[row*SCREEN_WIDTH+col];
uint16_t r = ((uint16_t)clr.red()) >> 3; // Keep 5 most significant bits
uint16_t g = ((uint16_t)clr.green()) >> 2; // Keep 6 most significant bits
uint16_t b = ((uint16_t)clr.blue()) >> 3; // Keep 5 most significant bits
*pixel = (r<<11) + (g << 5) + b; // Combine to R(5)G(6)B(5) into a 16 bit integer
// Swap bytes
char* p = (char*) pixel;
char tmp = p[0];
p[0] = p[1];
p[1] = tmp;
}
}
return displayBuffer;
}
void updateWeather(const struct tm& currentime)
{
static OpenWeatherGrabber grabber;
static DisplayImageGenerator generator(80,160);
std::vector<int> v = {0,1,2,3,4,5,6};
std::vector<OpenWeatherGrabber::WeatherData> results;
grabber.grabForecastWeather(OWLAT, OWLON, v, results);
for (int i = 0; i < NBDAYS && i < results.size(); i++)
{
const OpenWeatherGrabber::WeatherData& wdata = results[i];
weatherImages[i] = generator.createWeatherPixmap(wdata.icon, wdata.mintemp, wdata.maxtemp, wdata.date).toImage();
}
}
void displayWeather(int spi, int display, int dayoffset, const struct tm& currentime)
{
const QImage& img = weatherImages[dayoffset];
// convertToRGB565 now writes to static displayBuffer - no need to delete
lcdDrawImage(spi, display, (char*) convertToRGB565(img), img.height()*img.width()*2);
}
#endif
// ============ Main ======================
int main(int argc, char ** argv)
{
#ifdef WITH_QWEATHER
#ifdef WITH_QTWIN
QApplication app(argc, argv);
#else
QGuiApplication app(argc, argv);
#endif
#endif
#ifndef WITH_QTWIN
// Init pigpio lib
int res = gpioInitialise();
if (res < 0)
{
std::cerr << "Error in gpioInitialise(): " << res << std::endl;
exit(1);
}
// Init SPI port 0
int spi = spiOpen(0, 10000000, 0);
// Configure GPIO directions
gpioSetMode(PIN_CS1, PI_OUTPUT);
gpioSetMode(PIN_CS2, PI_OUTPUT);
gpioSetMode(PIN_CS3, PI_OUTPUT);
gpioSetMode(PIN_CS4, PI_OUTPUT);
gpioSetMode(PIN_CS5, PI_OUTPUT);
gpioSetMode(PIN_CS6, PI_OUTPUT);
gpioSetMode(PIN_DC, PI_OUTPUT);
gpioSetMode(PIN_RST, PI_OUTPUT);
gpioSetMode(PIN_BLK, PI_OUTPUT);
// Buttons
gpioSetMode(PIN_BTNA, PI_INPUT);
gpioSetAlertFunc(PIN_BTNA, gpio_callback);
gpioSetMode(PIN_BTNB, PI_INPUT);
gpioSetAlertFunc(PIN_BTNB, gpio_callback);
// Select all screens (to configure all at same time)
selectDisplay(0);
gpioWrite(PIN_RST, 1);
// Init screens
lcdInit(spi, st7735_initSeq);
// Switch on backlight
gpioWrite(PIN_BLK, 1);
lcdStartPx(spi);
#else
QDialog d;
QHBoxLayout *mainLayout = new QHBoxLayout(&d);
for (int i = 0; i < 6; i++)
{
screens[i] = new QLabel("SCREEN");
mainLayout->addWidget(screens[i]);
}
d.setLayout(mainLayout);
d.setStyleSheet("background-color: black;");
d.setWindowTitle("PiClock Emulator");
mainLayout->setSpacing(2);
mainLayout->setContentsMargins(2, 2, 2, 2);
d.show();
#endif
#ifdef WITH_HTTP
HttpServer server(nullptr, requestCB);
#endif
// Current digits (pointers to images)
Digits digits;
// Keyboard shortcuts for emulator (WITH_QTWIN)
#ifdef WITH_QTWIN
QShortcut *shortcutA = new QShortcut(QKeySequence("A"), &d);
QShortcut *shortcutB = new QShortcut(QKeySequence("B"), &d);
QObject::connect(shortcutA, &QShortcut::activated, [&]() {
currentTheme = (Theme)((int)currentTheme + 1);
if (currentTheme == Theme::Theme_Number)
currentTheme = (Theme) 0;
});
QObject::connect(shortcutB, &QShortcut::activated, [&]() {
weatherForecast = !weatherForecast;
lastUpdatedWeather = 0;
redrawWeather = true;
});
#endif
while (1)
{
// Update digit style according to current theme (changed by Button A)
changeDigits(digits, currentTheme);
// Get System time
time_t tim = time(nullptr);
struct tm t = *localtime(&tim);
#ifdef WITH_QWEATHER
if (tim - lastUpdatedWeather > 3600) // Update each hour
{
updateWeather(t);
lastUpdatedWeather = tim;
redrawWeather = true;
}
#endif
#ifdef WITH_QWEATHER
if (!weatherForecast)
{
#endif
// Display digits on LCD
// Hours + AM/PM for 12h mode
if (!mode24h)
{
// 12h mode
int hour = t.tm_hour;
if (hour > 12)
hour-=12;
else if (hour == 0)
hour = 12;
lcdDrawNumber(spi, display1, hour/10, digits);
lcdDrawNumber(spi, display2, hour%10, digits);
lcdDrawNumber(spi, display6, (t.tm_hour < 12)?AM:PM, digits);
}
else
{
// 24h mode
lcdDrawNumber(spi, display1, t.tm_hour/10, digits);
lcdDrawNumber(spi, display2, t.tm_hour%10, digits);
#ifdef WITH_QWEATHER
if (redrawWeather)
{
int dayoffset = (t.tm_hour >= 18)?1:0;
displayWeather(spi, display6, dayoffset, t);
redrawWeather = false;
}
#else
lcdDrawNumber(spi, display6, SPACE, digits);
#endif
}
// Minutes
lcdDrawNumber(spi, display4, t.tm_min/10, digits);
lcdDrawNumber(spi, display5, t.tm_min%10, digits);
// Blinking colon on each second
lcdDrawNumber(spi, display3, ((t.tm_sec % 2) == 0)? COLON:SPACE, digits);
#ifdef WITH_QWEATHER
}
else
{
// Display weather forecast one day per display
if (redrawWeather)
{
for (int i = 0; i < 6; i++)
displayWeather(spi, i+1, i, t);
redrawWeather = false;
}
}
#endif
#if defined(WITH_HTTP) || defined(WITH_QTWIN)
QCoreApplication::processEvents();
#endif
// Check if emulator window was closed
#ifdef WITH_QTWIN
if (!d.isVisible())
break;
#endif
// Wait a little bit to reduce cpu load
usleep(50000);
}
#ifndef WITH_QTWIN
gpioTerminate();
#endif
return 0;
}