-
Notifications
You must be signed in to change notification settings - Fork 2
/
Clockface.cpp
338 lines (274 loc) · 10.3 KB
/
Clockface.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
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
#include "Clockface.h"
unsigned long lastMillis = 0;
// TODO document size
static DynamicJsonDocument doc(32768);
Clockface::Clockface(Adafruit_GFX *display)
{
_display = display;
Locator::provide(display);
}
void Clockface::setup(CWDateTime *dateTime)
{
this->_dateTime = dateTime;
drawSplashScreen(0xFFE0, "Downloading");
if (deserializeDefinition()) {
clockfaceSetup();
}
}
void Clockface::drawSplashScreen(uint16_t color, const char *msg) {
Locator::getDisplay()->fillRect(0, 0, 64, 64, 0);
Locator::getDisplay()->drawBitmap(19, 18, CW_ICON_CANVAS, 27, 32, color);
StatusController::getInstance()->printCenter("- Canvas -", 7);
StatusController::getInstance()->printCenter(msg, 61);
}
void Clockface::update()
{
// Render animation
clockfaceLoop();
// Update Date/Time - Using a fixed interval (1000 milliseconds)
if (millis() - lastMillis >= 1000)
{
refreshDateTime();
lastMillis = millis();
}
}
void Clockface::setFont(const char *fontName)
{
if (strcmp(fontName, "picopixel") == 0)
{
Locator::getDisplay()->setFont(&Picopixel);
}
else if (strcmp(fontName, "square") == 0)
{
Locator::getDisplay()->setFont(&atariFont);
}
else if (strcmp(fontName, "big") == 0)
{
Locator::getDisplay()->setFont(&hour8pt7b);
}
else if (strcmp(fontName, "medium") == 0)
{
Locator::getDisplay()->setFont(&minute7pt7b);
}
else
{
Locator::getDisplay()->setFont();
}
}
void Clockface::renderText(String text, JsonVariantConst value)
{
int16_t x1, y1;
uint16_t w, h;
setFont(value["font"].as<const char *>());
Locator::getDisplay()->getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
// BG Color
Locator::getDisplay()->fillRect(
value["x"].as<const uint16_t>() + x1,
value["y"].as<const uint16_t>() + y1,
w,
h,
value["bgColor"].as<const uint16_t>());
Locator::getDisplay()->setTextColor(value["fgColor"].as<const uint16_t>());
Locator::getDisplay()->setCursor(value["x"].as<const uint16_t>(), value["y"].as<const uint16_t>());
Locator::getDisplay()->print(text);
}
void Clockface::refreshDateTime()
{
JsonArrayConst elements = doc["setup"].as<JsonArrayConst>();
for (JsonVariantConst value : elements)
{
const char *type = value["type"].as<const char *>();
if (strcmp(type, "datetime") == 0)
{
renderText(_dateTime->getFormattedTime(value["content"].as<const char *>()), value);
}
}
}
void Clockface::clockfaceSetup()
{
// Clear screen
Locator::getDisplay()->fillRect(0, 0, 64, 64, doc["bgColor"].as<const uint16_t>());
delay = doc["delay"].as<const uint16_t>();
// Draw static elements
renderElements(doc["setup"].as<JsonArrayConst>());
// Draw Date/Time
refreshDateTime();
// Create sprites
createSprites();
}
void Clockface::createSprites()
{
JsonArrayConst elements = doc["loop"].as<JsonArrayConst>();
uint8_t width = 0;
uint8_t height = 0;
for (JsonVariantConst value : elements)
{
const char *type = value["type"].as<const char *>();
if (strcmp(type, "sprite") == 0)
{
uint8_t ref = value["sprite"].as<const uint8_t>();
std::shared_ptr<CustomSprite> s = std::make_shared<CustomSprite>(value["x"].as<const int8_t>(), value["y"].as<const int8_t>());
getImageDimensions(doc["sprites"][ref][0]["image"].as<const char *>(), width, height);
s.get()->_spriteReference = value["sprite"].as<const uint8_t>();
s.get()->_totalFrames = doc["sprites"][ref].size();
s.get()->setDimensions(width, height);
sprites.push_back(s);
}
}
}
void Clockface::handleSpriteAnimation(std::shared_ptr<CustomSprite>& sprite) {
uint8_t totalFrames = sprite->_totalFrames;
uint32_t loopDelay = doc["loop"][sprite->_spriteReference]["loopDelay"].as<uint32_t>() ?: delay;
uint16_t frameDelay = doc["loop"][sprite->_spriteReference]["frameDelay"].as<uint16_t>() ?: delay;
if (millis() - sprite->_lastMillisSpriteFrames >= frameDelay && sprite->_currentFrameCount < totalFrames) {
sprite->incFrame();
// handle sprite movement
handleSpriteMovement(sprite);
// Render the frame of the sprite
renderImage(doc["sprites"][sprite->_spriteReference][sprite->_currentFrame]["image"].as<const char *>(), sprite->getX(), sprite->getY());
sprite->_currentFrameCount += 1;
sprite->_lastMillisSpriteFrames = millis();
}
if (millis() - sprite->_lastResetTime >= loopDelay) {
unsigned long currentMillis = millis();
unsigned long currentSecond = _dateTime->getSecond();
if ((currentSecond * 1000) % loopDelay == 0) {
sprite->_currentFrameCount = 0;
sprite->_lastResetTime = currentMillis;
}
}
}
void Clockface::handleSpriteMovement(std::shared_ptr<CustomSprite>& sprite) {
unsigned long moveStartTime = doc["loop"][sprite->_spriteReference]["moveStartTime"].as<unsigned long>() ?: 1;
unsigned long moveDuration = doc["loop"][sprite->_spriteReference]["moveDuration"].as<unsigned long>() ?: 0;
int8_t moveInitialX = doc["loop"][sprite->_spriteReference]["x"].as<int8_t>() ?:0;
int8_t moveInitialY = doc["loop"][sprite->_spriteReference]["y"].as<int8_t>() ?: 0;
int8_t moveTargetX = doc["loop"][sprite->_spriteReference]["moveTargetX"].as<int8_t>() ?: -1;
int8_t moveTargetY = doc["loop"][sprite->_spriteReference]["moveTargetY"].as<int8_t>() ?: -1;
bool shouldReturnToOrigin = doc["loop"][sprite->_spriteReference]["shouldReturnToOrigin"].as<bool>() ?: false;
// Check if the sprite is moving
if (sprite->isMoving()) {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - sprite->_moveStartTime;
float progress = (static_cast<float>(elapsedTime) / sprite->_moveDuration);
int8_t oldX = sprite->getX();
int8_t oldY = sprite->getY();
int8_t newX = sprite->lerp(sprite->_moveInitialX, sprite->_moveTargetX, progress);
int8_t newY = sprite->lerp(sprite->_moveInitialY, sprite->_moveTargetY, progress);
int8_t originX = min(oldX, newX);
int8_t originY = min(oldY, newY);
int8_t drawWidth = sprite->getWidth() + max(oldX, newX) - originX;
int8_t drawHeight = sprite->getHeight() + max(oldY, newY) - originY;
// Erase the previous position
Locator::getDisplay()->fillRect(
originX,
originY,
drawWidth,
drawHeight,
doc["bgColor"].as<const uint16_t>());
if (progress <= 1) {
// Update the sprite's position
sprite->setX(newX);
sprite->setY(newY);
} else if (sprite->shouldReturnToOrigin()) {
// Movement is complete
sprite->setX(sprite->_moveTargetX);
sprite->setY(sprite->_moveTargetY);
if (!sprite->_isReversing) {
sprite->reverseMoving(moveInitialX, moveInitialY);
}
} else {
sprite->stopMoving();
}
}
if ((moveDuration > 0 && (moveTargetX > -1 || moveTargetY > -1)) && (millis() - sprite->_lastResetMoveTime >= moveStartTime)) {
unsigned long currentMillis = millis();
unsigned long currentSecond = _dateTime->getSecond();
if ((currentSecond * 1000) % moveStartTime == 0) {
sprite->_lastResetMoveTime = currentMillis;
sprite->startMoving(moveTargetX, moveTargetY, moveDuration, shouldReturnToOrigin);
}
}
}
void Clockface::clockfaceLoop() {
if (sprites.empty()) {
return;
}
for (auto& sprite : sprites) {
handleSpriteAnimation(sprite);
}
}
void Clockface::renderElements(JsonArrayConst elements)
{
for (JsonVariantConst value : elements)
{
const char *type = value["type"].as<const char *>();
if (strcmp(type, "text") == 0)
{
renderText(value["content"].as<const char *>(), value);
}
else if (strcmp(type, "fillrect") == 0)
{
Locator::getDisplay()->fillRect(
value["x"].as<const uint16_t>(),
value["y"].as<const uint16_t>(),
value["width"].as<const uint16_t>(),
value["height"].as<const uint16_t>(),
value["color"].as<const uint16_t>());
}
else if (strcmp(type, "rect") == 0)
{
Locator::getDisplay()->drawRect(
value["x"].as<const uint16_t>(),
value["y"].as<const uint16_t>(),
value["width"].as<const uint16_t>(),
value["height"].as<const uint16_t>(),
value["color"].as<const uint16_t>());
}
else if (strcmp(type, "line") == 0)
{
Locator::getDisplay()->drawLine(
value["x"].as<const uint16_t>(),
value["y"].as<const uint16_t>(),
value["x1"].as<const uint16_t>(),
value["y1"].as<const uint16_t>(),
value["color"].as<const uint16_t>());
}
else if (strcmp(type, "image") == 0)
{
renderImage(value["image"].as<const char *>(), value["x"].as<const uint8_t>(), value["y"].as<const uint8_t>());
}
}
}
bool Clockface::deserializeDefinition()
{
WiFiClientSecure client;
//WiFiClient client;
//ClockwiseHttpClient::getInstance()->httpGet(&client, "raw.githubusercontent.com", "/jnthas/clock-club/v1/pac-man.json", 443);
//ClockwiseHttpClient::getInstance()->httpGet(&client, "192.168.3.19", "/nyan-cat.json", 4443);
if (ClockwiseParams::getInstance()->canvasServer.isEmpty() || ClockwiseParams::getInstance()->canvasFile.isEmpty()) {
drawSplashScreen(0xC904, "Params werent set");
return false;
}
String server = ClockwiseParams::getInstance()->canvasServer;
String file = String("/" + ClockwiseParams::getInstance()->canvasFile + ".json");
uint16_t port = 4443;
if (server.startsWith("raw.")) {
port = 443;
file = String("/robegamesios/clock-club/main/shared" + file);
}
ClockwiseHttpClient::getInstance()->httpGet(&client, server.c_str(), file.c_str(), port);
DeserializationError error = deserializeJson(doc, client);
if (error)
{
drawSplashScreen(0xC904, "Error! Check logs");
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
client.stop();
return false;
}
//TODO check if json is valid
Serial.printf("[Canvas] Building clockface '%s' by %s, version %d\n", doc["name"].as<const char *>(), doc["author"].as<const char *>(), doc["version"].as<const uint16_t>());
client.stop();
return true;
}