Skip to content

Commit 4b9eedc

Browse files
authored
Fix #12 MINUTES (#13)
* update license to 2023 * update GitHub actions * fix #12 MINUTES * fix initialization * update readme
1 parent 172ca07 commit 4b9eedc

File tree

14 files changed

+266
-65
lines changed

14 files changed

+266
-65
lines changed

.github/workflows/arduino-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
lint:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v2
9+
- uses: actions/checkout@v3
1010
- uses: arduino/arduino-lint-action@v1
1111
with:
1212
library-manager: update

.github/workflows/arduino_test_runner.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99

1010
steps:
11-
- uses: actions/checkout@v2
11+
- uses: actions/checkout@v3
1212
- uses: ruby/setup-ruby@v1
1313
with:
1414
ruby-version: 2.6

.github/workflows/jsoncheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
test:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v2
13+
- uses: actions/checkout@v3
1414
- name: json-syntax-check
1515
uses: limitusus/json-syntax-check@v1
1616
with:

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.0] - 2023-01-10
10+
- fix #12 MINUTES bug
11+
- label enum resolution to be printable u, m, s, M.
12+
- add getUnits()
13+
- add isStopped() for convenience.
14+
- fix initialization of private variables.
15+
- update GitHub actions
16+
- update license
17+
- update readme.md
18+
- update unit tests
19+
20+
----
21+
922
## [0.2.7] - 2022-10-30
1023
- add changelog.md
1124
- add rp2040 to build-CI
1225
- minor edit unit test
1326

14-
1527
## [0.2.6] - 2021-12-14
1628
- update library.json, license
1729

CountDown.cpp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: CountDown.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.7
4+
// VERSION: 0.3.0
55
// PURPOSE: CountDown library for Arduino
66
// URL: https://github.com/RobTillaart/CountDown
77
//
@@ -13,6 +13,11 @@
1313

1414
CountDown::CountDown(const enum Resolution res)
1515
{
16+
// _res = MILLIS; // set in setResolution
17+
// _ticks = 0; // set in setResolution
18+
_state = CountDown::STOPPED;
19+
_remaining = 0;
20+
_startTime = 0;
1621
setResolution(res);
1722
stop();
1823
}
@@ -25,29 +30,37 @@ void CountDown::setResolution(const enum Resolution res)
2530
}
2631

2732

33+
char CountDown::getUnits()
34+
{
35+
return _res;
36+
}
37+
38+
2839
bool CountDown::start(uint32_t ticks)
2940
{
3041
_ticks = ticks;
3142
_state = CountDown::RUNNING;
3243
if (_res == MICROS)
3344
{
34-
_starttime = micros();
45+
_startTime = micros();
3546
}
36-
else
47+
else
3748
{
38-
_starttime = millis();
49+
_startTime = millis();
3950
}
40-
return true; // can not overflow
51+
return true; // can not overflow
4152
}
4253

4354

