-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostLightController.cpp
More file actions
177 lines (143 loc) · 4.67 KB
/
PostLightController.cpp
File metadata and controls
177 lines (143 loc) · 4.67 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
/*-------------------------------------------------------------------------
This source file is a part of PostLightController
For the latest info, see https://github.com/cmarrin/PostLightController
Copyright (c) 2021-2025, Chris Marrin
All rights reserved.
Use of this source code is governed by the MIT license that can be
found in the LICENSE file.
-------------------------------------------------------------------------*/
#include "PostLightController.h"
static const char* TAG = "PostLightController";
static constexpr uint16_t MaxCmdSize = 16;
static constexpr int32_t MaxDelay = 1000; // ms
static constexpr int32_t IdleDelay = 100; // ms
PostLightController::PostLightController(mil::WiFiPortal* portal)
: mil::Application(portal, ConfigPortalName, true)
{
mil::System::initLED(1, PixelPin, PixelsPerPost * NumPosts);
}
static int16_t parseCmd(const std::string& cmd, uint8_t* buf, uint16_t size)
{
// Cmd form is: <cmd char>,<param0 (0-255)>,<param1 (0-255)>,...<paramN (0-255)>
uint16_t strIndex = 0;
uint16_t bufIndex = 0;
bool parsingCmd = true;
while (true) {
int16_t nextIndex = cmd.find(',', strIndex);
if (nextIndex == std::string::npos) {
// HACK ALERT: This is another case where string.length includes the terminating null on espidf!
nextIndex = strlen(cmd.c_str());
}
if (parsingCmd) {
// Cmd must be a single char
if (nextIndex != 1) {
return -1;
}
buf[bufIndex++] = uint8_t(cmd.c_str()[0]);
parsingCmd = false;
} else {
// Parse param. Must be 1 to 3 digits <= 255
uint16_t paramSize = nextIndex - strIndex;
if (paramSize < 1 || paramSize > 3) {
return -1;
}
uint16_t param = 0;
for (uint16_t i = 0; i < paramSize; ++i) {
uint16_t digit = uint16_t(cmd.c_str()[strIndex + i]) - '0';
if (digit > 9) {
return -1;
}
param = param * 10 + digit;
}
if (param > 255) {
return -1;
}
buf[bufIndex++] = uint8_t(param);
}
strIndex = nextIndex;
if (cmd[strIndex] == ',') {
strIndex += 1;
}
if (cmd[strIndex] == '\0') {
return bufIndex;
}
}
return -1;
}
void
PostLightController::processCommand(const std::string& cmd)
{
mil::System::logI(TAG, "cmd='%s'\n", cmd.c_str());
uint8_t buf[MaxCmdSize];
int16_t r = parseCmd(cmd, buf, MaxCmdSize);
if (r < 0) {
printf("**** parseCmd failed\n");
} else {
if (!sendCmd(buf, r)) {
printf("**** sendCmd failed\n");
}
}
_portal->sendHTTPResponse(200, "text/plain", "command processed");
}
void
PostLightController::setup()
{
mil::System::delay(500);
showColor(0, 0, 0, 0, 0);
Application::setup();
// Just in case
_effect = Effect::None;
_effectId = -1;
setTitle((std::string("<center>MarrinTech Post Light Controller v") + Version + "</center>").c_str());
addHTTPHandler("/command", [this](mil::WiFiPortal* p)
{
processCommand(_portal->getHTTPArg("cmd"));
return true;
});
mil::System::logI(TAG, "Post Light Controller v%s", Version);
showStatus(StatusColor::Green, 3, 2);
}
void
PostLightController::loop()
{
Application::loop();
int32_t delayInMs = IdleDelay;
if (_effect == Effect::Flash) {
delayInMs = _flash.loop();
}
if (delayInMs > MaxDelay) {
delayInMs = MaxDelay;
}
if (delayInMs < 0) {
// An effect has finished. End it and clear the display
showColor(0, 0, 0, 0, 0);
_effect = Effect::None;
_effectId = -1;
delayInMs = IdleDelay;
}
mil::System::delay(delayInMs);
}
bool
PostLightController::sendCmd(const uint8_t* cmd, uint16_t size)
{
if (size < 1) {
return false;
}
// Send a command to turn off the pixels. This will also
// Kill any Lua programs running
showColor(0, 0, 0, 0, 0);
if (cmd[0] == 'C') {
// Built-in color command
showColor(cmd[1], cmd[2], cmd[3], cmd[4], cmd[5]);
_effect = Effect::Flash;
return true;
}
_effect = Effect::Lua;
// Make a command with args
std::string luaCmd = std::string(1, cmd[0]);
for (int i = 1; i < size; ++i) {
luaCmd += " " + std::to_string(cmd[i]);
}
_effectId = handleShellCommand(luaCmd);
return true;
}