mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ version 0.1.03
+ added resolution
This commit is contained in:
parent
d73aa94100
commit
4976a9d7f6
@ -1,67 +1,83 @@
|
||||
//
|
||||
// FILE: StopWatch.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.02
|
||||
// VERSION: 0.1.03
|
||||
// PURPOSE: Simple StopWatch library for Arduino
|
||||
//
|
||||
// The library is based upon millis() and therefor has the same restrictions as millis() has wrt overflow.
|
||||
// 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
|
||||
// By mromani & Rob Tillaart
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include "StopWatch.h"
|
||||
#include "wiring.h"
|
||||
|
||||
StopWatch::StopWatch()
|
||||
StopWatch::StopWatch(enum Resolution res)
|
||||
{
|
||||
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 (STOPWATCH_RESET == _state || STOPWATCH_STOPPED == _state)
|
||||
{
|
||||
_state = STOPWATCH_RUNNING;
|
||||
unsigned long t = millis();
|
||||
_starttime += t - _stoptime;
|
||||
_stoptime = t;
|
||||
}
|
||||
if (_state == StopWatch::RESET || _state == StopWatch::STOPPED)
|
||||
{
|
||||
_state = StopWatch::RUNNING;
|
||||
unsigned long t = _gettime();
|
||||
_starttime += t - _stoptime;
|
||||
_stoptime = t;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long StopWatch::value()
|
||||
{
|
||||
if (STOPWATCH_RUNNING == _state) _stoptime = millis();
|
||||
return _stoptime - _starttime;
|
||||
if (_state == StopWatch::RUNNING) _stoptime = _gettime();
|
||||
return _stoptime - _starttime;
|
||||
}
|
||||
|
||||
void StopWatch::stop()
|
||||
{
|
||||
if (STOPWATCH_RUNNING == _state)
|
||||
{
|
||||
_state = STOPWATCH_STOPPED;
|
||||
_stoptime = millis();
|
||||
}
|
||||
if (_state == StopWatch::RUNNING)
|
||||
{
|
||||
_state = StopWatch::STOPPED;
|
||||
_stoptime = _gettime();
|
||||
}
|
||||
}
|
||||
|
||||
bool StopWatch::isRunning()
|
||||
{
|
||||
return (STOPWATCH_RUNNING == _state);
|
||||
return (_state == StopWatch::RUNNING);
|
||||
}
|
||||
|
||||
int StopWatch::state()
|
||||
enum StopWatch::State StopWatch::state()
|
||||
{
|
||||
return _state;
|
||||
return _state;
|
||||
}
|
||||
|
||||
// END OF FILE
|
@ -10,28 +10,36 @@
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#define STOPWATCH_LIB_VERSION "0.1.03"
|
||||
|
||||
#define STOPWATCH_LIB_VERSION "0.1.02"
|
||||
|
||||
#define STOPWATCH_RESET 0
|
||||
#define STOPWATCH_RUNNING 1
|
||||
#define STOPWATCH_STOPPED 2
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
class StopWatch
|
||||
{
|
||||
public:
|
||||
StopWatch();
|
||||
void start();
|
||||
void stop();
|
||||
void reset();
|
||||
unsigned long value();
|
||||
bool isRunning();
|
||||
int state();
|
||||
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; };
|
||||
|
||||
private:
|
||||
int _state;
|
||||
unsigned long _starttime;
|
||||
unsigned long _stoptime;
|
||||
enum State _state;
|
||||
enum Resolution _res;
|
||||
unsigned long _starttime;
|
||||
unsigned long _stoptime;
|
||||
unsigned long (*_gettime)(void);
|
||||
static unsigned long seconds() { return millis()/1000; };
|
||||
};
|
||||
|
||||
#endif
|
||||
|
79
libraries/StopWatch/examples/sample/sample.ino
Normal file
79
libraries/StopWatch/examples/sample/sample.ino
Normal file
@ -0,0 +1,79 @@
|
||||
#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());
|
||||
}
|
37
libraries/StopWatch/examples/sample2/sample2.ino
Normal file
37
libraries/StopWatch/examples/sample2/sample2.ino
Normal file
@ -0,0 +1,37 @@
|
||||
#include <StopWatch.h>
|
||||
#include <LiquidCrystal.h>
|
||||
|
||||
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
|
||||
|
||||
StopWatch sw_millis; // MILLIS (default)
|
||||
StopWatch sw_micros(StopWatch::MICROS);
|
||||
StopWatch sw_secs(StopWatch::SECONDS);
|
||||
|
||||
void setup() {
|
||||
lcd.begin(16,2);
|
||||
Serial.begin(9600);
|
||||
sw_millis.start();
|
||||
sw_micros.start();
|
||||
sw_secs.start();
|
||||
}
|
||||
|
||||
|
||||
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());
|
||||
|
||||
lcd.clear();
|
||||
lcd.print("s=");
|
||||
lcd.print(sw_secs.elapsed());
|
||||
lcd.print(" ms=");
|
||||
lcd.print(sw_millis.elapsed());
|
||||
lcd.setCursor(0, 1);
|
||||
lcd.print("us=");
|
||||
lcd.print(sw_micros.elapsed());
|
||||
|
||||
delay(1000);
|
||||
}
|
Loading…
Reference in New Issue
Block a user