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", "name":"SigLED",
"description":"Handle signalling LED easily", "description":"Handle signalling LED easily",
"keywords":"signalling,indiaction,led", "keywords":"signalling,indication,led",
"authors": "authors":
{ {
"name": "Pavel Brychta", "name": "Pavel Brychta",
@ -10,9 +10,9 @@
"repository": "repository":
{ {
"type": "git", "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", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "*", "platforms": "*",

View File

@ -1,9 +1,9 @@
name=SigLED name=SigLED
version=1.0.1 version=1.0.2
author=Pavel Brychta author=Pavel Brychta
maintainer=Pavel Brychta maintainer=Pavel Brychta
sentence=Handle signalling LED easily sentence=Handle signalling LED easily
paragraph=Handle signalling LED easily paragraph=Handle signalling LED easily
category=Other category=Other
url=http://git.xpablo.cz/xPablo.cz/SigLed url=https://git.xpablo.cz/xPablo.cz/SigLed
architectures=* architectures=*

View File

@ -1,69 +1,64 @@
// Obsluha LED signalizace // Obsluha LED signalizace
#include <Arduino.h>
#include "SigLed.h" #include "SigLed.h"
#include <Arduino.h>
#include <CriticalSection.hpp>
SigLED::SigLED(int pin, int ledon, int ledoff) SigLED::SigLED(int pin, int ledon, int ledoff)
{ : _pin(pin), _ledon(ledon), _ledoff(ledoff), _state(LS_NOTINITIALIZED)
{}
_pin = pin; void SigLED::rtLed()
_ledon = ledon;
_ledoff = ledoff;
_state = LS_NOTINITIALIZED;
}
void SigLED::rtLed(void)
{ {
switch (_state) { switch (_state) {
case LS_RUN: { case LS_RUN: {
uint8_t instr; uint8_t instr;
if (NULL != _signal) if (_signal)
instr = *_ptr; // instrukce instr = *_ptr; // instrukce
else if (NULL != _psignal) else if (_psignal)
instr = pgm_read_byte(_pptr); instr = pgm_read_byte(_pptr);
else else
instr = LEDS_STOP; instr = LEDS_STOP;
switch (instr & 0xc0) { switch (instr & 0xc0) {
case LEDS_ONFOR: case LEDS_ONFOR:
digitalWrite(_pin, _ledon); digitalWrite(_pin, _ledon);
_timer = 10ul * ((instr & 0x3F) + 1); _timer = 10ul * ((instr & 0x3F) + 1);
_state = LS_WAIT; _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; break;
case LEDS_OFFFOR: default:
digitalWrite(_pin, _ledoff);
_timer = 10ul * ((instr & 0x3F) + 1);
_state = LS_WAIT;
break; 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); pinMode(_pin, OUTPUT);
@ -72,47 +67,58 @@ void SigLED::_init(void)
_handler.attach_ms(10, lh, this); _handler.attach_ms(10, lh, this);
} }
void SigLED::set(const uint8_t *signal) void SigLED::set(const uint8_t * signal)
{ {
if (LS_NOTINITIALIZED == _state) if (LS_NOTINITIALIZED == _state)
_init(); prepare();
noInterrupts();
CriticalSection cs;
_signal = signal; _signal = signal;
_ptr = _signal; _ptr = _signal;
_psignal = NULL; _psignal = nullptr;
_state = LS_RUN; _state = LS_RUN;
interrupts();
} }
void SigLED::set_P(PGM_P signal) void SigLED::set_P(PGM_P signal)
{ {
if (LS_NOTINITIALIZED == _state) if (LS_NOTINITIALIZED == _state)
_init(); prepare();
noInterrupts();
CriticalSection cs;
_psignal = signal; _psignal = signal;
_pptr = _psignal; _pptr = _psignal;
_signal = NULL; _signal = nullptr;
_state = LS_RUN; _state = LS_RUN;
interrupts();
} }
void SigLED::start(void) void SigLED::setup()
{ {
if (LS_NOTINITIALIZED == _state) if (LS_NOTINITIALIZED == _state)
_init(); prepare();
noInterrupts();
CriticalSection cs;
_ptr = _signal; _ptr = _signal;
_pptr = _psignal; _pptr = _psignal;
_state = LS_RUN; _state = LS_RUN;
interrupts();
} }
void SigLED::lh(SigLED *ptr) void SigLED::lh(SigLED * ptr)
{ {
SigLED *pled = ptr; SigLED * pled = ptr;
pled->rtLed(); 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 * @file sigLed.h
* @author Pavel Brychta, http://www.xpablo.cz * @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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * 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 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* *
*/ */
#ifndef _SIGLED_H_ #pragma once
#define _SIGLED_H_
#include <Ticker.h> #include <Ticker.h>
#include <pgmspace.h> #include <pgmspace.h>
@ -32,17 +31,16 @@ enum {
LEDS_RESTART = 0xc0 LEDS_RESTART = 0xc0
}; };
class SigLED class SigLED {
{
protected: protected:
int _pin; // pin, na kterem je LED pripojena int _pin; // pin, na kterem je LED pripojena
Ticker _handler; // obsluha LED signalizace Ticker _handler; // obsluha LED signalizace
const uint8_t *_signal; // ukazatel na vzor signalizace const uint8_t * _signal = nullptr; // ukazatel na vzor signalizace
PGM_P _psignal; PGM_P _psignal = nullptr;
const uint8_t *_ptr; // ukazatel na aktualne zpracovavane misto v signalizaci const uint8_t * _ptr = nullptr; // ukazatel na aktualne zpracovavane misto v signalizaci
PGM_P _pptr; PGM_P _pptr = nullptr;
uint32_t _timer; // casovani uint32_t _timer = 0; // casovani
int _ledon; int _ledon;
int _ledoff; int _ledoff;
enum { enum {
@ -53,24 +51,14 @@ protected:
} _state; // stav automatu } _state; // stav automatu
bool _invert = false; 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); 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 set_P(PGM_P signal);
void start(void); void setup();
void setInvert(bool invert) 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
}; };
#endif