From 692a25771ae9afcb23fd2d490e3850c895ecf571 Mon Sep 17 00:00:00 2001 From: Pablo2048 Date: Sun, 28 Feb 2021 10:42:28 +0100 Subject: [PATCH] Moznost generovat impulz, lepsi rozhrani. --- src/wsLED.cpp | 106 ++++++++++++++++++++++++++++++++++++++------------ src/wsLED.h | 20 +++++++--- 2 files changed, 95 insertions(+), 31 deletions(-) diff --git a/src/wsLED.cpp b/src/wsLED.cpp index fab08c4..3d8df94 100644 --- a/src/wsLED.cpp +++ b/src/wsLED.cpp @@ -74,33 +74,51 @@ wsLED::wsLED(int pin, int order) _pin = pin; _order = order; + _ledState = LED_STOP; +} + +wsLED::wsLED(void) +{ + + _pin = NOT_A_PIN; + _order = RGB; + _ledState = LED_STOP; +} + +void wsLED::begin(int pin) +{ + + _pin = pin; + begin(); } void wsLED::begin(void) { + if (NOT_A_PIN != _pin) { #if defined(ARDUINO_ARCH_ESP8266) - pinMode(_pin, OUTPUT); + pinMode(_pin, OUTPUT); #endif #if defined(ARDUINO_ARCH_ESP32) // TODO: mozna nejak zkusit najit volny RMT kanal? - rmt_config_t config; + rmt_config_t config; - config.rmt_mode = RMT_MODE_TX; - config.channel = LED_RMT_TX_CHANNEL; - config.gpio_num = (gpio_num_t)_pin; - config.mem_block_num = 3; - config.tx_config.loop_en = false; - config.tx_config.carrier_en = false; - config.tx_config.idle_output_en = true; - config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW; - config.clk_div = 2; + config.rmt_mode = RMT_MODE_TX; + config.channel = LED_RMT_TX_CHANNEL; + config.gpio_num = (gpio_num_t)_pin; + config.mem_block_num = 3; + config.tx_config.loop_en = false; + config.tx_config.carrier_en = false; + config.tx_config.idle_output_en = true; + config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW; + config.clk_div = 2; - ESP_ERROR_CHECK(rmt_config(&config)); - ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0)); + ESP_ERROR_CHECK(rmt_config(&config)); + ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0)); #endif - _color[0] = _color[1] = _color[2] = 0; - show(); + _color[0] = _color[1] = _color[2] = 0; + show(); + } } void wsLED::setColor(LEDRGB &color) @@ -116,7 +134,6 @@ void ICACHE_RAM_ATTR wsLED::rtLed(void) switch (_ledState) { case LED_BLINK: - _scale = 0xff; if (_subState) { _subState = 0; setColor(_color2); @@ -139,6 +156,13 @@ void ICACHE_RAM_ATTR wsLED::rtLed(void) break; } + case LED_PULSE: { + _ledState = _savedState; + _handler.attach_ms(300, lh, this); // vratime zpet puvodni casovani + setColor(_color1); + break; + } + default: _handler.attach_ms(300, lh, this); // jen pomale casovani break; @@ -157,25 +181,37 @@ void wsLED::setColors(LEDRGB color1, LEDRGB color2) _color1 = color1; _color2 = color2; + if (LED_BREATH == _ledState) { + setColor(_color1); + } } void wsLED::blink(int speed) { - _handler.detach(); - _ledState = LED_BLINK; - _subState = 1; - _handler.attach_ms(300, lh, this); + if (NOT_A_PIN != _pin) { + if (LED_BLINK != _ledState) { + _handler.detach(); + _ledState = LED_BLINK; + _scale = 0xff; + _subState = 1; + _handler.attach_ms(300, lh, this); + } + } } void wsLED::breath(void) { - _handler.detach(); - _ledState = LED_BREATH; - _subState = 0; - setColor(_color1); - _handler.attach_ms(1, lh, this); + if (NOT_A_PIN != _pin) { + if (LED_BREATH != _ledState) { + _handler.detach(); + _ledState = LED_BREATH; + _subState = 0; + setColor(_color1); + _handler.attach_ms(1, lh, this); + } + } } void wsLED::setOrder(int neworder) @@ -184,4 +220,24 @@ void wsLED::setOrder(int neworder) _order = neworder; } +void wsLED::pulse(LEDRGB color, uint32_t duration) +{ + if (NOT_A_PIN != _pin) { + _handler.detach(); + _scale = 0xff; + setColor(color); + if (LED_PULSE != _ledState) { + _savedState = _ledState; + } + show(); + _ledState = LED_PULSE; + _handler.attach_ms(duration, lh, this); + } +} + +ledstate_t wsLED::getState(void) +{ + + return _ledState; +} diff --git a/src/wsLED.h b/src/wsLED.h index 781db78..433e474 100644 --- a/src/wsLED.h +++ b/src/wsLED.h @@ -113,6 +113,14 @@ enum ColorOrder { GRB = 0102 }; +typedef enum { + LED_STOP = 0, + LED_BLINK, + LED_BREATH, + LED_PULSE, + LED_PATTERN, +}ledstate_t; + class wsLED { protected: @@ -124,12 +132,8 @@ protected: uint8_t _scale; uint8_t _color[3]; Ticker _handler; - enum { - LED_STOP = 0, - LED_BLINK, - LED_BREATH, - LED_PATTERN, - } _ledState; // stav LEDky (blika, dycha, ...) + ledstate_t _ledState; // stav LEDky (blika, dycha, ...) + ledstate_t _savedState; // stav pro navrat int _subState; // pomocny stav/index v poli dychani #if defined(ARDUINO_ARCH_ESP32) int _rmtChannel; @@ -144,13 +148,17 @@ protected: public: wsLED(int pin, int order = RGB); + wsLED(void); void begin(void); + void begin(int pin); static void lh(wsLED *ptr); void rtLed(void); // vykonna metoda void setColors(LEDRGB color1, LEDRGB color2 = LEDRGB(0, 0, 0)); void blink(int speed = 300); void breath(void); void setOrder(int neworder); + ledstate_t getState(void); + void pulse(LEDRGB color, uint32_t duration); }; #endif