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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
|
#include "effect.h"
#include "font.h"
#include <SDL.h>
#include <cassert>
#include <cmath>
#define CUBE_SIZE 8
#define CUBE_BYTES CUBE_SIZE*CUBE_SIZE
#define AXIS_Z 2
#define AXIS_Y 1
#define AXIS_X 0
unsigned char leds[8][8];
volatile bool led_change = false;
//unsigned char pow2[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
//const unsigned char pow2[8] = {128, 64, 32, 16, 8, 4, 2, 1};
/*****************************************************************************
* ACCESSORS
*****************************************************************************/
unsigned char inrange(int x, int y, int z)
{
if (x >= 0 && x < CUBE_SIZE && y >= 0 && y < CUBE_SIZE && z >= 0 && z < CUBE_SIZE)
{
return 1;
} else
{
// One of the coordinates was outside the cube.
return 0;
}
}
bool get_led(unsigned char x, unsigned char y, unsigned char z)
{
/*
assert(x >= 0 && x <= 7);
assert(y >= 0 && y <= 7);
assert(z >= 0 && z <= 7);
*/
if (inrange(x, y, z)) {
return leds[y][z] & (1 << x);
}
return false;
}
void set_led(unsigned char x, unsigned char y, unsigned char z, bool on)
{
if (!inrange(x, y, z)) {
return;
}
/*
assert(x >= 0 && x <= 7);
assert(y >= 0 && y <= 7);
assert(z >= 0 && z <= 7);
*/
if (on) {
leds[y][z] |= ((unsigned char)1) << x;
}
else {
leds[y][z] &= ~(((unsigned char)1) << x);
}
}
void clear_led()
{
memset(leds, 0, sizeof(leds));
}
void set_voxel(int x, int y, int z)
{
set_led(x, y, z, true);
}
void clr_voxel(int x, int y, int z)
{
set_led(x, y, z, false);
}
void fill(char val)
{
memset(leds, val, sizeof(leds));
}
void delay(unsigned t)
{
led_change = true;
SDL_Delay(t);
}
/*****************************************************************************
* EFFECTS
*****************************************************************************/
void test_effect()
{
static int active_layer = 0;
for (int k = 0; k < 8; ++k) {
//std::cout << "boo" << std::endl;
for (int j = 0; j < 8; ++j) {
//std::cout << "boo2" << std::endl;
if (k == active_layer) {
//std::cerr << "active" << std::endl;
leds[j][k] = 0xFF;
}
else leds[j][k] = 0;
}
}
++active_layer;
if (active_layer >= 8) {
active_layer = 0;
}
}
void sendvoxels_z(int x, int y, int z, int delay)
{
assert(z == 0 || z == 7);
if (z == 7) {
for (int i = 0; i < 7; ++i) {
set_led(x, y, i, false);
set_led(x, y, i+1, true);
led_change = true;
SDL_Delay(delay);
}
}
else {
for(int i = 6; i >= 0; --i) {
set_led(x, y, i + 1, false);
set_led(x, y, i, true);
led_change = true;
SDL_Delay(delay);
}
}
}
void sendvoxels_random_z_effect(int maxiters,
int delay, /* speed of movement */
int wait) /* delay between 2 movement */
{
clear_led(); // clear the cube
for (int j = 0; j < 8; ++j) {
for (int i = 0; i < 8; ++i) {
if (rand()%2) {
set_led(i, j, 0, true);
}
else {
set_led(i, j, 7, true);
}
}
}
int previous_x = 0, previous_y = 0;
for (int n = 0; n < maxiters ; ++n ) {
int x = rand()%8, y = rand()%8;
if (previous_x == x && previous_y == y) {
--n;
continue;
}
if (get_led(x, y, 0)) {
sendvoxels_z(x, y, 7, delay);
}
else if (get_led(x, y, 7)) {
sendvoxels_z(x, y, 0, delay);
}
SDL_Delay(wait);
}
}
void rain_effect(int iterations)
{
for (int i = 0; i < iterations ; ++i) {
// clear layer 7
for (int j = 0 ; j < 8; ++j) {
leds[j][7] = 0;
}
// place pixels at random x and y coord at layer 7
int n = rand()%4;
for (int i2 = 0; i2 < n; ++i2) {
set_led(rand()%8, rand()%8, 7, 1);
}
led_change = true;
SDL_Delay(90);
// shift down
for (int k = 0; k < 7; ++k) {
for (int j = 0 ; j < 8; ++j) {
leds[j][k] = leds[j][k+1];
}
}
}
}
void set_plane(int axis, unsigned char index, bool on = true)
{
switch(axis) {
case 0: // X
for (int k = 0; k < 8; ++k) {
for (int j = 0; j < 8; ++j) {
set_led(index, j, k, on);
}
}
break;
case 1: // Y
for (int k = 0; k < 8; ++k) {
for (int i = 0; i < 8; ++i) {
set_led(i, index, k, on);
}
}
break;
case 2: // Z
for (int j = 0; j < 8; ++j) {
for (int i = 0; i < 8; ++i) {
set_led(i, j, index, on);
}
}
break;
default:
assert(false);
}
}
void planboing(int plane, int speed)
{
for (int i = 0; i < 8; ++i) {
clear_led(); // clear cube
set_plane(plane, i);
led_change = true;
SDL_Delay(speed);
}
for (int i = 7; i >= 0; --i) {
clear_led(); // clear cube
set_plane(plane, i);
led_change = true;
SDL_Delay(speed);
}
}
// Shift the entire contents of the cube along an axis
// This is great for effects where you want to draw something
// on one side of the cube and have it flow towards the other
// side. Like rain flowing down the Z axiz.
void shift (char axis, int direction)
{
int i, x ,y;
int ii, iii;
//int state;
for (i = 0; i < CUBE_SIZE; i++) {
if (direction == -1) {
ii = i;
}
else {
ii = (7-i);
}
for (x = 0; x < CUBE_SIZE; x++)
{
for (y = 0; y < CUBE_SIZE; y++)
{
if (direction == -1)
{
iii = ii+1;
} else
{
iii = ii-1;
}
if (axis == 2)
{
set_led(x, y, ii, get_led(x, y, iii));
//state = get_led(x,y,iii);
//set_led(x,y,ii,state?false:true);
}
if (axis == 1)
{
set_led(x, ii, y, get_led(x, iii, y));
//state = get_led(x,iii,y);
//altervoxel(x,ii,y,state);
//set_led(x,ii,y,state?false:true);
}
if (axis == 0)
{
set_led(ii, y, x, get_led(iii, y, x));
//state = get_led(iii,y,x);
//set_led(ii,y,x,state?false:true);
}
}
}
}
if (direction == -1) {
i = 7;
}
else {
i = 0;
}
/*
for (x = 0; x < CUBE_SIZE; x++) {
for (y = 0; y < CUBE_SIZE; y++) {
if (axis == AXIS_Z) set_led(x,y,i, false);
if (axis == AXIS_Y) set_led(x,i,y, false);
if (axis == AXIS_X) set_led(i,y,x, false);
}
}
*/
set_plane(axis, i, false);
}
// Flips a byte 180 degrees.
// MSB becomes LSB, LSB becomes MSB.
char flipbyte (char byte)
{
char flop = 0x00;
flop = (flop & 0b11111110) | (0b00000001 & (byte >> 7));
flop = (flop & 0b11111101) | (0b00000010 & (byte >> 5));
flop = (flop & 0b11111011) | (0b00000100 & (byte >> 3));
flop = (flop & 0b11110111) | (0b00001000 & (byte >> 1));
flop = (flop & 0b11101111) | (0b00010000 & (byte << 1));
flop = (flop & 0b11011111) | (0b00100000 & (byte << 3));
flop = (flop & 0b10111111) | (0b01000000 & (byte << 5));
flop = (flop & 0b01111111) | (0b10000000 & (byte << 7));
return flop;
}
// Flip the cube 180 degrees along the y axis.
void mirror_y (void)
{
unsigned char buffer[CUBE_SIZE][CUBE_SIZE];
unsigned char x,y,z;
memcpy(buffer, leds, CUBE_BYTES); // copy the current cube into a buffer.
//fill(0x00);
clear_led();
for (z=0; z<CUBE_SIZE; z++)
{
for (y=0; y<CUBE_SIZE; y++)
{
for (x=0; x<CUBE_SIZE; x++)
{
if (buffer[y][z] & (0x01 << x))
set_led(x,CUBE_SIZE-1-y,z,true);
}
}
}
}
// Flip the cube 180 degrees along the x axis
void mirror_x (void)
{
unsigned char buffer[CUBE_SIZE][CUBE_SIZE];
unsigned char y,z;
memcpy(buffer, leds, CUBE_BYTES); // copy the current cube into a buffer.
//fill(0x00);
clear_led();
for (z=0; z<CUBE_SIZE; z++)
{
for (y=0; y<CUBE_SIZE; y++)
{
// This will break with different buffer sizes..
leds[y][z] = flipbyte(buffer[y][z]);
}
}
}
// flip the cube 180 degrees along the z axis
void mirror_z (void)
{
unsigned char buffer[CUBE_SIZE][CUBE_SIZE];
unsigned char z, y;
memcpy(buffer, leds, CUBE_BYTES); // copy the current cube into a buffer.
for (y=0; y<CUBE_SIZE; y++)
{
for (z=0; z<CUBE_SIZE; z++)
{
leds[y][CUBE_SIZE-1-z] = buffer[y][z];
}
}
}
// Makes sure x1 is alwas smaller than x2
// This is usefull for functions that uses for loops,
// to avoid infinite loops
void argorder(int ix1, int ix2, int *ox1, int *ox2)
{
if (ix1>ix2)
{
int tmp;
tmp = ix1;
ix1= ix2;
ix2 = tmp;
}
*ox1 = ix1;
*ox2 = ix2;
}
// Returns a byte with a row of 1's drawn in it.
// byteline(2,5) gives 0b00111100
char byteline (int start, int end)
{
return ((0xff<<start) & ~(0xff<<(end+1)));
}
// Draw a wireframe box. This only draws the corners and edges,
// no walls.
void box_wireframe(
int x1, int y1, int z1,
int x2, int y2, int z2)
{
int iy;
int iz;
argorder(x1, x2, &x1, &x2);
argorder(y1, y2, &y1, &y2);
argorder(z1, z2, &z1, &z2);
// Lines along X axis
leds[y1][z1] = byteline(x1,x2);
leds[y2][z1] = byteline(x1,x2);
leds[y1][z2] = byteline(x1,x2);
leds[y2][z2] = byteline(x1,x2);
// Lines along Y axis
for (iy=y1;iy<=y2;iy++)
{
set_led(x1,iy,z1, true);
set_led(x1,iy,z2, true);
set_led(x2,iy,z1, true);
set_led(x2,iy,z2, true);
}
// Lines along Z axis
for (iz=z1;iz<=z2;iz++)
{
set_led(x1,y1,iz, true);
set_led(x1,y2,iz, true);
set_led(x2,y1,iz, true);
set_led(x2,y2,iz, true);
}
}
void box_shrink_grow_effect(int iterations, int rot, int flip, int delay)
{
for (int it = 0; it < iterations ; ++it) {
for (int it2 = 0; it2 < 16; ++it2) {
int xyz = it2 > 7 ? it2 - 8: 7 - it2;
clear_led();
box_wireframe(0, 0, 0, xyz, xyz, xyz);
if (flip > 0) mirror_z();
if (rot == 1 || rot == 3) mirror_y();
if (rot == 2 || rot == 3) mirror_x();
led_change = true;
SDL_Delay(delay);
}
}
}
void effect_box_woopwoop (int delay, int grow)
{
int i,ii;
//fill(0x00);
clear_led();
for (i=0;i<4;i++)
{
ii = i;
if (grow > 0)
ii = 3-i;
box_wireframe(4+ii,4+ii,4+ii,3-ii,3-ii,3-ii);
led_change = true;
SDL_Delay(delay);
clear_led();
//delay_ms(delay);
//fill(0x00);
}
}
void draw_positions_axis (char axis, unsigned char positions[64], int invert)
{
int x, y, p;
//fill(0x00);
clear_led();
for (x=0; x<8; x++)
{
for (y=0; y<8; y++)
{
if (invert)
{
p = (7-positions[(x*8)+y]);
} else
{
p = positions[(x*8)+y];
}
//if (axis == AXIS_Z)
if (axis == 2)
set_led(x,y,p, true);
if (axis == 1)
set_led(x,p,y, true);
if (axis == 0)
set_led(p,y,x, true);
}
}
}
void effect_axis_updown_randsuspend(char axis,
int delay, int sleep, int invert)
{
unsigned char positions[64];
unsigned char destinations[64];
int i,px;
// Set 64 random positions
for (i=0; i<64; i++)
{
positions[i] = 0; // Set all starting positions to 0
destinations[i] = rand()%8;
}
// Loop 8 times to allow destination 7 to reach all the way
for (i=0; i<8; i++)
{
// For every iteration, move all position one step closer to their destination
for (px=0; px<64; px++)
{
if (positions[px]<destinations[px])
{
positions[px]++;
}
}
// Draw the positions and take a nap
draw_positions_axis (axis, positions,invert);
led_change = true;
SDL_Delay(delay);
//delay_ms(delay);
}
// Set all destinations to 7 (opposite from the side they started out)
for (i=0; i<64; i++)
{
destinations[i] = 7;
}
// Suspend the positions in mid-air for a while
//delay_ms(sleep);
SDL_Delay(sleep);
// Then do the same thing one more time
for (i=0; i<8; i++)
{
for (px=0; px<64; px++)
{
if (positions[px]<destinations[px])
{
positions[px]++;
}
if (positions[px]>destinations[px])
{
positions[px]--;
}
}
draw_positions_axis (axis, positions,invert);
led_change = true;
SDL_Delay(delay);
//delay_ms(delay);
}
}
void effect_stringfly2(const char* str)
{
//int x,y,i;
unsigned char x,y,i;
unsigned char chr[5];
int delay = 80;
clear_led();
while (*str)
{
font_getchar(*str++, chr);
// Put a character on the back of the cube
for (x = 0; x < 5; x++)
{
for (y = 0; y < 8; y++)
{
if ((chr[x] & (0x80>>y)))
{
//setvoxel(7,x+2,y);
//set_led(7,x+2,y);
set_led(x+2, 7, y);
}
}
}
//led_change = true;
//SDL_Delay(1000);
//clear_led();
//continue;
// Shift the entire contents of the cube forward by 6 steps
// before placing the next character
for (i = 0; i<6; i++)
{
led_change = true;
//delay_ms(1000);
//SDL_Delay(1000);
SDL_Delay(delay);
//shift(AXIS_X,-1);
shift(1,-1);
//set_plane(1, 7, false);
}
}
//return;
// Shift the last character out of the cube.
for (i = 0; i<8; i++)
{
led_change = true;
SDL_Delay(delay);
//delay_ms(1000);
//shift(AXIS_X,-1);
shift(1,-1);
}
}
float distance2d (float x1, float y1, float x2, float y2)
{
float dist;
dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
return dist;
}
float distance3d (float x1, float y1, float z1, float x2, float y2, float z2)
{
float dist;
dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
return dist;
}
// Display a sine wave running out from the center of the cube.
void ripples (int iterations, int delay)
{
float distance, height, ripple_interval;
int x,y,i;
clear_led();
//fill(0x00);
for (i=0;i<iterations;i++)
{
for (x=0;x<8;x++)
{
for (y=0;y<8;y++)
{
distance = distance2d(3.5,3.5,x,y)/9.899495*8;
//distance = distance2d(3.5,3.5,x,y);
ripple_interval =1.3;
height = 4+sin(distance/ripple_interval+(float) i/50)*4;
//setvoxel(x,y,(int) height);
set_led(x,y,(int) height);
}
}
//delay_ms(delay);
led_change = true;
SDL_Delay(delay);
//fill(0x00);
clear_led();
}
}
/*****************************************************************************
* EFFECT LAUNCHER
*****************************************************************************/
void launch_effect(int effect)
{
switch(effect) {
case 0: rain_effect(100); break;
case 1: sendvoxels_random_z_effect(20, 50, 100); break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5: ripples(1000, 50); break;
case 6:
for (int i = 0; i < 8; ++i) {
box_shrink_grow_effect(1, i%4, i & 0x4, 50);
}
effect_box_woopwoop(80, 0);
effect_box_woopwoop(80, 1);
effect_box_woopwoop(80, 0);
effect_box_woopwoop(80, 1);
break;
case 7:
for (int i = 0; i < 6; ++i) {
planboing(i%3, 50);
}
break;
case 9:
effect_axis_updown_randsuspend(2, 55, 100, 0);
effect_axis_updown_randsuspend(2, 55, 100, 1);
effect_axis_updown_randsuspend(2, 55, 100, 0);
effect_axis_updown_randsuspend(2, 55, 100, 1);
effect_axis_updown_randsuspend(0, 55, 100, 0);
effect_axis_updown_randsuspend(0, 55, 100, 1);
effect_axis_updown_randsuspend(1, 55, 100, 0);
effect_axis_updown_randsuspend(1, 55, 100, 1);
break;
case 12:
//clear_led();
//set_led(0, 0, 7);
//SDL_Delay(1000);
effect_stringfly2("Vive Lyly");
break;
default:
break;
}
}
|