-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDD.cpp
100 lines (83 loc) · 2.21 KB
/
SDD.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
#include "FakeLedControl.h"
#include "SDD.hpp"
#include <iostream>
using namespace std;
SDD::SDD(FakeLedControl &ledControl):
ledControl(ledControl),
buffer(ledControl.getDeviceCount() * 8),
physicalDisplayLen(ledControl.getDeviceCount() * 8)
{
for (int i = 0; i < ledControl.getDeviceCount(); ++i)
{
ledControl.shutdown(i, false);
ledControl.clearDisplay(i);
}
}
bool SDD::tick()
{
switch (state)
{
case STATE::START:
if (!--delayCounter)
{
state = STATE::MIDDLE;
}
return false;
case STATE::MIDDLE:
{
startColumn += columnIncrement;
refreshDisplay();
bool done = (buffer.size() - startColumn) < physicalDisplayLen;
if (done)
{
state = STATE::END;
delayCounter = endDelay;
}
return false;
}
case STATE::END:
delayCounter--;
if (delayCounter == 0)
{
state = STATE::START;
delayCounter = endDelay;
startColumn = 0;
return true;
}
return false;
}
return true;
}
void SDD::refreshDisplay()
{
int physicalDisplayLen = 8 * ledControl.getDeviceCount();
for (int i = 0; i < physicalDisplayLen; ++i)
{
ledControl.setColumn(i / 8, i % 8, buffer[i+startColumn]);
}
ledControl.print();
}
void SDD::renderString(const std::string &message, const PyFont& font)
{
int physicalDisplayLen = 8 * ledControl.getDeviceCount();
size_t len = calculateRenderedLength(font, message.c_str());
startColumn = 0;
//here we make difference between a text that fits in the display and a text
//that has to be scrolled
if (len <= physicalDisplayLen)
{
buffer.resize(len, 0); //resize and zero
int margin = (physicalDisplayLen - len + 1) / 2; //calculate margin with rounding
renderText(font, message.c_str(), buffer.data() + margin, len);
state = STATE::END;
delayCounter = endDelay;
refreshDisplay();
return;
}
//just change the size, the values will be initialized anyway when rendering
buffer.resize(len);
renderText(font, message.c_str(), buffer.data(), len);
state = STATE::START;
delayCounter = endDelay;
refreshDisplay();
}