mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-10-28 11:14:58 +00:00
support newer rgb WS2812B V6 leds
This commit is contained in:
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
- Allow auto-download and install of firmware versions which are smaller to allow update to the latest 3.9.0-development version.
|
||||
|
||||
## Changed
|
||||
|
||||
## [3.8.3] 1 August 2026
|
||||
|
||||
## Added
|
||||
|
||||
@@ -2,3 +2,10 @@
|
||||
|
||||
For more details go to [emsesp.org](https://emsesp.org/).
|
||||
|
||||
## Added
|
||||
|
||||
- own rgb_led library supporting WS2812B V5 and the newer V6 RGB LEDs
|
||||
|
||||
## Fixed
|
||||
|
||||
## Changed
|
||||
|
||||
@@ -147,7 +147,5 @@ double ledcSetup(uint8_t chan, double freq, uint8_t bit_num) {
|
||||
void ledcAttachPin(uint8_t pin, uint8_t chan) {};
|
||||
void ledcAttach(uint8_t pin, uint8_t chan, uint8_t bit_num) {};
|
||||
void ledcWrite(uint8_t chan, uint32_t duty) {};
|
||||
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val) {};
|
||||
void rgbLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val) {};
|
||||
|
||||
#endif
|
||||
@@ -74,8 +74,6 @@ double ledcSetup(uint8_t chan, double freq, uint8_t bit_num);
|
||||
void ledcAttachPin(uint8_t pin, uint8_t chan);
|
||||
void ledcAttach(uint8_t pin, uint8_t chan, uint8_t bit_num);
|
||||
void ledcWrite(uint8_t chan, uint32_t duty);
|
||||
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
|
||||
void rgbLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
|
||||
|
||||
inline int rtc_get_reset_reason(int cpu_no) {
|
||||
return 0;
|
||||
|
||||
@@ -776,7 +776,6 @@ negcond
|
||||
negheat
|
||||
nein
|
||||
nennvolumentstrom
|
||||
neopixel
|
||||
netcmp
|
||||
netflowtemp
|
||||
netif
|
||||
|
||||
@@ -299,7 +299,7 @@ void AnalogSensor::reload(bool get_nvs) {
|
||||
uint8_t r = v / 10000;
|
||||
uint8_t g = (v - r * 10000) / 100;
|
||||
uint8_t b = v % 100;
|
||||
EMSESP_RGB_WRITE(sensor.gpio(), 2 * r, 2 * g, 2 * b);
|
||||
rgb_led::write(sensor.gpio(), 2 * r, 2 * g, 2 * b);
|
||||
LOG_DEBUG("RGB set to %d, %d, %d", r, g, b);
|
||||
} else if (sensor.type() == AnalogType::DIGITAL_OUT) {
|
||||
LOG_DEBUG("Digital Write on GPIO %02d", sensor.gpio());
|
||||
@@ -1001,7 +1001,7 @@ bool AnalogSensor::command_setvalue(const char * value, const int8_t gpio) {
|
||||
uint8_t r = v / 10000;
|
||||
uint8_t g = (v - r * 10000) / 100;
|
||||
uint8_t b = v % 100;
|
||||
EMSESP_RGB_WRITE(sensor.gpio(), 2 * r, 2 * g, 2 * b);
|
||||
rgb_led::write(sensor.gpio(), 2 * r, 2 * g, 2 * b);
|
||||
LOG_DEBUG("RGB set to %d, %d, %d", r, g, b);
|
||||
} else if (sensor.type() == AnalogType::PULSE) {
|
||||
uint8_t v = val;
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* EMS-ESP - https://github.com/emsesp/EMS-ESP
|
||||
* Copyright 2020-2025 emsesp.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "rgb_led.h"
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
#include <esp32-hal-rmt.h>
|
||||
#endif
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
namespace rgb_led {
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
|
||||
namespace {
|
||||
|
||||
// durations in 100 ns ticks
|
||||
constexpr uint16_t T0H = 3; // 300 ns
|
||||
constexpr uint16_t T0L = 9; // 900 ns
|
||||
constexpr uint16_t T1H = 8; // 800 ns
|
||||
constexpr uint16_t T1L = 6; // 600 ns
|
||||
|
||||
// rmt_data_t.val layout: duration0:15, level0:1, duration1:15, level1:1 (high then low)
|
||||
constexpr uint32_t SYMBOL_0 = T0H | (1UL << 15) | (static_cast<uint32_t>(T0L) << 16);
|
||||
constexpr uint32_t SYMBOL_1 = T1H | (1UL << 15) | (static_cast<uint32_t>(T1L) << 16);
|
||||
|
||||
constexpr uint32_t RESET_US = 300;
|
||||
constexpr uint8_t NO_PIN = 0xFF;
|
||||
constexpr uint8_t MAX_PINS = 2; // status LED + an RGB analog sensor; the ESP32-C3 only has 2 RMT TX channels
|
||||
|
||||
#if ESP_ARDUINO_VERSION_MAJOR < 3
|
||||
constexpr float TICK_NS = 100.0f; // 1 tick = 100 ns
|
||||
#else
|
||||
constexpr uint32_t RMT_FREQ_HZ = 10000000; // 10 MHz, 1 tick = 100 ns
|
||||
#endif
|
||||
|
||||
struct Channel {
|
||||
uint8_t pin = NO_PIN;
|
||||
uint32_t last_tx_us = 0;
|
||||
#if ESP_ARDUINO_VERSION_MAJOR < 3
|
||||
rmt_obj_t * rmt = nullptr; // Core 2 addresses the channel by handle, Core 3 by pin
|
||||
#endif
|
||||
};
|
||||
|
||||
Channel channels[MAX_PINS];
|
||||
|
||||
void release(Channel & ch) {
|
||||
if (ch.pin == NO_PIN) {
|
||||
return;
|
||||
}
|
||||
#if ESP_ARDUINO_VERSION_MAJOR < 3
|
||||
rmtDeinit(ch.rmt);
|
||||
ch.rmt = nullptr;
|
||||
#else
|
||||
rmtDeinit(ch.pin);
|
||||
#endif
|
||||
ch.pin = NO_PIN;
|
||||
}
|
||||
|
||||
Channel * init(uint8_t pin) {
|
||||
Channel * free_slot = nullptr;
|
||||
for (auto & ch : channels) {
|
||||
if (ch.pin == pin) {
|
||||
return &ch;
|
||||
}
|
||||
if (!free_slot && ch.pin == NO_PIN) {
|
||||
free_slot = &ch;
|
||||
}
|
||||
}
|
||||
|
||||
if (!free_slot) {
|
||||
// all slots in use: release the least recently written one
|
||||
const uint32_t now = micros();
|
||||
free_slot = &channels[0];
|
||||
for (auto & ch : channels) {
|
||||
if (now - ch.last_tx_us > now - free_slot->last_tx_us) {
|
||||
free_slot = &ch;
|
||||
}
|
||||
}
|
||||
release(*free_slot);
|
||||
}
|
||||
|
||||
#if ESP_ARDUINO_VERSION_MAJOR < 3
|
||||
rmt_obj_t * rmt = rmtInit(pin, RMT_TX_MODE, RMT_MEM_64);
|
||||
if (!rmt) {
|
||||
return nullptr;
|
||||
}
|
||||
rmtSetTick(rmt, TICK_NS);
|
||||
free_slot->rmt = rmt;
|
||||
#else
|
||||
if (!rmtInit(pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, RMT_FREQ_HZ)) {
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
free_slot->pin = pin;
|
||||
free_slot->last_tx_us = micros() - RESET_US; // no wait on first write
|
||||
return free_slot;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void write(uint8_t pin, uint8_t red, uint8_t green, uint8_t blue, Order order) {
|
||||
Channel * ch = init(pin);
|
||||
if (!ch) {
|
||||
return;
|
||||
}
|
||||
|
||||
// enforce the latch/reset gap since the previous frame (V6 needs > 280 us)
|
||||
const uint32_t elapsed = micros() - ch->last_tx_us;
|
||||
if (elapsed < RESET_US) {
|
||||
delayMicroseconds(RESET_US - elapsed);
|
||||
}
|
||||
|
||||
const uint8_t bytes[3] = {order == Order::GRB ? green : red, order == Order::GRB ? red : green, blue};
|
||||
|
||||
rmt_data_t frame[24];
|
||||
for (uint8_t i = 0; i < 24; i++) {
|
||||
frame[i].val = (bytes[i >> 3] & (0x80 >> (i & 7))) ? SYMBOL_1 : SYMBOL_0;
|
||||
}
|
||||
|
||||
#if ESP_ARDUINO_VERSION_MAJOR < 3
|
||||
rmtWriteBlocking(ch->rmt, frame, 24);
|
||||
#else
|
||||
rmtWrite(pin, frame, 24, RMT_WAIT_FOR_EVER);
|
||||
#endif
|
||||
ch->last_tx_us = micros();
|
||||
}
|
||||
|
||||
void end(uint8_t pin) {
|
||||
for (auto & ch : channels) {
|
||||
if (ch.pin == pin) {
|
||||
release(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void write(uint8_t, uint8_t, uint8_t, uint8_t, Order) {
|
||||
}
|
||||
|
||||
void end(uint8_t) {
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace rgb_led
|
||||
|
||||
} // namespace emsesp
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* EMS-ESP - https://github.com/emsesp/EMS-ESP
|
||||
* Copyright 2020-2025 emsesp.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef EMSESP_RGB_LED_H_
|
||||
#define EMSESP_RGB_LED_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
// WS2812B driver replacing arduino-esp32's rgbLedWrite()/neopixelWrite(), which hard-code
|
||||
// T0H = 400 ns, T1L = 400 ns and have no reset gap. The WS2812B-2020-V6 needs T0H 220-380 ns,
|
||||
// T1L 580-1000 ns and a reset > 280 us, otherwise it misreads 0-bits as 1-bits (washed-out white).
|
||||
// Timings used (10 MHz RMT clock, 1 tick = 100 ns) fit both the original WS2812B and the V6:
|
||||
//
|
||||
// sent old WS2812B WS2812B-2020-V6
|
||||
// T0H 300 ns 250-550 ns 220-380 ns
|
||||
// T0L 900 ns 700-1000 ns 580-1000 ns
|
||||
// T1H 800 ns 650-950 ns 580-1000 ns
|
||||
// T1L 600 ns 300-600 ns 580-1000 ns
|
||||
// Reset >300 us >50 us >280 us
|
||||
//
|
||||
// Supports both Arduino Core 2.x (handle based RMT API) and Core 3.x (pin based RMT API).
|
||||
// Do not mix with rgbLedWrite()/neopixelWrite() on the same pin, as both claim the pin's RMT channel.
|
||||
namespace rgb_led {
|
||||
|
||||
enum class Order : uint8_t { GRB, RGB }; // WS2812B is GRB
|
||||
|
||||
// Blocks ~30 us for the transmit, plus up to the reset gap if called again right after the previous update.
|
||||
// Returns void so it can be used as a drop-in for neopixelWrite()/rgbLedWrite(), e.g. in a ternary with digitalWrite()
|
||||
void write(uint8_t pin, uint8_t red, uint8_t green, uint8_t blue, Order order = Order::GRB);
|
||||
void end(uint8_t pin); // release the pin's RMT channel, e.g. before reusing the pin for something else
|
||||
|
||||
} // namespace rgb_led
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
#endif
|
||||
+13
-14
@@ -664,7 +664,7 @@ void System::led_flash() {
|
||||
|
||||
if (led_flash_type_) {
|
||||
uint8_t intensity = led_flash_state_ ? RGB_LED_BRIGHTNESS : 0;
|
||||
EMSESP_RGB_WRITE(led_flash_gpio_, intensity, intensity, 0); // RGB LED - Yellow
|
||||
rgb_led::write(led_flash_gpio_, intensity, intensity, 0); // RGB LED - Yellow
|
||||
} else {
|
||||
digitalWrite(led_flash_gpio_, led_flash_state_ ? LED_ON : !LED_ON); // Standard LED
|
||||
}
|
||||
@@ -673,7 +673,7 @@ void System::led_flash() {
|
||||
// after duration, turn off the LED
|
||||
if (current_time - led_flash_start_time_ >= led_flash_duration_) {
|
||||
if (led_flash_type_) {
|
||||
EMSESP_RGB_WRITE(led_flash_gpio_, 0, 0, 0);
|
||||
rgb_led::write(led_flash_gpio_, 0, 0, 0);
|
||||
} else {
|
||||
digitalWrite(led_flash_gpio_, !LED_ON);
|
||||
}
|
||||
@@ -732,12 +732,11 @@ void System::button_init() {
|
||||
// set the LED to on or off when in normal operating mode
|
||||
void System::led_init() {
|
||||
// disabled old led port before setting new one
|
||||
led_type_ ? EMSESP_RGB_WRITE(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON);
|
||||
led_type_ ? rgb_led::write(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON);
|
||||
|
||||
if ((led_gpio_)) { // 0 means disabled
|
||||
if (led_type_) {
|
||||
// rgb LED WS2812B, use Neopixel
|
||||
EMSESP_RGB_WRITE(led_gpio_, 0, 0, 0);
|
||||
rgb_led::write(led_gpio_, 0, 0, 0); // rgb LED WS2812B
|
||||
} else {
|
||||
pinMode(led_gpio_, OUTPUT);
|
||||
digitalWrite(led_gpio_, !LED_ON); // start with LED off
|
||||
@@ -1008,13 +1007,13 @@ void System::system_check() {
|
||||
// everything is healthy, show LED permanently on or off depending on setting
|
||||
// Green on RGB LED, on/off on standard LED
|
||||
if (led_gpio_) {
|
||||
led_type_ ? EMSESP_RGB_WRITE(led_gpio_, 0, hide_led_ ? 0 : RGB_LED_BRIGHTNESS, 0)
|
||||
led_type_ ? rgb_led::write(led_gpio_, 0, hide_led_ ? 0 : RGB_LED_BRIGHTNESS, 0)
|
||||
: digitalWrite(led_gpio_, hide_led_ ? !LED_ON : LED_ON); // Green
|
||||
}
|
||||
} else {
|
||||
// turn off LED so we're ready for the warning flashes
|
||||
if (led_gpio_) {
|
||||
led_type_ ? EMSESP_RGB_WRITE(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON);
|
||||
led_type_ ? rgb_led::write(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1050,7 +1049,7 @@ void System::led_monitor() {
|
||||
if (button_busy_ != myPButton_.button_busy()) {
|
||||
button_busy_ = myPButton_.button_busy();
|
||||
if (led_type_) {
|
||||
EMSESP_RGB_WRITE(led_gpio_, button_busy_ ? RGB_LED_BRIGHTNESS : 0, button_busy_ ? RGB_LED_BRIGHTNESS : 0, 0); // Yellow
|
||||
rgb_led::write(led_gpio_, button_busy_ ? RGB_LED_BRIGHTNESS : 0, button_busy_ ? RGB_LED_BRIGHTNESS : 0, 0); // Yellow
|
||||
} else {
|
||||
digitalWrite(led_gpio_, button_busy_ ? LED_ON : !LED_ON);
|
||||
}
|
||||
@@ -1085,7 +1084,7 @@ void System::led_monitor() {
|
||||
// reset the whole sequence
|
||||
led_long_timer_ = uuid::get_uptime();
|
||||
led_flash_step_ = 0;
|
||||
led_type_ ? EMSESP_RGB_WRITE(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON); // LED off
|
||||
led_type_ ? rgb_led::write(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON); // LED off
|
||||
} else if (led_flash_step_ % 2) {
|
||||
// handle the step events (on odd numbers 3,5,7,etc). see if we need to turn on a LED
|
||||
// 1 flash (blue) is the EMS bus is not connected
|
||||
@@ -1097,16 +1096,16 @@ void System::led_monitor() {
|
||||
if (led_type_) {
|
||||
if (led_flash_step_ == 3) {
|
||||
if (no_network) {
|
||||
EMSESP_RGB_WRITE(led_gpio_, RGB_LED_BRIGHTNESS, 0, 0); // red
|
||||
rgb_led::write(led_gpio_, RGB_LED_BRIGHTNESS, 0, 0); // red
|
||||
} else if (no_bus) {
|
||||
EMSESP_RGB_WRITE(led_gpio_, 0, 0, RGB_LED_BRIGHTNESS); // blue
|
||||
rgb_led::write(led_gpio_, 0, 0, RGB_LED_BRIGHTNESS); // blue
|
||||
}
|
||||
}
|
||||
if (led_flash_step_ == 5 && no_network) {
|
||||
EMSESP_RGB_WRITE(led_gpio_, RGB_LED_BRIGHTNESS, 0, 0); // red
|
||||
rgb_led::write(led_gpio_, RGB_LED_BRIGHTNESS, 0, 0); // red
|
||||
}
|
||||
if ((led_flash_step_ == 7) && no_network && no_bus) {
|
||||
EMSESP_RGB_WRITE(led_gpio_, 0, 0, RGB_LED_BRIGHTNESS); // blue
|
||||
rgb_led::write(led_gpio_, 0, 0, RGB_LED_BRIGHTNESS); // blue
|
||||
}
|
||||
} else {
|
||||
if ((led_flash_step_ == 3) && (no_network || no_bus)) {
|
||||
@@ -1128,7 +1127,7 @@ void System::led_monitor() {
|
||||
} else {
|
||||
// turn the led off after the flash, on even number count
|
||||
if (led_on_) {
|
||||
led_type_ ? EMSESP_RGB_WRITE(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON);
|
||||
led_type_ ? rgb_led::write(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON);
|
||||
led_on_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-5
@@ -37,11 +37,8 @@
|
||||
#include <uuid/log.h>
|
||||
#include <PButton.h>
|
||||
|
||||
#if ESP_ARDUINO_VERSION_MAJOR < 3
|
||||
#define EMSESP_RGB_WRITE neopixelWrite
|
||||
#else
|
||||
#define EMSESP_RGB_WRITE rgbLedWrite
|
||||
#endif
|
||||
#include "rgb_led.h"
|
||||
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
// there is no official API available on the original ESP32
|
||||
|
||||
Reference in New Issue
Block a user