Skip to content

Commit

Permalink
v1.2.33
Browse files Browse the repository at this point in the history
- Upgraded to *espressif32 6.8.1*
- Added support for ESP32-S3-FH4R2
- `color-light': added 2 new FX
- `color-light': added widget option field for changing the number of LEDs from the app
  • Loading branch information
genemars committed Aug 20, 2024
1 parent 5eb37ff commit 1dbdc78
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 93 deletions.
56 changes: 56 additions & 0 deletions boards/esp32-s3-fh4r2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"build": {
"arduino":{
"partitions": "default.csv",
"memory_type": "qio_qspi"
},
"core": "esp32",
"extra_flags": [
"-DARDUINO_ESP32S3_DEV",
"-DARDUINO_RUNNING_CORE=1",
"-DARDUINO_EVENT_RUNNING_CORE=1",
"-DARDUINO_USB_CDC_ON_BOOT=1",
"-DBOARD_HAS_PSRAM"
],
"f_cpu": "240000000L",
"f_flash": "80000000L",
"flash_mode": "qio",
"psram_type": "qio",
"hwids": [
[
"0x303A",
"0x1001"
]
],
"mcu": "esp32s3",
"variant": "esp32s3"
},
"connectivity": [
"wifi",
"bluetooth"
],
"debug": {
"default_tool": "esp-builtin",
"onboard_tools": [
"esp-builtin"
],
"openocd_target": "esp32s3.cfg"
},
"frameworks": [
"arduino",
"espidf"
],
"platforms" : [
"espressif32"
],
"name": "Espressif ESP32-S3-FH4R2 (4 MB QD, 2MB PSRAM)",
"upload": {
"flash_size": "4MB",
"maximum_ram_size": 327680,
"maximum_size": 4194304,
"require_upload_port": true,
"speed": 921600
},
"url": "https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/hw-reference/esp32s3/user-guide-devkitc-1.html",
"vendor": "Espressif"
}
14 changes: 7 additions & 7 deletions examples/color-light/color-fx.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ void fx_solid(Adafruit_NeoPixel* pixels, LightColor& color, int transitionMs = 2
float currentSaturation;
float cursorDirection = 1;
unsigned long rainbow_refresh_ts = 0;
void fx_rainbow(Adafruit_NeoPixel* pixels, LightColor& color) {
void fx_rainbow(Adafruit_NeoPixel* pixels, LightColor& color, float iterations = 1) {
if (pixels == nullptr) return;

currentSaturation = color.getSaturation();
float length = pixels->numPixels() * hueZoom;

float hueStep = 1.0f / (float) length;
//float offsetIncrement = (0.128f / length);
float hueStep = 1.0f / (float) length * iterations;

// animate
if (millis() - rainbow_refresh_ts > 100) {
rainbow_refresh_ts = millis();

float v = color.getValue();
for (int i = 0; i < pixels->numPixels(); i++) {
float h = FX_DEG_NORM((color.getHue() + hueOffset) + ((float)i * hueStep));
float inc = (i < pixels->numPixels() / iterations ? (float)i : (float)(pixels->numPixels() - i)) * hueStep;
float h = FX_DEG_NORM((color.getHue() + hueOffset) + inc);
animatedColors[i]->setColor(h, currentSaturation, v, currentSaturation * 100);
}

Expand All @@ -103,7 +103,7 @@ int stripe_step = 3;
float stripe_cycle = 0;
float stripe_previous_hue;

void fx_white_stripes(Adafruit_NeoPixel* pixels, LightColor& color) {
void fx_white_stripes(Adafruit_NeoPixel* pixels, LightColor& color, bool brightWhite = false) {
if (pixels == nullptr) return;

int stripe_length = (int)round((float)pixels->numPixels() / 7.0f);
Expand All @@ -127,8 +127,8 @@ void fx_white_stripes(Adafruit_NeoPixel* pixels, LightColor& color) {
float s = color.getSaturation();
if ((int)round(i + shift) % stripe_length < stripe_step) {
// draw stripe
animatedColors[i]->setColor(0, 0, 1,
(float)stripe_transition / v);
animatedColors[i]->setColor(0, 0, brightWhite && v > 0 ? 1 : v,
(float)stripe_transition / pixels->numPixels());
} else {
// draw solid color
animatedColors[i]->setColor(color.getHue(), s, v,
Expand Down
158 changes: 105 additions & 53 deletions examples/color-light/color-light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ bool isConfigured = false;
Adafruit_NeoPixel* statusLED = nullptr;

// LED strip/array
int count = 0; int pin = -1;
uint16_t ledsCount = 0; int16_t ledsPin = -1;
int16_t pixelsType, pixelsSpeed;
Adafruit_NeoPixel* pixels = nullptr;

// LED Blink callback when statusLED is configured
Expand All @@ -59,12 +60,13 @@ unsigned long lastRefreshTs = 0;
LightColor currentColor;
ColorLight* mainModule;

ModuleParameter* fxStyle;
ModuleParameter* fxStrobe;
ModuleParameter* mpLedCount;
ModuleParameter* mpFxStyle;
ModuleParameter* mpFxStrobe;

unsigned long strobeFxTickMs = 0;
unsigned int strobeFxDurationMs = 25;
unsigned int strobeFxIntervalMs = 75; // limit strobe to 10Hz (25+75 -> 100ms interval)
unsigned int strobeFxDurationMs = 20;
unsigned int strobeFxIntervalMs = 80; // limit strobe to 10Hz (20+80 -> 100ms interval)
bool strobeOff = true;

String currentStyle = "solid";
Expand All @@ -78,29 +80,81 @@ void refresh() {
}
}

void renderPixels() {
if (pixels != nullptr) {
for (int i = 0; i < ledsCount; i++) {
pixels->setPixelColor(i,
animatedColors[i]->getRed(),
animatedColors[i]->getGreen(),
animatedColors[i]->getBlue());
}
}
if (statusLED != nullptr) {
statusLED->setPixelColor(0, currentColor.getRed(), currentColor.getGreen(), currentColor.getBlue());
}
}

void createPixels()
{
if (ledsPin >= 0) {
pixels = new Adafruit_NeoPixel(ledsCount, ledsPin, pixelsType + pixelsSpeed);
// turn off
//pixels->clear();
//*
for (int i = 0; i < ledsCount; i++) {
pixels->setPixelColor(i, 0, 0, 0);
}
//*/
pixels->begin();
}
}
void disposePixels() {

// turn off all pixels
if (pixels != nullptr) {
//pixels->clear();
//*
for (int i = 0; i < ledsCount; i++) {
pixels->setPixelColor(i, 0, 0, 0);
}
refresh();
//*/
delete pixels;
delay(1000);
}

}

void setup() {
homeGenie = HomeGenie::getInstance();
auto miniModule = homeGenie->getDefaultModule();
miniModule->name = "Smart light";

miniModule->properties.add(
new ModuleParameter("Widget.OptionField.LED.count",
"number:LED.count:1:1920:1:led_count"));
miniModule->properties.add(
new ModuleParameter("Widget.OptionField.FX.Rainbow",
"select:FX.Style:light_style:solid|rainbow|white_stripes|kaleidoscope"));
"select:FX.Style:light_style:solid|rainbow|rainbow_2|white_stripes|white_stripes_2|kaleidoscope"));
miniModule->properties.add(
new ModuleParameter("Widget.OptionField.FX.Strobe",
"select:FX.Strobe:strobe_effect:off|slow|medium|fast"));

fxStyle = new ModuleParameter("FX.Style", currentStyle);
miniModule->properties.add(fxStyle);
fxStrobe = new ModuleParameter("FX.Strobe", "off");
miniModule->properties.add(fxStrobe);
mpLedCount = new ModuleParameter("LED.count", "0");
miniModule->properties.add(mpLedCount);
mpFxStyle = new ModuleParameter("FX.Style", currentStyle);
miniModule->properties.add(mpFxStyle);
mpFxStrobe = new ModuleParameter("FX.Strobe", "off");
miniModule->properties.add(mpFxStrobe);

// Get status LED config
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();
int statusLedType = Config::getSetting("stld-typ", "6").toInt();
int statusLedSpeed = Config::getSetting("stld-spd", "0").toInt();
statusLED = new Adafruit_NeoPixel(1, statusLedPin, statusLedType + statusLedSpeed);
statusLED->setPixelColor(0, 0, 0, 0);
statusLED->begin();
}

isConfigured = Config::isDeviceConfigured();
Expand All @@ -112,25 +166,23 @@ void setup() {
}

Config::setOnWiFiConfiguredCallback([]{
// Device successfully configured
Serial.println("Restarting now.");
delay(2000);
ESP.restart();
// TODO: re-arrange code so that it won't require a restart
});

} else {

// Get LED strip config
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);
// turn off
for (int i = 0; i < count; i++) {
pixels->setPixelColor(i, 0, 0, 0);
}
}
ledsCount = abs(Config::getSetting("leds-cnt").toInt());
ledsPin = (int16_t)Config::getSetting("leds-pin").toInt();
pixelsType = (int16_t)Config::getSetting("leds-typ").toInt();
pixelsSpeed = (int16_t)Config::getSetting("leds-spd").toInt();
createPixels();

mpLedCount->value = String(ledsCount);

// Setup main LEDs control module
mainModule = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, "C1", "Main");
Expand All @@ -142,21 +194,17 @@ void setup() {
});
homeGenie->addAPIHandler(mainModule);

// Initialize FX buffer
fx_init(count, currentColor);
}
// TODO: add 2 "GpioButton" modules for handling manual control (on/off, level, switch fx, ...)

homeGenie->begin();
// Initialize FX buffer
fx_init(ledsCount, currentColor);

// 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();

refresh();
}

