Skip to content

Instantly share code, notes, and snippets.

@trx1138
Forked from axodox/adalight.cpp
Created November 5, 2020 23:16
Show Gist options
  • Save trx1138/868e41a474dcd0efb35d06910619f2eb to your computer and use it in GitHub Desktop.
Save trx1138/868e41a474dcd0efb35d06910619f2eb to your computer and use it in GitHub Desktop.
Dim on and off
/*
* Arduino interface for the use of WS2812 strip LEDs
* Uses Adalight protocol and is compatible with Boblight, Prismatik etc...
* "Magic Word" for synchronisation is 'Ada' followed by LED High, Low and Checksum
* @author: Wifsimster <wifsimster@gmail.com>
* @library: FastLED v3.001
* @date: 11/22/2015
*/
#include "FastLED.h"
#define MAX_LEDS 156
#define DATA_PIN 8
//Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf)
const uint32_t _serialRate = 500000;
//Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
uint8_t _prefix[] = {'A', 'd', 'a'};
//Initialise LED-array
CRGB _leds[MAX_LEDS];
void setup() {
//Use NEOPIXEL to keep true colors
FastLED.addLeds<NEOPIXEL, DATA_PIN>(_leds, MAX_LEDS);
FastLED.setDither(DISABLE_DITHER);
//Initial RGB flash
LEDS.showColor(CRGB(10, 0, 0));
delay(500);
LEDS.showColor(CRGB(0, 10, 0));
delay(500);
LEDS.showColor(CRGB(0, 0, 10));
delay(500);
LEDS.showColor(CRGB(0, 0, 0));
//Initialize serial connection
Serial.begin(_serialRate);
//Send "Magic Word" string to host
Serial.print("Ada\n");
}
struct led_count_t
{
uint8_t high, low;
uint8_t checksum;
};
uint16_t _lastLedCount = 0;
uint32_t _lastOnTime = 0, _lastOffTime = 0;
bool _isActive = false;
const uint32_t _timeout = 1000;
const uint32_t _dimTime = 2000;
void loop() {
waitLoop:
//Search for frame
for(auto i = 0; i < sizeof _prefix; ++i) {
//Wait for data and dim OFF
while (!Serial.available()) {
if(_isActive)
{
auto now = millis();
auto timeDiff = now - _lastOnTime;
if(timeDiff > _timeout)
{
auto overTime = timeDiff - _timeout;
if(overTime < _dimTime)
{
FastLED.setBrightness(255 * (1.0f - (float)overTime / (float)_dimTime));
FastLED.show();
_lastOffTime = now;
}
else
{
memset((char*)_leds, 0, MAX_LEDS * sizeof(CRGB));
FastLED.show();
_isActive = false;
}
}
}
};
//Check next byte in Magic Word
if(_prefix[i] == Serial.read()) continue;
// otherwise, start over
i = 0;
goto waitLoop;
}
//Read LED count
led_count_t rawLedCount;
Serial.readBytes((char*)&rawLedCount, sizeof(led_count_t));
// If checksum does not match go back to wait
if (rawLedCount.checksum != (rawLedCount.high ^ rawLedCount.low ^ 0x55)) {
goto waitLoop;
}
//Dim ON
auto now = millis();
if(!_isActive)
{
_isActive = true;
_lastOffTime = now;
}
{
auto timeDiff = now - _lastOffTime;
if(timeDiff < _dimTime)
{
FastLED.setBrightness(255 * (float)timeDiff / (float)_dimTime);
}
else
{
FastLED.setBrightness(255);
}
}
_lastOnTime = now;
//Read LED colors
auto requestedLedCount = (rawLedCount.high << 8) + rawLedCount.low + 1;
auto actualLedCount = max(requestedLedCount, MAX_LEDS);
Serial.readBytes((char*)_leds, actualLedCount * sizeof(CRGB));
//Zero out further bytes
if(_lastLedCount != actualLedCount)
{
_lastLedCount = actualLedCount;
if(actualLedCount < MAX_LEDS) memset((char*)(_leds + actualLedCount), 0, (MAX_LEDS - actualLedCount) * sizeof(CRGB));
}
//Show new values
FastLED.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment