mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.4 refactoring
This commit is contained in:
parent
40812a0d93
commit
46fa36b1ae
@ -1,83 +1,77 @@
|
||||
//
|
||||
//
|
||||
// FILE: StopWatch.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.03
|
||||
// VERSION: 0.1.4
|
||||
// PURPOSE: Simple StopWatch library for Arduino
|
||||
//
|
||||
// The library is based upon millis() and therefore
|
||||
// has the same restrictions as millis() has wrt overflow.
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.00 - 2011-01-04 initial version
|
||||
// 0.1.01 - 2011-01-04 Added better state
|
||||
// 0.1.02 - 2011-06-15 Added state() + #defines + lib version
|
||||
// 0.1.03 - 2012-01-22 Added several improvements
|
||||
// HISTORY:
|
||||
// 0.1.00 2011-01-04 initial version
|
||||
// 0.1.01 2011-01-04 Added better state
|
||||
// 0.1.02 2011-06-15 Added state() + #defines + lib version
|
||||
// 0.1.03 2012-01-22 Added several improvements
|
||||
// By mromani & Rob Tillaart
|
||||
//
|
||||
// 0.1.4 2017-07-16 refactored
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include "StopWatch.h"
|
||||
|
||||
StopWatch::StopWatch(enum Resolution res)
|
||||
StopWatch::StopWatch(const enum Resolution res)
|
||||
{
|
||||
_res = res;
|
||||
switch(_res) {
|
||||
case MICROS:
|
||||
_gettime = micros;
|
||||
break;
|
||||
case MILLIS:
|
||||
_gettime = millis;
|
||||
break;
|
||||
case SECONDS:
|
||||
_gettime = seconds;
|
||||
break;
|
||||
default:
|
||||
_gettime = millis;
|
||||
break;
|
||||
}
|
||||
reset();
|
||||
_res = res;
|
||||
switch(_res) {
|
||||
case MICROS:
|
||||
_gettime = micros;
|
||||
break;
|
||||
case MILLIS:
|
||||
_gettime = millis;
|
||||
break;
|
||||
case SECONDS:
|
||||
_gettime = seconds;
|
||||
break;
|
||||
default:
|
||||
_gettime = millis;
|
||||
break;
|
||||
}
|
||||
reset();
|
||||
}
|
||||
|
||||
void StopWatch::reset()
|
||||
{
|
||||
_state = StopWatch::RESET;
|
||||
_starttime = _stoptime = 0;
|
||||
_state = StopWatch::RESET;
|
||||
_starttime = _stoptime = 0;
|
||||
}
|
||||
|
||||
void StopWatch::start()
|
||||
{
|
||||
if (_state == StopWatch::RESET || _state == StopWatch::STOPPED)
|
||||
{
|
||||
_state = StopWatch::RUNNING;
|
||||
unsigned long t = _gettime();
|
||||
_starttime += t - _stoptime;
|
||||
_stoptime = t;
|
||||
}
|
||||
if (_state == StopWatch::RESET || _state == StopWatch::STOPPED)
|
||||
{
|
||||
_state = StopWatch::RUNNING;
|
||||
uint32_t t = _gettime();
|
||||
_starttime += t - _stoptime;
|
||||
_stoptime = t;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long StopWatch::value()
|
||||
uint32_t StopWatch::value()
|
||||
{
|
||||
if (_state == StopWatch::RUNNING) _stoptime = _gettime();
|
||||
return _stoptime - _starttime;
|
||||
if (_state == StopWatch::RUNNING)
|
||||
{
|
||||
_stoptime = _gettime();
|
||||
}
|
||||
return _stoptime - _starttime;
|
||||
}
|
||||
|
||||
void StopWatch::stop()
|
||||
{
|
||||
if (_state == StopWatch::RUNNING)
|
||||
{
|
||||
_state = StopWatch::STOPPED;
|
||||
_stoptime = _gettime();
|
||||
}
|
||||
}
|
||||
|
||||
bool StopWatch::isRunning()
|
||||
{
|
||||
return (_state == StopWatch::RUNNING);
|
||||
}
|
||||
|
||||
enum StopWatch::State StopWatch::state()
|
||||
{
|
||||
return _state;
|
||||
if (_state == StopWatch::RUNNING)
|
||||
{
|
||||
_stoptime = _gettime();
|
||||
_state = StopWatch::STOPPED;
|
||||
}
|
||||
}
|
||||
// END OF FILE
|
@ -1,6 +1,6 @@
|
||||
#ifndef StopWatch_h
|
||||
#define StopWatch_h
|
||||
//
|
||||
//
|
||||
// FILE: StopWatch.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: Simple StopWatch library for Arduino
|
||||
@ -10,36 +10,41 @@
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#define STOPWATCH_LIB_VERSION "0.1.03"
|
||||
#define STOPWATCH_LIB_VERSION "0.1.4"
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
class StopWatch
|
||||
class StopWatch
|
||||
{
|
||||
public:
|
||||
enum State { RESET, RUNNING, STOPPED };
|
||||
enum Resolution { MILLIS, MICROS, SECONDS };
|
||||
StopWatch(enum Resolution res = MILLIS);
|
||||
void start();
|
||||
void stop();
|
||||
void reset();
|
||||
unsigned long value();
|
||||
unsigned long elapsed() { return value(); };
|
||||
bool isRunning();
|
||||
enum State state();
|
||||
enum Resolution resolution() { return _res; };
|
||||
enum State { RESET, RUNNING, STOPPED };
|
||||
enum Resolution { MILLIS, MICROS, SECONDS };
|
||||
|
||||
explicit StopWatch(const enum Resolution res = MILLIS);
|
||||
void start();
|
||||
void stop();
|
||||
void reset();
|
||||
|
||||
uint32_t value();
|
||||
uint32_t elapsed() const { return value(); };
|
||||
|
||||
bool isRunning() const { return _state == StopWatch::RUNNING; };
|
||||
enum State state() const { return _state; };
|
||||
enum Resolution resolution() const { return _res; };
|
||||
|
||||
private:
|
||||
enum State _state;
|
||||
enum Resolution _res;
|
||||
unsigned long _starttime;
|
||||
unsigned long _stoptime;
|
||||
unsigned long (*_gettime)(void);
|
||||
static unsigned long seconds() { return millis()/1000; };
|
||||
enum State _state;
|
||||
enum Resolution _res;
|
||||
|
||||
uint32_t _starttime;
|
||||
uint32_t _stoptime;
|
||||
|
||||
uint32_t (*_gettime)(void);
|
||||
static uint32_t seconds() { return millis() / 1000; };
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,14 @@
|
||||
//
|
||||
// FILE: sample.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: sample demo stopwatch class
|
||||
// DATE:
|
||||
// URL:
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include <StopWatch.h>
|
||||
|
||||
StopWatch MySW;
|
||||
|
@ -1,79 +0,0 @@
|
||||
#include <StopWatch.h>
|
||||
|
||||
StopWatch MySW;
|
||||
StopWatch SWarray[5];
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("Stopwatch demo");
|
||||
Serial.print("Version: ");
|
||||
Serial.println(STOPWATCH_LIB_VERSION);
|
||||
|
||||
SWarray[0].start();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println(MySW.isRunning());
|
||||
delay(100);
|
||||
|
||||
MySW.start();
|
||||
Serial.println(MySW.isRunning());
|
||||
Serial.println("START 1");
|
||||
for(int i=0; i<5; i++)
|
||||
{
|
||||
delay(10);
|
||||
Serial.println(MySW.value());
|
||||
}
|
||||
|
||||
MySW.stop();
|
||||
Serial.println(MySW.isRunning());
|
||||
Serial.println("STOP");
|
||||
for(int i=0; i<5; i++)
|
||||
{
|
||||
delay(10);
|
||||
Serial.println(MySW.value());
|
||||
}
|
||||
|
||||
MySW.start();
|
||||
Serial.println(MySW.isRunning());
|
||||
Serial.println("START 2");
|
||||
for(int i=0; i<5; i++)
|
||||
{
|
||||
delay(10);
|
||||
Serial.println(MySW.value());
|
||||
}
|
||||
MySW.reset();
|
||||
Serial.println(MySW.isRunning());
|
||||
Serial.println("RESET");
|
||||
|
||||
MySW.start();
|
||||
Serial.println(MySW.isRunning());
|
||||
Serial.println("START 3");
|
||||
for(int i=0; i<5; i++)
|
||||
{
|
||||
delay(10);
|
||||
Serial.println(MySW.value());
|
||||
}
|
||||
|
||||
switch(MySW.state())
|
||||
{
|
||||
case STOPWATCH_RESET:
|
||||
Serial.println("reset"); // e.g. disable stop/reset
|
||||
break;
|
||||
case STOPWATCH_RUNNING:
|
||||
Serial.println("running"); // display laptime
|
||||
break;
|
||||
case STOPWATCH_STOPPED:
|
||||
Serial.println("stopped"); // display finaltime
|
||||
break;
|
||||
default:
|
||||
Serial.println("unknown");
|
||||
break;
|
||||
}
|
||||
|
||||
delay(3000);
|
||||
Serial.print(" >>> laptime loop() : ");
|
||||
Serial.println(SWarray[0].value());
|
||||
}
|
@ -1,3 +1,14 @@
|
||||
//
|
||||
// FILE: sample2.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: sample demo stopwatch class
|
||||
// DATE:
|
||||
// URL:
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include <StopWatch.h>
|
||||
|
||||
StopWatch sw_millis; // MILLIS (default)
|
||||
@ -6,7 +17,7 @@ StopWatch sw_secs(StopWatch::SECONDS);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
sw_millis.start();
|
||||
sw_micros.start();
|
||||
sw_secs.start();
|
||||
@ -16,11 +27,12 @@ void setup()
|
||||
void loop()
|
||||
{
|
||||
Serial.print("sw_millis=");
|
||||
Serial.println(sw_millis.elapsed());
|
||||
Serial.print("sw_micros=");
|
||||
Serial.println(sw_micros.elapsed());
|
||||
Serial.print("sw_secs=");
|
||||
Serial.println(sw_secs.elapsed());
|
||||
Serial.print(sw_millis.elapsed());
|
||||
Serial.print("\tsw_micros=");
|
||||
Serial.print(sw_micros.elapsed());
|
||||
Serial.print("\tsw_secs=");
|
||||
Serial.print(sw_secs.elapsed());
|
||||
Serial.println();
|
||||
|
||||
delay(1000);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
name=StopWatch
|
||||
version=0.1.3
|
||||
version=0.1.4
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library to implement a stopwatch.
|
||||
|
Loading…
x
Reference in New Issue
Block a user