-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunUi.cpp
More file actions
424 lines (413 loc) · 11 KB
/
Copy pathRunUi.cpp
File metadata and controls
424 lines (413 loc) · 11 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* This file is part of the Eclipse2024 project. It is subject to the GPLv3
* license terms in the LICENSE file found in the top-level directory of this
* distribution and at
* https://github.com/jjackowski/eclipse2024/blob/master/LICENSE.
* No part of the Eclipse2024 project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*
* Copyright (C) 2024 Jeff Jackowski
*/
#include <boost/exception/diagnostic_information.hpp>
#include <duds/ui/graphics/BppFontPool.hpp>
#include "RunUi.hpp"
#include "SchedulePage.hpp"
#include "NetworkPage.hpp"
#include "MenuPage.hpp"
#include "SunPages.hpp"
#include <filesystem>
extern std::atomic_bool quit;
extern duds::ui::graphics::BppFontPool FontPool;
static const duds::os::linux::EventTypeCode EventDial = { EV_REL, REL_DIAL };
static const duds::os::linux::EventTypeCode EventSelect = { EV_KEY, BTN_SOUTH };
static const duds::os::linux::EventTypeCode EventUp = { EV_KEY, BTN_DPAD_UP };
static const duds::os::linux::EventTypeCode EventDown = { EV_KEY, BTN_DPAD_DOWN };
static const duds::os::linux::EventTypeCode EventLeft = { EV_KEY, BTN_DPAD_LEFT };
static const duds::os::linux::EventTypeCode EventRight = { EV_KEY, BTN_DPAD_RIGHT };
RunUi::RunUi(
duds::hardware::display::BppGraphicDisplaySptr &&gdisp,
duds::ui::graphics::BppImageArchiveSptr &&iarc,
const duds::hardware::devices::clocks::LinuxClockSptr &lcptr,
DisplayStuff &dstuff,
duds::hardware::interface::DigitalPin &buz
//int toff
) : disp(std::move(gdisp)), iconArc(std::move(iarc)), clock(lcptr), page(0),
screen(iconArc, disp->dimensions()), displaystuff(dstuff), attn(lcptr, buz)
{
pages[Clock_Page] = std::make_unique<ClockPage>();
pages[GPS_Status] = std::make_unique<GpsPage>();
pages[Eclipse_Times] = std::make_unique<EclipsePage>();
pages[Totality_Times] = std::make_unique<TotalityPage>();
pages[Schedule] = std::make_unique<SchedulePage>(
FontPool.getStringCache("Text"),
attn
);
pages[SunAz] = std::make_unique<SunAzPage>(lcptr);
pages[SunEl] = std::make_unique<SunElPage>(lcptr);
pages[System] = std::make_unique<SystemPage>();
pages[Network] = std::make_unique<NetworkPage>();
pages[Sensors] = std::make_unique<SensorPage>();
pages[Menu] = std::make_unique<MenuPage>(
FontPool.getStringCache("Text"),
dstuff,
attn
);
}
void RunUi::incPage(const DisplayInfo &di, Page::SelectionCause sc, int val) {
// do nothing if a button is released
if (val == 0) return;
int p = page;
Page::SelectionResponse sr = Page::SkipPage;
do {
if (++p >= PageCycle) {
p = Clock_Page;
}
if (pages[p]) {
sr = pages[p]->select(di, sc);
}
} while (sr == Page::SkipPage);
changePage(di, sc, p);
}
void RunUi::decPage(const DisplayInfo &di, Page::SelectionCause sc, int val) {
// do nothing if a button is released
if (val == 0) return;
int p = page;
Page::SelectionResponse sr = Page::SkipPage;
do {
if (--p < Clock_Page) {
p = PageCycle - 1;
}
if (pages[p]) {
sr = pages[p]->select(di, sc);
}
} while (sr == Page::SkipPage);
changePage(di, sc, p);
}
void RunUi::advancePageTime() {
if (!pagepinned) {
duds::data::Measurement::TimeSample time;
clock->sampleTime(time);
pageswitch = time.value + pagetime * 5;
}
}
void RunUi::changePage(const DisplayInfo &di, Page::SelectionCause sc, int p) {
pages[page]->hide(di, &screen);
page = p;
pages[page]->show(di, &screen);
if (sc == Page::SelectUser) {
advancePageTime();
}
// additional input for the schedule page
if (page == Schedule) {
SchedulePage *sp = (SchedulePage*)pages[Schedule].get();
if (!sp) {
// shouldn't happen
return;
}
upKey = inhand->connect(
EventUp,
[sp, this](auto ig, auto val) {
sp->moveNeg(val);
advancePageTime();
}
);
downKey = inhand->connect(
EventDown,
[sp, this](auto ig, auto val) {
sp->move(val);
advancePageTime();
}
);
/*
rotor = inhand->connect(
EventDial,
[sp, this](auto ig, auto val) {
sp->move(val);
advancePageTime();
}
);
*/
}
// additional input for the menu page
else if (page == Menu) {
MenuPage *sp = (MenuPage*)pages[Menu].get();
if (!sp) {
// shouldn't happen
return;
}
upKey = inhand->connect(
EventUp,
[sp, this](auto ig, auto val) {
sp->moveNeg(val);
}
);
downKey = inhand->connect(
EventDown,
[sp, this](auto ig, auto val) {
sp->move(val);
}
);
blockPrev.block();
blockNext.block();
blockPin.block();
backKey = inhand->connect(
EventLeft,
[this, sp, &di](auto ig, auto val) {
if (val) {
if (sp->back()) {
decPage(di, Page::SelectUser);
advancePageTime();
}
}
}
);
forwardKey = inhand->connect(
EventRight,
[this, sp, &di](auto ig, auto val) {
if (val) {
if (sp->forward()) {
incPage(di, Page::SelectUser);
advancePageTime();
}
}
}
);
selectKey = inhand->connect(
EventSelect,
[sp, this](auto ig, auto val) {
if (val) {
sp->chose();
}
}
);
// keep page up until user or attention selects another page; avoids
// messing with pinned page status
pageswitch = duds::time::interstellar::NanoTime::max();
} else {
// if no other page is using the input, these could probably remain
// connected with no noticeable effect
upKey.disconnect();
downKey.disconnect();
//rotor.disconnect();
backKey.disconnect();
forwardKey.disconnect();
selectKey.disconnect();
blockPrev.unblock();
blockNext.unblock();
blockPin.unblock();
}
}
void RunUi::pinPage(int val) {
// do nothing if a button is released
if (val == 0) return;
if (pagepinned) {
pagepinned = false;
screen.hidePin();
duds::data::Measurement::TimeSample time;
clock->sampleTime(time);
pageswitch = time.value + pagetime;
} else {
pagepinned = true;
screen.showPin();
pageswitch = duds::time::interstellar::NanoTime::max();
}
}
struct CheckInputEvent {
const char *name;
duds::os::linux::EventTypeCode etc;
bool required;
bool found = false;
constexpr CheckInputEvent(
const char *n,
duds::os::linux::EventTypeCode c,
bool req = true
) : name(n), etc(c), required(req) { }
};
bool RunUi::initInput() {
CheckInputEvent checkInputEvents[] = {
{ "dial", EventDial, false },
{ "select", EventSelect },
{ "up", EventUp },
{ "down", EventDown },
{ "left", EventLeft },
{ "right", EventRight }
};
int found = 0;
const std::filesystem::path inputdevpath("/dev/input");
for (auto const &infile : std::filesystem::directory_iterator(inputdevpath)) {
if (!infile.is_character_file()) continue;
std::cout << "Trying " << infile.path() << std::endl;
duds::os::linux::EvdevInputSptr eis;
try {
eis = duds::os::linux::EvdevInput::make(infile.path());
} catch (...) { }
if (!eis) continue;
// look for input events
bool keep = false;
for (CheckInputEvent &cie : checkInputEvents) {
if (eis->hasEvent(cie.etc)) {
if (!cie.found) {
cie.found = true;
++found;
}
keep = true;
}
}
if (keep) {
std::cout << "\tkeeping" << std::endl;
evdevs.emplace_back(std::move(eis));
}
}
// show inputs found
for (CheckInputEvent &cie : checkInputEvents) {
if (cie.required) {
if (cie.found) {
std::cout << "Found required input for ";
} else {
std::cout << "MISSING REQUIRED input for ";
}
} else {
if (cie.found) {
std::cout << "Found optional input for ";
} else {
std::cout << "Missing optional input for ";
}
}
std::cout << cie.name << std::endl;
}
// make input handlers; used even if no input
inhand = std::make_shared<duds::os::linux::InputHandlers>();
// check for adequate input
const auto numEvents = std::end(checkInputEvents) - std::begin(checkInputEvents);
if (
(found == numEvents) ||
((found == (numEvents - 1)) && !checkInputEvents[0].found)
) {
// provide input handlers
for (const duds::os::linux::EvdevInputSptr &eis : evdevs) {
eis->connect(inhand);
eis->usePoller(poller);
}
// eat input
for (int limit = 64; limit && (poller.respond() == poller.maxEvents); --limit) { }
return true;
}
return false;
}
void RunUi::run()
try {
duds::ui::graphics::BppImage frame(disp->dimensions());
DisplayInfo dinfo;
duds::data::Measurement::TimeSample time;
duds::data::Measurement::TimeSample::Value stime;
clock->sampleTime(time);
stime = time.value;
pageswitch = time.value + pagetime;
movePrev = inhand->connect(
EventLeft,
std::bind(
&RunUi::decPage,
this,
std::ref(dinfo),
Page::SelectUser,
std::placeholders::_2
)
);
blockPrev = boost::signals2::shared_connection_block(movePrev, false);
moveNext = inhand->connect(
EventRight,
std::bind(
&RunUi::incPage,
this,
std::ref(dinfo),
Page::SelectUser,
std::placeholders::_2
)
);
blockNext = boost::signals2::shared_connection_block(moveNext, false);
rotor = inhand->connect(
EventDial,
[this, &dinfo](auto ig, auto val) {
if (val > 0) {
incPage(dinfo, Page::SelectUser, val);
} else {
decPage(dinfo, Page::SelectUser, val);
}
}
);
//blockRotor = boost::signals2::shared_connection_block(rotor, false);
pagePin = inhand->connect(
EventSelect,
std::bind(
&RunUi::pinPage,
this,
std::placeholders::_2
)
);
blockPin = boost::signals2::shared_connection_block(pagePin, false);
// used to prevent page changes during critical times
bool pagechange = true;
// UI loop
do {
clock->sampleTime(time);
// advance time by 64ms (the display is slow)
time.value += duds::time::interstellar::Milliseconds(64);
displaystuff.setTime(
duds::time::planetary::earth->posix(
time.value
).time_of_day().total_seconds()
);
displaystuff.getInfo(dinfo);
if (dinfo.errormsg.empty()) {
screen.hideInfoMsg();
} else {
screen.showInfoMsg(dinfo.errormsg);
}
if (pagechange) {
// change on attention event
int chgp = attn.changeToPage();
if (chgp > 0) {
changePage(dinfo, Page::SelectUser, chgp);
if (!pagepinned) {
pageswitch = time.value + pagetime * 5;
}
} else {
// auto page change
if (time.value > pageswitch) {
incPage(dinfo, Page::SelectAuto);
pageswitch = time.value + pagetime;
}
}
}
// update the displayed page
pages[page]->update(dinfo, &screen);
// show the time 64ms in the future; it'll take a while to update the
// display
screen.render(frame, time);
disp->write(&frame);
// wait on input until 60ms before the next second
if (
poller.wait(
std::max(
// delay to next second
duds::time::interstellar::Milliseconds(1000) -
(
duds::time::interstellar::MilliClock::now().time_since_epoch() %
duds::time::interstellar::Milliseconds(1000)
)
- duds::time::interstellar::Milliseconds(60),
// minimum delay; ensure not <= 0
duds::time::interstellar::Milliseconds(1)
)
) == poller.maxEvents
) {
// get more input
for (int limit = 8; limit && (poller.respond() == poller.maxEvents); --limit) { }
}
} while (!quit);
} catch (...) {
std::cerr << "Program failed in RunUi::run():\n" <<
boost::current_exception_diagnostic_information() << std::endl;
quit = true;
}