-
Notifications
You must be signed in to change notification settings - Fork 1
/
leds.h
120 lines (98 loc) · 2.22 KB
/
leds.h
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
#ifndef LEDS_H
#define FASTLED_ESP8266_RAW_PIN_ORDER
#include <FastLED.h>
FASTLED_USING_NAMESPACE
#define DATA_PIN D6
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 1
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 255
#define FRAMES_PER_SECOND 60
typedef void (*PatternFunc)();
void _rainbow ();
void _blinker ();
PatternFunc pattern = _rainbow;
CRGB color_1;
CRGB color_2;
void led_setup () {
Serial.println ("Configuring LEDs...");
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
unsigned long next_led_time = 0;
bool bstate = false;
unsigned long the_delay = 10;
unsigned long _hextocolor (String color) {
return strtol (color.c_str(), NULL, 16);
}
void _rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
FastLED.show();
EVERY_N_MILLISECONDS( 20 ) { gHue++; }
}
void _blinker () {
EVERY_N_MILLISECONDS( the_delay ) {
if (bstate) {
leds [0] = color_1;
}
else {
leds [0] = color_2;
}
bstate = !bstate;
FastLED.show();
}
}
void _beater () {
EVERY_N_MILLISECONDS( the_delay ) {
leds [0] = color_1;
}
leds [0].fadeToBlackBy (48);
FastLED.show();
}
void _color () {
if (bstate) {
leds [0] = color_1;
FastLED.show();
bstate = false;
}
}
void LEDBlink (String c1, unsigned long speed) {
color_1 = _hextocolor (c1);
color_2 = CRGB::Black;
pattern = _blinker;
the_delay = speed;
bstate = true;
}
void LEDBeat (String c1, unsigned long speed) {
color_1 = _hextocolor (c1);
color_2 = CRGB::Black;
pattern = _beater;
the_delay = speed;
}
void LEDFlash (String c1, String c2, unsigned long speed) {
color_1 = _hextocolor (c1);
color_2 = _hextocolor (c2);
pattern = _blinker;
the_delay = speed;
bstate = true;
}
void LEDColor (String c1) {
color_1 = _hextocolor (c1);
color_2 = CRGB::Black;
pattern = _color;
bstate = true;
pattern();
}
void led_loop () {
unsigned long m = millis();
if (next_led_time < m) {
next_led_time = m + 1000/FRAMES_PER_SECOND;
pattern ();
}
}
#define LEDS_H 1
#endif