-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLedCube.cpp
317 lines (270 loc) · 7.03 KB
/
LedCube.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
LedCube.cpp - Library for controlling an LED cube
Created by Gamaiel Zavala (gzip), 2009-2012
MIT License. See accompanying LICENSE file for terms.
*/
#include <new.h>
#include "newfix.h"
#include "LedCube.h"
LedCube::LedCube(byte size, byte lp[], byte cp[]) :
levels(size), cols(size*size), num(size*size*size),
bufferEnabled(false), bufferInverted(false)
{
randomSeed(analogRead(0));
// allocate memory for dynamic members
buffer = new byte*[levels];
levelPins = new byte[levels];
colPins = new byte[cols];
// configure level pins and finish allocation for buffer
for (byte i = 0; i < levels; i++)
{
levelPins[i] = lp[i];
pinMode(levelPins[i], OUTPUT);
buffer[i] = new byte[cols];
}
// configure column pins
for (byte i = 0; i < cols; i++)
{
colPins[i] = cp[i];
pinMode(colPins[i], OUTPUT);
}
clearBuffer();
//Serial.begin(9600);
}
// destructor frees dynamically allocated memory
LedCube::~LedCube()
{
for (byte i = 0; i < levels; i++)
{
delete buffer[i];
}
delete buffer;
delete levelPins;
delete colPins;
}
// low level methods, zero based
void LedCube::light(byte lv, byte col, byte val)
{
if(lv < levels && col < cols)
{
if(bufferEnabled)
{
buffer[lv][col] = val;
}
else
{
digitalWrite(colPins[col], val);
digitalWrite(levelPins[lv], val);
}
}
}
void LedCube::lightOff(byte level, byte col)
{
light(level,col,(bufferInverted ? HIGH : LOW));
}
void LedCube::lightOn(byte lv, byte col)
{
light(lv,col,(bufferInverted ? LOW : HIGH));
}
void LedCube::lightPulse(byte lv, byte col, unsigned int wait)
{
lightOn(lv, col);
if(!bufferEnabled)
{
delay(wait);
lightOff(lv, col);
}
}
// basic effects
void LedCube::randomLight(byte numLights, unsigned int wait)
{
for (byte l=0; l<numLights; l++)
{
lightPulse(random(0,levels), random(0,cols), wait);
}
}
void LedCube::lightDrop(byte col, unsigned int wait)
{
for(byte r=levels; r; r--)
{
lightPulse(r-1, col-1, wait);
}
}
// sequence effects
// time is overall, gap is in between individual lights
void LedCube::lightSequence(byte seq[], byte length, unsigned int time, byte gap)
{
// should be pairs of coords
if(length%2){ return; }
if(bufferEnabled){ time = 1; }
//Serial.print('{');
for (unsigned int d=0; d<time; d++)
{
for (byte s=0; s<length; s+=2)
{
if(bufferEnabled){
lightOn(seq[s], seq[s+1]);
} else {
lightPulse(seq[s],seq[s+1], gap);
}
//Serial.print(seq[s],DEC); if(s < length-1) Serial.print(', ');
}
}
//Serial.println('}');
}
cubeFrame* LedCube::createFrame(byte sequence[], unsigned int size, unsigned int delay)
{
// allocate memory which will be reclaimed in lightFrames
struct cubeFrame *f = (cubeFrame*) malloc(sizeof(struct cubeFrame));
f->sequence = (byte*) malloc(sizeof(byte) * size);
memcpy((void*) f->sequence , (void*) sequence, sizeof(byte) * size);
f->size = size;
f->delay = delay;
return f;
}
void LedCube::destroyFrame(cubeFrame* frame)
{
free(frame->sequence);
free(frame);
}
void LedCube::lightFrames(cubeFrame* frames[], unsigned int length)
{
for(byte f=0; f<length; f++)
{
lightSequence(frames[f]->sequence, frames[f]->size, frames[f]->delay);
// reclaim memory allocated in createFrame to prevent a leak
destroyFrame(frames[f]);
}
}
void LedCube::lightLevel(byte lv, unsigned int delay)
{
if(lv && lv<=levels)
{
byte seq[cols*2];
for(byte c=0, i=0; c<cols; c++, i+=2)
{
seq[i] = lv-1;
seq[i+1] = c;
}
lightSequence(seq,sizeof(seq),delay);
}
}
void LedCube::lightRow(byte r, byte level, unsigned int wait)
{
if(r && level && r<=cols*2 && level<=levels)
{
byte start = r<=levels ? r-1 : (r - levels - 1) * levels;
byte inc = r<=levels ? levels : 1;
byte seq[] = {level-1,start, level-1,start+inc, level-1,start+inc*2};
lightSequence(seq, sizeof(seq), wait);
}
}
void LedCube::lightPlane(byte r, unsigned int wait)
{
if(r && r<=cols*2)
{
byte start = r<=levels ? r-1 : (r - levels - 1) * levels;
byte inc = r<=levels ? levels : 1;
byte seq[cols*2];
byte index = 0;
for(byte level=0; level<levels; level++)
{
for(byte i=0; i<3; i++)
{
seq[index] = level;
seq[index+1] = start + inc*i;
index += 2;
}
}
lightSequence(seq, sizeof(seq), wait);
}
}
void LedCube::lightColumn(byte col, unsigned int wait)
{
if(col && col<=cols)
{
byte seq[] = {0,col-1,1,col-1,2,col-1};
lightSequence(seq,sizeof(seq),wait);
}
}
void LedCube::lightPerimeter(byte level, byte rotations, unsigned int wait)
{
byte seq[] = {level,0,level,1,level,2,level,5,level,8,level,7,level,6,level,3};
lightSequence(seq, sizeof(seq), rotations, wait);
}
void LedCube::randomColumn(byte numColumns, unsigned int wait)
{
for (byte c=0; c < numColumns; c++) {
lightColumn(random(1,cols+1), wait);
}
}
// buffer methods
void LedCube::enableBuffer(boolean enable)
{
bufferEnabled = enable;
if(!bufferEnabled) invertBuffer(false);
}
void LedCube::invertBuffer(boolean invert)
{
bufferInverted = invert;
}
void LedCube::clearBuffer()
{
setBuffer(0);
}
void LedCube::fillBuffer()
{
setBuffer(1);
}
void LedCube::setBuffer(byte val)
{
for(byte lv=0; lv < levels; lv++)
for(byte col=0; col < cols; col++)
buffer[lv][col] = val;
}
byte LedCube::getBufferAt(byte lv, byte col)
{
return buffer[lv][col];
}
void LedCube::drawBuffer(unsigned int wait)
{
byte seq[num*2];
byte n = 0;
for(byte lv=0; lv<levels; lv++)
{
for(byte col=0; col<cols; col++)
{
if(buffer[lv][col])
{
//Serial.print(buffer[r][c],DEC); Serial.print(': ');
//Serial.print(r,DEC); Serial.print(','); Serial.print(c,DEC); Serial.print(' ');
seq[n] = lv;
seq[n+1] = col;
n += 2;
}
}
}
// Serial.print('('); Serial.println(num,DEC); Serial.print(')');
enableBuffer(false);
lightSequence(seq, sizeof(seq), wait);
enableBuffer();
}
void LedCube::lightsOut(unsigned int wait)
{
enableBuffer();
fillBuffer();
drawBuffer(25);
for(byte w=0, l, c, max = num; w<max; )
{
// lower bound is inclusive, upper is exclusive
l = random(0, levels);
c = random(0, cols);
if(getBufferAt(l,c) == HIGH)
{
lightOff(l,c);
drawBuffer(wait);
w++;
}
}
enableBuffer(false);
}