Permalink
Browse files

Merge remote-tracking branch 'sofian-superchrono/master' into sofian-…

…merge-superchrono

Conflicts:
	README.md
	keywords.txt
  • Loading branch information...
2 parents e821abc + 48b7860 commit 3bbe26144414b4482a547699baaf0b22c19f9697 @sofian sofian committed May 7, 2015
Showing with 386 additions and 14 deletions.
  1. +86 −10 README.md
  2. +112 −0 SuperChrono.cpp
  3. +112 −0 SuperChrono.h
  4. +28 −0 examples/Resolutions/Resolutions.ino
  5. +39 −0 examples/StopResume/StopResume.ino
  6. +9 −4 keywords.txt
View
@@ -1,10 +1,9 @@
CHRONO
=====================
Chrono library for Arduino or Wiring
-by Thomas Ouellet Fredericks and Sofian Audry
+by Sofian Audry and Thomas Ouellet Fredericks
-Simple chronometer/stopwatch class that counts the time passed since started.
-For a chronometer with more advanced features (pause, resume, add time, multiple time units support, etc) please use the [SuperChrono](http://github.com/sofian/SuperChrono) library.
+Chronometer/stopwatch library that counts the time passed since started.
Repository
=====================
@@ -33,11 +32,11 @@ void setup() {
}
void loop() {
- // Check whether the chronometer has reached 1000 ms
+ // Check whether the chronometer has reached 1000 time units.
if (myChrono.hasPassed(1000)) {
// Do something here...
- // Restart the chrono.
- myChrono.start();
+ // Restart the chronometer.
+ myChrono.restart();
}
}
@@ -62,6 +61,22 @@ Chrono myChrono;
```
+You can create a Chrono that counts in microseconds or seconds:
+```arduino
+Chrono myChronoMicros(SuperChrono::MICROS);
+Chrono myChronoSeconds(SuperChrono::SECONDS);
+
+```
+
+Alternatively you can create a Chrono with a custom time function:
+```arduino
+unsigned long mySpecialTimeFunction();
+
+Chrono myChronoMicros(mySpecialTimeFunction);
+
+```
+
+
elapsed()
=====================
@@ -74,22 +89,46 @@ unsigned long elapsed = myChrono.elapsed();
```
restart()
+restart(offset)
=====================
-Restarts the chronometer.
+Starts/restarts the chronometer.
```arduino
// RESTART THE CHRONOMETER :
myChrono.restart();
```
+stop()
+=====================
+
+Stops/pauses the chronometer.
+
+```arduino
+// STOPS/PAUSES THE CHRONOMETER :
+mySuperChrono.stop();
+
+```
+
+resume()
+=====================
+
+Resumes the chronometer.
+
+```arduino
+// STOPS/PAUSES THE CHRONOMETER :
+mySuperChrono.stop();
+
+```
+
+
hasPassed(timeout)
=====================
-Returns true if the chronometer passed the timeout in milliseconds.
+Returns true if the chronometer passed the timeout.
```arduino
if ( myChrono.hasPassed(500) ) {
- // DO SOMETHING IF 500 MS HAVE PASSED.
+ // DO SOMETHING IF 500 TIME UNITS HAVE PASSED.
}
```
@@ -98,7 +137,44 @@ Combined with restart() you can have a metronome :
```arduino
if ( myChrono.hasPassed(200) ) {
myChrono.restart();
- // DO SOMETHING EVERY 200 MS.
+ // DO SOMETHING EVERY 200 TIME UNITS.
}
```
+
+add(time)
+=====================
+
+Adds some time to the chronometer.
+
+```arduino
+// ADDS 500 TIME UNITS TO THE CHRONOMETER :
+myChrono.add(500);
+
+```
+
+isRunning()
+=====================
+
+Returns true if the chronometer is currently running.
+
+```arduino
+myChrono.restart();
+Serial.println( myChrono.isRunning() ); // will print "1" (true)
+myChrono.stop();
+Serial.println( myChrono.isRunning() ); // will print "0" (false)
+myChrono.resume();
+Serial.println( myChrono.isRunning() ); // will print "1" (true)
+
+```
+
+delay(time)
+=====================
+
+Waits for some time (in the time unit of the chronometer).
+
+```arduino
+// WAIT FOR 1000 TIME UNITS :
+myChrono.delay(1000);
+
+```
View
@@ -0,0 +1,112 @@
+/*
+ * Advanced chronometer class
+ * Advanced chronometer/stopwatch class that counts the time passed since started.
+ *
+ * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
+ * (c) Thomas O Fredericks :: tof(@)t-o-f(.)info
+ * (c) Rob Tillaart
+ *
+ * See also the Chrono library (simple chronometer):
+ * https://github.com/sofian/Chrono
+ *
+ * Based on code by Sofian Audry:
+ * https://github.com/sofian/libinteract/blob/master/trunk/arduino/SuperTimer.h
+ * http://accrochages.drone.ws/node/90
+ *
+ * Thomas O Fredericks (tof@t-o-f.info)
+ * https://github.com/thomasfredericks/Chrono
+ *
+ * Rob Tillaart StopWatch library:
+ * http://playground.arduino.cc/Code/StopWatchClass
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#if defined(ARDUINO) && ARDUINO >= 100
+#include "Arduino.h"
+#else
+#include "WProgram.h"
+#endif
+#include "SuperChrono.h"
+
+SuperChrono::SuperChrono(Resolution resolution) {
+ // Assign appropriate time function.
+ switch(resolution) {
+ case SECONDS:
+ _getTime = seconds;
+ break;
+ case MICROS:
+ _getTime = micros;
+ break;
+ case MILLIS:
+ default:
+ _getTime = millis;
+ break;
+ }
+ // Start.
+ restart();
+}
+
+SuperChrono::SuperChrono(unsigned long (*getTime_)(void), bool startNow) : _getTime(getTime_) {
+ if (startNow)
+ restart();
+ else {
+ _startTime = _offset = 0;
+ _isRunning = false;
+ }
+}
+
+void SuperChrono::restart(unsigned long offset) {
+ _startTime = _getTime();
+ _offset = offset;
+ _isRunning = true;
+}
+
+void SuperChrono::stop() {
+ _offset = elapsed(); // save currently elapsed time
+ _isRunning = false;
+}
+
+void SuperChrono::resume() {
+ _startTime = _getTime();
+ _isRunning = true;
+}
+
+void SuperChrono::add(unsigned long t) {
+ _offset += t;
+}
+
+bool SuperChrono::isRunning() const {
+ return (_isRunning);
+}
+
+void SuperChrono::delay(unsigned long time) {
+ time += elapsed();
+ while (!passed(time));
+}
+
+unsigned long SuperChrono::elapsed() const {
+ return _offset + (_isRunning ? (_getTime() - _startTime) : 0);
+}
+
+bool SuperChrono::passed(unsigned long timeout) const
+{
+ return (elapsed() >= timeout);
+}
+
+unsigned long SuperChrono::seconds() {
+ return (millis()/1000);
+}
+
+
+
View
@@ -0,0 +1,112 @@
+/*
+ * Advanced chronometer class
+ * Advanced chronometer/stopwatch class that counts the time passed since started.
+ *
+ * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
+ * (c) Thomas O Fredericks :: tof(@)t-o-f(.)info
+ * (c) Rob Tillaart
+ *
+ * See also the Chrono library (simple chronometer):
+ * https://github.com/sofian/Chrono
+ *
+ * Based on code by Sofian Audry:
+ * https://github.com/sofian/libinteract/blob/master/trunk/arduino/SuperTimer.h
+ * http://accrochages.drone.ws/node/90
+ *
+ * Thomas O Fredericks (tof@t-o-f.info)
+ * https://github.com/thomasfredericks/Chrono
+ *
+ * Rob Tillaart StopWatch library:
+ * http://playground.arduino.cc/Code/StopWatchClass
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef SUPERCHRONO_H_
+#define SUPERCHRONO_H_
+
+/*
+ * Example code:
+ *
+ * SuperChrono chrono;
+ * // ...
+ * chrono.restart(); // start/restart
+ * // do some stuff
+ * chrono.pause();
+ * // do more stuff
+ * chrono.resume();
+ * // do some "out of the clock" processing, such as shutting the power down using
+ * // the watchdog for 8000 ms
+ * chrono.add(8000); // add the time that wasn't accounted for
+ */
+class SuperChrono
+{
+public:
+ // Different sorts of ways to get time.
+ enum Resolution { MILLIS, MICROS, SECONDS };
+
+public:
+ // Keeps track of start time (in milliseconds).
+ unsigned long _startTime;
+
+ // Time offset.
+ unsigned long _offset;
+
+ // Time function.
+ unsigned long (*_getTime)(void);
+
+ // Tells if the chrono is currently running or not.
+ bool _isRunning;
+
+public:
+ /// Constructor.
+ SuperChrono(Resolution resolution = MILLIS);
+
+ /**
+ * Custom time method constructor. Optional parameter can be used to prevent
+ * the chronometer from starting at construction since some functions might
+ * trigger errors when called statically.
+ */
+ SuperChrono(unsigned long (*getTime_)(void), bool startNow=true);
+
+ // Starts/restarts the chronometer with optional starting offset.
+ void restart(unsigned long offset = 0);
+
+ // Stops/pauses the chronometer.
+ void stop();
+
+ // Resumes the chronometer.
+ void resume();
+
+ /// Adds some time to the chronometer.
+ void add(unsigned long t);
+
+ /// Returns the elapsed time since start (in milliseconds).
+ unsigned long elapsed() const;
+
+ /// Returns true iff elapsed time has passed given timeout.
+ bool passed(unsigned long timeout) const;
+
+ /// Returns true iff the chronometer is currently running.
+ bool isRunning() const;
+
+ // Blocks execution for a given time.
+ void delay(unsigned long time);
+
+ /// Returns the time in seconds (millis() / 1000).
+ static unsigned long seconds();
+};
+
+#endif
+
+
Oops, something went wrong.

0 comments on commit 3bbe261

Please sign in to comment.