diff --git a/Chrono.cpp b/Chrono.cpp index 5cef33b..230ecea 100644 --- a/Chrono.cpp +++ b/Chrono.cpp @@ -1,33 +1,30 @@ /* * Chronometer class - * Simple chronometer/stopwatch class that counts the time passed since started. + * Chronometer/stopwatch class that counts the time passed since started. * * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com - * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info + * (c) Thomas O Fredericks :: tof(@)t-o-f(.)info + * (c) Rob Tillaart * - * Partly based on code by Sofian Audry: - * https://github.com/sofian/libinteract/blob/master/trunk/arduino/Timer.h + * Based on code by Sofian Audry: + * https://github.com/sofian/libinteract/blob/master/trunk/arduino/SuperTimer.h * http://accrochages.drone.ws/node/90 * - * The MIT License (MIT) + * Rob Tillaart StopWatch library: + * http://playground.arduino.cc/Code/StopWatchClass * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * 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. * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * 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. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ #if defined(ARDUINO) && ARDUINO >= 100 #include "Arduino.h" @@ -36,23 +33,74 @@ #endif #include "Chrono.h" -Chrono::Chrono() -{ +Chrono::Chrono(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(); } -void Chrono::restart() -{ - _startTime = millis(); +Chrono::Chrono(unsigned long (*getTime_)(void), bool startNow) : _getTime(getTime_) { + if (startNow) + restart(); + else { + _startTime = _offset = 0; + _isRunning = false; + } } -unsigned long Chrono::elapsed() const { - return (millis() - _startTime); +void Chrono::restart(unsigned long offset) { + _startTime = _getTime(); + _offset = offset; + _isRunning = true; +} + +void Chrono::stop() { + _offset = elapsed(); // save currently elapsed time + _isRunning = false; +} + +void Chrono::resume() { + _startTime = _getTime(); + _isRunning = true; +} + +void Chrono::add(unsigned long t) { + _offset += t; +} + +bool Chrono::isRunning() const { + return (_isRunning); +} + +void Chrono::delay(unsigned long time) { + time += elapsed(); + while (!passed(time)); } +unsigned long Chrono::elapsed() const { + return _offset + (_isRunning ? (_getTime() - _startTime) : 0); +} -bool Chrono::hasPassed(unsigned long timeout) const +bool Chrono::passed(unsigned long timeout) const { return (elapsed() >= timeout); } +unsigned long Chrono::seconds() { + return (millis()/1000); +} + + + diff --git a/Chrono.h b/Chrono.h index 3fa0206..0e77d6b 100644 --- a/Chrono.h +++ b/Chrono.h @@ -1,65 +1,104 @@ /* * Chronometer class - * Simple chronometer/stopwatch class that counts the time passed since started. + * Chronometer/stopwatch class that counts the time passed since started. * * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com - * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info + * (c) Thomas O Fredericks :: tof(@)t-o-f(.)info + * (c) Rob Tillaart * - * Partly based on code by Sofian Audry: - * https://github.com/sofian/libinteract/blob/master/trunk/arduino/Timer.h + * Based on code by Sofian Audry: + * https://github.com/sofian/libinteract/blob/master/trunk/arduino/SuperTimer.h * http://accrochages.drone.ws/node/90 * - * The MIT License (MIT) + * Rob Tillaart StopWatch library: + * http://playground.arduino.cc/Code/StopWatchClass * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * 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. * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * 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. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ #ifndef CHRONO_H_ #define CHRONO_H_ /* * Example code: - * - * Chrono myChrono; // chronometer automatically starts at creation - * // ... - * myChrono.restart(); // you can start (restart) it later - * while (!myChrono.hasPassed(2000)) // a 2000 ms loop - * Serial.println(myChrono.elapsed()); // current time - * // do something + * + * Chrono 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 Chrono { -private: - unsigned long _startTime; // keeps track of start time (in milliseconds) +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. - Chrono(); + Chrono(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. + */ + Chrono(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(); - // Starts/restarts the chronometer. - void restart(); + // 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 hasPassed(unsigned long timeout) const; + 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 diff --git a/LightChrono.cpp b/LightChrono.cpp new file mode 100644 index 0000000..075b946 --- /dev/null +++ b/LightChrono.cpp @@ -0,0 +1,54 @@ +/* + * Lightweight chronometer class. + * Simple chronometer/stopwatch class that counts the time passed since started. + * + * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com + * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info + * + * The MIT License (MIT) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif +#include "LightChrono.h" + +LightChrono::LightChrono() +{ + restart(); +} + +void LightChrono::restart() +{ + _startTime = millis(); +} + +unsigned long LightChrono::elapsed() const { + return (millis() - _startTime); +} + + +bool LightChrono::hasPassed(unsigned long timeout) const +{ + return (elapsed() >= timeout); +} + diff --git a/LightChrono.h b/LightChrono.h new file mode 100644 index 0000000..5c31057 --- /dev/null +++ b/LightChrono.h @@ -0,0 +1,63 @@ +/* + * Lightweight chronometer class. + * Simple chronometer/stopwatch class that counts the time passed since started. + * + * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com + * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info + * + * The MIT License (MIT) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef LIGHT_CHRONO_H_ +#define LIGHT_CHRONO_H_ + +/* + * Example code: + * + * LightChrono myLightChrono; // chronometer automatically starts at creation + * // ... + * myLightChrono.restart(); // you can start (restart) it later + * while (!myLightChrono.hasPassed(2000)) // a 2000 ms loop + * Serial.println(myLightChrono.elapsed()); // current time + * // do something + * // ... + */ +class LightChrono +{ +private: + unsigned long _startTime; // keeps track of start time (in milliseconds) + +public: + /// Constructor. + LightChrono(); + + // Starts/restarts the chronometer. + void restart(); + + /// Returns the elapsed time since start (in milliseconds). + unsigned long elapsed() const; + + /// Returns true iff elapsed time has passed given timeout. + bool hasPassed(unsigned long timeout) const; +}; + +#endif + + diff --git a/SuperChrono.cpp b/SuperChrono.cpp deleted file mode 100644 index 94a1bf6..0000000 --- a/SuperChrono.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 . - */ -#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); -} - - - diff --git a/SuperChrono.h b/SuperChrono.h deleted file mode 100644 index 0f411fc..0000000 --- a/SuperChrono.h +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 . - */ -#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 - -