#include "FastLED.h"
#define NUM_LEDS 56
#define DATA_PIN 6
// Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf)
#define serialRate 115200
// Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
// uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
// Initialise LED-array
CRGB leds[NUM_LEDS];
CRGB leds_to[NUM_LEDS];
CRGB leds_idle[NUM_LEDS];
CRGB color;
uint16_t leds_i = 0;
void setup() {
// Use NEOPIXEL to keep true colors
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
for( leds_i = 0; leds_i < NUM_LEDS; leds_i++ ) {
leds_idle[ leds_i ] = CRGB(0, 0, 0);
}
while ( false ) {
//if ( ini_led == 200 ) {
// delay(100);
// animation = true;
//}
// false for fade in animation, true for fade out
//if ( !animation ) {
// ini_led = map(i_step, 0, 49, /*start rgb*/ 0, /*final rgb*/200);
// if ( ini_led > 80 )
// ms_del = ini_led / 5;
// else
// ms_del = 10;
//}
//else {
// ini_led = map(i_step, 0, 49, /*start rgb*/ 200, /*final rgb*/0);
// if ( ini_led > 45 )
// ms_del = ini_led / 10;
//}
//delay(ms_del);
//LEDS.showColor(CRGB(ini_led, ini_led, ini_led));
//i_step++;
//if( i_step == end_step ) break;
/*// Initial RGB flash
ini_led += 3;
if ( ini_led > 80 ) {
ms_del = ini_led / 5;
} else {
ms_del = 10;
}
if ( ini_led >= 200 ) {
ini_led = 200;
}
delay(ms_del);
LEDS.showColor(CRGB(ini_led, ini_led, ini_led));*/
}
//while ( ini_led > 0 ) {
/*// Initial RGB flash
ini_led += -3;
if ( ini_led > 45 ) {
ms_del = ini_led / 10;
}
if ( ini_led <= 0 ) {
ini_led = 0;
}
delay(ms_del);
LEDS.showColor(CRGB(ini_led, ini_led, ini_led));*/
//}
LEDS.showColor(CRGB(255, 255, 255));
FastLED.show();
delay(1000);
Serial.begin(serialRate);
// Send "Magic Word" string to host
Serial.print("Ada\n");
}
/*
void loop() {
if (Serial) Serial.print("-Serial- \n");
Serial.print("0 \n");
// Wait for first byte of Magic Word
for (i = 0; i < sizeof prefix; ++i) {
Serial.print("1 \n");
waitLoop: while (!Serial.available()) ;;
Serial.print("2 \n");
// Check next byte in Magic Word
if (prefix[i] == Serial.read()) continue;
// otherwise, start over
i = 0;
Serial.print("3 \n");
goto waitLoop;
Serial.print("4 \n");
}
// Hi, Lo, Checksum
while (!Serial.available()) ;;
hi = Serial.read();
while (!Serial.available()) ;;
lo = Serial.read();
while (!Serial.available()) ;;
chk = Serial.read();
// If checksum does not match go back to wait
if (chk != (hi ^ lo ^ 0x55)) {
i = 0;
goto waitLoop;
}
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
// Read the transmission data and set LED values
for (uint8_t i = 0; i < NUM_LEDS; i++) {
byte r, g, b;
while (!Serial.available());
r = Serial.read();
while (!Serial.available());
g = Serial.read();
while (!Serial.available());
b = Serial.read();
leds[i].r = r;
leds[i].g = g;
leds[i].b = b;
}
// Shows new values
FastLED.show();
}
// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8(uint8_t ¤t, const uint8_t target)
{
if (current == target)
{
return;
}
if (current < target)
{
uint8_t delta = target - current;
delta = scale8_video(delta, 22);
current += delta;
}
else
{
uint8_t delta = current - target;
delta = scale8_video(delta, 22);
current -= delta;
}
}*/
// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount )
{
if( cur == target ) return;
if( cur < target ) {
uint8_t delta = target - cur;
delta = scale8_video( delta, amount );
cur += delta;
} else {
uint8_t delta = cur - target;
delta = scale8_video( delta, amount );
cur -= delta;
}
}
// Blend one CRGB color toward another CRGB color by a given amount.
// Blending is linear, and done in the RGB color space.
// This function modifies 'cur' in place.
/*CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount)
{
nblendU8TowardU8( cur.red, target.red, amount);
nblendU8TowardU8( cur.green, target.green, amount);
nblendU8TowardU8( cur.blue, target.blue, amount);
return cur;
}*/
// Fade an entire array of CRGBs toward a given background color by a given amount
// This function modifies the pixel array in place.
/*void fadeTowardColor( CRGB* L, uint16_t N, const CRGB& bgColor, uint8_t fadeAmount )
{
for( uint16_t i = 0; i < N; i++ ) {
fadeTowardColor( L[i], bgColor, fadeAmount );
}
}*/
CRGB blendColorTo( CRGB& cur, const CRGB& target, uint8_t amount)
{
nblendU8TowardU8( cur.red, target.red, amount );
nblendU8TowardU8( cur.green, target.green, amount );
nblendU8TowardU8( cur.blue, target.blue, amount );
return cur;
}
void fadeTowardColor( CRGB* L, uint16_t N, CRGB* LO, uint8_t fadeAmount )
{
for( uint16_t i = 0; i < N; i++ ) {
blendColorTo( L[i], LO[i], fadeAmount );
}
}
boolean papu = false;
void loop()
{
//if (!Serial.available()) {
if (papu) {
for( leds_i = 0; leds_i < NUM_LEDS; leds_i++ ) {
leds_to[ leds_i ] = leds_idle[ leds_i ];
}
} else {
EVERY_N_MILLISECONDS( 3000 ) {
//uint16_t pos = random16(NUM_LEDS);
for( leds_i = 0; leds_i < NUM_LEDS; leds_i++ ) {
color = CHSV(random8(), 255, 255);
leds_to[ leds_i ] = color;
}
}
}
EVERY_N_MILLISECONDS( 6000 ) {
papu = !papu;
}
//CRGB bgColor(0, 0, 0);
// fade all existing pixels toward bgColor by "5" (out of 255)
//fadeTowardColor(leds, NUM_LEDS, bgColor, 5);
// periodically set random pixel to a random color, to show the fading
fadeTowardColor( leds, NUM_LEDS, leds_to, 5 );
FastLED.show();
FastLED.delay(20);
}