Expand All @@ -169,19 +217,19 @@ void loop()
if (millis() - lastRefreshTs > refreshMs)
{
// Update current rendering style if changed
if (currentStyle != fxStyle->value) {
currentStyle = fxStyle->value;
if (currentStyle != mpFxStyle->value) {
currentStyle = mpFxStyle->value;
}

// enable / disable strobe light
if (strobeFxTickMs > 0 && fxStrobe->value == "off") {
if (strobeFxTickMs > 0 && mpFxStrobe->value == "off") {
strobeFxTickMs = 0;
} else if (strobeFxTickMs == 0 && fxStrobe->value != "off") {
} else if (strobeFxTickMs == 0 && mpFxStrobe->value != "off") {
strobeFxTickMs = millis();
}


if (!strobeOff && strobeFxTickMs > 0 && millis() - strobeFxTickMs > ((strobeFxDurationMs + strobeFxIntervalMs) * (fxStrobe->value == "slow" ? 3 : fxStrobe->value == "medium" ? 2 : 1))) {
if (!strobeOff && strobeFxTickMs > 0 && millis() - strobeFxTickMs > ((strobeFxDurationMs + strobeFxIntervalMs) * (mpFxStrobe->value == "slow" ? 3 : mpFxStrobe->value == "medium" ? 2 : 1))) {

// strobe off
strobeOff = true;
Expand All @@ -200,29 +248,20 @@ void loop()
if (currentStyle == "solid") {
fx_solid(pixels, currentColor, strobeFxTickMs > 0 ? 0 : 200);
} else if (currentStyle == "rainbow") {
fx_rainbow(pixels, currentColor);
fx_rainbow(pixels, currentColor, 1);
} else if (currentStyle == "rainbow_2") {
fx_rainbow(pixels, currentColor, 2);
} else if (currentStyle == "kaleidoscope") {
fx_kaleidoscope(pixels, currentColor);
} else if (currentStyle == "white_stripes") {
fx_white_stripes(pixels, currentColor);
} else if (currentStyle == "white_stripes_2") {
fx_white_stripes(pixels, currentColor, true);
}

}


// render pixels
if (pixels != nullptr) {
for (int i = 0; i < pixels->numPixels(); i++) {
pixels->setPixelColor(i,
animatedColors[i]->getRed(),
animatedColors[i]->getGreen(),
animatedColors[i]->getBlue());
}
}
if (statusLED != nullptr) {
statusLED->setPixelColor(0, currentColor.getRed(), currentColor.getGreen(), currentColor.getBlue());
}

renderPixels();
// show pixels
refresh();

Expand All @@ -232,5 +271,18 @@ void loop()
}
}

// check if number of pixels was changed
if (ledsCount != mpLedCount->value.toInt()) {

disposePixels();

ledsCount = mpLedCount->value.toInt();
Config::saveSetting("leds-cnt", mpLedCount->value);

// re-create pixels with the new length
createPixels();

}

}
}
Loading

0 comments on commit 1dbdc78

Please sign in to comment.