4455
bool CountDown::start(uint8_t days, uint16_t hours, uint32_t minutes, uint32_t seconds)
4556
{
46-
float _days = seconds / 86400.0 + minutes / 1440.0 + hours / 24.0 + days;
57+
float _days = seconds / 86400.0 + minutes / 1440.0 + hours / 24.0 + days;
4758
bool rv = (_days < 49.7102696);
4859

4960
uint32_t ticks = 86400UL * days + 3600UL * hours + 60UL * minutes + seconds;
50-
if (ticks > 4294967) ticks = 4294967; // prevent underlying millis() overflow
61+
// prevent underlying millis() overflow
62+
// 4294967 = number of SECONDS in 2^32 milliseconds
63+
if (ticks > 4294967) ticks = 4294967;
5164
setResolution(SECONDS);
5265
start(ticks);
5366

@@ -57,11 +70,13 @@ bool CountDown::start(uint8_t days, uint16_t hours, uint32_t minutes, uint32_t s
5770

5871
bool CountDown::start(uint8_t days, uint16_t hours, uint32_t minutes)
5972
{
60-
float _days = minutes / 1440.0 + hours / 24.0 + days;
73+
float _days = minutes / 1440.0 + hours / 24.0 + days;
6174
bool rv = (_days < 49.7102696);
6275

63-
uint32_t ticks = 86400UL * days + 3600UL * hours + 60UL * minutes;
64-
if (ticks > 4294967) ticks = 4294967; // prevent underlying millis() overflow
76+
uint32_t ticks = 1440UL * days + 60UL * hours + minutes;
77+
// prevent underlying millis() overflow
78+
// 71582 = number of MINUTES in 2^32 milliseconds
79+
if (ticks > 71582) ticks = 71582;
6580
setResolution(MINUTES);
6681
start(ticks);
6782

@@ -85,21 +100,32 @@ void CountDown::cont()
85100
}
86101

87102

88-
bool CountDown::isRunning()
103+
uint32_t CountDown::remaining()
104+
{
105+
calcRemaining();
106+
if (_remaining == 0) _state = CountDown::STOPPED;
107+
return _remaining;
108+
}
109+
110+
111+
bool CountDown::isRunning()
89112
{
90113
calcRemaining();
91114
return (_state == CountDown::RUNNING);
92115
}
93116

94117

95-
uint32_t CountDown::remaining()
118+
bool CountDown::isStopped()
96119
{
97120
calcRemaining();
98-
if (_remaining == 0) _state = CountDown::STOPPED;
99-
return _remaining;
121+
return (_state == CountDown::STOPPED);
100122
}
101123

102124

125+
//////////////////////////////////////////////////
126+
//
127+
// PRIVATE
128+
//
103129
void CountDown::calcRemaining()
104130
{
105131
uint32_t t = 0;
@@ -108,28 +134,28 @@ void CountDown::calcRemaining()
108134
switch(_res)
109135
{
110136
case MINUTES:
111-
t = (millis() - _starttime) / 60000UL;
137+
t = (millis() - _startTime) / 60000UL;
112138
break;
113139
case SECONDS:
114-
t = (millis() - _starttime) / 1000UL;;
140+
t = (millis() - _startTime) / 1000UL;;
115141
break;
116142
case MICROS:
117-
t = micros() - _starttime;
143+
t = micros() - _startTime;
118144
break;
119145
case MILLIS:
120146
default:
121-
t = millis() - _starttime;
147+
t = millis() - _startTime;
122148
break;
123149
}
124150
_remaining = _ticks > t ? _ticks - t : 0;
125-
if (_remaining == 0)
151+
if (_remaining == 0)
126152
{
127153
_state = CountDown::STOPPED;
128154
}
129155
return;
130156
}
131-
// do not change
132157
}
133158

134159

135160
// -- END OF FILE --
161+

CountDown.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,41 @@
22
//
33
// FILE: CountDown.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.7
5+
// VERSION: 0.3.0
66
// PURPOSE: CountDown library for Arduino
77
// URL: https://github.com/RobTillaart/CountDown
8-
//
98

109

1110
#include "Arduino.h"
1211

13-
#define COUNTDOWN_LIB_VERSION (F("0.2.7"))
12+
#define COUNTDOWN_LIB_VERSION (F("0.3.0"))
1413

1514

1615
class CountDown
1716
{
1817
public:
19-
enum Resolution { MILLIS, MICROS, SECONDS, MINUTES };
18+
enum Resolution { MICROS = 'u', MILLIS = 'm', SECONDS = 's', MINUTES = 'M' };
2019

2120
explicit CountDown(const enum Resolution res = MILLIS);
2221

2322
void setResolution(const enum Resolution res = MILLIS);
23+
enum Resolution resolution() { return _res; };
24+
char getUnits();
2425

26+
// one need to set the resolution before calling start(ticks).
2527
bool start(uint32_t ticks);
26-
// Implicit set resolution to SECONDS.
28+
// Implicit set resolution to SECONDS.
2729
bool start(uint8_t days, uint16_t hours, uint32_t minutes, uint32_t seconds);
28-
// Implicit set resolution to MINUTES.
30+
// Implicit set resolution to MINUTES.
2931
bool start(uint8_t days, uint16_t hours, uint32_t minutes);
3032

3133
void stop();
3234
void cont();
3335

3436
uint32_t remaining();
3537
bool isRunning();
36-
enum Resolution resolution() const { return _res; };
38+
bool isStopped();
39+
3740

3841
private:
3942
enum State { RUNNING, STOPPED };
@@ -42,9 +45,10 @@ class CountDown
4245
uint32_t _remaining;
4346
enum State _state;
4447
enum Resolution _res;
45-
uint32_t _starttime;
48+
uint32_t _startTime;
4649
void calcRemaining();
4750
};
4851

4952

50-
// -- END OF FILE --
53+
// -- END OF FILE --
54+

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2015-2022 Rob Tillaart
3+
Copyright (c) 2015-2023 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)