-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledarray.cpp
113 lines (91 loc) · 2.15 KB
/
ledarray.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
#include <FastLED.h>
#include "ledarray.h"
LEDArray::LEDArray(struct CRGB *_leds, long _NUM_LEDS){
leds=_leds;
num_leds=_NUM_LEDS;
}
void LEDArray::init_noise(){
for(int i=0; i<num_leds; i++){
leds[i] = CHSV(random(255),255,255);
}
}
void LEDArray::init_randbow(){
CHSV current=CHSV(random(255),255,255);
CHSV prev=CHSV(random(255),255,255);
CHSV first=prev;
int width=20;
int i;
for(i=0; i<num_leds-width; i+=width){
fill_gradient(leds,i,prev,i+width,current,SHORTEST_HUES);
prev=current;
current=CHSV(random(255),255,255);
}
fill_gradient(leds,i,prev,num_leds,first,SHORTEST_HUES);
}
void LEDArray::init_rainbow(){
fill_gradient(leds,num_leds,CHSV(0,255,255),CHSV(1,255,255),LONGEST_HUES);
}
void LEDArray::init_single(){
fill_solid(leds,num_leds,CHSV(0,0,0));
leds[1]=CRGB(255,255,255);
}
void LEDArray::rotate(){
CRGB led0=leds[0];
for(int i=0; i<num_leds-1; i++){
leds[i] = leds[i+1];
}
leds[num_leds-1]=led0;
}
void LEDArray::rotate_hue(){
for(int i=0; i<num_leds; i++){
CHSV hsv=rgb2hsv_approximate(leds[i]);
hsv.hue=(( (int) hsv.hue)+10) % 256;
//hsv.hue=3;
leds[i] = hsv;
}
}
uint8_t blend_hues(uint8_t one, uint8_t two){
long diff=one-two;
long avg;
if(abs(diff)<128){
avg=0.5*one+0.5*two;
} else {
avg=((long) (0.5*one+0.5*two+128)) % 256;
}
return (uint8_t) avg-1;
}
void LEDArray::blur(){
CHSV prev=CHSV(random(255),255,255);
CHSV current=rgb2hsv_approximate(leds[0]);
CHSV next;
int i;
for(i=0; i<num_leds-1; i++){
CHSV next=rgb2hsv_approximate(leds[i+1]);
leds[i]=CHSV(blend_hues(current.hue,blend_hues(prev.hue,next.hue)),255,255);
prev=current;
current=next;
}
next=CHSV(random(255),255,255);
leds[i]=CHSV(blend_hues(current.hue,blend_hues(prev.hue,next.hue)),255,255);
}
void LEDArray::rand_swap(){
long pos=random(num_leds);
long pos2=random(num_leds);
CRGB tmp = leds[pos];
leds[pos] = leds[pos2];
leds[pos2] = tmp;
}
void LEDArray::rand_swapn(){
for(int i=0; i<100; i++){
rand_swap();
}
}
void LEDArray::rand_hue(){
long pos=random(num_leds);
leds[pos] = CHSV(random(255),255,255);
}
void LEDArray::rand_huen(){
for(int i=0; i<100; i++){
rand_hue();
}
}