Browse Source

Initial import

master
Maze Modderman-Lenstra 3 years ago
parent
commit
7f00deed4e
3 changed files with 167 additions and 5 deletions
  1. +17
    -4
      LICENSE
  2. +23
    -1
      README.md
  3. +127
    -0
      Timer.h

+ 17
- 4
LICENSE View File

@ -1,9 +1,22 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2017 Wijnand Modderman-Lenstra <maze@pyth0n.org>
Copyright (c) 2013 FastLED
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

+ 23
- 1
README.md View File

@ -1,3 +1,25 @@
# Timer
Periodic timers
Periodic timers for AVR/ESP8266/ESP32/etc.
## Compatibility
This is targeting AVR/ESP8266/ESP32/etc. but requires C++11 (ie `-std=c++11` for GCC).
## Usage
Example:
```cpp
#include <Timer.h>
void setup() {
Serial.begin(115200);
}
void loop() {
EVERY_N_SECONDS(1) {
Serial.println("... another second has passed");
}
}
```

+ 127
- 0
Timer.h View File

@ -0,0 +1,127 @@
#pragma once
#define TIMER_STATIC __attribute__ ((unused)) static inline
#define TIMER_STATIC_ALWAYS_INLINE __attribute__ ((always_inline)) static inline
TIMER_STATIC uint16_t div1024_32_16(uint32_t in32) {
uint16_t out16;
#if defined(__AVR__)
asm volatile (
" lsr %D[in] \n\t"
" ror %C[in] \n\t"
" ror %B[in] \n\t"
" lsr %D[in] \n\t"
" ror %C[in] \n\t"
" ror %B[in] \n\t"
" mov %B[out],%C[in] \n\t"
" mov %A[out],%B[in] \n\t"
: [in] "+r" (in32),
[out] "=r" (out16)
);
#elif defined(ESP8266)
asm volatile (
" lsr %D[in] \n\t"
" ror %C[in] \n\t"
" ror %B[in] \n\t"
" lsr %D[in] \n\t"
" ror %C[in] \n\t"
" ror %B[in] \n\t"
" mov %B[out],%C[in] \n\t"
" mov %A[out],%B[in] \n\t"
: [in] "+r" (in32),
[out] "=r" (out16)
);
#else
out16 = (in32 >> 10) & 0xffff;
#endif
return out16;
}
TIMER_STATIC uint32_t milliseconds() {
return millis();
}
TIMER_STATIC uint16_t seconds() {
uint32_t ms = millis();
uint16_t s = ms / 1000;
return s;
}
TIMER_STATIC uint16_t minutes() {
uint32_t ms = millis();
uint16_t m;
m = (ms / (60000L)) & 0xffff;
return m;
}
TIMER_STATIC uint8_t hours() {
uint32_t ms = millis();
uint8_t h;
h = (ms / (3600000L)) & 0xff;
return h;
}
template<typename timeType,timeType (*timeGetter)()>
class EveryTimePeriod {
public:
timeType previous;
timeType period;
EveryTimePeriod() {
reset();
period = 1;
}
EveryTimePeriod(timeType period) {
reset();
setPeriod(period);
}
void setPeriod(timeType newPeriod) {
period = newPeriod;
}
timeType getTime() {
return reinterpret_cast<timeType>(timeGetter());
}
timeType getPeriod() {
return period;
}
timeType getElapsed() {
return getTime() - previous;
}
timeType getRemaining() {
return period - getElapsed();
}
void reset() {
previous = getTime();
}
bool ready() {
bool passed = (getElapsed() >= period);
if (passed) {
reset();
}
return passed;
}
operator bool() {
return ready();
}
};
typedef EveryTimePeriod<uint32_t,milliseconds> EveryNMilliseconds;
typedef EveryTimePeriod<uint16_t,seconds> EveryNSeconds;
typedef EveryTimePeriod<uint16_t,minutes> EveryNMinutes;
typedef EveryTimePeriod<uint8_t,hours> EveryNHours;
#define CONCAT_HELPER(x, y) x##y
#define CONCAT_MACRO(x, y) CONCAT_HELPER(x, y)
#define EVERY_N_MS(PERIOD) EVERY_N_MS_IF(CONCAT_MACRO(PER, __COUNTER__), PERIOD)
#define EVERY_N_MS_IF(NAME, PERIOD) static EveryNMilliseconds NAME(PERIOD); if (NAME)
#define EVERY_N_SECONDS(PERIOD) EVERY_N_SECONDS_IF(CONCAT_MACRO(PER, __COUNTER__), PERIOD)
#define EVERY_N_SECONDS_IF(NAME, PERIOD) static EveryNSeconds NAME(PERIOD); if (NAME)

Loading…
Cancel
Save