Move lighting logic in to its own class. Add definition guards around classes.
This commit is contained in:
parent
0052ac0169
commit
a3068a2d15
6 changed files with 111 additions and 64 deletions
55
Lighting.cpp
Normal file
55
Lighting.cpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
#include "Lighting.h"
|
||||
#include <FastLED.h>
|
||||
|
||||
#define MAX_HEAT_INDEX 240.0
|
||||
|
||||
Lighting::Lighting(int ledCount, NaturalLight* naturalLight, Weather* weather, float cloudCoverLimit) {
|
||||
_ledCount = ledCount;
|
||||
_naturalLight = naturalLight;
|
||||
_weather = weather;
|
||||
_cloudCoverLimit = cloudCoverLimit;
|
||||
|
||||
_leds = CRGB(_ledCount);
|
||||
}
|
||||
|
||||
float Lighting::getIndexMultiplier(int now, int startTime, int endTime, bool swap) {
|
||||
float a = swap ? endTime - now : now - startTime;
|
||||
float b = endTime - startTime;
|
||||
return constrain(a / b, 0, 1);
|
||||
}
|
||||
|
||||
float Lighting::getHeatIndex(int now) {
|
||||
float indexMultiplier = now >= _naturalLight->getSunset()
|
||||
? getIndexMultiplier(now, _naturalLight->getSunset(), _naturalLight->getAstronomicalTwilightEnd(), true)
|
||||
: getIndexMultiplier(now, _naturalLight->getAstronomicalTwilightBegin(), _naturalLight->getSunrise(), false);
|
||||
return MAX_HEAT_INDEX * indexMultiplier;
|
||||
}
|
||||
|
||||
float Lighting::getBrightness() {
|
||||
float cloudCover = (float)_weather->getCloudCover() / 100.0;
|
||||
float multiplier = cloudCover * (_cloudCoverLimit / 100.0);
|
||||
if (_weather->getCondition() == WeatherCondition::Other)
|
||||
multiplier *= 2.0;
|
||||
return 255.0 - (multiplier * 255.0);
|
||||
}
|
||||
|
||||
void Lighting::update(int now) {
|
||||
_heatIndex = getHeatIndex(now);
|
||||
_brightness = getBrightness();
|
||||
|
||||
CRGB colour = ColorFromPalette(_sunrise, _heatIndex, _brightness, LINEARBLEND);
|
||||
|
||||
if (_weather->getCondition() == WeatherCondition::Thunder && millis() >= _nextLightningFlash) {
|
||||
int flashes = random8(2, 8);
|
||||
for (int i = 0; i < flashes; i++) {
|
||||
FastLED.showColor(CRGB::White);
|
||||
delay(random8(10, 20));
|
||||
FastLED.showColor(colour);
|
||||
delay(random8(40, 80));
|
||||
}
|
||||
_nextLightningFlash = millis() + (random8(1, 60) * 1000);
|
||||
}
|
||||
|
||||
fill_solid(&_leds, _ledCount, colour);
|
||||
FastLED.show();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue