From 15be6b4674551d86d96d85f5a648cb5266760aa8 Mon Sep 17 00:00:00 2001 From: Pavel Brychta Date: Wed, 9 Oct 2019 09:41:11 +0200 Subject: [PATCH] Konstruktor pro periodic autostart --- library.json | 2 +- library.properties | 2 +- src/interval.cpp | 84 ++++++++++++++++++++-------------------------- src/interval.h | 38 ++++++++++----------- 4 files changed, 58 insertions(+), 68 deletions(-) diff --git a/library.json b/library.json index 636330d..059e969 100644 --- a/library.json +++ b/library.json @@ -12,7 +12,7 @@ "type": "git", "url": "http://git.xpablo.cz/pablo2048/Interval.git" }, - "version": "0.0.3", + "version": "0.0.4", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index a90e147..27d820d 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Interval -version=0.0.3 +version=0.0.4 author=Pavel Brychta maintainer=Pavel Brychta sentence=Make timing by using intervals instead of delay() diff --git a/src/interval.cpp b/src/interval.cpp index 64972b1..e955999 100644 --- a/src/interval.cpp +++ b/src/interval.cpp @@ -1,84 +1,74 @@ -extern "C" { - #include - #include - #include -} -#include #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 (_done) - { - if (1 == _mode) - { // oneshot mode - if ((millis() - _timefrom) >= _timeout) - { - _done = 0; - result = true; - } + bool result = 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; + } } - 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; + return result; } void Interval::set(uint32_t tmout) { - _timefrom = millis(); - _timeout = tmout; - _mode = 0; - _done = 0xff; + _timefrom = millis(); + _timeout = tmout; + _mode = 0; + _done = 0xff; } void Interval::setOneshot(uint32_t tmout) { - _timefrom = millis(); - _timeout = tmout; - _mode = 1; - _done = 0xff; + _timefrom = millis(); + _timeout = tmout; + _mode = 1; + _done = 0xff; } void Interval::setPeriodic(uint32_t tmout) { - _timefrom = millis(); - _timeout = tmout; - _mode = 2; - _done = 0xff; + _timefrom = millis(); + _timeout = tmout; + _mode = 2; + _done = 0xff; } void Interval::reload(void) { - _timefrom = millis(); - _done = 0xff; + _timefrom = millis(); + _done = 0xff; } diff --git a/src/interval.h b/src/interval.h index d8f1d39..d1f7356 100644 --- a/src/interval.h +++ b/src/interval.h @@ -1,31 +1,31 @@ - -#ifndef interval_h -#define interval_h +#ifndef _INTERVAL_H_ +#define _INTERVAL_H_ /* Interval - * Copyright (C) 2014, 2016, 2018 Pavel Brychta http://www.xpablo.cz + * 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 +#include class Interval { - 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) {} - 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); +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