Konstruktor pro periodic autostart

This commit is contained in:
Pavel Brychta 2019-10-09 09:41:11 +02:00
parent acffaeef87
commit 15be6b4674
4 changed files with 58 additions and 68 deletions

View File

@ -12,7 +12,7 @@
"type": "git", "type": "git",
"url": "http://git.xpablo.cz/pablo2048/Interval.git" "url": "http://git.xpablo.cz/pablo2048/Interval.git"
}, },
"version": "0.0.3", "version": "0.0.4",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=Interval name=Interval
version=0.0.3 version=0.0.4
author=Pavel Brychta author=Pavel Brychta
maintainer=Pavel Brychta maintainer=Pavel Brychta
sentence=Make timing by using intervals instead of delay() sentence=Make timing by using intervals instead of delay()

View File

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

View File

@ -1,31 +1,31 @@
#ifndef _INTERVAL_H_
#ifndef interval_h #define _INTERVAL_H_
#define interval_h
/* Interval /* 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 * 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 class Interval
{ {
protected: protected:
uint32_t _timefrom; uint32_t _timefrom;
uint32_t _timeout; uint32_t _timeout;
uint8_t _mode; // mode of actual operation (compatibility, oneshot, periodic) uint8_t _mode; // mode of actual operation (compatibility, oneshot, periodic)
uint8_t _done; uint8_t _done;
public: public:
Interval(): _mode(0), _done(0xff) {} Interval(): _mode(0), _done(0xff) {} // compatibility mode autostart
bool expired(void); Interval(uint32_t tmout): _timeout(tmout), _mode(2), _done(0xff) {} // periodic mode autostart
void set(uint32_t tmout); bool expired(void);
void setOneshot(uint32_t tmout); void set(uint32_t tmout);
void setPeriodic(uint32_t tmout); void setOneshot(uint32_t tmout);
void reload(void); void setPeriodic(uint32_t tmout);
uint32_t elapsed(void); void reload(void);
uint32_t remains(void); uint32_t elapsed(void);
uint32_t remains(void);
}; };
#endif #endif