9 Commits

6 changed files with 115 additions and 41 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2018
Copyright (c) 2014, 2016, 2018 Pavel Brychta http://www.xpablo.cz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -1,2 +1,36 @@
# Interval
Arduino knihovna pro časování pomocí intervalů
# Interval - Arduino knihovna pro časování pomocí intervalů
Koncepce programové konstrukce aplikace pro Arduino spočívá ve dvou hlavních metodách **setup()** a **loop()**, ve které program neustále běží.
Pro pohodlnější práci s obsluhou periodických procesů jsem napsal jednoduchou knihovnu, která tyto úkoly umožňuje realizovat velmi elegantním způsobem.
Knihovna **Interval** vytváří časovací objekty a umožňuje jejich ovládání pomocí následujících metod.
## **set**
Metoda **set** se používá k nastavení timeoutu a definici začátku časování.
## **expired**
Metoda **expired** pak slouží k ověření, zda nastavený interval již vypršel.
## **setOneshot**
Metoda **setOneshot** nastaví jednorázový běh časovače. Po jeho vypršení je časovač zastavený.
## **setPeriodic**
Metoda **setPeriodic** nastaví opakovaný běh časovače. Po jeho vypršení je časovač automaticky nastavený na zadaný čas.
## **reload**
Metoda **reload** způsobí nový start časovače s poslední zadanou časovou konstantou.
## **elapsed**
Metoda **elapsed** vrátí čas, který už uplynul od začátku běhu časovače.
## **remains**
Metoda **remains** vrátí čas, který ještě zbývá do konce běhu časovače, nebo do startu nového cyklu.
Zajímavostí je, že knihovna korektně ošetřuje přetečení vnitřního milisekundového čítače, takže nehrozí nebezpečí špatného časování i při velmi dlouhé době chodu zařízení.

View File

@ -10,12 +10,12 @@
"repository":
{
"type": "git",
"url": "https://github.com/Pablo2048/Interval.git"
"url": "http://git.xpablo.cz/pablo2048/Interval.git"
},
"version": "0.0.1",
"version": "0.0.4",
"license": "MIT",
"frameworks": "arduino",
"platforms": "espressif8266",
"platforms": "*",
"build": {
"libCompatMode": 2
}

View File

@ -1,9 +1,9 @@
name=Interval
version=0.0.1
version=0.0.4
author=Pavel Brychta
maintainer=Pavel Brychta
sentence=Make timing by using intervals instead of delay()
paragraph=Make timing by using intervals instead of delay()
category=Other
url=https://github.com/Pablo2048/Interval
url=http://git.xpablo.cz/pablo2048/Interval
architectures=*

View File

@ -1,36 +1,74 @@
extern "C" {
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
}
#include <Arduino.h>
#include "interval.h"
// Public Methods //////////////////////////////////////////////////////////////
uint32_t Interval::remains(void)
{
return timeout - (millis() - timefrom);
return _timeout - (millis() - _timefrom);
}
uint32_t Interval::elapsed(void)
{
return millis() - timefrom;
return millis() - _timefrom;
}
bool Interval::expired(void)
{
bool result = false;
if ((millis() - timefrom) >= timeout)
return true;
else
return false;
if (_done) {
if (1 == _mode) {
// oneshot mode
if ((millis() - _timefrom) >= _timeout) {
_done = 0;
result = true;
}
} else if (2 == _mode) {
// periodic mode
if ((millis() - _timefrom) >= _timeout) {
result = true;
_timefrom = millis();
}
} else {
// compatibility mode
if ((millis() - _timefrom) >= _timeout)
result = true;
}
}
return result;
}
void Interval::set(uint32_t tmout)
{
timefrom = millis();
timeout = tmout;
_timefrom = millis();
_timeout = tmout;
_mode = 0;
_done = 0xff;
}
void Interval::setOneshot(uint32_t tmout)
{
_timefrom = millis();
_timeout = tmout;
_mode = 1;
_done = 0xff;
}
void Interval::setPeriodic(uint32_t tmout)
{
_timefrom = millis();
_timeout = tmout;
_mode = 2;
_done = 0xff;
}
void Interval::reload(void)
{
_timefrom = millis();
_done = 0xff;
}

View File

@ -1,29 +1,31 @@
#ifndef interval_h
#define interval_h
#ifndef _INTERVAL_H_
#define _INTERVAL_H_
/* Interval
* Copyright (C) 2014, 2016 Pavel Brychta http://www.xpablo.cz
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses
* Copyright (C) 2014, 2016, 2018, 2019 Pavel Brychta http://www.xpablo.cz
*
* This program is free software: you can redistribute it and/or modify it under the terms of the MIT License
*/
#include <inttypes.h>
#include <Arduino.h>
class Interval
{
protected:
uint32_t timefrom;
uint32_t timeout;
public:
bool expired(void);
void set(uint32_t tmout);
uint32_t elapsed(void);
uint32_t remains(void);
protected:
uint32_t _timefrom;
uint32_t _timeout;
uint8_t _mode; // mode of actual operation (compatibility, oneshot, periodic)
uint8_t _done;
public:
Interval(): _mode(0), _done(0xff) {} // compatibility mode autostart
Interval(uint32_t tmout): _timeout(tmout), _mode(2), _done(0xff) {} // periodic mode autostart
bool expired(void);
void set(uint32_t tmout);
void setOneshot(uint32_t tmout);
void setPeriodic(uint32_t tmout);
void reload(void);
uint32_t elapsed(void);
uint32_t remains(void);
};
#endif