Upravy pro vyssi spolehlivost na ESP32

This commit is contained in:
Pavel Brychta 2021-08-02 11:06:13 +02:00
parent 707d6a3375
commit c5fab3dec2
2 changed files with 27 additions and 15 deletions

View File

@ -128,6 +128,7 @@ void ICACHE_RAM_ATTR wsLED::rtLed(void)
switch (_ledState) { switch (_ledState) {
case LED_BLINK: case LED_BLINK:
_handler.detach();
if (_subState) { if (_subState) {
_subState = 0; _subState = 0;
setColor(_color2); setColor(_color2);
@ -136,6 +137,7 @@ void ICACHE_RAM_ATTR wsLED::rtLed(void)
setColor(_color1); setColor(_color1);
} }
show(); show();
_handler.attach_ms(_blinkSpeed, lh, this);
break; break;
@ -147,23 +149,31 @@ void ICACHE_RAM_ATTR wsLED::rtLed(void)
_scale = (uint8_t)val; _scale = (uint8_t)val;
show(); show();
val >>= 8; val >>= 8;
_lastTiming = 30ul * val; uint32_t timing = 30ul * val;
_handler.attach_ms(_lastTiming, lh, this); _handler.attach_ms(timing, lh, this);
break; break;
} }
case LED_PULSE: { case LED_PULSE: {
_handler.detach(); _handler.detach();
_ledState = _savedState; _ledState = _savedState;
_subState = 0;
setColor(_color1); setColor(_color1);
show(); show();
_handler.attach_ms(_lastTiming, lh, this); // vratime zpet puvodni casovani _handler.attach_ms(1, lh, this); // co nejrychleji do predchoziho stavu
break; break;
} }
case LED_PULSE_START:
_handler.detach();
setColor(_pulseColor);
show();
_ledState = LED_PULSE;
_handler.attach_ms(_pulseDuration, lh, this);
break;
default: default:
_handler.detach(); _handler.detach();
// _handler.attach_ms(300, lh, this); // jen pomale casovani
break; break;
} }
} }
@ -185,7 +195,7 @@ void wsLED::setColors(LEDRGB color1, LEDRGB color2)
} }
} }
void wsLED::blink(int speed) void wsLED::blink(uint32_t speed)
{ {
if (NOT_A_PIN != _pin) { if (NOT_A_PIN != _pin) {
@ -194,8 +204,8 @@ void wsLED::blink(int speed)
_ledState = LED_BLINK; _ledState = LED_BLINK;
_scale = 0xff; _scale = 0xff;
_subState = 1; _subState = 1;
_lastTiming = 300; _blinkSpeed = speed;
_handler.attach_ms(_lastTiming, lh, this); _handler.attach_ms(1, lh, this);
} }
} }
} }
@ -209,8 +219,7 @@ void wsLED::breath(void)
_ledState = LED_BREATH; _ledState = LED_BREATH;
_subState = 0; _subState = 0;
setColor(_color1); setColor(_color1);
_lastTiming = 1; _handler.attach_ms(1, lh, this);
_handler.attach_ms(_lastTiming, lh, this);
} }
} }
} }
@ -227,13 +236,13 @@ void wsLED::pulse(LEDRGB color, uint32_t duration)
if (NOT_A_PIN != _pin) { if (NOT_A_PIN != _pin) {
_handler.detach(); _handler.detach();
_scale = 0xff; _scale = 0xff;
setColor(color); _pulseColor = color;
if (LED_PULSE != _ledState) { if (LED_PULSE != _ledState) {
_savedState = _ledState; _savedState = _ledState;
} }
show(); _pulseDuration = duration;
_ledState = LED_PULSE; _ledState = LED_PULSE_START;
_handler.attach_ms(duration, lh, this); _handler.attach_ms(5, lh, this);
} }
} }

View File

@ -117,6 +117,7 @@ typedef enum {
LED_STOP = 0, LED_STOP = 0,
LED_BLINK, LED_BLINK,
LED_BREATH, LED_BREATH,
LED_PULSE_START,
LED_PULSE, LED_PULSE,
LED_PATTERN, LED_PATTERN,
}ledstate_t; }ledstate_t;
@ -135,7 +136,9 @@ protected:
ledstate_t _ledState; // stav LEDky (blika, dycha, ...) ledstate_t _ledState; // stav LEDky (blika, dycha, ...)
ledstate_t _savedState; // stav pro navrat ledstate_t _savedState; // stav pro navrat
int _subState; // pomocny stav/index v poli dychani int _subState; // pomocny stav/index v poli dychani
uint32_t _lastTiming; // posledni nastaveny cas uint32_t _pulseDuration;
uint32_t _blinkSpeed;
LEDRGB _pulseColor;
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
int _rmtChannel; int _rmtChannel;
#endif #endif
@ -155,7 +158,7 @@ public:
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(uint32_t speed = 300);
void breath(void); void breath(void);
void setOrder(int neworder); void setOrder(int neworder);
ledstate_t getState(void); ledstate_t getState(void);