minor changes

This commit is contained in:
Rob Tillaart 2013-08-17 18:38:38 +02:00
parent 680ccbc1cc
commit bd2f632b7d
3 changed files with 47 additions and 74 deletions

View File

@ -1,83 +1,67 @@
//
//
// FILE: StopWatch.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.03
// VERSION: 0.1.02
// PURPOSE: Simple StopWatch library for Arduino
//
// The library is based upon millis() and therefore
// has the same restrictions as millis() has wrt overflow.
// The library is based upon millis() and therefor has the same restrictions as millis() has wrt overflow.
//
// HISTORY:
// 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
//
// Released to the public domain
//
// Released to the public domain
//
#include "StopWatch.h"
#include "wiring.h"
StopWatch::StopWatch(enum Resolution res)
StopWatch::StopWatch()
{
_res = res;
switch(_res) {
case MICROS:
_gettime = micros;
break;
case MILLIS:
_gettime = millis;
break;
case SECONDS:
_gettime = seconds;
break;
default:
_gettime = millis;
break;
}
reset();
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 (STOPWATCH_RESET == _state || STOPWATCH_STOPPED == _state)
{
_state = STOPWATCH_RUNNING;
unsigned long t = millis();
_starttime += t - _stoptime;
_stoptime = t;
}
}
unsigned long StopWatch::value()
{
if (_state == StopWatch::RUNNING) _stoptime = _gettime();
return _stoptime - _starttime;
if (STOPWATCH_RUNNING == _state) _stoptime = millis();
return _stoptime - _starttime;
}
void StopWatch::stop()
{
if (_state == StopWatch::RUNNING)
{
_state = StopWatch::STOPPED;
_stoptime = _gettime();
}
if (STOPWATCH_RUNNING == _state)
{
_state = STOPWATCH_STOPPED;
_stoptime = millis();
}
}
bool StopWatch::isRunning()
{
return (_state == StopWatch::RUNNING);
return (STOPWATCH_RUNNING == _state);
}
enum StopWatch::State StopWatch::state()
int StopWatch::state()
{
return _state;
return _state;
}
// END OF FILE

View File

@ -1,45 +1,37 @@
#ifndef StopWatch_h
#define StopWatch_h
//
//
// FILE: StopWatch.h
// AUTHOR: Rob Tillaart
// PURPOSE: Simple StopWatch library for Arduino
// HISTORY: See StopWatch.cpp
// URL: http://www.arduino.cc/playground/Code/StopWatchClass
//
// Released to the public domain
// Released to the public domain
//
#define STOPWATCH_LIB_VERSION "0.1.03"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define STOPWATCH_LIB_VERSION "0.1.02"
class StopWatch
#define STOPWATCH_RESET 0
#define STOPWATCH_RUNNING 1
#define STOPWATCH_STOPPED 2
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; };
StopWatch();
void start();
void stop();
void reset();
unsigned long value();
bool isRunning();
int state();
private:
enum State _state;
enum Resolution _res;
unsigned long _starttime;
unsigned long _stoptime;
unsigned long (*_gettime)(void);
static unsigned long seconds() { return millis()/1000; };
int _state;
unsigned long _starttime;
unsigned long _stoptime;
};
#endif

View File

@ -1,9 +1,6 @@
TODO
=====================
- base upon micro's ?
- add examples
-