Merge pull request #1: modernization
This commit is contained in:
commit
1bf8d37baa
|
@ -0,0 +1,164 @@
|
|||
// Slightly modified Adalight protocol implementation that uses FastLED
|
||||
// library (http://fastled.io) for driving WS2811/WS2812 led strip
|
||||
|
||||
#include "FastLED.h"
|
||||
|
||||
#define NUM_LEDS 80 // strip length
|
||||
#define LED_PIN 6 // Arduino data output pin
|
||||
#define GROUND_PIN 10 // additional grounding pin (optional)
|
||||
#define BRIGHTNESS 255 // maximum brightness
|
||||
#define SPEED 115200 // serial port speed, max available
|
||||
|
||||
// If no serial data is received for a while, the LEDs are shut off
|
||||
// automatically. Value in milliseconds.
|
||||
static const unsigned long serialTimeout = 150000; // 150 seconds
|
||||
|
||||
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
|
||||
// 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
|
||||
// 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
|
||||
// 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[] = {
|
||||
'A','d','a'};
|
||||
#define MAGICSIZE sizeof(magic)
|
||||
#define HEADERSIZE (MAGICSIZE + 3)
|
||||
|
||||
#define MODE_HEADER 0
|
||||
#define MODE_DATA 2
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(GROUND_PIN, OUTPUT);
|
||||
digitalWrite(GROUND_PIN, LOW);
|
||||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
|
||||
// Dirty trick: the circular buffer for serial data is 256 bytes,
|
||||
// and the "in" and "out" indices are unsigned 8-bit types -- this
|
||||
// much simplifies the cases where in/out need to "wrap around" the
|
||||
// beginning/end of the buffer. Otherwise there'd be a ton of bit-
|
||||
// masking and/or conditional code every time one of these indices
|
||||
// needs to change, slowing things down tremendously.
|
||||
uint8_t
|
||||
buffer[256],
|
||||
indexIn = 0,
|
||||
indexOut = 0,
|
||||
mode = MODE_HEADER,
|
||||
hi, lo, chk, i, spiFlag;
|
||||
int16_t
|
||||
bytesBuffered = 0,
|
||||
hold = 0,
|
||||
c;
|
||||
int32_t
|
||||
bytesRemaining;
|
||||
unsigned long
|
||||
startTime,
|
||||
lastByteTime,
|
||||
lastAckTime,
|
||||
t;
|
||||
int32_t outPos = 0;
|
||||
|
||||
Serial.begin(SPEED); // Teensy/32u4 disregards baud rate; is OK!
|
||||
|
||||
Serial.print("Ada\n"); // Send ACK string to host
|
||||
|
||||
startTime = micros();
|
||||
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((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) {
|
||||
buffer[indexIn++] = c;
|
||||
bytesBuffered++;
|
||||
lastByteTime = lastAckTime = t; // Reset timeout counters
|
||||
}
|
||||
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
|
||||
}
|
||||
// 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
|
||||
FastLED.show();
|
||||
lastByteTime = t; // Reset counter
|
||||
}
|
||||
}
|
||||
|
||||
switch(mode) {
|
||||
|
||||
case MODE_HEADER:
|
||||
|
||||
// In header-seeking mode. Is there enough data to check?
|
||||
if(bytesBuffered >= HEADERSIZE) {
|
||||
// Indeed. Check for a 'magic word' match.
|
||||
for(i=0; (i<MAGICSIZE) && (buffer[indexOut++] == magic[i++]););
|
||||
if(i == MAGICSIZE) {
|
||||
// Magic word matches. Now how about the checksum?
|
||||
hi = buffer[indexOut++];
|
||||
lo = buffer[indexOut++];
|
||||
chk = buffer[indexOut++];
|
||||
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.
|
||||
bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
|
||||
bytesBuffered -= 3;
|
||||
outPos = 0;
|
||||
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
|
||||
mode = MODE_DATA; // Proceed to latch wait mode
|
||||
}
|
||||
else {
|
||||
// Checksum didn't match; search resumes after magic word.
|
||||
indexOut -= 3; // Rewind
|
||||
}
|
||||
} // else no header match. Resume at first mismatched byte.
|
||||
bytesBuffered -= i;
|
||||
}
|
||||
break;
|
||||
|
||||
case MODE_DATA:
|
||||
|
||||
if(bytesRemaining > 0) {
|
||||
if(bytesBuffered > 0) {
|
||||
if (outPos < sizeof(leds))
|
||||
ledsRaw[outPos++] = buffer[indexOut++]; // Issue next byte
|
||||
bytesBuffered--;
|
||||
bytesRemaining--;
|
||||
}
|
||||
// If serial buffer is threatening to underrun, start
|
||||
// introducing progressively longer pauses to allow more
|
||||
// data to arrive (up to a point).
|
||||
}
|
||||
else {
|
||||
// End of data -- issue latch:
|
||||
startTime = micros();
|
||||
mode = MODE_HEADER; // Begin next header search
|
||||
FastLED.show();
|
||||
}
|
||||
} // end switch
|
||||
} // end for(;;)
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Not used. See note in setup() function.
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
## Synopsis
|
||||
|
||||
This is the standard Adalight library, modified to work with the FastLED library (fastled.io) and 3-pin WS2812B LED strips (2016-era Adafruit NeoPixels).
|
||||
|
||||
In addition to ambilight setups, the protocol can be used to stream any color data from a computer to a WS2812B strip (data rate limited by serial throughput).
|
||||
|
||||
|
||||
## Configuration
|
||||
|
||||
Open the WS2812B file in the Arduino IDE and edit the definitions at the top for your setup. Specifically:
|
||||
|
||||
- Number of LEDs
|
||||
- LED data pin
|
||||
- LED grounding pin (optional)
|
||||
- Brightness
|
||||
- Serial speed
|
||||
- Serial timeout length
|
||||
|
||||
Upload to your Arduino and use a corresponding PC application to stream color data. The Processing files are included, though I would recommend using Patrick Siegler's (@psieg) fork of Lightpacks's Prismatik, which you can find [here](https://github.com/psieg/Lightpack).
|
||||
|
||||
## Tutorial
|
||||
|
||||
If you'd like you can follow Adafruit's tutorial, which is fairly comprehensive for the WS2801 they use but is otherwise out of date. You can find the tutorial here:
|
||||
|
||||
<https://learn.adafruit.com/adalight-diy-ambient-tv-lighting>
|
||||
|
||||
|
||||
## License
|
||||
|
||||
Adalight is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
Adalight is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with Adalight. If not, see <http://www.gnu.org/licenses/>.
|
12
README.txt
12
README.txt
|
@ -1,12 +0,0 @@
|
|||
Adalight is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
Adalight is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with Adalight. If not, see <http://www.gnu.org/licenses/>.
|
Loading…
Reference in New Issue