Skip to content

Commit 377362d

Browse files
authored
Merge pull request #12 from dmadison/development
'Serial Flush' and Housekeeping
2 parents 393a4ac + 9fa76f4 commit 377362d

2 files changed

Lines changed: 45 additions & 18 deletions

File tree

Arduino/LEDstream_FastLED/LEDstream_FastLED.ino

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
/* LEDstream_FastLED
22
*
3-
* Modified version of Adalight protocol that uses the FastLED
3+
* Modified version of Adalight that uses the FastLED
44
* library (http://fastled.io) for driving led strips.
55
*
66
* http://github.com/dmadison/Adalight-FastLED
7-
* Last Updated: 2017-05-05
7+
*
8+
* --------------------------------------------------------------------
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
* --------------------------------------------------------------------
822
*/
923

1024
// --- General Settings
11-
static const uint8_t
12-
Num_Leds = 80, // strip length
25+
static const uint16_t
26+
Num_Leds = 80; // strip length
27+
static const uint8_t
1328
Led_Pin = 6, // Arduino data output pin
1429
Brightness = 255; // maximum brightness
1530

@@ -19,11 +34,12 @@ static const uint8_t
1934

2035
// --- Serial Settings
2136
static const unsigned long
22-
SerialSpeed = 115200; // serial port speed, max available
37+
SerialSpeed = 115200; // serial port speed
2338
static const uint16_t
2439
SerialTimeout = 150; // time before LEDs are shut off if no data (in seconds)
2540

2641
// --- Optional Settings (uncomment to add)
42+
#define SERIAL_FLUSH // Serial buffer cleared on LED latch
2743
//#define CLEAR_ON_START // LEDs are cleared on reset
2844
//#define GROUND_PIN 10 // additional grounding pin (optional)
2945
//#define CALIBRATE // sets all LEDs to the color of the first
@@ -62,11 +78,8 @@ static const uint8_t magic[] = {
6278
#define LOCHECK (MAGICSIZE + 1)
6379
#define CHECKSUM (MAGICSIZE + 2)
6480

65-
#define MODE_HEADER 0
66-
#define MODE_DATA 1
81+
enum processModes_t {Header, Data} mode = Header;
6782

68-
static uint8_t
69-
mode = MODE_HEADER;
7083
static int16_t
7184
c;
7285
static uint16_t
@@ -134,10 +147,10 @@ void adalight(){
134147
lastByteTime = lastAckTime = t; // Reset timeout counters
135148

136149
switch(mode) {
137-
case MODE_HEADER:
150+
case Header:
138151
headerMode();
139152
break;
140-
case MODE_DATA:
153+
case Data:
141154
dataMode();
142155
break;
143156
}
@@ -178,7 +191,7 @@ void headerMode(){
178191
bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
179192
outPos = 0;
180193
memset(leds, 0, Num_Leds * sizeof(struct CRGB));
181-
mode = MODE_DATA; // Proceed to latch wait mode
194+
mode = Data; // Proceed to latch wait mode
182195
}
183196
headPos = 0; // Reset header position regardless of checksum result
184197
break;
@@ -195,10 +208,13 @@ void dataMode(){
195208

196209
if(bytesRemaining == 0) {
197210
// End of data -- issue latch:
198-
mode = MODE_HEADER; // Begin next header search
211+
mode = Header; // Begin next header search
199212
FastLED.show();
200213
D_FPS;
201214
D_LED(OFF);
215+
#ifdef SERIAL_FLUSH
216+
serialFlush();
217+
#endif
202218
}
203219
}
204220

@@ -226,7 +242,14 @@ void timeouts(){
226242
if((t - lastByteTime) > SerialTimeout * 1000) {
227243
memset(leds, 0, Num_Leds * sizeof(struct CRGB)); //filling Led array by zeroes
228244
FastLED.show();
245+
mode = Header;
229246
lastByteTime = t; // Reset counter
230247
}
231248
}
232249
}
250+
251+
void serialFlush(){
252+
while(Serial.available() > 0) {
253+
byte r = Serial.read();
254+
}
255+
}

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@ In addition to ambilight setups, the protocol can be used to stream *any* color
1010

1111
For this sketch to work, you'll need to have a copy of the FastLED library. You can download FastLED [from GitHub](https://github.com/FastLED/FastLED) or through the libraries manager of the Arduino IDE. This program was writen using FastLED 3.1.3 - note that earlier versions of FastLED are untested and may not work properly.
1212

13-
## Configuration
13+
For more information on my own ambilight setup, see [the project page](http://www.partsnotincluded.com/projects/ambilight/) on [PartsNotIncluded.com](http://www.partsnotincluded.com/).
14+
15+
## Basic Setup
1416

1517
Open the LEDstream_FastLED file in the Arduino IDE and customize the settings at the top for your setup. You will need to change:
1618

1719
- Number of LEDs
1820
- LED data pin
1921
- LED type
2022

21-
Additional settings allow for adjusting:
23+
Upload to your Arduino and use a corresponding PC application to stream color data. You can get the Processing files from the [main Adalight repository](https://github.com/adafruit/Adalight), though I would recommend using [Patrick Siegler's](https://github.com/psieg/) fork of Lightpacks's Prismatik, which you can find [here](https://github.com/psieg/Lightpack/releases).
24+
25+
## Additional Settings
26+
27+
There are additional settings to allow for adjusting:
2228

2329
- Max brightness
2430
- LED color order
2531
- Serial speed
2632
- Serial timeout length
2733

28-
There are also optional settings to clear the LEDs on reset, configure a dedicated ground pin, and to put the Arduino into a "calibration" mode, where all LED colors match the first LED.
29-
30-
Upload to your Arduino and use a corresponding PC application to stream color data. You can get the Processing files from the [main Adalight repository](https://github.com/adafruit/Adalight), though I would recommend using [Patrick Siegler's](https://github.com/psieg/) fork of Lightpacks's Prismatik, which you can find [here](https://github.com/psieg/Lightpack/releases).
34+
There are also optional settings to clear the LEDs on reset, configure a dedicated ground pin, and to put the Arduino into a "calibration" mode, where all LED colors match the first LED. To help with flickering, the option to flush the serial buffer after every latch is enabled by default.
3135

3236
## Debug Settings
3337

0 commit comments

Comments
 (0)