2015-10-29 09:28:29 -04:00
|
|
|
//
|
|
|
|
// FILE: CountDown.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2020-11-27 05:10:47 -05:00
|
|
|
// VERSION: 0.2.2
|
2015-10-29 09:28:29 -04:00
|
|
|
// PURPOSE: CountDown library for Arduino
|
2020-11-27 05:10:47 -05:00
|
|
|
// URL: https://github.com/RobTillaart/CountDown
|
2015-10-29 09:28:29 -04:00
|
|
|
//
|
|
|
|
// HISTORY:
|
2020-11-27 05:10:47 -05:00
|
|
|
// 0.2.2 2020-07-08 add MINUTES; refactor
|
|
|
|
// 0.2.1 2020-06-05 fix library.json
|
|
|
|
// 0.2.0 2020-03-29 #pragma once, removed pre 1.0 support
|
|
|
|
// 0.1.3 2017-07-16 TODO improved seconds - OdoMeter see below ... TODO
|
|
|
|
// 0.1.2 2017-07-16 added start(days, hours, minutes, seconds) + cont() == continue countdown
|
|
|
|
// 0.1.1 2015-10-29 added start(h, m, s)
|
|
|
|
// 0.1.0 2015-10-27 initial version
|
2015-10-29 09:28:29 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "CountDown.h"
|
|
|
|
|
|
|
|
CountDown::CountDown(const enum Resolution res)
|
|
|
|
{
|
2017-07-16 05:46:43 -04:00
|
|
|
setResolution(res);
|
|
|
|
stop();
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CountDown::setResolution(const enum Resolution res)
|
|
|
|
{
|
2017-07-16 05:46:43 -04:00
|
|
|
_res = res;
|
|
|
|
_ticks = 0;
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CountDown::start(uint32_t ticks)
|
|
|
|
{
|
2017-07-16 05:46:43 -04:00
|
|
|
_ticks = ticks;
|
2020-11-27 05:10:47 -05:00
|
|
|
_state = CountDown::RUNNING;
|
|
|
|
if (_res == MICROS)
|
|
|
|
{
|
|
|
|
_starttime = micros();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_starttime = millis();
|
|
|
|
}
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
void CountDown::start(uint8_t days, uint16_t hours, uint32_t minutes, uint32_t seconds)
|
2015-10-29 09:28:29 -04:00
|
|
|
{
|
2017-07-16 05:46:43 -04:00
|
|
|
uint32_t ticks = 86400UL * days + 3600UL * hours + 60UL * minutes + seconds;
|
|
|
|
if (ticks > 4294967) ticks = 4294967; // prevent underlying millis() overflow
|
|
|
|
setResolution(SECONDS);
|
|
|
|
start(ticks);
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
void CountDown::start(uint8_t days, uint16_t hours, uint32_t minutes)
|
|
|
|
{
|
|
|
|
uint32_t ticks = 86400UL * days + 3600UL * hours + 60UL * minutes;
|
|
|
|
if (ticks > 4294967) ticks = 4294967; // prevent underlying millis() overflow
|
|
|
|
setResolution(MINUTES);
|
|
|
|
start(ticks);
|
|
|
|
}
|
|
|
|
|
2015-10-29 09:28:29 -04:00
|
|
|
void CountDown::stop()
|
|
|
|
{
|
2017-07-16 05:46:43 -04:00
|
|
|
calcRemaining();
|
|
|
|
_state = CountDown::STOPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountDown::cont()
|
|
|
|
{
|
|
|
|
if (_state == CountDown::STOPPED)
|
|
|
|
{
|
|
|
|
start(_remaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CountDown::remaining()
|
|
|
|
{
|
|
|
|
calcRemaining();
|
2020-11-27 05:10:47 -05:00
|
|
|
if (_remaining == 0) _state = CountDown::STOPPED;
|
2017-07-16 05:46:43 -04:00
|
|
|
return _remaining;
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CountDown::calcRemaining()
|
|
|
|
{
|
2020-11-27 05:10:47 -05:00
|
|
|
uint32_t t = 0;
|
2017-07-16 05:46:43 -04:00
|
|
|
if (_state == CountDown::RUNNING)
|
|
|
|
{
|
2020-11-27 05:10:47 -05:00
|
|
|
switch(_res)
|
2015-10-29 09:28:29 -04:00
|
|
|
{
|
2020-11-27 05:10:47 -05:00
|
|
|
case MINUTES:
|
|
|
|
t = (millis() - _starttime) / 60000UL;
|
|
|
|
break;
|
|
|
|
case SECONDS:
|
|
|
|
t = (millis() - _starttime) / 1000UL;;
|
|
|
|
break;
|
|
|
|
case MICROS:
|
|
|
|
t = micros() - _starttime;
|
|
|
|
break;
|
|
|
|
case MILLIS:
|
|
|
|
default:
|
|
|
|
t = millis() - _starttime;
|
|
|
|
break;
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
2020-11-27 05:10:47 -05:00
|
|
|
_remaining = _ticks > t ? _ticks - t: 0;
|
|
|
|
return;
|
2017-07-16 05:46:43 -04:00
|
|
|
}
|
2020-11-27 05:10:47 -05:00
|
|
|
// do not change
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
2020-11-27 05:10:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|