-
Notifications
You must be signed in to change notification settings - Fork 0
/
RainbowAnimation.cpp
65 lines (59 loc) · 1.38 KB
/
RainbowAnimation.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
#include "Arduino.h"
#include "RainbowAnimation.h"
RainbowAnimation::RainbowAnimation():Animation() {
_state = 0;
}
void RainbowAnimation::tick() {
_state = (_state + 1) % 6*255;
}
void RainbowAnimation::getPixelMatrix(int strip, int pixels, uint32_t *colors) {
for(int i = 0; i < pixels; i++) {
int n = _state + i;
colors[i] = ((uint32_t)r(n) << 16) | ((uint32_t)g(n) << 8) | b(n);
}
}
uint8_t RainbowAnimation::r(int n) {
n = n % 6*255;
if(n <= 1*255 || 5*255 < 255) {
return (uint8_t) 255;
}
else if(1*255 < n && n <= 2*255) {
return (uint8_t) 255 - (n % 255);
}
else if(2*255 < n && n <= 4*255) {
return (uint8_t) 0;
}
else if(4*255 < n && n <= 5*255) {
return (uint8_t) n % 255;
}
}
uint8_t RainbowAnimation::g(int n) {
n = n % 6*255;
if(n <= 1*255) {
return (uint8_t) n % 255;
}
else if(1*255 < n && n <= 3*255) {
return (uint8_t) 255;
}
else if(3*255 < n && n <= 4*255) {
return (uint8_t) 255 - (n % 255);
}
else if(4*255 < n) {
return (uint8_t) 0;
}
}
uint8_t RainbowAnimation::b(int n) {
n = n % 6*255;
if(n <= 2*255) {
return (uint8_t) 0;
}
else if(2*255 < n && n <= 3*255) {
return (uint8_t) n % 255;
}
else if(3*255 < n && n <= 5*255) {
return (uint8_t) 255;
}
else if(5*255 < n) {
return (uint8_t) 255 - (n % 255);
}
}