GY-63_MS5611/libraries/CountDown/README.md

162 lines
7.0 KiB
Markdown
Raw Normal View History

2021-01-29 06:31:58 -05:00
[![Arduino CI](https://github.com/RobTillaart/CountDown/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
2021-10-19 15:40:11 -04:00
[![Arduino-lint](https://github.com/RobTillaart/CountDown/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/CountDown/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/CountDown/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/CountDown/actions/workflows/jsoncheck.yml)
2021-01-29 06:31:58 -05:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/CountDown/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/CountDown.svg?maxAge=3600)](https://github.com/RobTillaart/CountDown/releases)
2020-11-27 05:10:47 -05:00
# CountDown
Arduino Library to implement a CountDown clock (in SW polling, no HW timer).
2021-01-29 06:31:58 -05:00
2020-11-27 05:10:47 -05:00
## Description
2021-01-29 06:31:58 -05:00
The countdown library is a clock that counts down from a given time to zero.
It does not call a function or so as the user is responsible to check the time remaining.
2023-01-11 05:04:32 -05:00
Typically one checks the remaining time at least once in every **loop()**.
2021-01-29 06:31:58 -05:00
2022-10-30 14:56:11 -04:00
Under the hood the function uses **micros()** or **millis()** which results in a maximum time
of 4294 seconds in micros (1h 10m) or about 49+ days when using milliseconds.
2021-01-29 06:31:58 -05:00
2022-10-30 14:56:11 -04:00
For longer periods one could cascade countDown objects, so when one is finished the next one starts.
Note the countdown object is as accurate as the underlying **millis()** or **micros()**.
Interrupts etc might cause deviations.
2021-01-29 06:31:58 -05:00
## Interface
2020-11-27 05:10:47 -05:00
The main functions of the CountDown clock are:
2022-10-30 14:56:11 -04:00
- **CountDown(const enum Resolution res = MILLIS)** constructor, with default resolution of milliseconds.
2023-01-11 05:04:32 -05:00
- **void setResolution(const enum Resolution res = MILLIS)** set the resolution,
default to MILLIS.
- **enum Resolution resolution()** return the current resolution (integer).
- **char getUnits()** return the current resolution as printable char (u,m,s,M)
2022-10-30 14:56:11 -04:00
- **bool start(uint32_t ticks)** (re)start in current resolution.
2023-01-11 05:04:32 -05:00
Typical used for MILLIS and MICROS which must be set manually.
2022-10-30 14:56:11 -04:00
- **bool start(uint8_t days, uint16_t hours, uint32_t minutes, uint32_t seconds)** Implicit set resolution to SECONDS.
2023-01-11 05:04:32 -05:00
Returns false if total exceeds 2^32 milliseconds ~49 days.
Note that **remaining()** will report in SECONDS.
2022-10-30 14:56:11 -04:00
- **bool start(uint8_t days, uint16_t hours, uint32_t minutes)** Implicit set resolution to MINUTES.
2023-01-11 05:04:32 -05:00
Returns false if total exceeds 2^32 milliseconds ~49 days.
Note that **remaining()** will report in MINUTES.
- **void stop()** stop the count down.
- **void cont()** resumes / continue the count down.
2022-10-30 14:56:11 -04:00
*(note continue is a C-Keyword)*
- **uint32_t remaining()** returns the remaining ticks in current resolution.
- **bool isRunning()** idem.
2023-01-11 05:04:32 -05:00
- **bool isStopped()** idem.
2020-11-27 05:10:47 -05:00
These functions work straightforward.
2021-01-29 06:31:58 -05:00
## Operation
2023-01-11 05:04:32 -05:00
The function **start(days, hours, minutes, seconds)** allows all combinations
as long as the total time may not exceed 2^32 milliseconds.
The function will return false if it exceeds this (rounded) maximum.
Example calls are:
2022-10-30 14:56:11 -04:00
- four hundred minutes **start(0, 0, 400, 0)**
- a million seconds **start(0, 0, 0, 1000000)**
2023-01-11 05:04:32 -05:00
- an unusual mix **start(0, 0, 400, 1000)** as parameter.
Note: the resolution is implicitly set to **CountDown::SECONDS**.
2020-11-27 05:10:47 -05:00
2023-01-11 05:04:32 -05:00
Since 0.2.4 all **start()** functions will check if the parameters cause an overflow
2022-10-30 14:56:11 -04:00
in the underlying math.
2023-01-11 05:04:32 -05:00
- If there is no overflow, **start()** returns true.
- If there is an overflow, **start()** returns false.
2020-11-27 05:10:47 -05:00
2022-10-30 14:56:11 -04:00
Total amount of time to countdown for **CountDown::MICROS** may not exceed 2\^32 micros
which equals about 1 hour and 10 minutes.
Total amount of time to countdown for **CountDown::MILLIS**, **CountDown::SECONDS**
and **CountDown::MINUTES** may not exceed 2\^32 milliseconds, about 49 days.
2021-01-29 06:31:58 -05:00
The function **start(days, hours, minutes)** is new since 0.2.2.
2022-10-30 14:56:11 -04:00
It also uses **millis()** under the hood.
The resolution is implicitly set to **CountDown::MINUTES**.
2021-01-29 06:31:58 -05:00
2022-10-30 14:56:11 -04:00
| Call to start() | resolution | max time | comments |
|:-------------------------------------|:-----------------|:--------:|:---------|
| start(days, hours, minutes, seconds) | SECONDS = millis | 49+ days | |
| start(days, hours, minutes) | MINUTES = millis | 49+ days | |
| start(ticks) | MILLIS = millis | 49+ days | default |
| start(ticks) | MICROS = micros | ~70 min | setResolution(CountDown::MICROS) |
| start(ticks) | SECONDS = millis | 49+ days | setResolution(CountDown::SECONDS) |
| start(ticks) | MINUTES = millis | 49+ days | setResolution(CountDown::MINUTES) |
2021-01-29 06:31:58 -05:00
2020-11-27 05:10:47 -05:00
The Countdown clock uses by default **millis()** to do the time keeping,
2022-10-30 14:56:11 -04:00
although this can be changed runtime by **setResolution(res)**.
The parameter **res** can be:
2023-01-11 05:04:32 -05:00
| unit | uses | getUnits() | Notes |
|:---------------------|:-----------|:------------:|:--------|
| CountDown::MICROS | micros() | u |
| CountDown::MILLIS | millis() | m | default
| CountDown::SECONDS | millis() | s |
| CountDown::MINUTES | millis() | M |
2020-11-27 05:10:47 -05:00
Although possible one should not change the resolution of the CountDown
clock while it is running as you mix up different timescales.
2023-01-11 05:04:32 -05:00
The user should handle this by selecting the smallest resolution needed.
Alternative one can get the remaining units, stop the countdown, and start
with another resolution and converted time.
This will probably lead to rounding errors i the total countdown time.
See example **countdown_adaptive_display.ino**
Finally the user has to check **remaining()** as frequent as needed to meet
the accuracy. E.g checking once a minute while doing milliseconds makes only sense
if the number of milliseconds is still very large. Think of an adaptive strategy.
2020-11-27 05:10:47 -05:00
2022-10-30 14:56:11 -04:00
#### Watchdog
One can call **start(...)** at any time to reset the running clock to a new value.
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.
2020-11-27 05:10:47 -05:00
2021-10-19 15:40:11 -04:00
## Future
2022-10-30 14:56:11 -04:00
#### must
2023-01-11 05:04:32 -05:00
2022-10-30 14:56:11 -04:00
- documentation
#### should
#### could
2023-01-11 05:04:32 -05:00
- add examples
2022-10-30 14:56:11 -04:00
- visualisations - hexadecimal - alphabetical (radix 26)
- depends on sensor
2023-01-11 05:04:32 -05:00
- (semi)watchdog()
- add resolution::HOURS + **start(days, hours)**
- extend adaptive display example
- add call-back function when **0** is reached
- example
2022-10-30 14:56:11 -04:00
2023-01-11 05:04:32 -05:00
#### wont (unless)
2021-10-19 15:40:11 -04:00
2023-01-11 05:04:32 -05:00
- if counting MINUTES and reaching 1 MINUTES and automatic
change to SECONDS could be NICE
- easy implementable by user, by starting in seconds
and handle the display oneself.
- incorporate a real time clock
- or EEPROM to be reboot proof? cannot be guaranteed.
- Countdown based upon external pulses.
- pulse counter class
- uint64_t version ==> **CountDown64** class?
- would be useful for micros() in theory
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)