Skip to content

Commit

Permalink
fix #14 add restart (#15)
Browse files Browse the repository at this point in the history
- fix #14 add **restart()** function.
- add demo for restart **countdown_restart.ino**
  • Loading branch information
RobTillaart authored Mar 14, 2023
1 parent 11ba72b commit 69eedf0
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 35 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.1] - 2023-03-14
- fix #14 add **restart()** function.
- add demo for restart **countdown_restart.ino**


## [0.3.0] - 2023-01-10
- fix #12 MINUTES bug
- label enum resolution to be printable u, m, s, M.
Expand Down
10 changes: 7 additions & 3 deletions CountDown.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//
// FILE: CountDown.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// VERSION: 0.3.1
// PURPOSE: CountDown library for Arduino
// URL: https://github.com/RobTillaart/CountDown
//
// HISTORY: see changelog.md


#include "CountDown.h"
Expand Down Expand Up @@ -100,6 +98,12 @@ void CountDown::cont()
}


void CountDown::restart()
{
start(_ticks);
}


uint32_t CountDown::remaining()
{
calcRemaining();
Expand Down
53 changes: 27 additions & 26 deletions CountDown.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,52 @@
//
// FILE: CountDown.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// VERSION: 0.3.1
// PURPOSE: CountDown library for Arduino
// URL: https://github.com/RobTillaart/CountDown


#include "Arduino.h"

#define COUNTDOWN_LIB_VERSION (F("0.3.0"))
#define COUNTDOWN_LIB_VERSION (F("0.3.1"))


class CountDown
{
public:
enum Resolution { MICROS = 'u', MILLIS = 'm', SECONDS = 's', MINUTES = 'M' };
enum Resolution { MICROS = 'u', MILLIS = 'm', SECONDS = 's', MINUTES = 'M' };

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

void setResolution(const enum Resolution res = MILLIS);
enum Resolution resolution() { return _res; };
char getUnits();
void setResolution(const enum Resolution res = MILLIS);
enum Resolution resolution() { return _res; };
char getUnits();

// one need to set the resolution before calling start(ticks).
bool start(uint32_t ticks);
// Implicit set resolution to SECONDS.
bool start(uint8_t days, uint16_t hours, uint32_t minutes, uint32_t seconds);
// Implicit set resolution to MINUTES.
bool start(uint8_t days, uint16_t hours, uint32_t minutes);
// one need to set the resolution before calling start(ticks).
bool start(uint32_t ticks);
// Implicit set resolution to SECONDS.
bool start(uint8_t days, uint16_t hours, uint32_t minutes, uint32_t seconds);
// Implicit set resolution to MINUTES.
bool start(uint8_t days, uint16_t hours, uint32_t minutes);

void stop();
void cont();
void stop();
void cont();
void restart();

uint32_t remaining();
bool isRunning();
bool isStopped();
uint32_t remaining();
bool isRunning();
bool isStopped();


private:
enum State { RUNNING, STOPPED };

uint32_t _ticks;
uint32_t _remaining;
enum State _state;
enum Resolution _res;
uint32_t _startTime;
void calcRemaining();
enum State { RUNNING, STOPPED };

uint32_t _ticks;
uint32_t _remaining;
enum State _state;
enum Resolution _res;
uint32_t _startTime;
void calcRemaining();
};


Expand Down
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ Note the countdown object is as accurate as the underlying **millis()** or **mic
Interrupts etc might cause deviations.


#### Links

Relates to - https://github.com/RobTillaart/StopWatch_RT


## Interface

```cpp
#include "CountDown.h"
```


The main functions of the CountDown clock are:

- **CountDown(const enum Resolution res = MILLIS)** constructor, with default resolution of milliseconds.
Expand All @@ -46,6 +56,8 @@ Note that **remaining()** will report in MINUTES.
- **void stop()** stop the count down.
- **void cont()** resumes / continue the count down.
*(note continue is a C-Keyword)*
- **void restart()** restart the CountDown with the same resolution and ticks as before.
resets the \_ticks and starts again.
- **uint32_t remaining()** returns the remaining ticks in current resolution.
- **bool isRunning()** idem.
- **bool isStopped()** idem.
Expand Down Expand Up @@ -121,6 +133,11 @@ One can call **start(...)** at any time to reset the running clock to a new valu
This allows to implement a sort of watchdog clock in which e.g.
the user must press a button at least once per minute to show he is still awake.

Since version 0.3.1 the library supports **restart()** to start the countdown with
the last used parameters of **start()**. The user does not need to remember the
number of ticks or hours + minutes + seconds any more. Much easier tom implement
a repeating (timed) function or a watchdog. See examples.


## Future

Expand All @@ -133,14 +150,13 @@ the user must press a button at least once per minute to show he is still awake.

#### could

- does **restart()** need to return some information? what?
- add examples
- visualisations - hexadecimal - alphabetical (radix 26)
- depends on sensor
- (semi)watchdog()
- add resolution::HOURS + **start(days, hours)**
- extend adaptive display example
- add call-back function when **0** is reached
- example
- or default 00 minutes?


#### wont (unless)
Expand All @@ -158,4 +174,6 @@ the user must press a button at least once per minute to show he is still awake.
but drift / interrupts would make it fail in practice.
- countdown with a big number e.g. billions/ second ==> national deficit counter.
- not time triggered (is just a big variable)

- printable interface (stopwatch_rt)
- add call-back function when **0** is reached
- cannot be guaranteed as interface polls.
41 changes: 41 additions & 0 deletions examples/countdown_restart/countdown_restart.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// FILE: countdown_restart.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo restart
// URL: https://github.com/RobTillaart/CountDown


#include "CountDown.h"

CountDown cdt; // default millis

uint8_t lines = 0;


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("COUNTDOWN_LIB_VERSION: ");
Serial.println(COUNTDOWN_LIB_VERSION);

cdt.start(10000UL);
}


void loop()
{
// time for a new sample
if (cdt.remaining() == 0)
{
// restart countDownTimer same values
cdt.restart();
// make a sample and print it.
Serial.print(millis());
Serial.print("\t");
Serial.println(analogRead(A0));
}
}


// -- END OF FILE --
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ getUnits KEYWORD2
start KEYWORD2
stop KEYWORD2
cont KEYWORD2
restart KEYWORD2

remaining KEYWORD2
isRunning KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/CountDown.git"
},
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=CountDown
version=0.3.0
version=0.3.1
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library to implement a CountDown clock in SW.
Expand Down

0 comments on commit 69eedf0

Please sign in to comment.