Adalight-FastLED_rgbwMod/Arduino/LEDstream_FastLED/LEDstream_FastLED.ino

212 lines
5.7 KiB
Arduino
Raw Normal View History

/* LEDstream_FastLED
2016-12-21 17:02:29 +00:00
*
* Modified version of Adalight protocol that uses the FastLED
* library (http://fastled.io) for driving led strips.
*
* http://github.com/dmadison/Adalight-FastLED
2017-04-08 13:57:42 +00:00
* Last Updated: 2017-04-08
2016-12-21 17:02:29 +00:00
*/
2016-12-21 16:01:46 +00:00
2017-03-19 22:48:14 +00:00
// --- General Settings
static const uint8_t
2017-03-27 21:35:44 +00:00
Num_Leds = 80, // strip length
Led_Pin = 6, // Arduino data output pin
Brightness = 255; // maximum brightness
2016-12-21 16:19:03 +00:00
2016-12-21 16:29:46 +00:00
// --- FastLED Setings
#define LED_TYPE WS2812B // led strip type for FastLED
#define COLOR_ORDER GRB // color order for bitbang
2016-12-21 16:19:03 +00:00
// --- Serial Settings
static const unsigned long
2017-03-27 21:35:44 +00:00
SerialSpeed = 115200, // serial port speed, max available
2017-03-19 22:48:14 +00:00
SerialTimeout = 150000; // time before LEDs are shut off, if no data
2017-03-27 21:35:44 +00:00
// (150 seconds)
2017-03-19 22:48:14 +00:00
// --- Optional Settings (uncomment to add)
2017-03-27 19:14:02 +00:00
//#define CLEAR_ON_START // LEDs are cleared on reset
2016-12-21 16:19:03 +00:00
//#define GROUND_PIN 10 // additional grounding pin (optional)
2016-12-21 19:13:59 +00:00
//#define CALIBRATE // sets all LEDs to the color of the first
2017-04-19 02:52:37 +00:00
//#define DEBUG_LED 13 // turns on the Arduino's built-in LED
// if the magic word + checksum match
2016-12-21 16:19:03 +00:00
// --------------------------------------------------------------------
2016-12-21 16:29:46 +00:00
#include <FastLED.h>
CRGB leds[Num_Leds];
uint8_t * ledsRaw = (uint8_t *)leds;
// A 'magic word' (along with LED count & checksum) precedes each block
// of LED data; this assists the microcontroller in syncing up with the
// host-side software and properly issuing the latch (host I/O is
2017-03-27 21:35:44 +00:00
// likely buffered, making usleep() unreliable for latch). You may see
// an initial glitchy frame or two until the two come into alignment.
// The magic word can be whatever sequence you like, but each character
// should be unique, and frequent pixel values like 0 and 255 are
2017-03-27 21:35:44 +00:00
// avoided -- fewer false positives. The host software will need to
// generate a compatible header: immediately following the magic word
// are three bytes: a 16-bit count of the number of LEDs (high byte
// first) followed by a simple checksum value (high byte XOR low byte
2017-03-27 21:35:44 +00:00
// XOR 0x55). LED data follows, 3 bytes per LED, in order R, G, B,
// where 0 = off and 255 = max brightness.
static const uint8_t magic[] = {
2017-03-27 21:35:44 +00:00
'A','d','a'};
#define MAGICSIZE sizeof(magic)
// Check values are header byte # - 1, as they are indexed from 0
#define HICHECK (MAGICSIZE)
#define LOCHECK (MAGICSIZE + 1)
#define CHECKSUM (MAGICSIZE + 2)
#define MODE_HEADER 0
#define MODE_DATA 1
void setup(){
2017-03-27 21:35:44 +00:00
#ifdef GROUND_PIN
pinMode(GROUND_PIN, OUTPUT);
digitalWrite(GROUND_PIN, LOW);
#endif
2016-12-21 16:01:46 +00:00
2017-04-19 02:52:37 +00:00
#ifdef DEBUG_LED
pinMode(DEBUG_LED, OUTPUT);
digitalWrite(DEBUG_LED, LOW);
#endif
2017-03-27 21:35:44 +00:00
FastLED.addLeds<LED_TYPE, Led_Pin, COLOR_ORDER>(leds, Num_Leds);
FastLED.setBrightness(Brightness);
2017-03-27 21:35:44 +00:00
#ifdef CLEAR_ON_START
FastLED.show();
#endif
2017-03-27 19:14:02 +00:00
2017-03-27 21:35:44 +00:00
Serial.begin(SerialSpeed);
2017-03-27 21:35:44 +00:00
adalight();
}
void adalight(){
static uint8_t
mode = MODE_HEADER;
static uint8_t
headPos,
hi, lo, chk;
2017-03-27 21:35:44 +00:00
int16_t
c;
static uint16_t
2017-03-27 21:35:44 +00:00
outPos;
static uint32_t
2017-03-28 02:59:53 +00:00
bytesRemaining;
2017-03-27 21:35:44 +00:00
unsigned long
t;
static unsigned long
lastByteTime,
lastAckTime;
2017-03-27 21:35:44 +00:00
Serial.print("Ada\n"); // Send ACK string to host
lastByteTime = lastAckTime = millis();
// loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput.
for(;;) {
// Implementation is a simple finite-state machine.
// Regardless of mode, check for serial input each time:
t = millis();
if((c = Serial.read()) >= 0){
2017-03-27 21:35:44 +00:00
lastByteTime = lastAckTime = t; // Reset timeout counters
switch(mode) {
case MODE_HEADER:
if(headPos < MAGICSIZE){
if(c == magic[headPos]) headPos++;
else headPos = 0;
}
else{
switch(headPos){
case HICHECK:
hi = c;
headPos++;
break;
case LOCHECK:
lo = c;
headPos++;
break;
case CHECKSUM:
chk = c;
if(chk == (hi ^ lo ^ 0x55)) {
// Checksum looks valid. Get 16-bit LED count, add 1
// (# LEDs is always > 0) and multiply by 3 for R,G,B.
2017-04-19 02:52:37 +00:00
#ifdef DEBUG_LED
digitalWrite(DEBUG_LED, HIGH);
#endif
bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
outPos = 0;
memset(leds, 0, Num_Leds * sizeof(struct CRGB));
mode = MODE_DATA; // Proceed to latch wait mode
}
headPos = 0; // Reset header position regardless of checksum result
break;
2017-03-27 21:35:44 +00:00
}
}
break;
case MODE_DATA:
2017-03-27 21:35:44 +00:00
if(bytesRemaining > 0) {
2017-03-27 21:35:44 +00:00
if (outPos < sizeof(leds)){
#ifdef CALIBRATE
if(outPos < 3)
ledsRaw[outPos++] = c;
2017-03-27 21:35:44 +00:00
else{
ledsRaw[outPos] = ledsRaw[outPos%3]; // Sets RGB data to first LED color
outPos++;
}
#else
ledsRaw[outPos++] = c; // Issue next byte
2017-03-27 21:35:44 +00:00
#endif
}
bytesRemaining--;
}
if(bytesRemaining == 0) {
// End of data -- issue latch:
mode = MODE_HEADER; // Begin next header search
FastLED.show();
2017-04-19 02:52:37 +00:00
#ifdef DEBUG_LED
digitalWrite(DEBUG_LED, LOW);
#endif
}
break;
} // end switch
} // end serial if
else {
// No data received. If this persists, send an ACK packet
// to host once every second to alert it to our presence.
if((t - lastAckTime) > 1000) {
Serial.print("Ada\n"); // Send ACK string to host
lastAckTime = t; // Reset counter
2017-03-27 21:35:44 +00:00
}
// If no data received for an extended time, turn off all LEDs.
if((t - lastByteTime) > SerialTimeout) {
memset(leds, 0, Num_Leds * sizeof(struct CRGB)); //filling Led array by zeroes
2017-03-27 21:35:44 +00:00
FastLED.show();
lastByteTime = t; // Reset counter
2017-03-27 21:35:44 +00:00
}
} // end else
2017-03-27 21:35:44 +00:00
} // end for(;;)
}
void loop()
{
// loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput.
}