-
Notifications
You must be signed in to change notification settings - Fork 14
/
FxKnightRider.ino
82 lines (65 loc) · 1.71 KB
/
FxKnightRider.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
//StripInvader (c) 2011 Michael Vogt <[email protected]> // pixelinvaders.ch
//
//Knight Rider effects
uint8_t pixelsPerBlock;
uint8_t blockSize;
uint16_t kr=0;
byte krDirection=0;
byte krSize;
byte howMany;
byte krMode;
//krSize = the size of the moving parts in pixel
//howMany = how many movers should be active?
//krMode = 0:single pixel update, 1: block update
void setupKnightRider(byte _krSize, byte _howMany, byte _krMode) {
krSize = _krSize;
howMany = _howMany;
krMode = _krMode;
pixelsPerBlock = strip.numPixels() / 16;
blockSize = strip.numPixels() / pixelsPerBlock;
}
void loopKnightRider() {
if (krMode==0) {
//single pixel mode
uint32_t clearCol = complementaryColor();
for (uint16_t i=0; i < strip.numPixels(); i++) {
setTintPixelColor(i, clearCol);
}
drawKnightRider();
} else {
//block mode
uint16_t ofs = kr*pixelsPerBlock;
uint32_t cc = Wheel((frames+kr)%255);
for (uint16_t i=0; i<pixelsPerBlock; i++) {
setTintPixelColor(ofs++, cc);
}
if (frames % 8==0) checkSwapDirection(blockSize);
}
}
//draw nrOf knight rider lines and check for updates
void drawKnightRider() {
uint16_t ofs=0;
for (uint16_t n=0; n<howMany; n++) {
for (uint16_t i=kr; i<kr+krSize && i<strip.numPixels(); i++) {
setTintPixelColor(i+ofs, WHITE_COLOR);
}
ofs+=strip.numPixels()/howMany;
}
checkSwapDirection(strip.numPixels()/howMany);
}
//should the direction swapped?
void checkSwapDirection(uint16_t lengthOfStrip) {
if (krDirection==0) {
kr++;
} else {
kr--;
}
if (kr>lengthOfStrip-krSize) {
krDirection = 1;
kr = lengthOfStrip-krSize;
}
if (kr==0) {
krDirection = 0;
kr = 0;
}
}