GY-63_MS5611/libraries/timing/timing.h

76 lines
1.9 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
2023-02-11 07:26:57 -05:00
//
2021-01-29 06:31:58 -05:00
// FILE: timing.h
// AUTHOR: Rob Tillaart
2023-02-11 07:26:57 -05:00
// VERSION: 0.2.5
// PURPOSE: Arduino library with wrapper classes for seconds millis micros
2021-01-29 06:31:58 -05:00
// URL: https://github.com/RobTillaart/timing
2023-02-11 07:26:57 -05:00
#define TIMING_LIB_VERSION (F("0.2.5"))
2021-05-28 07:47:03 -04:00
2021-01-29 06:31:58 -05:00
class microSeconds
{
public:
2023-02-11 07:26:57 -05:00
microSeconds() { set(0); };
uint32_t now() { return micros() - _offset; };
void set(uint32_t value = 0UL) { _offset = micros() - value; };
void add(uint32_t value) { _offset -= value; };
2021-01-29 06:31:58 -05:00
uint32_t getOffset() { return _offset; };
2023-02-11 07:26:57 -05:00
double toSeconds() { return (millis() - _offset) * 0.000001; };
2021-01-29 06:31:58 -05:00
private:
uint32_t _offset = 0UL;
};
class milliSeconds
{
public:
milliSeconds() { set(0); };
uint32_t now() { return millis() - _offset; };
2021-12-29 05:16:45 -05:00
void set(uint32_t value = 0UL) { _offset = millis() - value; };
2023-02-11 07:26:57 -05:00
void add(uint32_t value) { _offset -= value; };
2021-01-29 06:31:58 -05:00
uint32_t getOffset() { return _offset; };
2023-02-11 07:26:57 -05:00
double toSeconds() { return (millis() - _offset) * 0.001; };
2021-01-29 06:31:58 -05:00
private:
uint32_t _offset = 0UL;
};
class seconds
{
public:
2023-02-11 07:26:57 -05:00
seconds() { set(0); };
uint32_t now() { return millis() / 1000UL - _offset; };
void set(uint32_t value = 0UL) { _offset = millis() / 1000UL - value; };
void add(uint32_t value) { _offset -= value; };
2021-01-29 06:31:58 -05:00
uint32_t getOffset() { return _offset; };
2023-02-11 07:26:57 -05:00
// for completeness
double toSeconds() { return millis() * 0.001 - _offset; };
// experimental
char * toClock()
{
static char buf[12];
uint32_t _now = now();
int hh = _now / 3600;
_now = _now - hh * 3600;
int mm = _now / 60;
int ss = _now - mm * 60;
// ESP32 warning breaks Arduino build. next line.
sprintf(buf, "%02d:%02d:%02d", hh, mm, ss);
return buf;
}
2021-01-29 06:31:58 -05:00
private:
2023-02-11 07:26:57 -05:00
uint32_t _offset = 0UL;
2021-01-29 06:31:58 -05:00
};
2021-05-28 07:47:03 -04:00
2023-02-11 07:26:57 -05:00
// -- END OF FILE --
2021-12-29 05:16:45 -05:00