2015-10-29 09:28:29 -04:00
|
|
|
//
|
|
|
|
// FILE: CountDown.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-01-11 05:04:32 -05:00
|
|
|
// VERSION: 0.3.0
|
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
|
|
|
//
|
2022-10-30 14:56:11 -04:00
|
|
|
// HISTORY: see changelog.md
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2015-10-29 09:28:29 -04:00
|
|
|
|
|
|
|
#include "CountDown.h"
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2015-10-29 09:28:29 -04:00
|
|
|
CountDown::CountDown(const enum Resolution res)
|
|
|
|
{
|
2023-01-11 05:04:32 -05:00
|
|
|
// _res = MILLIS; // set in setResolution
|
|
|
|
// _ticks = 0; // set in setResolution
|
|
|
|
_state = CountDown::STOPPED;
|
|
|
|
_remaining = 0;
|
|
|
|
_startTime = 0;
|
2017-07-16 05:46:43 -04:00
|
|
|
setResolution(res);
|
|
|
|
stop();
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2023-01-11 05:04:32 -05:00
|
|
|
char CountDown::getUnits()
|
|
|
|
{
|
|
|
|
return _res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
bool CountDown::start(uint32_t ticks)
|
2015-10-29 09:28:29 -04:00
|
|
|
{
|
2017-07-16 05:46:43 -04:00
|
|
|
_ticks = ticks;
|
2020-11-27 05:10:47 -05:00
|
|
|
_state = CountDown::RUNNING;
|
|
|
|
if (_res == MICROS)
|
|
|
|
{
|
2023-01-11 05:04:32 -05:00
|
|
|
_startTime = micros();
|
2020-11-27 05:10:47 -05:00
|
|
|
}
|
2023-01-11 05:04:32 -05:00
|
|
|
else
|
2020-11-27 05:10:47 -05:00
|
|
|
{
|
2023-01-11 05:04:32 -05:00
|
|
|
_startTime = millis();
|
2020-11-27 05:10:47 -05:00
|
|
|
}
|
2023-01-11 05:04:32 -05:00
|
|
|
return true; // can not overflow
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
bool CountDown::start(uint8_t days, uint16_t hours, uint32_t minutes, uint32_t seconds)
|
2015-10-29 09:28:29 -04:00
|
|
|
{
|
2023-01-11 05:04:32 -05:00
|
|
|
float _days = seconds / 86400.0 + minutes / 1440.0 + hours / 24.0 + days;
|
2021-01-29 06:31:58 -05:00
|
|
|
bool rv = (_days < 49.7102696);
|
|
|
|
|
2017-07-16 05:46:43 -04:00
|
|
|
uint32_t ticks = 86400UL * days + 3600UL * hours + 60UL * minutes + seconds;
|
2023-01-11 05:04:32 -05:00
|
|
|
// prevent underlying millis() overflow
|
|
|
|
// 4294967 = number of SECONDS in 2^32 milliseconds
|
|
|
|
if (ticks > 4294967) ticks = 4294967;
|
2017-07-16 05:46:43 -04:00
|
|
|
setResolution(SECONDS);
|
|
|
|
start(ticks);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
return rv;
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
bool CountDown::start(uint8_t days, uint16_t hours, uint32_t minutes)
|
2020-11-27 05:10:47 -05:00
|
|
|
{
|
2023-01-11 05:04:32 -05:00
|
|
|
float _days = minutes / 1440.0 + hours / 24.0 + days;
|
2021-01-29 06:31:58 -05:00
|
|
|
bool rv = (_days < 49.7102696);
|
|
|
|
|
2023-01-11 05:04:32 -05:00
|
|
|
uint32_t ticks = 1440UL * days + 60UL * hours + minutes;
|
|
|
|
// prevent underlying millis() overflow
|
|
|
|
// 71582 = number of MINUTES in 2^32 milliseconds
|
|
|
|
if (ticks > 71582) ticks = 71582;
|
2020-11-27 05:10:47 -05:00
|
|
|
setResolution(MINUTES);
|
|
|
|
start(ticks);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
return rv;
|
2020-11-27 05:10:47 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2015-10-29 09:28:29 -04:00
|
|
|
void CountDown::stop()
|
|
|
|
{
|
2017-07-16 05:46:43 -04:00
|
|
|
calcRemaining();
|
|
|
|
_state = CountDown::STOPPED;
|
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2017-07-16 05:46:43 -04:00
|
|
|
void CountDown::cont()
|
|
|
|
{
|
|
|
|
if (_state == CountDown::STOPPED)
|
|
|
|
{
|
|
|
|
start(_remaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2023-01-11 05:04:32 -05:00
|
|
|
uint32_t CountDown::remaining()
|
|
|
|
{
|
|
|
|
calcRemaining();
|
|
|
|
if (_remaining == 0) _state = CountDown::STOPPED;
|
|
|
|
return _remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CountDown::isRunning()
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
calcRemaining();
|
|
|
|
return (_state == CountDown::RUNNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 05:04:32 -05:00
|
|
|
bool CountDown::isStopped()
|
2017-07-16 05:46:43 -04:00
|
|
|
{
|
|
|
|
calcRemaining();
|
2023-01-11 05:04:32 -05:00
|
|
|
return (_state == CountDown::STOPPED);
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2023-01-11 05:04:32 -05:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PRIVATE
|
|
|
|
//
|
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:
|
2023-01-11 05:04:32 -05:00
|
|
|
t = (millis() - _startTime) / 60000UL;
|
2020-11-27 05:10:47 -05:00
|
|
|
break;
|
|
|
|
case SECONDS:
|
2023-01-11 05:04:32 -05:00
|
|
|
t = (millis() - _startTime) / 1000UL;;
|
2020-11-27 05:10:47 -05:00
|
|
|
break;
|
|
|
|
case MICROS:
|
2023-01-11 05:04:32 -05:00
|
|
|
t = micros() - _startTime;
|
2020-11-27 05:10:47 -05:00
|
|
|
break;
|
|
|
|
case MILLIS:
|
|
|
|
default:
|
2023-01-11 05:04:32 -05:00
|
|
|
t = millis() - _startTime;
|
2020-11-27 05:10:47 -05:00
|
|
|
break;
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
_remaining = _ticks > t ? _ticks - t : 0;
|
2023-01-11 05:04:32 -05:00
|
|
|
if (_remaining == 0)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_state = CountDown::STOPPED;
|
|
|
|
}
|
2020-11-27 05:10:47 -05:00
|
|
|
return;
|
2017-07-16 05:46:43 -04:00
|
|
|
}
|
2015-10-29 09:28:29 -04:00
|
|
|
}
|
2020-11-27 05:10:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
2023-01-11 05:04:32 -05:00
|
|
|
|