Skip to content

Commit 7b20881

Browse files
extract timeService, add basic dashboard functionality
1 parent fcbe089 commit 7b20881

File tree

5 files changed

+110
-38
lines changed

5 files changed

+110
-38
lines changed

src/controller.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#pragma once
22

33
#include "display.h"
4+
45
#include "modes/clock.h"
56
#include "modes/colorwave.h"
7+
#include "modes/dashboard.h"
68
#include "modes/fire.h"
9+
710
#include "shared/network/WiFiLoginManager.h"
811
#include "shared/persistence/persistenceManager.h"
912
#include "shared/serialWrapper.h"
@@ -40,19 +43,19 @@ void _toMode(Mode new_mode) {
4043
break;
4144
case CLOCK:
4245
println(F("CLOCK"));
43-
Display::clear();
46+
Modes_Clock::updateScreen();
4447
break;
4548
case DASHBOARD:
4649
println(F("DASHBOARD"));
47-
Display::clear();
50+
Modes_Dashboard::updateScreen();
4851
break;
4952
case COLORWAVE:
5053
println(F("COLORWAVE"));
51-
Display::clear();
54+
Modes_Colorwave::updateScreen();
5255
break;
5356
case FIRE:
5457
println(F("FIRE"));
55-
Display::clear();
58+
Modes_Fire::updateScreen();
5659
break;
5760
default:
5861
println(F("UNKNOWN, discarding..."));
@@ -120,7 +123,7 @@ void setup() {
120123
PersistenceManager::registerListener(updateConfig);
121124

122125
Modes_Clock::setup();
123-
// TODO DASHBOARD
126+
Modes_Dashboard::setup();
124127
Modes_Colorwave::setup();
125128
Modes_Fire::setup();
126129
}
@@ -140,9 +143,7 @@ void loop() {
140143
Modes_Clock::loop();
141144
break;
142145
case DASHBOARD:
143-
// TODO pass to dashboard extension
144-
Display::printText("Too Cold!\n (TODO)");
145-
delay(1000);
146+
Modes_Dashboard::loop();
146147
break;
147148
case COLORWAVE:
148149
Modes_Colorwave::loop();

src/main.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55

66
#include "pinout.h"
77

8-
#include "shared/serialWrapper.h"
98
#include "shared/fileServer.h"
109
#include "shared/network/WiFiLoginManager.h"
1110
#include "shared/persistence/persistenceManager.h"
11+
#include "shared/serialWrapper.h"
12+
#include "shared/timeService.h"
1213

1314
//#include "inputs/telegram.h"
1415
#include "inputs/touch.h"
1516
#include "inputs/website.h"
1617

17-
#include "display.h"
1818
#include "controller.h"
19+
#include "display.h"
1920

2021
void setup() {
2122
delay(0); // watchdog timer (WDT)
@@ -29,14 +30,15 @@ void setup() {
2930

3031
println(F(".prepare outputs."));
3132
Display::setup();
32-
Display::colorDot(Display::blue); // asap
33+
Display::colorDot(Display::blue); // asap
3334

3435
println(F(".prepare connections."));
35-
WiFiLoginManager::onConfigNeeded = [] {Display::colorDot(Display::orange);};
36+
WiFiLoginManager::onConfigNeeded = [] { Display::colorDot(Display::orange); };
3637
WiFiLoginManager::setup("ModischMatrix");
3738
delay(10);
3839

3940
println(F(".prepare inputs."));
41+
TimeService::setup();
4042
Website::setup();
4143
Input_Touch::setup();
4244
// Input_Telegram::setup();
@@ -50,11 +52,10 @@ void setup() {
5052
digitalWrite(Pinout::STATUS_LED, false);
5153
Display::colorDot(Display::black);
5254
delay(10);
53-
//Controller::showLogin();
5455
}
5556

5657
void loop() {
57-
// impossible to align loop times with variable length web calls!
58+
TimeService::loop();
5859
Input_Touch::loop();
5960
Controller::loop();
6061

src/modes/clock.h

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,35 @@
22

33
#include "display.h"
44
#include "shared/serialWrapper.h"
5-
#include "time.h"
5+
#include "shared/timeService.h"
66
#include <Arduino.h>
77

88
// maybe morph using https://github.com/hwiguna/HariFun_166_Morphing_Clock/blob/master/Latest/MorphingClock/Digit.cpp
99

1010
namespace Modes_Clock {
1111
namespace {
12-
const char* ntpServer = "pool.ntp.org";
13-
const long gmtOffset_sec = 3600;
14-
const int daylightOffset_sec = 3600;
15-
1612
const uint16_t UPDATE_DELAY = 30 * 1000;
1713

18-
struct tm timeinfo;
19-
void updateLocalTime() {
20-
if (!getLocalTime(&timeinfo)) {
21-
logError(F("Failed to obtain time"));
22-
}
23-
}
24-
2514
char out[10];
26-
uint32_t nextUpdate = 0;
27-
void displayLocalTime() {
28-
uint32_t now = millis();
29-
if (now < nextUpdate) return;
30-
nextUpdate = now + UPDATE_DELAY;
31-
strftime(out, sizeof(out), "%H:%M", &timeinfo);
15+
void updateScreen() {
16+
strftime(out, sizeof(out), "%H:%M", &TimeService::timeinfo);
3217
Display::screen->setTextSize(2);
3318
Display::screen->setCursor(3, 8);
3419
Display::screen->clearScreen();
3520
Display::screen->print(out);
36-
print(F("C:"));
37-
printlnRaw(out);
21+
print(F("."));
3822
}
3923
} // namespace
4024

41-
void setup() { configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); }
25+
void setup() {}
4226

27+
uint32_t nextUpdate = 0;
4328
void loop() {
44-
updateLocalTime();
45-
displayLocalTime();
46-
delay(100);
29+
uint32_t now = millis();
30+
if (now < nextUpdate)
31+
return;
32+
nextUpdate = now + UPDATE_DELAY;
33+
updateScreen();
4734
}
4835

4936
} // namespace Modes_Clock

src/modes/dashboard.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include "display.h"
4+
#include "shared/serialWrapper.h"
5+
#include "shared/timeService.h"
6+
#include <Arduino.h>
7+
8+
// maybe morph using https://github.com/hwiguna/HariFun_166_Morphing_Clock/blob/master/Latest/MorphingClock/Digit.cpp
9+
10+
namespace Modes_Dashboard {
11+
namespace {
12+
const uint16_t UPDATE_DELAY = 30 * 1000;
13+
14+
char out[16];
15+
void updateScreen() {
16+
MatrixPanel_I2S_DMA* s = Display::screen;
17+
s->clearScreen();
18+
// TODO draw all static background elements as icon
19+
20+
s->setTextSize(1);
21+
22+
s->setCursor(4, 4);
23+
strftime(out, sizeof(out), "%H:%M", &TimeService::timeinfo);
24+
s->print(out);
25+
26+
s->setCursor(2, 24);
27+
strftime(out, sizeof(out), "%d.%m.%Y", &TimeService::timeinfo);
28+
s->print(out);
29+
30+
// TODO weather
31+
32+
print(F("."));
33+
}
34+
} // namespace
35+
36+
void setup() {}
37+
38+
uint32_t nextUpdate = 0;
39+
void loop() {
40+
uint32_t now = millis();
41+
if (now < nextUpdate)
42+
return;
43+
nextUpdate = now + UPDATE_DELAY;
44+
updateScreen();
45+
}
46+
47+
} // namespace Modes_Dashboard

src/shared/timeService.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include "shared/serialWrapper.h"
4+
#include <Arduino.h>
5+
#include <time.h>
6+
7+
namespace TimeService {
8+
// see https://www.cplusplus.com/reference/ctime/strftime/ for formatting hints
9+
struct tm timeinfo;
10+
11+
namespace {
12+
const uint16_t UPDATE_DELAY = 30 * 1000;
13+
14+
const char* ntpServer = "pool.ntp.org";
15+
const long gmtOffset_sec = 3600;
16+
const int daylightOffset_sec = 3600;
17+
18+
void updateLocalTime() {
19+
if (!getLocalTime(&timeinfo)) {
20+
logError(F("Failed to obtain time"));
21+
}
22+
}
23+
} // namespace
24+
25+
void setup() { configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); }
26+
27+
uint32_t nextUpdate = 0;
28+
void loop() {
29+
uint32_t now = millis();
30+
if (now < nextUpdate)
31+
return;
32+
nextUpdate = now + UPDATE_DELAY;
33+
updateLocalTime();
34+
}
35+
36+
} // namespace TimeService

0 commit comments

Comments
 (0)