Repository navigation
Expand file tree
/
Copy pathInitSequence.cpp
More file actions
210 lines (196 loc) · 5.35 KB
/
Copy pathInitSequence.cpp
File metadata and controls
210 lines (196 loc) · 5.35 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
#include "InitSequence.h"
#include <cctype>
#include <cstdlib>
namespace DekiDisplay
{
InitSequence InitSequenceFromTable(const uint8_t* table)
{
constexpr uint8_t kDelayFollows = 0x80;
InitSequence out;
if (!table)
{
return out;
}
for (;;)
{
const uint8_t cmd = *table++;
const uint8_t num = *table++;
if (cmd == 0xFF && num == 0xFF)
{
break;
}
InitStep step;
step.cmd = cmd;
const uint8_t count = num & static_cast<uint8_t>(~kDelayFollows);
step.params.assign(table, table + count);
table += count;
if (num & kDelayFollows)
{
const uint8_t ms = *table++;
step.delayMs = ms == 255 ? 500 : ms;
}
out.push_back(std::move(step));
}
return out;
}
namespace
{
bool ParseHexByte(const std::string& token, uint8_t& value)
{
std::string digits = token;
if (digits.size() > 2 && digits[0] == '0' && (digits[1] == 'x' || digits[1] == 'X'))
{
digits = digits.substr(2);
}
if (digits.empty() || digits.size() > 2)
{
return false;
}
for (char c : digits)
{
if (!std::isxdigit(static_cast<unsigned char>(c)))
{
return false;
}
}
value = static_cast<uint8_t>(std::strtoul(digits.c_str(), nullptr, 16));
return true;
}
bool ParseMs(const std::string& token, uint32_t& value)
{
if (token.empty() || token.size() > 5)
{
return false;
}
for (char c : token)
{
if (!std::isdigit(static_cast<unsigned char>(c)))
{
return false;
}
}
value = static_cast<uint32_t>(std::strtoul(token.c_str(), nullptr, 10));
return true;
}
std::vector<std::string> Tokens(const std::string& item)
{
std::vector<std::string> out;
std::string cur;
for (char c : item)
{
if (std::isspace(static_cast<unsigned char>(c)) || c == ',')
{
if (!cur.empty())
{
out.push_back(cur);
cur.clear();
}
}
else
{
cur += c;
}
}
if (!cur.empty())
{
out.push_back(cur);
}
return out;
}
bool IsDelay(const std::string& token)
{
if (token.size() != 5)
{
return false;
}
std::string lower;
for (char c : token)
{
lower += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return lower == "delay";
}
} // namespace
bool ParseInitSequence(const std::string& text, InitSequence& out, std::string& error)
{
out.clear();
error.clear();
int lineNo = 0;
size_t start = 0;
while (start <= text.size())
{
size_t end = text.find('\n', start);
if (end == std::string::npos)
{
end = text.size();
}
std::string line = text.substr(start, end - start);
start = end + 1;
++lineNo;
const size_t hash = line.find('#');
if (hash != std::string::npos)
{
line.resize(hash);
}
size_t itemStart = 0;
while (itemStart <= line.size())
{
size_t itemEnd = line.find(';', itemStart);
if (itemEnd == std::string::npos)
{
itemEnd = line.size();
}
const std::vector<std::string> tokens = Tokens(line.substr(itemStart, itemEnd - itemStart));
itemStart = itemEnd + 1;
if (tokens.empty())
{
continue;
}
size_t i = 0;
if (!IsDelay(tokens[0]))
{
InitStep step;
if (!ParseHexByte(tokens[0], step.cmd))
{
error = "line " + std::to_string(lineNo) + ": '" + tokens[0] + "' is not a hex command byte";
return false;
}
for (i = 1; i < tokens.size() && !IsDelay(tokens[i]); ++i)
{
uint8_t b = 0;
if (!ParseHexByte(tokens[i], b))
{
error = "line " + std::to_string(lineNo) + ": '" + tokens[i] + "' is not a hex byte";
return false;
}
step.params.push_back(b);
}
out.push_back(std::move(step));
}
if (i < tokens.size())
{
// "delay <ms>", for the command before it.
if (out.empty())
{
error = "line " + std::to_string(lineNo) + ": a delay needs a command before it";
return false;
}
uint32_t ms = 0;
if (i + 1 >= tokens.size() || !ParseMs(tokens[i + 1], ms))
{
error = "line " + std::to_string(lineNo) + ": 'delay' takes a number of milliseconds";
return false;
}
if (i + 2 < tokens.size())
{
error = "line " + std::to_string(lineNo) + ": '" + tokens[i + 2] +
"' after a delay; start the next command with ';' or a new line";
return false;
}
out.back().delayMs += ms;
}
}
}
return true;
}
} // namespace DekiDisplay