save color on OFF
This commit is contained in:
parent
cb81d71218
commit
f986478e62
@ -4,6 +4,16 @@
|
||||
|
||||
class MyLed {
|
||||
public:
|
||||
typedef std::array<uint8_t, 3> ColorArr;
|
||||
|
||||
typedef std::function<void()> CallbackFunc;
|
||||
|
||||
typedef struct {
|
||||
ColorArr color;
|
||||
uint8_t brightness;
|
||||
bool isOn;
|
||||
} State;
|
||||
|
||||
MyLed(int ledPin, int ledCount, bool effects = false);
|
||||
|
||||
void initialize();
|
||||
@ -14,7 +24,7 @@ public:
|
||||
|
||||
void toggle();
|
||||
|
||||
//! @param state Json package
|
||||
//! @param jsonState Json package
|
||||
//! {
|
||||
//! "brightness": 255,
|
||||
//! "color": {
|
||||
@ -26,8 +36,13 @@ public:
|
||||
//! }
|
||||
void set(String jsonState);
|
||||
|
||||
void setState(State);
|
||||
|
||||
//! @return a Json string on the same format as for MyLed::set(jsonState)
|
||||
String get();
|
||||
|
||||
State getState();
|
||||
|
||||
//! @param brightness in [0, 1]
|
||||
void setBrightness(float brightness);
|
||||
|
||||
@ -38,10 +53,11 @@ public:
|
||||
|
||||
void run();
|
||||
|
||||
private:
|
||||
void _apply();
|
||||
//! @param CallbackFunc will be called when the LED is turned off
|
||||
void registerOffCallback(CallbackFunc);
|
||||
|
||||
typedef std::array<uint8_t, 3> ColorArr;
|
||||
private:
|
||||
void apply();
|
||||
|
||||
bool _useEffects;
|
||||
|
||||
@ -54,4 +70,6 @@ private:
|
||||
const ColorArr _defColor;
|
||||
|
||||
ColorArr _color;
|
||||
|
||||
CallbackFunc _callback;
|
||||
};
|
18
src/main.cpp
18
src/main.cpp
@ -159,6 +159,21 @@ void mqttCallback(char* topic, byte* payload, unsigned int length)
|
||||
}
|
||||
}
|
||||
|
||||
void offCallback()
|
||||
{
|
||||
auto state = myLed.getState();
|
||||
if (config.data.color == state.color &&
|
||||
config.data.brightness == state.brightness)
|
||||
{
|
||||
return;
|
||||
} else
|
||||
{
|
||||
config.data.color = state.color;
|
||||
config.data.brightness = state.brightness;
|
||||
config.write();
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(boardLedPin, OUTPUT);
|
||||
@ -169,8 +184,11 @@ void setup()
|
||||
Serial.begin(9600);
|
||||
|
||||
myLed.initialize();
|
||||
myLed.registerOffCallback(offCallback);
|
||||
|
||||
config.load();
|
||||
myLed.setState(
|
||||
MyLed::State({config.data.color, config.data.brightness, true}));
|
||||
WiFi.begin(config.data.ssid, config.data.pass);
|
||||
WiFi.hostname(config.data.hostname);
|
||||
Serial.print("Connecting");
|
||||
|
@ -14,6 +14,7 @@ MyLed::MyLed(int ledPin, int ledCount, bool effects)
|
||||
, _isOn(true)
|
||||
, _defColor({255, 147, 41})
|
||||
, _color(_defColor)
|
||||
, _callback(nullptr)
|
||||
{}
|
||||
|
||||
void MyLed::initialize()
|
||||
@ -31,11 +32,14 @@ void MyLed::setOnState(bool on)
|
||||
Serial.println(on ? "Turning on" : "Turning off");
|
||||
if (on)
|
||||
{
|
||||
_apply();
|
||||
apply();
|
||||
} else
|
||||
{
|
||||
_strip.fill({});
|
||||
_strip.show();
|
||||
|
||||
if (_callback)
|
||||
_callback();
|
||||
};
|
||||
}
|
||||
|
||||
@ -80,6 +84,13 @@ void MyLed::set(String jsonState)
|
||||
}
|
||||
}
|
||||
|
||||
void MyLed::setState(State s)
|
||||
{
|
||||
setColor(s.color.at(0), s.color.at(1), s.color.at(2));
|
||||
setBrightness(static_cast<float>(s.brightness) / 255.0f);
|
||||
setOnState(s.isOn);
|
||||
}
|
||||
|
||||
String MyLed::get()
|
||||
{
|
||||
return "{\"brightness\": " + String(_brightness * 255.0f) +
|
||||
@ -89,6 +100,11 @@ String MyLed::get()
|
||||
String(_isOn ? "ON" : "OFF") + "\"}";
|
||||
}
|
||||
|
||||
MyLed::State MyLed::getState()
|
||||
{
|
||||
return {_color, static_cast<uint8_t>(_brightness * 255.0f), _isOn};
|
||||
}
|
||||
|
||||
void MyLed::setBrightness(float brightness)
|
||||
{
|
||||
brightness = max(minBrightness, brightness);
|
||||
@ -99,7 +115,7 @@ void MyLed::setBrightness(float brightness)
|
||||
} else
|
||||
{
|
||||
_brightness = brightness;
|
||||
_apply();
|
||||
apply();
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,7 +128,7 @@ void MyLed::adjustBrightness(float diff)
|
||||
void MyLed::setColor(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
_color = {r, g, b};
|
||||
_apply();
|
||||
apply();
|
||||
}
|
||||
|
||||
void MyLed::run()
|
||||
@ -124,11 +140,16 @@ void MyLed::run()
|
||||
const float mixFactor = 0.2;
|
||||
_brightness =
|
||||
_brightness * (1 - mixFactor) + _targetBrightness * mixFactor;
|
||||
_apply();
|
||||
apply();
|
||||
}
|
||||
}
|
||||
|
||||
void MyLed::_apply()
|
||||
void MyLed::registerOffCallback(CallbackFunc callback)
|
||||
{
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
void MyLed::apply()
|
||||
{
|
||||
_strip.fill(_strip.Color(static_cast<uint8_t>(_color.at(0) * _brightness),
|
||||
static_cast<uint8_t>(_color.at(1) * _brightness),
|
||||
|
Loading…
x
Reference in New Issue
Block a user