diff --git a/Libraries/Metro/Metro.cpp b/Libraries/Metro/Metro.cpp new file mode 100755 index 0000000..3d13747 --- /dev/null +++ b/Libraries/Metro/Metro.cpp @@ -0,0 +1,73 @@ + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif +#include "Metro.h" + + +Metro::Metro(unsigned long interval_millis) +{ + this->autoreset = 0; + interval(interval_millis); + reset(); +} + +// New creator so I can use either the original check behavior or benjamin.soelberg's +// suggested one (see below). +// autoreset = 0 is benjamin.soelberg's check behavior +// autoreset != 0 is the original behavior + +Metro::Metro(unsigned long interval_millis, uint8_t autoreset) +{ + this->autoreset = autoreset; // Fix by Paul Bouchier + interval(interval_millis); + reset(); +} + +void Metro::interval(unsigned long interval_millis) +{ + this->interval_millis = interval_millis; +} + +// Benjamin.soelberg's check behavior: +// When a check is true, add the interval to the internal counter. +// This should guarantee a better overall stability. + +// Original check behavior: +// When a check is true, add the interval to the current millis() counter. +// This method can add a certain offset over time. + +char Metro::check() +{ + if (millis() - this->previous_millis >= this->interval_millis) { + // As suggested by benjamin.soelberg@gmail.com, the following line + // this->previous_millis = millis(); + // was changed to + // this->previous_millis += this->interval_millis; + + // If the interval is set to 0 we revert to the original behavior + if (this->interval_millis <= 0 || this->autoreset ) { + this->previous_millis = millis(); + } else { + this->previous_millis += this->interval_millis; + } + + return 1; + } + + + + return 0; + +} + +void Metro::reset() +{ + + this->previous_millis = millis(); + +} + + diff --git a/Libraries/Metro/Metro.h b/Libraries/Metro/Metro.h new file mode 100755 index 0000000..cb21776 --- /dev/null +++ b/Libraries/Metro/Metro.h @@ -0,0 +1,26 @@ + + +#ifndef Metro_h +#define Metro_h + +#include + +class Metro +{ + +public: + Metro(unsigned long interval_millis); + Metro(unsigned long interval_millis, uint8_t autoreset); + void interval(unsigned long interval_millis); + char check(); + void reset(); + +private: + uint8_t autoreset; + unsigned long previous_millis, interval_millis; + +}; + +#endif + + diff --git a/Libraries/Metro/Metro.pdf b/Libraries/Metro/Metro.pdf new file mode 100755 index 0000000..f42f502 Binary files /dev/null and b/Libraries/Metro/Metro.pdf differ diff --git a/Universal_Serial_Adapter/Libraries/Metro/Metro.cpp b/Universal_Serial_Adapter/Libraries/Metro/Metro.cpp new file mode 100755 index 0000000..3d13747 --- /dev/null +++ b/Universal_Serial_Adapter/Libraries/Metro/Metro.cpp @@ -0,0 +1,73 @@ + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif +#include "Metro.h" + + +Metro::Metro(unsigned long interval_millis) +{ + this->autoreset = 0; + interval(interval_millis); + reset(); +} + +// New creator so I can use either the original check behavior or benjamin.soelberg's +// suggested one (see below). +// autoreset = 0 is benjamin.soelberg's check behavior +// autoreset != 0 is the original behavior + +Metro::Metro(unsigned long interval_millis, uint8_t autoreset) +{ + this->autoreset = autoreset; // Fix by Paul Bouchier + interval(interval_millis); + reset(); +} + +void Metro::interval(unsigned long interval_millis) +{ + this->interval_millis = interval_millis; +} + +// Benjamin.soelberg's check behavior: +// When a check is true, add the interval to the internal counter. +// This should guarantee a better overall stability. + +// Original check behavior: +// When a check is true, add the interval to the current millis() counter. +// This method can add a certain offset over time. + +char Metro::check() +{ + if (millis() - this->previous_millis >= this->interval_millis) { + // As suggested by benjamin.soelberg@gmail.com, the following line + // this->previous_millis = millis(); + // was changed to + // this->previous_millis += this->interval_millis; + + // If the interval is set to 0 we revert to the original behavior + if (this->interval_millis <= 0 || this->autoreset ) { + this->previous_millis = millis(); + } else { + this->previous_millis += this->interval_millis; + } + + return 1; + } + + + + return 0; + +} + +void Metro::reset() +{ + + this->previous_millis = millis(); + +} + + diff --git a/Universal_Serial_Adapter/Libraries/Metro/Metro.h b/Universal_Serial_Adapter/Libraries/Metro/Metro.h new file mode 100755 index 0000000..cb21776 --- /dev/null +++ b/Universal_Serial_Adapter/Libraries/Metro/Metro.h @@ -0,0 +1,26 @@ + + +#ifndef Metro_h +#define Metro_h + +#include + +class Metro +{ + +public: + Metro(unsigned long interval_millis); + Metro(unsigned long interval_millis, uint8_t autoreset); + void interval(unsigned long interval_millis); + char check(); + void reset(); + +private: + uint8_t autoreset; + unsigned long previous_millis, interval_millis; + +}; + +#endif + + diff --git a/Universal_Serial_Adapter/Libraries/Metro/Metro.pdf b/Universal_Serial_Adapter/Libraries/Metro/Metro.pdf new file mode 100755 index 0000000..f42f502 Binary files /dev/null and b/Universal_Serial_Adapter/Libraries/Metro/Metro.pdf differ