-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
276 lines (235 loc) · 7.39 KB
/
main.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
/* File: main.cpp */
/* Date: Wed 25 Mar 2015 10:04:48 PM CET */
/* Author: Fabian Wermelinger */
/* Tag: Main application */
/* Copyright 2015 Fabian Wermelinger. All Rights Reserved. */
#include <iostream>
#include <string>
#include <map>
#include <unistd.h>
#include <cstdlib>
#include <time.h>
#include <cmath>
#include "ArgumentParser.h"
#include "led_spi_controller.h"
#include "apa102.h"
using namespace std;
typedef typename APA102::LED LED;
typedef led_spi_controller<APA102> mycontroller;
typedef mycontroller::iterator myiterator;
void default_demo(mycontroller& leds, ArgumentParser& parser)
{
leds.all_black();
LED colors[4];
colors[0] = LED(0xffffff); // white
colors[1] = LED(0xff0000); // red
colors[2] = LED(0x00ff00); // green
colors[3] = LED(0x0000ff); // blue
const unsigned int wait_ms = parser("-wait_ms").asInt(20);
unsigned int k = 0;
while (true)
{
for (unsigned int i=0; i < leds.size(); ++i)
{
leds[i] = colors[k%4];
leds.update();
usleep(wait_ms * 1000);
}
++k;
for (unsigned int i=leds.size()-1; i >= 0; --i)
{
leds[i] = colors[k%4];
leds.update();
usleep(wait_ms * 1000);
}
++k;
}
}
void random_lights(mycontroller& leds, ArgumentParser& parser)
{
srand(time(NULL));
const unsigned int wait_ms = parser("-wait_ms").asInt(60);
while (true)
{
for (myiterator i=leds; i.more(); ++i)
{
const LED rand_led(rand()%256, rand()%256, rand()%256);
*i = rand_led;
}
leds.update();
usleep(wait_ms * 1000);
}
}
void brightness(mycontroller& leds, ArgumentParser& parser)
{
leds.all_black();
unsigned int colors[4] = {0xffffff, 0xff0000, 0x00ff00, 0x0000ff};
const unsigned int wait_ms = parser("-wait_ms").asInt(60);
unsigned int k = 0;
while (true)
{
for (myiterator i=leds; i.more(); ++i)
i->set_color(colors[k%4]);
++k;
for (unsigned int b=0; b < 32; ++b)
{
for (myiterator i=leds; i.more(); ++i)
i->set_brightness31(b);
leds.update();
usleep(wait_ms * 1000);
}
for (unsigned int b=30; b >= 0; --b)
{
for (myiterator i=leds; i.more(); ++i)
i->set_brightness31(b);
leds.update();
usleep(wait_ms * 1000);
}
}
}
void cycle(mycontroller& leds, ArgumentParser& parser)
{
}
/* float f(const float t, const unsigned int N) */
/* { */
/* return 0.5*(sin(2*2*M_PI*t) + 1.0f)*N; */
/* } */
int main(int argc, char *argv[])
{
ArgumentParser parser(argc, (const char**)argv);
const unsigned int Nleds = parser("-N").asInt(144);
const int channel = parser("-channel").asInt(0);
const int mode = parser("-mode").asInt(1);
const int speed = parser("-speed").asInt(600*1000);
/* mycontroller myleds(Nleds, speed, channel, mode); */
mycontroller myleds(Nleds, speed, channel, mode, SPI_DOWN);
// run demo
const string demo = parser("-demo").asString("default");
if (demo == "random_lights") random_lights(myleds, parser);
else if (demo == "brightness") brightness(myleds, parser);
else if (demo == "cycle") cycle(myleds, parser);
else if (demo == "default") default_demo(myleds, parser);
else
{
cerr << "Unknown demo \"" << demo << "\"" << endl;
exit(EXIT_FAILURE);
}
/* const int brightness = parser("-brightness").asInt(100); */
/* const int spread = parser("-spread").asInt(15); */
/* myleds.all_color(0xff0000); */
/* myleds.all_brightness31(12); */
/* myleds.state(); */
/* const unsigned int brightness = 100; */
/* const unsigned int wait = 1000000; */
/* for (unsigned int i=0; i<myleds.size(); ++i) */
/* { */
/* LED led(brightness, 0xff0000); */
/* myleds.set_led(i, led); */
/* } */
/* myleds.update(); */
/* usleep(wait); */
/* for (unsigned int i=0; i<myleds.size(); ++i) */
/* { */
/* LED led(brightness, 0x00ff00); */
/* myleds.set_led(i, led); */
/* } */
/* myleds.update(); */
/* usleep(wait); */
/* for (unsigned int i=0; i<myleds.size(); ++i) */
/* { */
/* LED led(brightness, 0x0000ff); */
/* myleds.set_led(i, led); */
/* } */
/* myleds.update(); */
/* usleep(wait); */
/* for (unsigned int i=0; i<myleds.size(); ++i) */
/* { */
/* LED led(brightness, 0xffffff); */
/* myleds.set_led(i, led); */
/* } */
/* myleds.update(); */
/* while (true) {} */
/* usleep(wait); */
/* for (unsigned int i=0; i<myleds.size(); ++i) */
/* { */
/* LED led(brightness, 0x000000); */
/* myleds.set_led(i, led); */
/* } */
/* myleds.update(); */
/* usleep(wait); */
/* int flipflop = 0; */
/* for (;;) */
/* { */
/* if (flipflop) */
/* { */
/* LED led1(brightness, 0x000000); */
/* myleds.set_led(0, led1); */
/* LED led2(brightness, 0x00ff00); */
/* myleds.set_led(1, led2); */
/* LED led3(brightness, 0xe00000); */
/* myleds.set_led(2, led3); */
/* } */
/* else */
/* { */
/* LED led(brightness, 0xff0000); */
/* myleds.set_led(0, led); */
/* } */
/* myleds.update(); */
/* usleep(200000); */
/* flipflop = 1-flipflop; */
/* } */
/* srand(time(NULL)); */
/* while (true) */
/* { */
/* for (unsigned int i=0; i<myleds.size(); ++i) */
/* { */
/* LED led(brightness, rand()%256, rand()%256, rand()%256); */
/* myleds.set_led(i, led); */
/* } */
/* myleds.update(); */
/* usleep(50000); */
/* } */
/* const LED red(brightness, 0xff0000); */
/* const LED green(brightness, 0x00ff00); */
/* const LED blue(brightness, 0x0000ff); */
/* const LED white(brightness, 0xffffff); */
/* const LED off(brightness, 0x000000); */
/* for (unsigned int i=0; i<myleds.size(); ++i) */
/* { */
/* /1* LED led(brightness, 0x000000); *1/ */
/* /1* myleds.set_led(i, led); *1/ */
/* } */
/* myleds.update(); */
/* for (unsigned int j=0; j<myleds.size(); ++j) */
/* { */
/* for (unsigned int i=0; i<myleds.size(); ++i) */
/* { */
/* if (j<=i) myleds.set_led(i, red); */
/* else myleds.set_led(i, off); */
/* } */
/* myleds.update(); */
/* usleep(2000); */
/* } */
/* float t = 0; */
/* const float dt = 0.001; */
/* /1* const int spread = 15; *1/ */
/* int k = 0; */
/* while (true) */
/* { */
/* /1* int j = f(t, Nleds); *1/ */
/* /1* int j = k%Nleds; *1/ */
/* /1* for (int i=0; i<myleds.size(); ++i) *1/ */
/* /1* myleds.set_led(i, off); *1/ */
/* /1* for (int s=k-spread; s<=k+spread; ++s) *1/ */
/* /1* { *1/ */
/* /1* /2* LED led(brightness, k%256, k%256, k%256); *2/ *1/ */
/* /1* myleds.set_led(s%Nleds, white); *1/ */
/* /1* /2* myleds.set_led(s%Nleds, led); *2/ *1/ */
/* /1* } *1/ */
/* myleds.update(); */
/* usleep((int)dt*1000000); */
/* ++k; */
/* t += dt; */
/* } */
return 0;
}