Nove ovladani preruseni, zprehledneni kodu.

This commit is contained in:
Pavel Brychta 2023-10-07 18:07:14 +02:00
parent 66acdd9805
commit ed47d70b22
4 changed files with 93 additions and 99 deletions

View File

@ -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": "*",

View File

@ -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=*

View File

@ -1,26 +1,22 @@
// Obsluha LED signalizace
#include <Arduino.h>
#include "SigLed.h"
#include <Arduino.h>
#include <CriticalSection.hpp>
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;
if (NULL != _signal)
if (_signal)
instr = *_ptr; // instrukce
else if (NULL != _psignal)
else if (_psignal)
instr = pgm_read_byte(_pptr);
else
instr = LEDS_STOP;
@ -46,8 +42,7 @@ void SigLED::rtLed(void)
_pptr = _psignal;
break;
}
}
break;
} break;
case LS_WAIT:
--_timer;
@ -63,7 +58,7 @@ void SigLED::rtLed(void)
}
}
void SigLED::_init(void)
void SigLED::prepare()
{
pinMode(_pin, OUTPUT);
@ -76,38 +71,38 @@ 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)
@ -116,3 +111,14 @@ void SigLED::lh(SigLED *ptr)
SigLED * pled = ptr;
pled->rtLed();
}
void SigLED::setInvert(bool invert)
{
if (_invert != invert) {
_invert = invert;
int temp = _ledon;
_ledon = _ledoff;
_ledoff = temp;
}
}

View File

@ -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 <Ticker.h>
#include <pgmspace.h>
@ -32,17 +31,16 @@ enum {
LEDS_RESTART = 0xc0
};
class SigLED
{
class SigLED {
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:
SigLED(int pin, int ledon, int ledoff);
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