GY-63_MS5611/libraries/Stopwatch_RT/StopWatch.h

71 lines
1.8 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: StopWatch.h
// AUTHOR: Rob Tillaart
2023-01-11 14:05:21 -05:00
// VERSION: 0.3.4
2021-01-29 06:31:58 -05:00
// PURPOSE: Arduino Library implementing a stopwatch including seconds, milliseconds microseconds
// HISTORY: See StopWatch.cpp
// URL: https://github.com/RobTillaart/StopWatch_RT
// http://playground.arduino.cc/Code/StopWatchClass
#include "Arduino.h"
2023-01-11 14:05:21 -05:00
#define STOPWATCH_LIB_VERSION (F("0.3.4"))
2021-01-29 06:31:58 -05:00
2022-11-25 14:09:48 -05:00
// NOTE: adjust divider can fix timing inaccuracies (to some extend)
2021-01-29 06:31:58 -05:00
#ifndef STOPWATCH_SECONDS_DIVIDER
2021-12-28 11:04:43 -05:00
#define STOPWATCH_SECONDS_DIVIDER 1000
2021-01-29 06:31:58 -05:00
#endif
#ifndef STOPWATCH_MINUTES_DIVIDER
2021-12-28 11:04:43 -05:00
#define STOPWATCH_MINUTES_DIVIDER 60000
2021-01-29 06:31:58 -05:00
#endif
class StopWatch: public Printable
{
public:
enum State { RESET, RUNNING, STOPPED };
2023-01-11 14:05:21 -05:00
enum Resolution { MICROS = 'u', MILLIS = 'm', SECONDS = 's', MINUTES = 'M' };
2021-01-29 06:31:58 -05:00
2021-12-28 11:04:43 -05:00
explicit StopWatch(const enum Resolution resolution = MILLIS);
2021-01-29 06:31:58 -05:00
void start();
void stop();
void reset();
2022-11-25 14:09:48 -05:00
// elapsed() made const as printTo expect a const object.
uint32_t elapsed() const;
2023-01-11 14:05:21 -05:00
char getUnits(); // returns u, s, m , M
2021-01-29 06:31:58 -05:00
2022-11-25 14:09:48 -05:00
bool isRunning();
2023-01-11 14:05:21 -05:00
bool isStopped();
bool isReset();
2021-01-29 06:31:58 -05:00
enum State state() { return _state; };
2021-12-28 11:04:43 -05:00
void setResolution(const enum Resolution resolution);
enum Resolution resolution() { return _resolution; };
2021-01-29 06:31:58 -05:00
2022-11-25 14:09:48 -05:00
// PRINTABLE
2021-01-29 06:31:58 -05:00
size_t printTo(Print& p) const;
private:
2021-12-28 11:04:43 -05:00
enum State _state;
enum Resolution _resolution;
2021-01-29 06:31:58 -05:00
2023-01-11 14:05:21 -05:00
uint32_t _startTime;
uint32_t _stopTime;
2021-01-29 06:31:58 -05:00
uint32_t (*_gettime)(void);
2022-11-25 14:09:48 -05:00
static uint32_t _micros() { return micros(); }; // wrapper for ESP proc.
2021-01-29 06:31:58 -05:00
static uint32_t _millis() { return millis(); };
static uint32_t seconds() { return millis() / STOPWATCH_SECONDS_DIVIDER; };
static uint32_t minutes() { return millis() / STOPWATCH_MINUTES_DIVIDER; };
};
2021-12-28 11:04:43 -05:00
2022-11-25 14:09:48 -05:00
// -- END OF FILE --
2021-12-28 11:04:43 -05:00