Moznost generovat impulz, lepsi rozhrani.

This commit is contained in:
Pavel Brychta 2021-02-28 10:42:28 +01:00
parent 3bbb76672a
commit 692a25771a
2 changed files with 95 additions and 31 deletions

View File

@ -74,11 +74,28 @@ wsLED::wsLED(int pin, int order)
_pin = pin; _pin = pin;
_order = order; _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) void wsLED::begin(void)
{ {
if (NOT_A_PIN != _pin) {
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
pinMode(_pin, OUTPUT); pinMode(_pin, OUTPUT);
#endif #endif
@ -102,6 +119,7 @@ void wsLED::begin(void)
_color[0] = _color[1] = _color[2] = 0; _color[0] = _color[1] = _color[2] = 0;
show(); show();
} }
}
void wsLED::setColor(LEDRGB &color) void wsLED::setColor(LEDRGB &color)
{ {
@ -116,7 +134,6 @@ void ICACHE_RAM_ATTR wsLED::rtLed(void)
switch (_ledState) { switch (_ledState) {
case LED_BLINK: case LED_BLINK:
_scale = 0xff;
if (_subState) { if (_subState) {
_subState = 0; _subState = 0;
setColor(_color2); setColor(_color2);
@ -139,6 +156,13 @@ void ICACHE_RAM_ATTR wsLED::rtLed(void)
break; break;
} }
case LED_PULSE: {
_ledState = _savedState;
_handler.attach_ms(300, lh, this); // vratime zpet puvodni casovani
setColor(_color1);
break;
}
default: default:
_handler.attach_ms(300, lh, this); // jen pomale casovani _handler.attach_ms(300, lh, this); // jen pomale casovani
break; break;
@ -157,26 +181,38 @@ void wsLED::setColors(LEDRGB color1, LEDRGB color2)
_color1 = color1; _color1 = color1;
_color2 = color2; _color2 = color2;
if (LED_BREATH == _ledState) {
setColor(_color1);
}
} }
void wsLED::blink(int speed) void wsLED::blink(int speed)
{ {
if (NOT_A_PIN != _pin) {
if (LED_BLINK != _ledState) {
_handler.detach(); _handler.detach();
_ledState = LED_BLINK; _ledState = LED_BLINK;
_scale = 0xff;
_subState = 1; _subState = 1;
_handler.attach_ms(300, lh, this); _handler.attach_ms(300, lh, this);
} }
}
}
void wsLED::breath(void) void wsLED::breath(void)
{ {
if (NOT_A_PIN != _pin) {
if (LED_BREATH != _ledState) {
_handler.detach(); _handler.detach();
_ledState = LED_BREATH; _ledState = LED_BREATH;
_subState = 0; _subState = 0;
setColor(_color1); setColor(_color1);
_handler.attach_ms(1, lh, this); _handler.attach_ms(1, lh, this);
} }
}
}
void wsLED::setOrder(int neworder) void wsLED::setOrder(int neworder)
{ {
@ -184,4 +220,24 @@ void wsLED::setOrder(int neworder)
_order = 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;
}

View File

@ -113,6 +113,14 @@ enum ColorOrder {
GRB = 0102 GRB = 0102
}; };
typedef enum {
LED_STOP = 0,
LED_BLINK,
LED_BREATH,
LED_PULSE,
LED_PATTERN,
}ledstate_t;
class wsLED class wsLED
{ {
protected: protected:
@ -124,12 +132,8 @@ protected:
uint8_t _scale; uint8_t _scale;
uint8_t _color[3]; uint8_t _color[3];
Ticker _handler; Ticker _handler;
enum { ledstate_t _ledState; // stav LEDky (blika, dycha, ...)
LED_STOP = 0, ledstate_t _savedState; // stav pro navrat
LED_BLINK,
LED_BREATH,
LED_PATTERN,
} _ledState; // stav LEDky (blika, dycha, ...)
int _subState; // pomocny stav/index v poli dychani int _subState; // pomocny stav/index v poli dychani
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
int _rmtChannel; int _rmtChannel;
@ -144,13 +148,17 @@ protected:
public: public:
wsLED(int pin, int order = RGB); wsLED(int pin, int order = RGB);
wsLED(void);
void begin(void); void begin(void);
void begin(int pin);
static void lh(wsLED *ptr); static void lh(wsLED *ptr);
void rtLed(void); // vykonna metoda void rtLed(void); // vykonna metoda
void setColors(LEDRGB color1, LEDRGB color2 = LEDRGB(0, 0, 0)); void setColors(LEDRGB color1, LEDRGB color2 = LEDRGB(0, 0, 0));
void blink(int speed = 300); void blink(int speed = 300);
void breath(void); void breath(void);
void setOrder(int neworder); void setOrder(int neworder);
ledstate_t getState(void);
void pulse(LEDRGB color, uint32_t duration);
}; };
#endif #endif