From d1f76042e1994ca25a0dc8e344571aa0aa6733fe Mon Sep 17 00:00:00 2001 From: ChuckMash <86080247+ChuckMash@users.noreply.github.com> Date: Tue, 12 Apr 2022 01:20:08 -0700 Subject: [PATCH 1/6] bugfix for outgoing serial TPM2 message length (#2628) bugfix for outgoing serial TPM2 message length --- wled00/wled_serial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wled00/wled_serial.cpp b/wled00/wled_serial.cpp index 5dbcb01e7..2fafaceb1 100644 --- a/wled00/wled_serial.cpp +++ b/wled00/wled_serial.cpp @@ -86,8 +86,8 @@ void handleSerial() Serial.write(0xC9); Serial.write(0xDA); uint16_t used = strip.getLengthTotal(); uint16_t len = used*3; - Serial.write((len << 8) & 0xFF); - Serial.write( len & 0xFF); + Serial.write(highByte(len)); + Serial.write(lowByte(len)); for (uint16_t i=0; i < used; i++) { uint32_t c = strip.getPixelColor(i); Serial.write(qadd8(W(c), R(c))); //R, add white channel to RGB channels as a simple RGBW -> RGB map From 1a513c7bbf819741478ae9e99ee86d1115cfaa06 Mon Sep 17 00:00:00 2001 From: Thomas <4719352+FUB4R@users.noreply.github.com> Date: Sat, 16 Apr 2022 23:08:27 +0100 Subject: [PATCH 2/6] wled.cpp: Wrap Serial calls in `#ifdef WLED_ENABLE_ADALIGHT`. (#2630) handleImprovPacket() unconditionally gobbles serial data which a problem if we want another feature to consume it. This patch uses the same guard as the existing call in `handleSerial()`. Co-authored-by: Thomas Fubar --- wled00/wled.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 69e58dfea..54c723c80 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -389,7 +389,9 @@ void WLED::setup() sprintf(mqttClientID + 5, "%*s", 6, escapedMac.c_str() + 6); } +#ifdef WLED_ENABLE_ADALIGHT if (Serial.available() > 0 && Serial.peek() == 'I') handleImprovPacket(); +#endif strip.service(); @@ -409,7 +411,10 @@ void WLED::setup() initDMX(); #endif +#ifdef WLED_ENABLE_ADALIGHT if (Serial.available() > 0 && Serial.peek() == 'I') handleImprovPacket(); +#endif + // HTTP server page init initServer(); From 23d39e5366cb85f185ed9f5ed03fb73c3c0138b3 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Fri, 29 Apr 2022 09:56:48 +0200 Subject: [PATCH 3/6] Compile time options for Multi Relay & PWM Fan --- usermods/PWM_fan/readme.md | 4 ++-- usermods/PWM_fan/usermod_PWM_fan.h | 11 +++++++++-- usermods/multi_relay/readme.md | 4 +++- usermods/multi_relay/usermod_multi_relay.h | 9 +++++++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/usermods/PWM_fan/readme.md b/usermods/PWM_fan/readme.md index a40098c15..976d6b24c 100644 --- a/usermods/PWM_fan/readme.md +++ b/usermods/PWM_fan/readme.md @@ -19,8 +19,8 @@ You will also need `-D USERMOD_DALLASTEMPERATURE`. All of the parameters are configured during run-time using Usermods settings page. This includes: -* PWM output pin -* tacho input pin +* PWM output pin (can be configured at compile time `-D PWM_PIN=xx`) +* tacho input pin (can be configured at compile time `-D TACHO_PIN=xx`) * sampling frequency in seconds * threshold temperature in degees C diff --git a/usermods/PWM_fan/usermod_PWM_fan.h b/usermods/PWM_fan/usermod_PWM_fan.h index 82aa917bb..943ba1ae6 100644 --- a/usermods/PWM_fan/usermod_PWM_fan.h +++ b/usermods/PWM_fan/usermod_PWM_fan.h @@ -10,6 +10,13 @@ // https://github.com/KlausMu/esp32-fan-controller/tree/main/src // adapted for WLED usermod by @blazoncek +#ifndef TACHO_PIN + #define TACHO_PIN -1 +#endif + +#ifndef PWM_PIN + #define PWM_PIN -1 +#endif // tacho counter static volatile unsigned long counter_rpm = 0; @@ -37,8 +44,8 @@ class PWMFanUsermod : public Usermod { #endif // configurable parameters - int8_t tachoPin = -1; - int8_t pwmPin = -1; + int8_t tachoPin = TACHO_PIN; + int8_t pwmPin = PWM_PIN; uint8_t tachoUpdateSec = 30; float targetTemperature = 25.0; uint8_t minPWMValuePct = 50; diff --git a/usermods/multi_relay/readme.md b/usermods/multi_relay/readme.md index 2d933cdab..a0a3e9e9f 100644 --- a/usermods/multi_relay/readme.md +++ b/usermods/multi_relay/readme.md @@ -81,11 +81,13 @@ void registerUsermods() Usermod can be configured in Usermods settings page. * `enabled` - enable/disable usermod -* `pin` - GPIO pin where relay is attached to ESP +* `pin` - GPIO pin where relay is attached to ESP (can be configured at compile time `-D MULTI_RELAY_PINS=xx,xx,...`) * `delay-s` - delay in seconds after on/off command is received * `active-high` - toggle high/low activation of relay (can be used to reverse relay states) * `external` - if enabled WLED does not control relay, it can only be triggered by external command (MQTT, HTTP, JSON or button) * `button` - button (from LED Settings) that controls this relay +* `broadcast`- time in seconds between state broadcasts using MQTT +* `HA-discovery`- enable Home Assistant auto discovery If there is no MultiRelay section, just save current configuration and re-open Usermods settings page. diff --git a/usermods/multi_relay/usermod_multi_relay.h b/usermods/multi_relay/usermod_multi_relay.h index 6143a6b99..b38c8a25c 100644 --- a/usermods/multi_relay/usermod_multi_relay.h +++ b/usermods/multi_relay/usermod_multi_relay.h @@ -6,6 +6,10 @@ #define MULTI_RELAY_MAX_RELAYS 4 #endif +#ifndef MULTI_RELAY_PINS + #define MULTI_RELAY_PINS -1 +#endif + #define WLED_DEBOUNCE_THRESHOLD 50 //only consider button input of at least 50ms as valid (debouncing) #define ON true @@ -177,8 +181,9 @@ class MultiRelay : public Usermod { * constructor */ MultiRelay() { + const int8_t defPins[] = {MULTI_RELAY_PINS}; for (uint8_t i=0; i Date: Tue, 3 May 2022 03:18:21 -0700 Subject: [PATCH 4/6] WiZ Lights usermod - Adding more options and features (#2638) * Update wizlights.h adds new features and options for wizlights usermod * Update wizlights.h Change how IPs are numbered. Non-programmers incorrectly start counting at 1 * Update wizlights.h updated default cold white enhanced white setting to a lower value. * Update wizlights.h added logic for connection check before UDP sending. Seems more important for ESP32 * Update readme.md --- usermods/wizlights/readme.md | 28 ++++- usermods/wizlights/wizlights.h | 192 +++++++++++++++++---------------- 2 files changed, 127 insertions(+), 93 deletions(-) diff --git a/usermods/wizlights/readme.md b/usermods/wizlights/readme.md index 271c30763..802a3798c 100644 --- a/usermods/wizlights/readme.md +++ b/usermods/wizlights/readme.md @@ -6,8 +6,30 @@ The mod takes the colors from the first few pixels and sends them to the lights. ## Configuration -First, enter how often the data will be sent to the lights (in ms). - -Then enter the IPs for the lights to be controlled, in order. There is currently a limit of 10 devices that can be controled, but that number +- Interval (ms) + - How frequently to update the WiZ lights, in milliseconds. + - Setting too low may causse ESP to become unresponsive. +- Send Delay (ms) + - An optional millisecond delay after updating each WiZ light. + - Can help smooth out effects when using a larger number of WiZ lights +- Use Enhanced White + - Enables using the WiZ lights onboard white LEDs instead of sending maximum RGB values. + - Tunable with warm and cool LEDs as supported by WiZ bulbs + - Note: Only sent when max RGB value is set, need to have automatic brightness limiter disabled + - ToDo: Have better logic for white value mixing to better take advantage of the lights capabilities +- Always Force Update + - Can be enabled to always send update message to light, even when color matches what was previously sent. +- Force update every x minutes + - Configuration option to allow adjusting the default force update timeout of 5 minutes. + - Setting to 0 has the same impact as enabling Always Force Update + - +Then enter the IPs for the lights to be controlled, in order. There is currently a limit of 15 devices that can be controled, but that number can be easily changed by updating _MAX_WIZ_LIGHTS_. + + + +## Related project + +If you use these lights and python, make sure to check out the [pywizlight](https://github.com/sbidy/pywizlight) project. I learned how to +format the messages to control the lights from that project. diff --git a/usermods/wizlights/wizlights.h b/usermods/wizlights/wizlights.h index 230a7a8ad..c588f00ea 100644 --- a/usermods/wizlights/wizlights.h +++ b/usermods/wizlights/wizlights.h @@ -4,117 +4,134 @@ #include // Maximum number of lights supported -#define MAX_WIZ_LIGHTS 10 +#define MAX_WIZ_LIGHTS 15 -// UDP object, to send messages WiFiUDP UDP; -// Function to send a color to a light -void sendColor(IPAddress ip, uint32_t color) { - UDP.beginPacket(ip, 38899); - if (color == 0) { - UDP.print("{\"method\":\"setPilot\",\"params\":{\"state\":false}}"); - } else { - UDP.print("{\"method\":\"setPilot\",\"params\":{\"state\":true, \"r\":"); - UDP.print(R(color)); - UDP.print(",\"g\":"); - UDP.print(G(color)); - UDP.print(",\"b\":"); - UDP.print(B(color)); - UDP.print("}}"); - } - UDP.endPacket(); -} -// Create label for the usermode page (I cannot make it work with JSON arrays...) -String getJsonLabel(uint8_t i) { - return "ip_light_" + String(i); -} + class WizLightsUsermod : public Usermod { + private: - // Keep track of the last time the lights were updated unsigned long lastTime = 0; - - // Specify how often WLED sends data to the Wiz lights long updateInterval; + long sendDelay; - // Save the IP of the lights - IPAddress lightsIP[MAX_WIZ_LIGHTS]; - bool lightsValid[MAX_WIZ_LIGHTS]; + long forceUpdateMinutes; + bool forceUpdate; + + bool useEnhancedWhite; + long warmWhite; + long coldWhite; + + IPAddress lightsIP[MAX_WIZ_LIGHTS]; // Stores Light IP addresses + bool lightsValid[MAX_WIZ_LIGHTS]; // Stores Light IP address validity + uint32_t colorsSent[MAX_WIZ_LIGHTS]; // Stores last color sent for each light + - // Variable that keeps track of RBG values for the lights - uint32_t colorsSent[MAX_WIZ_LIGHTS]; public: - //Functions called by WLED - /* - * loop() is called continuously. Here you can check for events, read sensors, etc. - */ - void loop() { - // Calculate how long since the last update - unsigned long ellapsedTime = millis() - lastTime; - if (ellapsedTime > updateInterval) { - // Keep track of whether we are updating any of the lights - bool update = false; - // Loop through the lights - for (uint8_t i = 0; i < MAX_WIZ_LIGHTS; i++) { - // Check if we have a valid IP - if (!lightsValid[i]) { continue; } + // Send JSON blob to WiZ Light over UDP + // RGB or C/W white + // TODO: + // Better utilize WLED existing white mixing logic + void wizSendColor(IPAddress ip, uint32_t color) { + UDP.beginPacket(ip, 38899); - // Get the first colors in the strip - uint32_t new_color = strip.getPixelColor(i); + // If no LED color, turn light off. Note wiz light setting for "Off fade-out" will be applied by the light itself. + if (color == 0) { + UDP.print("{\"method\":\"setPilot\",\"params\":{\"state\":false}}"); - // Check if the color has changed from the last one sent - // Force an update every 5 minutes, in case the colors don't change - // (the lights could have been reset by turning off and on) - if ((new_color != colorsSent[i]) | (ellapsedTime > 5*60000)) { - // It has changed, send the new color to the light - update = true; - sendColor(lightsIP[i], new_color); - colorsSent[i] = new_color; - } - } - - // We sent an update, wait until we do this again - if (update) { - lastTime = millis(); - } + // If color is WHITE, try and use the lights WHITE LEDs instead of mixing RGB LEDs + } else if (color == 16777215 && useEnhancedWhite){ + + // set cold white light only + if (coldWhite > 0 && warmWhite == 0){ + UDP.print("{\"method\":\"setPilot\",\"params\":{\"c\":"); UDP.print(coldWhite) ;UDP.print("}}");} + + // set warm white light only + if (warmWhite > 0 && coldWhite == 0){ + UDP.print("{\"method\":\"setPilot\",\"params\":{\"w\":"); UDP.print(warmWhite) ;UDP.print("}}");} + + // set combination of warm and cold white light + if (coldWhite > 0 && warmWhite > 0){ + UDP.print("{\"method\":\"setPilot\",\"params\":{\"c\":"); UDP.print(coldWhite) ;UDP.print(",\"w\":"); UDP.print(warmWhite); UDP.print("}}");} + + // Send color as RGB + } else { + UDP.print("{\"method\":\"setPilot\",\"params\":{\"r\":"); + UDP.print(R(color)); + UDP.print(",\"g\":"); + UDP.print(G(color)); + UDP.print(",\"b\":"); + UDP.print(B(color)); + UDP.print("}}"); } + + UDP.endPacket(); } - /* - * addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object. - * It will be called by WLED when settings are actually saved (for example, LED settings are saved) - * If you want to force saving the current state, use serializeConfig() in your loop(). - */ + + + // TODO: Check millis() rollover + void loop() { + + // Make sure we are connected first + if (!WLED_CONNECTED) return; + + unsigned long ellapsedTime = millis() - lastTime; + if (ellapsedTime > updateInterval) { + bool update = false; + for (uint8_t i = 0; i < MAX_WIZ_LIGHTS; i++) { + if (!lightsValid[i]) { continue; } + uint32_t newColor = strip.getPixelColor(i); + if (forceUpdate || (newColor != colorsSent[i]) || (ellapsedTime > forceUpdateMinutes*60000)){ + wizSendColor(lightsIP[i], newColor); + colorsSent[i] = newColor; + update = true; + delay(sendDelay); + } + } + if (update) lastTime = millis(); + } + } + + + void addToConfig(JsonObject& root) { JsonObject top = root.createNestedObject("wizLightsUsermod"); - top["interval_ms"] = updateInterval; + top["Interval (ms)"] = updateInterval; + top["Send Delay (ms)"] = sendDelay; + top["Use Enhanced White *"] = useEnhancedWhite; + top["* Warm White Value (0-255)"] = warmWhite; + top["* Cold White Value (0-255)"] = coldWhite; + top["Always Force Update"] = forceUpdate; + top["Force Update Every x Minutes"] = forceUpdateMinutes; + for (uint8_t i = 0; i < MAX_WIZ_LIGHTS; i++) { top[getJsonLabel(i)] = lightsIP[i].toString(); } } - /* - * readFromConfig() can be used to read back the custom settings you added with addToConfig(). - * This is called by WLED when settings are loaded (currently this only happens immediately after boot, or after saving on the Usermod Settings page) - */ + + bool readFromConfig(JsonObject& root) { - // default settings values could be set here (or below using the 3-argument getJsonValue()) instead of in the class definition or constructor - // setting them inside readFromConfig() is slightly more robust, handling the rare but plausible use case of single value being missing after boot (e.g. if the cfg.json was manually edited and a value was removed) - JsonObject top = root["wizLightsUsermod"]; - bool configComplete = !top.isNull(); - // Read interval to update the lights - configComplete &= getJsonValue(top["interval_ms"], updateInterval, 1000); + configComplete &= getJsonValue(top["Interval (ms)"], updateInterval, 1000); // How frequently to update the wiz lights + configComplete &= getJsonValue(top["Send Delay (ms)"], sendDelay, 0); // Optional delay after sending each UDP message + configComplete &= getJsonValue(top["Use Enhanced White *"], useEnhancedWhite, false); // When color is white use wiz white LEDs instead of mixing RGB + configComplete &= getJsonValue(top["* Warm White Value (0-255)"], warmWhite, 0); // Warm White LED value for Enhanced White + configComplete &= getJsonValue(top["* Cold White Value (0-255)"], coldWhite, 50); // Cold White LED value for Enhanced White + configComplete &= getJsonValue(top["Always Force Update"], forceUpdate, false); // Update wiz light every loop, even if color value has not changed + configComplete &= getJsonValue(top["Force Update Every x Minutes"], forceUpdateMinutes, 5); // Update wiz light if color value has not changed, every x minutes // Read list of IPs String tempIp; @@ -123,20 +140,15 @@ class WizLightsUsermod : public Usermod { lightsValid[i] = lightsIP[i].fromString(tempIp); // If the IP is not valid, force the value to be empty - if (!lightsValid[i]) { - lightsIP[i].fromString("0.0.0.0"); + if (!lightsValid[i]){lightsIP[i].fromString("0.0.0.0");} } - } return configComplete; } - - /* - * getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!). - * This could be used in the future for the system to determine whether your usermod is installed. - */ - uint16_t getId() - { - return USERMOD_ID_WIZLIGHTS; - } -}; \ No newline at end of file + + + // Create label for the usermod page (I cannot make it work with JSON arrays...) + String getJsonLabel(uint8_t i) {return "WiZ Light IP #" + String(i+1);} + + uint16_t getId(){return USERMOD_ID_WIZLIGHTS;} +}; From bef9c68f8152cad463fc8dbf6c143d6c81ea3434 Mon Sep 17 00:00:00 2001 From: Luke Plassman <40614799+lplassman@users.noreply.github.com> Date: Wed, 4 May 2022 20:28:09 -0400 Subject: [PATCH 5/6] Working DMX Libraries (#2652) * added SparkFunDMX library dependencies * changed variable names to avoid conflicts with SparkFunDMX library * board version checks * minor changes to DMX * fix brightness when no shutter DMX channel is set * fix compile issue on newer ESP32 variants --- wled00/dmx.cpp | 31 ++-- wled00/src/dependencies/dmx/ESPDMX.cpp | 22 +-- wled00/src/dependencies/dmx/LICENSE.md | 55 +++++++ wled00/src/dependencies/dmx/SparkFunDMX.cpp | 160 ++++++++++++++++++++ wled00/src/dependencies/dmx/SparkFunDMX.h | 38 +++++ wled00/wled.h | 10 +- 6 files changed, 297 insertions(+), 19 deletions(-) create mode 100644 wled00/src/dependencies/dmx/LICENSE.md create mode 100644 wled00/src/dependencies/dmx/SparkFunDMX.cpp create mode 100644 wled00/src/dependencies/dmx/SparkFunDMX.h diff --git a/wled00/dmx.cpp b/wled00/dmx.cpp index 284219547..0bdb4b646 100644 --- a/wled00/dmx.cpp +++ b/wled00/dmx.cpp @@ -1,10 +1,13 @@ #include "wled.h" /* - * Support for DMX via MAX485. - * Change the output pin in src/dependencies/ESPDMX.cpp if needed. - * Library from: + * Support for DMX Output via MAX485. + * Change the output pin in src/dependencies/ESPDMX.cpp, if needed (ESP8266) + * Change the output pin in src/dependencies/SparkFunDMX.cpp, if needed (ESP32) + * ESP8266 Library from: * https://github.com/Rickgg/ESP-Dmx + * ESP32 Library from: + * https://github.com/sparkfun/SparkFunDMX */ #ifdef WLED_ENABLE_DMX @@ -14,10 +17,16 @@ void handleDMX() // don't act, when in DMX Proxy mode if (e131ProxyUniverse != 0) return; - // TODO: calculate brightness manually if no shutter channel is set - uint8_t brightness = strip.getBrightness(); + bool calc_brightness = true; + + // check if no shutter channel is set + for (byte i = 0; i < DMXChannels; i++) + { + if (DMXFixtureMap[i] == 5) calc_brightness = false; + } + uint16_t len = strip.getLengthTotal(); for (int i = DMXStartLED; i < len; i++) { // uses the amount of LEDs as fixture count @@ -35,16 +44,16 @@ void handleDMX() dmx.write(DMXAddr, 0); break; case 1: // Red - dmx.write(DMXAddr, r); + dmx.write(DMXAddr, calc_brightness ? (r * brightness) / 255 : r); break; case 2: // Green - dmx.write(DMXAddr, g); + dmx.write(DMXAddr, calc_brightness ? (g * brightness) / 255 : g); break; case 3: // Blue - dmx.write(DMXAddr, b); + dmx.write(DMXAddr, calc_brightness ? (b * brightness) / 255 : b); break; case 4: // White - dmx.write(DMXAddr, w); + dmx.write(DMXAddr, calc_brightness ? (w * brightness) / 255 : w); break; case 5: // Shutter channel. Controls the brightness. dmx.write(DMXAddr, brightness); @@ -60,7 +69,11 @@ void handleDMX() } void initDMX() { + #ifdef ESP8266 dmx.init(512); // initialize with bus length + #else + dmx.initWrite(512); // initialize with bus length + #endif } #else diff --git a/wled00/src/dependencies/dmx/ESPDMX.cpp b/wled00/src/dependencies/dmx/ESPDMX.cpp index 6ad1268e7..9f7c6e56c 100644 --- a/wled00/src/dependencies/dmx/ESPDMX.cpp +++ b/wled00/src/dependencies/dmx/ESPDMX.cpp @@ -11,6 +11,8 @@ // - - - - - /* ----- LIBRARIES ----- */ +#ifdef ESP8266 + #include #include "ESPDMX.h" @@ -29,12 +31,12 @@ bool dmxStarted = false; int sendPin = 2; //dafault on ESP8266 //DMX value array and size. Entry 0 will hold startbyte -uint8_t dmxData[dmxMaxChannel] = {}; -int chanSize; +uint8_t dmxDataStore[dmxMaxChannel] = {}; +int channelSize; void DMXESPSerial::init() { - chanSize = defaultMax; + channelSize = defaultMax; Serial1.begin(DMXSPEED); pinMode(sendPin, OUTPUT); @@ -48,7 +50,7 @@ void DMXESPSerial::init(int chanQuant) { chanQuant = defaultMax; } - chanSize = chanQuant; + channelSize = chanQuant; Serial1.begin(DMXSPEED); pinMode(sendPin, OUTPUT); @@ -61,7 +63,7 @@ uint8_t DMXESPSerial::read(int Channel) { if (Channel < 1) Channel = 1; if (Channel > dmxMaxChannel) Channel = dmxMaxChannel; - return(dmxData[Channel]); + return(dmxDataStore[Channel]); } // Function to send DMX data @@ -69,15 +71,15 @@ void DMXESPSerial::write(int Channel, uint8_t value) { if (dmxStarted == false) init(); if (Channel < 1) Channel = 1; - if (Channel > chanSize) Channel = chanSize; + if (Channel > channelSize) Channel = channelSize; if (value < 0) value = 0; if (value > 255) value = 255; - dmxData[Channel] = value; + dmxDataStore[Channel] = value; } void DMXESPSerial::end() { - chanSize = 0; + channelSize = 0; Serial1.end(); dmxStarted = false; } @@ -96,10 +98,12 @@ void DMXESPSerial::update() { //send data Serial1.begin(DMXSPEED, DMXFORMAT); digitalWrite(sendPin, LOW); - Serial1.write(dmxData, chanSize); + Serial1.write(dmxDataStore, channelSize); Serial1.flush(); delay(1); Serial1.end(); } // Function to update the DMX bus + +#endif \ No newline at end of file diff --git a/wled00/src/dependencies/dmx/LICENSE.md b/wled00/src/dependencies/dmx/LICENSE.md new file mode 100644 index 000000000..e64bd4ef3 --- /dev/null +++ b/wled00/src/dependencies/dmx/LICENSE.md @@ -0,0 +1,55 @@ +SparkFun License Information +============================ + +SparkFun uses two different licenses for our files — one for hardware and one for code. + +Hardware +--------- + +**SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).** + +Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode). + +You are free to: + +Share — copy and redistribute the material in any medium or format +Adapt — remix, transform, and build upon the material +for any purpose, even commercially. +The licensor cannot revoke these freedoms as long as you follow the license terms. +Under the following terms: + +Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. +ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. +No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. +Notices: + +You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. +No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. + + +Code +-------- + +**SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).** + +The MIT License (MIT) + +Copyright (c) 2016 SparkFun Electronics + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/wled00/src/dependencies/dmx/SparkFunDMX.cpp b/wled00/src/dependencies/dmx/SparkFunDMX.cpp new file mode 100644 index 000000000..79202a6a4 --- /dev/null +++ b/wled00/src/dependencies/dmx/SparkFunDMX.cpp @@ -0,0 +1,160 @@ +/****************************************************************************** +SparkFunDMX.h +Arduino Library for the SparkFun ESP32 LED to DMX Shield +Andy England @ SparkFun Electronics +7/22/2019 + +Development environment specifics: +Arduino IDE 1.6.4 + +This code is released under the [MIT License](http://opensource.org/licenses/MIT). +Please review the LICENSE.md file included with this example. If you have any questions +or concerns with licensing, please contact techsupport@sparkfun.com. +Distributed as-is; no warranty is given. +******************************************************************************/ + +/* ----- LIBRARIES ----- */ +#ifdef ESP32 + +#include + +#include "SparkFunDMX.h" +#include + +#define dmxMaxChannel 512 +#define defaultMax 32 + +#define DMXSPEED 250000 +#define DMXFORMAT SERIAL_8N2 +#define BREAKSPEED 83333 +#define BREAKFORMAT SERIAL_8N1 + +int enablePin = -1; // disable the enable pin because it is not needed +int rxPin = -1; // disable the receiving pin because it is not needed +int txPin = 2; // transmit DMX data over this pin (default is pin 2) + +//DMX value array and size. Entry 0 will hold startbyte +uint8_t dmxData[dmxMaxChannel] = {}; +int chanSize; +int currentChannel = 0; + +HardwareSerial DMXSerial(2); + +/* Interrupt Timer for DMX Receive */ +hw_timer_t * timer = NULL; +portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; + +volatile int _interruptCounter; +volatile bool _startCodeDetected = false; + + +/* Start Code is detected by 21 low interrupts */ +void IRAM_ATTR onTimer() { + if (digitalRead(rxPin) == 1) + { + _interruptCounter = 0; //If the RX Pin is high, we are not in an interrupt + } + else + { + _interruptCounter++; + } + if (_interruptCounter > 9) + { + portENTER_CRITICAL_ISR(&timerMux); + _startCodeDetected = true; + DMXSerial.begin(DMXSPEED, DMXFORMAT, rxPin, txPin); + portEXIT_CRITICAL_ISR(&timerMux); + _interruptCounter = 0; + } +} + +void SparkFunDMX::initRead(int chanQuant) { + + timer = timerBegin(0, 1, true); + timerAttachInterrupt(timer, &onTimer, true); + timerAlarmWrite(timer, 320, true); + timerAlarmEnable(timer); + _READWRITE = _READ; + if (chanQuant > dmxMaxChannel || chanQuant <= 0) + { + chanQuant = defaultMax; + } + chanSize = chanQuant; + pinMode(enablePin, OUTPUT); + digitalWrite(enablePin, LOW); + pinMode(rxPin, INPUT); +} + +// Set up the DMX-Protocol +void SparkFunDMX::initWrite (int chanQuant) { + + _READWRITE = _WRITE; + if (chanQuant > dmxMaxChannel || chanQuant <= 0) { + chanQuant = defaultMax; + } + + chanSize = chanQuant + 1; //Add 1 for start code + + DMXSerial.begin(DMXSPEED, DMXFORMAT, rxPin, txPin); + pinMode(enablePin, OUTPUT); + digitalWrite(enablePin, HIGH); +} + +// Function to read DMX data +uint8_t SparkFunDMX::read(int Channel) { + if (Channel > chanSize) Channel = chanSize; + return(dmxData[Channel - 1]); //subtract one to account for start byte +} + +// Function to send DMX data +void SparkFunDMX::write(int Channel, uint8_t value) { + if (Channel < 0) Channel = 0; + if (Channel > chanSize) chanSize = Channel; + dmxData[0] = 0; + dmxData[Channel] = value; //add one to account for start byte +} + + + +void SparkFunDMX::update() { + if (_READWRITE == _WRITE) + { + //Send DMX break + digitalWrite(txPin, HIGH); + DMXSerial.begin(BREAKSPEED, BREAKFORMAT, rxPin, txPin);//Begin the Serial port + DMXSerial.write(0); + DMXSerial.flush(); + delay(1); + DMXSerial.end(); + + //Send DMX data + DMXSerial.begin(DMXSPEED, DMXFORMAT, rxPin, txPin);//Begin the Serial port + DMXSerial.write(dmxData, chanSize); + DMXSerial.flush(); + DMXSerial.end();//clear our DMX array, end the Hardware Serial port + } + else if (_READWRITE == _READ)//In a perfect world, this function ends serial communication upon packet completion and attaches RX to a CHANGE interrupt so the start code can be read again + { + if (_startCodeDetected == true) + { + while (DMXSerial.available()) + { + dmxData[currentChannel++] = DMXSerial.read(); + } + if (currentChannel > chanSize) //Set the channel counter back to 0 if we reach the known end size of our packet + { + + portENTER_CRITICAL(&timerMux); + _startCodeDetected = false; + DMXSerial.flush(); + DMXSerial.end(); + portEXIT_CRITICAL(&timerMux); + currentChannel = 0; + } + } + } +} + +// Function to update the DMX bus + +#endif \ No newline at end of file diff --git a/wled00/src/dependencies/dmx/SparkFunDMX.h b/wled00/src/dependencies/dmx/SparkFunDMX.h new file mode 100644 index 000000000..388425b77 --- /dev/null +++ b/wled00/src/dependencies/dmx/SparkFunDMX.h @@ -0,0 +1,38 @@ +/****************************************************************************** +SparkFunDMX.h +Arduino Library for the SparkFun ESP32 LED to DMX Shield +Andy England @ SparkFun Electronics +7/22/2019 + +Development environment specifics: +Arduino IDE 1.6.4 + +This code is released under the [MIT License](http://opensource.org/licenses/MIT). +Please review the LICENSE.md file included with this example. If you have any questions +or concerns with licensing, please contact techsupport@sparkfun.com. +Distributed as-is; no warranty is given. +******************************************************************************/ + +#include + + +#ifndef SparkFunDMX_h +#define SparkFunDMX_h + +// ---- Methods ---- + +class SparkFunDMX { +public: + void initRead(int maxChan); + void initWrite(int maxChan); + uint8_t read(int Channel); + void write(int channel, uint8_t value); + void update(); +private: + uint8_t _startCodeValue = 0xFF; + bool _READ = true; + bool _WRITE = false; + bool _READWRITE; +}; + +#endif \ No newline at end of file diff --git a/wled00/wled.h b/wled00/wled.h index 6dabdd0b5..d8e40475b 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -111,7 +111,11 @@ #endif #ifdef WLED_ENABLE_DMX + #ifdef ESP8266 #include "src/dependencies/dmx/ESPDMX.h" + #else //ESP32 + #include "src/dependencies/dmx/SparkFunDMX.h" + #endif #endif #include "src/dependencies/e131/ESPAsyncE131.h" @@ -347,7 +351,11 @@ WLED_GLOBAL bool arlsDisableGammaCorrection _INIT(true); // activate if WLED_GLOBAL bool arlsForceMaxBri _INIT(false); // enable to force max brightness if source has very dark colors that would be black #ifdef WLED_ENABLE_DMX -WLED_GLOBAL DMXESPSerial dmx; + #ifdef ESP8266 + WLED_GLOBAL DMXESPSerial dmx; + #else //ESP32 + WLED_GLOBAL SparkFunDMX dmx; + #endif WLED_GLOBAL uint16_t e131ProxyUniverse _INIT(0); // output this E1.31 (sACN) / ArtNet universe via MAX485 (0 = disabled) #endif WLED_GLOBAL uint16_t e131Universe _INIT(1); // settings for E1.31 (sACN) protocol (only DMX_MODE_MULTIPLE_* can span over consequtive universes) From db8e1dec3e0dc4f500bc2167ba531df4e17b8c75 Mon Sep 17 00:00:00 2001 From: Jamie Stoom Date: Mon, 9 May 2022 17:33:15 -0700 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=90=9B=20fix(json):=20allow=20for=20u?= =?UTF-8?q?sing=20`~-16`=20or=20`~16`=20when=20setting=20a=20segments=20br?= =?UTF-8?q?ightness=20though=20the=20JSON=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wled00/json.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wled00/json.cpp b/wled00/json.cpp index 4c43c9f0a..f92bd4360 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -95,7 +95,7 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId) if (stop > start && of > len -1) of = len -1; strip.setSegment(id, start, stop, grp, spc, of); - byte segbri = 0; + byte segbri = seg.opacity; if (getVal(elem["bri"], &segbri)) { if (segbri > 0) seg.setOpacity(segbri, id); seg.setOption(SEG_OPTION_ON, segbri, id);