Skip to content

Instantly share code, notes, and snippets.

@bhagman
Last active June 19, 2019 05:12
Show Gist options
  • Save bhagman/5c02eca43b0d7489f7493b1ce3adc1a1 to your computer and use it in GitHub Desktop.
Save bhagman/5c02eca43b0d7489f7493b1ce3adc1a1 to your computer and use it in GitHub Desktop.
/*
|| @author Brett Hagman <[email protected]>
|| @url http://wiring.org.co/
|| @contribution Hernando Barragan <[email protected]>
|| @contribution Alexander Brevig <[email protected]>
||
|| @description
|| | Time methods.
|| |
|| | Wiring Framework
|| | Common
|| | Global Methods
|| #
||
|| @example
|| | void setup()
|| | {
|| | pinMode(WLED, OUTPUT);
|| | }
|| |
|| | void loop()
|| | {
|| | static boolean ledstate = LOW;
|| |
|| | every(1000)
|| | {
|| | digitalWrite(WLED, ledstate);
|| | ledstate = !ledstate;
|| | }
|| | }
|| |
|| #
||
|| @license Please see cores/Common/License.txt.
||
|| @name Time Methods
|| @type Core Framework Header
|| @target Common (Target Independent)
*/
#ifndef WTIME_H
#define WTIME_H
// returns difference
//#define millisDiff(VAR) (millis() - VAR)
static inline uint32_t since(uint32_t lastTime)
{
return millis() - lastTime;
}
static inline bool hasPassed(uint32_t lastTime, const uint32_t expiryTime)
{
return ((millis() - lastTime) >= expiryTime);
}
//static inline void startAgain(uint32_t &lastTime)
//{
// lastTime = millis();
// return true;
//}
#define PPMASH(x,y) x ## y
#define PPCONCAT(x,y) PPMASH(x,y)
#define every(N) \
static uint32_t PPCONCAT(lastTime,__LINE__);\
for (; hasPassed(PPCONCAT(lastTime,__LINE__), N) && (PPCONCAT(lastTime,__LINE__) = millis(), true);)
#define forMillis(N) \
for (uint32_t start = millis(); millis() - start < N;)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment