-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledstrip_controller.ino
190 lines (160 loc) · 3.7 KB
/
ledstrip_controller.ino
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
//#include <GDBStub.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#define NOP __asm__ __volatile__ ("nop\n\t")
#define NOP2 __asm__ __volatile__ ("nop\n\t" "nop\n\t")
#define out 0
#define id 0
int numLeds = 120;
//AP credidentials
const char *ssid = "";
const char *password = "";
HTTPClient client;
void setup()
{
Serial.begin(115200);
delay(100);
pinMode(out, OUTPUT);
// Connect to AP
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//http://{ip}/controller?id=
String url = "";
url = String(url + id);
client.begin(url);
int httpCode = client.GET();
//Serial.println(httpCode);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = client.getString();
//Serial.println("payload: ");
//Serial.print(payload);;
handleResponse(payload);
}
}
}
void stretchPattern(uint8_t* pattern, int patternLenght) {
patternLenght *= 3;
int it = 0;
for (size_t i = patternLenght; i < numLeds * 3; i++)
{
pattern[i] = pattern[it];
if (it == patternLenght - 1)
{
it = 0;
}
else {
it++;
}
}
}
void append(char* s, char c)
{
int len = strlen(s);
s[len] = c;
s[len + 1] = '\0';
}
void sendBytes(const int numBytes, uint8_t* output)
{
#define WAIT0H NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
#define WAIT1H NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;
#define WAIT0L NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP2;NOP2;
#define WAIT1L NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;NOP2;
noInterrupts();
for (int i = 0; i < numBytes; i++)
{
for (int j = 7; j > -1; j--)
{
bool val = (output[i] & (1 << j)) != 0;
if (val) {
GPOS = (1 << out);
WAIT1H;
GPOC = (1 << out);
WAIT1L;
}
else {
GPOS = (1 << out);
WAIT0H;
GPOC = (1 << out);
WAIT0L;
}
}
}
interrupts();
}
void handleResponse(String response) {
String l;
int i = 0;
while (response[i] != '\n')
{
l += response[i];
i++;
}
int length = l.toInt();
response = response.substring(l.length());
uint8_t *colors;
colors = (uint8_t *) malloc(numLeds * 3);
//if (colors != 0) {
// delete[] colors;
//}
//colors = new uint8_t[numLeds * 3];
int charCount = 1;
for (size_t i = 0; i < length*3; i+=3)
{
for (size_t j = 0; j < 3; j++)
{
char color[3];
for (size_t i = 0; i < 3; i++)
{
color[i] = '\0';
}
//int pos = 0;
char c = response[charCount];
while (c != ';' && c != '\n')
{
append(color, c);
//pos++;
charCount++;
c = response[charCount];
}
charCount++;
uint8_t col = (uint8_t)atoi(color);
colors[i+j] = col;
}
}
stretchPattern(colors, length);
//Serial.println("pattern:");
//for (size_t i = 0; i < numLeds * 3; i++)
//{
// Serial.println(colors[i]);
//}
sendBytes(numLeds * 3 ,colors);
free(colors);
}
void loop()
{
client.setTimeout(-1);
//http://{ip}/poll?id=
String url = "";
url = String(url + id);
client.begin(url);
//Serial.println("listening");
int httpCode = client.GET();
//Serial.println(httpCode);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = client.getString();
//Serial.println("payload: ");
//Serial.print(payload);;
handleResponse(payload);
}
}
//else
//{
// Serial.println(client.errorToString(httpCode));
//}
}