From cbd566ed547577b906a25a48c32633613957ba40 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Sun, 9 Oct 2011 22:21:55 +0200 Subject: [PATCH] stopwatch library --- libraries/StopWatch/StopWatch.cpp | 67 ++++++++++++++++ libraries/StopWatch/StopWatch.h | 38 +++++++++ .../StopWatch/examples/sample/sample.pde | 79 +++++++++++++++++++ libraries/StopWatch/readme.txt | 9 +++ 4 files changed, 193 insertions(+) create mode 100644 libraries/StopWatch/StopWatch.cpp create mode 100644 libraries/StopWatch/StopWatch.h create mode 100644 libraries/StopWatch/examples/sample/sample.pde create mode 100644 libraries/StopWatch/readme.txt diff --git a/libraries/StopWatch/StopWatch.cpp b/libraries/StopWatch/StopWatch.cpp new file mode 100644 index 00000000..da7a62d4 --- /dev/null +++ b/libraries/StopWatch/StopWatch.cpp @@ -0,0 +1,67 @@ +// +// FILE: StopWatch.cpp +// AUTHOR: Rob Tillaart +// VERSION: 0.1.02 +// PURPOSE: Simple StopWatch library for Arduino +// +// The library is based upon millis() and therefor 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 +// +// Released to the public domain +// + +#include "StopWatch.h" +#include "wiring.h" + +StopWatch::StopWatch() +{ + reset(); +} + +void StopWatch::reset() +{ + _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; + } +} + +unsigned long StopWatch::value() +{ + if (STOPWATCH_RUNNING == _state) _stoptime = millis(); + return _stoptime - _starttime; +} + +void StopWatch::stop() +{ + if (STOPWATCH_RUNNING == _state) + { + _state = STOPWATCH_STOPPED; + _stoptime = millis(); + } +} + +bool StopWatch::isRunning() +{ + return (STOPWATCH_RUNNING == _state); +} + +int StopWatch::state() +{ + return _state; +} + +// END OF FILE \ No newline at end of file diff --git a/libraries/StopWatch/StopWatch.h b/libraries/StopWatch/StopWatch.h new file mode 100644 index 00000000..21069773 --- /dev/null +++ b/libraries/StopWatch/StopWatch.h @@ -0,0 +1,38 @@ +#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 +// + + +#define STOPWATCH_LIB_VERSION "0.1.02" + +#define STOPWATCH_RESET 0 +#define STOPWATCH_RUNNING 1 +#define STOPWATCH_STOPPED 2 + +class StopWatch +{ +public: + StopWatch(); + void start(); + void stop(); + void reset(); + unsigned long value(); + bool isRunning(); + int state(); + +private: + int _state; + unsigned long _starttime; + unsigned long _stoptime; +}; + +#endif +// END OF FILE \ No newline at end of file diff --git a/libraries/StopWatch/examples/sample/sample.pde b/libraries/StopWatch/examples/sample/sample.pde new file mode 100644 index 00000000..b111b360 --- /dev/null +++ b/libraries/StopWatch/examples/sample/sample.pde @@ -0,0 +1,79 @@ +#include + +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()); +} diff --git a/libraries/StopWatch/readme.txt b/libraries/StopWatch/readme.txt new file mode 100644 index 00000000..649b8ad3 --- /dev/null +++ b/libraries/StopWatch/readme.txt @@ -0,0 +1,9 @@ + +TODO +===================== +- base upon micro's ? + +- add examples + +- +