-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightbar.ino
194 lines (176 loc) · 5.06 KB
/
lightbar.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
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
/*
Project Name: lightbox
Developer: Eric Klein Jr. ([email protected])
Description: Control LED strips for easyGrade card photo capture box
See README.md for target information, revision history, feature requests, etc.
*/
// hardware and internet configuration parameters
#include "config.h"
// Library initialization
#ifdef RGBWSupport
#include <Adafruit_NeoPixel.h>
#else // in every other case use FastLED
#include <FastLED.h>
#endif
#include <buttonhandler.h>
#include <Encoder.h>
#ifdef RGBWSupport
Adafruit_NeoPixel strip(ledCount, ledDataPin, NEO_GRBW + NEO_KHZ800);
#else
struct CRGB ledStrip[ledCount];
#endif
// LED globals
int ledStripBrightness = 10;
int activeLEDBank = 0;
// bank states associated with activeLEDBank
enum { noLEDs = 0,
backLEDs,
rightLEDs,
frontLEDs,
leftLEDs,
allLEDs };
// Rotary encoder
#ifndef blinkytape
// initalization
Encoder rotaryEncoderOne(rotaryEncoderOnePin1, rotaryEncoderOnePin2);
// global variables
long encoderOnePosition = 0;
#endif
// Instantiate button objects
// common button to all configurations
#define buttonLongPressDelay 2500
ButtonHandler buttonOne(buttonOnePin, buttonLongPressDelay);
// button states
enum { BTN_NOPRESS = 0,
BTN_SHORTPRESS,
BTN_LONGPRESS };
void setup() {
#ifdef DEBUG
Serial.begin(115200);
// wait for serial port connection
while (!Serial)
;
debugMessage("Lightbox started");
#endif
// setup LED strip
#ifdef blinkytape
FastLED.addLeds<WS2811, ledDataPin, GRB>(ledStrip, ledCount);
#else
#ifdef RGBWSupport
strip.begin();
strip.setBrightness(ledStripMinBrightness);
strip.show(); // Initialize all pixels to 'off'
#else
// LPD8806
//FastLED.addLeds<LPD8806, ledDataPin, ledClockPin, GRB>(ledStrip, ledCount);
// WS2812b
FastLED.addLeds<NEOPIXEL, ledDataPin>(ledStrip, ledCount); // GRB ordering is assumed
FastLED.setBrightness(ledStripMinBrightness);
FastLED.clear();
#endif
#endif
// Setup push button(s)
buttonOne.init();
}
void loop() {
resolveButtons();
#ifndef blinkytape
resolveRotaryEncoderOne();
#endif
}
void resolveRotaryEncoderOne()
{
long newEncoderPosition;
newEncoderPosition = rotaryEncoderOne.read() >> 1;
if (newEncoderPosition != encoderOnePosition)
{
debugMessage(String("encoder 1 old position ") + encoderOnePosition + ",new " + newEncoderPosition);
//debugMessage(String("encoder 1 new position ") + newEncoderPosition);
ledStripBrightness = ledStripBrightness + ((newEncoderPosition - encoderOnePosition) * ledStripBrightnessStepChange);
ledStripBrightness = constrain(ledStripBrightness, ledStripMinBrightness, ledStripMaxBrightness);
changeLEDStripBrightness();
encoderOnePosition = newEncoderPosition;
}
}
void changeLEDStripBrightness()
{
#ifdef RGBWSupport
strip.setBrightness(ledStripBrightness);
strip.show();
#else
FastLED.setBrightness(ledStripBrightness);
FastLED.show();
#endif
debugMessage(String("Brightness changed to ") + ledStripBrightness);
}
void resolveButtons()
{
// turn on and off LED banks
switch (buttonOne.handle())
{
case BTN_SHORTPRESS:
debugMessage("button one short press");
changeLEDBank();
break;
case BTN_LONGPRESS:
debugMessage("LightFieldEffect button long press");
break;
}
}
void changeLEDBank()
{
switch (activeLEDBank)
{
case noLEDs:
// turn on back bank
debugMessage("turn on back bank of LEDs");
for (int range = (activeLEDBank * ledsPerBank); range < ((activeLEDBank * ledsPerBank) + ledsPerBank); range++) {
debugMessage(String("white LED # ") + range);
ledStrip[range] = CRGB::White;
}
FastLED.show();
break;
case backLEDs:
case rightLEDs:
case frontLEDs:
// turn off previous LED bank
debugMessage("turn off previous LED bank");
for (int range = ((activeLEDBank - 1) * ledsPerBank); range < (((activeLEDBank - 1) * ledsPerBank) + ledsPerBank); range++) {
debugMessage(String("turn off LED # ") + range);
ledStrip[range] = CRGB::Black;
}
// turn on next LED bank
debugMessage("turn on next LED bank");
for (int range = (activeLEDBank * ledsPerBank); range < ((activeLEDBank * ledsPerBank) + ledsPerBank); range++) {
debugMessage(String("white LED # ") + range);
ledStrip[range] = CRGB::White;
}
FastLED.show();
break;
case leftLEDs:
// turn on all LEDs
debugMessage("turn on all LED banks");
fill_solid(ledStrip, ledCount, CRGB::White);
FastLED.show();
break;
case allLEDs:
// turn off LEDs
debugMessage("turn off all LED banks");
FastLED.clear();
FastLED.show();
break;
}
if (activeLEDBank == allLEDs)
activeLEDBank = noLEDs;
else
activeLEDBank++;
debugMessage(String("Current LED bank is now ") + activeLEDBank);
}
void debugMessage(String messageText)
// wraps Serial.println as #define conditional
{
#ifdef DEBUG
Serial.println(messageText);
Serial.flush(); // Make sure the message gets output (before any sleeping...)
#endif
}