From ed47d70b22bd6955aef2947bed4c236e07496843 Mon Sep 17 00:00:00 2001 From: Pablo2048 Date: Sat, 7 Oct 2023 18:07:14 +0200 Subject: [PATCH] Nove ovladani preruseni, zprehledneni kodu. --- library.json | 6 +- library.properties | 4 +- src/SigLed.cpp | 138 +++++++++++++++++++++++---------------------- src/SigLed.h | 44 ++++++--------- 4 files changed, 93 insertions(+), 99 deletions(-) diff --git a/library.json b/library.json index 4f3682e..478de00 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { "name":"SigLED", "description":"Handle signalling LED easily", - "keywords":"signalling,indiaction,led", + "keywords":"signalling,indication,led", "authors": { "name": "Pavel Brychta", @@ -10,9 +10,9 @@ "repository": { "type": "git", - "url": "http://git.xpablo.cz/xPablo.cz/SigLed" + "url": "https://git.xpablo.cz/xPablo.cz/SigLed" }, - "version": "1.0.1", + "version": "1.0.2", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index 9d5a3ff..9f2d887 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=SigLED -version=1.0.1 +version=1.0.2 author=Pavel Brychta maintainer=Pavel Brychta sentence=Handle signalling LED easily paragraph=Handle signalling LED easily category=Other -url=http://git.xpablo.cz/xPablo.cz/SigLed +url=https://git.xpablo.cz/xPablo.cz/SigLed architectures=* diff --git a/src/SigLed.cpp b/src/SigLed.cpp index a50fa32..182561f 100644 --- a/src/SigLed.cpp +++ b/src/SigLed.cpp @@ -1,69 +1,64 @@ // Obsluha LED signalizace -#include #include "SigLed.h" +#include +#include SigLED::SigLED(int pin, int ledon, int ledoff) -{ + : _pin(pin), _ledon(ledon), _ledoff(ledoff), _state(LS_NOTINITIALIZED) +{} - _pin = pin; - _ledon = ledon; - _ledoff = ledoff; - _state = LS_NOTINITIALIZED; -} - -void SigLED::rtLed(void) +void SigLED::rtLed() { switch (_state) { - case LS_RUN: { - uint8_t instr; + case LS_RUN: { + uint8_t instr; - if (NULL != _signal) - instr = *_ptr; // instrukce - else if (NULL != _psignal) - instr = pgm_read_byte(_pptr); - else - instr = LEDS_STOP; - switch (instr & 0xc0) { - case LEDS_ONFOR: - digitalWrite(_pin, _ledon); - _timer = 10ul * ((instr & 0x3F) + 1); - _state = LS_WAIT; + if (_signal) + instr = *_ptr; // instrukce + else if (_psignal) + instr = pgm_read_byte(_pptr); + else + instr = LEDS_STOP; + switch (instr & 0xc0) { + case LEDS_ONFOR: + digitalWrite(_pin, _ledon); + _timer = 10ul * ((instr & 0x3F) + 1); + _state = LS_WAIT; + break; + + case LEDS_OFFFOR: + digitalWrite(_pin, _ledoff); + _timer = 10ul * ((instr & 0x3F) + 1); + _state = LS_WAIT; + break; + + case LEDS_STOP: + _state = LS_IDLE; + break; + + case LEDS_RESTART: + _ptr = _signal; + _pptr = _psignal; + break; + } + } break; + + case LS_WAIT: + --_timer; + if (0 == _timer) { + _state = LS_RUN; + ++_ptr; + ++_pptr; + } break; - case LEDS_OFFFOR: - digitalWrite(_pin, _ledoff); - _timer = 10ul * ((instr & 0x3F) + 1); - _state = LS_WAIT; + default: break; - - case LEDS_STOP: - _state = LS_IDLE; - break; - - case LEDS_RESTART: - _ptr = _signal; - _pptr = _psignal; - break; - } - } - break; - - case LS_WAIT: - --_timer; - if (0 == _timer) { - _state = LS_RUN; - ++_ptr; - ++_pptr; - } - break; - - default: - break; } } -void SigLED::_init(void) +void SigLED::prepare() { pinMode(_pin, OUTPUT); @@ -72,47 +67,58 @@ void SigLED::_init(void) _handler.attach_ms(10, lh, this); } -void SigLED::set(const uint8_t *signal) +void SigLED::set(const uint8_t * signal) { if (LS_NOTINITIALIZED == _state) - _init(); - noInterrupts(); + prepare(); + + CriticalSection cs; _signal = signal; _ptr = _signal; - _psignal = NULL; + _psignal = nullptr; _state = LS_RUN; - interrupts(); } void SigLED::set_P(PGM_P signal) { if (LS_NOTINITIALIZED == _state) - _init(); - noInterrupts(); + prepare(); + + CriticalSection cs; _psignal = signal; _pptr = _psignal; - _signal = NULL; + _signal = nullptr; _state = LS_RUN; - interrupts(); } -void SigLED::start(void) +void SigLED::setup() { if (LS_NOTINITIALIZED == _state) - _init(); - noInterrupts(); + prepare(); + + CriticalSection cs; _ptr = _signal; _pptr = _psignal; _state = LS_RUN; - interrupts(); } -void SigLED::lh(SigLED *ptr) +void SigLED::lh(SigLED * ptr) { - SigLED *pled = ptr; + SigLED * pled = ptr; pled->rtLed(); } + +void SigLED::setInvert(bool invert) +{ + + if (_invert != invert) { + _invert = invert; + int temp = _ledon; + _ledon = _ledoff; + _ledoff = temp; + } +} \ No newline at end of file diff --git a/src/SigLed.h b/src/SigLed.h index cccdec9..e243a26 100644 --- a/src/SigLed.h +++ b/src/SigLed.h @@ -2,7 +2,7 @@ * @file sigLed.h * @author Pavel Brychta, http://www.xpablo.cz * - * Copyright (c) 2015,16 Pavel Brychta. All rights reserved. + * Copyright (c) 2015,16,23 Pavel Brychta. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -19,8 +19,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * */ -#ifndef _SIGLED_H_ -#define _SIGLED_H_ +#pragma once #include #include @@ -32,17 +31,16 @@ enum { LEDS_RESTART = 0xc0 }; -class SigLED -{ +class SigLED { -protected: + protected: int _pin; // pin, na kterem je LED pripojena Ticker _handler; // obsluha LED signalizace - const uint8_t *_signal; // ukazatel na vzor signalizace - PGM_P _psignal; - const uint8_t *_ptr; // ukazatel na aktualne zpracovavane misto v signalizaci - PGM_P _pptr; - uint32_t _timer; // casovani + const uint8_t * _signal = nullptr; // ukazatel na vzor signalizace + PGM_P _psignal = nullptr; + const uint8_t * _ptr = nullptr; // ukazatel na aktualne zpracovavane misto v signalizaci + PGM_P _pptr = nullptr; + uint32_t _timer = 0; // casovani int _ledon; int _ledoff; enum { @@ -53,24 +51,14 @@ protected: } _state; // stav automatu bool _invert = false; - void _init(void); + void prepare(); + static void lh(SigLED * ptr); + void rtLed(); // vykonna metoda -public: + public: SigLED(int pin, int ledon, int ledoff); - void set(const uint8_t *signal); + void set(const uint8_t * signal); void set_P(PGM_P signal); - void start(void); - void setInvert(bool invert) - { - - if (_invert != invert) { - _invert = invert; - int temp = _ledon; - _ledon = _ledoff; - _ledoff = temp; - } - } - static void lh(SigLED *ptr); - void rtLed(void); // vykonna metoda + void setup(); + void setInvert(bool invert); }; -#endif