Last active
November 30, 2020 15:48
-
-
Save axodox/fac6c9a75c17ead82e49d90523599765 to your computer and use it in GitHub Desktop.
Dim on and off
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 214 | |
#define DATA_PIN 8 | |
const uint8_t _brightness = 255; | |
//Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf) | |
const uint32_t _serialRate = 1000000; | |
//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<WS2812B, DATA_PIN, GRB>(_leds, MAX_LEDS); | |
//.setTemperature(Halogen); | |
//.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, SERIAL_8N1); | |
//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 = 1500; | |
const uint32_t _dimTime = 1000; | |
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(_brightness * (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(_brightness * (float)timeDiff / (float)_dimTime); | |
} | |
else | |
{ | |
FastLED.setBrightness(_brightness); | |
} | |
} | |
_lastOnTime = now; | |
//Read LED colors | |
auto requestedLedCount = (rawLedCount.high << 8) + rawLedCount.low + 1; | |
auto actualLedCount = min(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