Skip to content

Commit

Permalink
v1.2.27
Browse files Browse the repository at this point in the history
- added module `Rainbow Fx` to `color-light` firmware as an example data-processing/visualization module
- fixed bug in applying default firmware options that was affecting firmware boot after a factory reset
  • Loading branch information
genemars committed Jun 27, 2024
1 parent 3d64223 commit 609ba95
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 75 deletions.
125 changes: 71 additions & 54 deletions examples/color-light/color-light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,27 @@ void statusLedCallback(bool isLedOn) {
statusLED->show();
}

void show() {
if (changed && millis() - lastRefreshTs > 25) { // force 40 fps max
if (statusLED != nullptr) {
statusLED->show();
}
if (pixels != nullptr) {
pixels->show();
}
lastRefreshTs = millis();
changed = false;
}
}

void setup() {
homeGenie = HomeGenie::getInstance();

// Get status LED config
int statusLedPin = Config::getSetting("stld-pin", "0").toInt();
if (statusLedPin > 0) {
int statusLedType = Config::getSetting("stld-typ", "0").toInt();
int statusLedSpeed = Config::getSetting("stld-spd", "0").toInt();
int statusLedPin = Config::getSetting("stld-pin", "-1").toInt();
if (statusLedPin >= 0) {
int statusLedType = Config::getSetting("stld-typ").toInt();
int statusLedSpeed = Config::getSetting("stld-spd").toInt();
statusLED = new Adafruit_NeoPixel(1, statusLedPin, statusLedType + statusLedSpeed);
}

Expand All @@ -77,83 +90,87 @@ void setup() {
} else {

// Get LED strip config
count = Config::getSetting("leds-cnt", "0").toInt(); // 90 = 3mt // 30 LEDs per meter (3 mt. strip)
pin = (int16_t)Config::getSetting("leds-pin", "-1").toInt();
if (count > 0 && pin != -1) {
auto pixelsType = (int16_t)Config::getSetting("leds-typ", "-1").toInt();
auto pixelsSpeed = (int16_t)Config::getSetting("leds-spd", "0").toInt();
count = Config::getSetting("leds-cnt").toInt();
pin = (int16_t)Config::getSetting("leds-pin").toInt();
if (count > 0 && pin >= 0) {
auto pixelsType = (int16_t)Config::getSetting("leds-typ").toInt();
auto pixelsSpeed = (int16_t)Config::getSetting("leds-spd").toInt();
pixels = new Adafruit_NeoPixel(count, pin, pixelsType + pixelsSpeed);
}

// Setup Status LED as master channel
auto colorLight = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, "C1", "Demo Light");
colorLight->onSetColor([](float r, float g, float b) {
auto colorLight = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, "C1", "Main");
colorLight->onSetColor([](LightColor color) {
float r = color.getRed();
float g = color.getGreen();
float b = color.getBlue();

if (statusLED != nullptr) {
statusLED->setPixelColor(0, r, g, b);
}

for (int i = 0; i < count; i++) {
pixels->setPixelColor(i, r, g, b);
if (pixels != nullptr) {
for (int i = 0; i < count; i++) {
pixels->setPixelColor(i, r, g, b);
}
}

changed = true;
if (millis() - lastRefreshTs > 50) { // force 20fps max
if (statusLED != nullptr) {
statusLED->show();
}
if (pixels != nullptr) {
pixels->show();
}
lastRefreshTs = millis();
changed = false;
}
show();
});
homeGenie->addAPIHandler(colorLight);

// Setup LED strip/array
if (pixels != nullptr) {
for (int i = 0; i < count; i++) {
auto address = String("L") + String(i + 1);
auto cl = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, address.c_str(), "Demo Light");
cl->onSetColor([i](float r, float g, float b) {
pixels->setPixelColor(i, r, g, b);
changed = true;
if (millis() - lastRefreshTs > 50) { // force 20fps max
pixels->show();
lastRefreshTs = millis();
changed = false;
}
});
homeGenie->addAPIHandler(cl);
// Setup example input "processor" module
// changing color of this module will affect
// all LED pixels with a color cycle effect
auto fxModule = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, "F1", "Rainbow");
auto soundLightFeature = new ModuleParameter("Widget.Preference.AudioLight", "true");
fxModule->module->properties.add(soundLightFeature);
fxModule->onSetColor([](LightColor color) {

if (statusLED != nullptr) {
statusLED->setPixelColor(0, color.getRed(), color.getGreen(), color.getBlue());
}
}
// TODO: implement color/status recall on start
// TODO: implement color/status recall on start
// TODO: implement color/status recall on start

if (pixels != nullptr) {
float s = color.getSaturation();
if (s > 1) {
s = 1;
}
float h = color.getHue();
float hueStep = 1.0f / (float) count;
float v = color.getValue();
for (int i = 0; i < count; i++) {
h += hueStep;
auto rgb = Utility::hsv2rgb(h, s, v);
pixels->setPixelColor(i, rgb.r, rgb.g, rgb.b);
}
}

changed = true;
show();

});
homeGenie->addAPIHandler(fxModule);

}

homeGenie->begin();

// TODO: implement color/status recall on start
// TODO: implement color/status recall on start
// TODO: implement color/status recall on start

if (statusLED != nullptr) {
statusLED->begin();
}
if (pixels != nullptr) {
pixels->begin();
}
homeGenie->begin();
}

void loop()
{
homeGenie->loop();

if (changed) { // trailing fx
changed = false;
if (statusLED != nullptr) {
statusLED->show();
}
if (pixels != nullptr) {
pixels->show();
}
lastRefreshTs = millis();
}
show();
}
4 changes: 2 additions & 2 deletions examples/ir-transceiver/ir-transceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ void setup() {

#ifdef ESP32_C3
auto colorLight = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, "C1", "Demo Light");
colorLight->onSetColor([](float r, float g, float b) {
pixels.setPixelColor(0, r, g, b);
colorLight->onSetColor([](LightColor c) {
pixels.setPixelColor(0, c.getRed(), c.getGreen(), c.getBlue());
pixels.show();
});
homeGenie->addAPIHandler(colorLight);
Expand Down
4 changes: 2 additions & 2 deletions examples/shutter/shutter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ void setup() {

#ifdef ESP32_C3
auto colorLight = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, "C1", "Demo Light");
colorLight->onSetColor([](float r, float g, float b) {
pixels.setPixelColor(0, r, g, b);
colorLight->onSetColor([](LightColor c) {
pixels.setPixelColor(0, c.getRed(), c.getGreen(), c.getBlue());
pixels.show();
});
homeGenie->addAPIHandler(colorLight);
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
build_flags = ${env.build_flags} -I examples -I src -D ESP32_C3 -D DISABLE_UI -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/color-light>
-D DEFAULT_CONFIG='{"sys-rb-n":"9","sys-sl-n":"-1","leds-typ":"6","leds-cnt":"64","leds-spd":"0","leds-pin":"8"}'
-D DEFAULT_CONFIG='{"sys-rb-n":"9","sys-sl-n":"-1","leds-typ":"6","leds-cnt":"25","leds-spd":"0","leds-pin":"8"}'
lib_deps = ${env.lib_deps}
https://github.com/adafruit/Adafruit_NeoPixel#1.12.0

Expand Down
8 changes: 4 additions & 4 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Config {
public:
static short ServiceButtonPin;
static short StatusLedPin;
const static uint16_t ConfigureButtonPushInterval = 5000;
const static uint16_t ConfigureButtonPushInterval = 3000;
static ZoneConfig zone;
static SystemConfig system;
#ifdef ESP32
Expand Down Expand Up @@ -149,7 +149,7 @@ class Config {
preferences.end();
// TODO: should use "pref.isKey(...)" for proper checking
if (value == "") {
// Lookup config factory defaults only if the value does not exists
// Lookup config factory defaults only if the value does not exist
String config = STRING_VALUE(DEFAULT_CONFIG);
if (!config.isEmpty()) {
JsonDocument doc;
Expand Down Expand Up @@ -177,8 +177,8 @@ class Config {

static void init() {
// Setup status LED and factory reset buttons
ServiceButtonPin = getSetting("sys-rb-n", "-1").toInt();
StatusLedPin = getSetting("sys-sl-n", "-1").toInt();
ServiceButtonPin = getSetting("sys-rb-n").toInt();
StatusLedPin = getSetting("sys-sl-n").toInt();
// Setup status LED
if (StatusLedPin >= 0) pinMode(StatusLedPin, OUTPUT);
Preferences preferences;
Expand Down
4 changes: 2 additions & 2 deletions src/service/api/devices/ColorLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ namespace Service { namespace API { namespace devices {
void ColorLight::loop() {
Dimmer::loop(); // parent

if (color.isAnimating) {
if (color.isAnimating()) {
if (setColorCallback != nullptr) {
setColorCallback(color.getRed(), color.getGreen(), color.getBlue());
setColorCallback(color);
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/service/api/devices/ColorLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,24 @@ namespace Service { namespace API { namespace devices {

class LightColor {
public:
unsigned long duration;
bool isAnimating = false;
unsigned long duration = 400;
void setColor(float hue, float saturation, float value, unsigned long transitionMs) {
duration = transitionMs;
if (duration <= 0) duration = 1;
oh = h;
os = s;
ov = v;
h = hue;
s = saturation;
v = value;
startTime = millis();
isAnimating = true;
}
float getProgress() {
bool isAnimating() const {
return ((float)(millis() - startTime) / (float)duration) < 1;
}
float getProgress() const {
float p = (float)(millis() - startTime) / (float)duration;
if (p >= 1) {
isAnimating = false;
p = 1;
}
return p;
Expand Down Expand Up @@ -88,7 +89,7 @@ namespace Service { namespace API { namespace devices {
float s;
float v;
float oh, os, ov;
unsigned long startTime;
unsigned long startTime = -1;
static float hueFix(float h) {
return 1.325f - h;
}
Expand All @@ -102,12 +103,12 @@ namespace Service { namespace API { namespace devices {
void loop() override;
bool handleRequest(APIRequest*, ResponseCallback*) override;

void onSetColor(std::function<void(float,float,float)> callback) {
void onSetColor(std::function<void(LightColor)> callback) {
setColorCallback = std::move(callback);
}
private:
LightColor color;
std::function<void(float,float,float)> setColorCallback = nullptr;
std::function<void(LightColor)> setColorCallback = nullptr;
};

}}}
Expand Down
3 changes: 2 additions & 1 deletion src/service/api/devices/Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ namespace Service { namespace API { namespace devices {
void onSetStatus(std::function<void(SwitchStatus)> callback) {
setStatusCallback = std::move(callback);
}

Module* module;
private:
LinkedList<Module*> moduleList;
std::function<void(SwitchStatus)> setStatusCallback = nullptr;
protected:
Module* module;
SwitchStatus status = SWITCH_STATUS_NOT_SET;
float onLevel = 1;
};
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#define VERSION_MAJOR 1
#define VERSION_MINOR 2
#define VERSION_PATCH 26
#define VERSION_PATCH 27

#define STRING_VALUE(...) STRING_VALUE__(__VA_ARGS__)
#define STRING_VALUE__(...) #__VA_ARGS__
Expand Down

0 comments on commit 609ba95

Please sign in to comment.