Skip to content

Instantly share code, notes, and snippets.

@tedder
Created April 29, 2016 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedder/8690eb95b915dcdc0da9a0a12a741e74 to your computer and use it in GitHub Desktop.
Save tedder/8690eb95b915dcdc0da9a0a12a741e74 to your computer and use it in GitHub Desktop.
particle.io photon fastled tv backlight controller
//This #include statement was automatically added by the Particle IDE.
#include "Sunrise/Sunrise.h"
#include "FastLED/FastLED.h"
FASTLED_USING_NAMESPACE;
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN D2
//#define CLK_PIN 4
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 128
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 120
int sunset_minute_of_day = 0; // minutes past midnight for sunset
int minute_of_day = 0; // minutes past midnight
int curr_brightness = 0;
int mins_to_sunset = -1;
int mins_past_sunset = -1;
//unsigned long currentTime;
//UDP UDPClient;
//SparkTime rtc;
//unsigned long currentTime;
Sunrise s(45.5, -122.7, 0); // 0=UTC
void setup() {
Serial.begin(115200);
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
// init with full color
fill_solid( leds, NUM_LEDS, CRGB( 40, 255, 255) );
FastLED.setBrightness(60);
FastLED.show();
// register vars
Particle.variable("sunset_mins", sunset_minute_of_day);
Particle.variable("now_mins", minute_of_day);
Particle.variable("brightness", curr_brightness);
Particle.variable("mto_sunset", mins_to_sunset);
Particle.variable("mpast_sunset", mins_past_sunset);
// register functions
Particle.function("dot_chaser", dotChaser);
Particle.function("off", off);
// make sure we are in UTC
Time.zone(0);
Particle.syncTime();
s.Civil();
// wait for the time to be set, but only wait 20 seconds
while (Time.year() <= 1970 && millis() < 20000) {
Serial.println("delaying while we wait for SyncTime");
delay(1000);
}
// done with setup, alter color, reduce brightness
fill_solid( leds, NUM_LEDS, CRGB( 255, 40, 255) );
FastLED.setBrightness(40);
FastLED.show();
}
bool do_dot_chaser = false;
void loop()
{
Serial.println("starting loop.");
minute_of_day = Time.hour()*60 + Time.minute();
// recalculate sunset ONLY if our cached value is way out of date. this will init too.
if(sunset_minute_of_day == 0 or minute_of_day > (sunset_minute_of_day + 60*12)) {
sunset_minute_of_day = s.Set(Time.month(),Time.day());
}
// shouldn't be negative
mins_to_sunset = diffMinutes(minute_of_day, sunset_minute_of_day, true);
// may be negative
mins_past_sunset = diffMinutes(minute_of_day, sunset_minute_of_day, false);
// start calculating brightness around 2pm, end 11pm
if(Time.hour() > 8 or Time.hour() < 6) {
if (mins_past_sunset > -120 and mins_past_sunset < 0) {
// full blast after sunset
curr_brightness = 100;
} else if (mins_to_sunset < 100) {
// get brighter as the sun sets
curr_brightness = 100 - mins_to_sunset;
}
} else {
curr_brightness = 0;
}
if (!do_dot_chaser) {
fill_solid( leds, NUM_LEDS, CRGB( 255, 84, 0) );
FastLED.setBrightness(curr_brightness);
FastLED.show();
}
delay(1000);
// send the 'leds' array out to the actual LED strip
//FastLED.show();
// insert a delay to keep the framerate modest
//FastLED.delay(1000/FRAMES_PER_SECOND);
if (do_dot_chaser) { // only enabled by remote func
dotChaser("");
}
}
int diffMinutes(int from, int to, bool rollover) {
if(from > to and rollover) {
to += 60*24;
}
int delta = to - from;
return delta;
}
int off(String input) {
FastLED.setBrightness(0);
FastLED.show();
do_dot_chaser = false;
delay(5000);
return 42;
}
int dotChaser(String input) {
do_dot_chaser = true;
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Blue;
FastLED.show();
// clear this led for the next time around the loop
leds[dot] = CRGB( 0, 0, 40);
delay(50);
if (dot == NUM_LEDS-1) {
delay(1000);
}
}
return 42;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment