Metro SW timers as in Arduino

Metro SW timer as for Arduino. Almost unchanged. Please use with "Arduino" library.

Committer:
eduardoG26
Date:
Fri Mar 27 15:32:41 2015 +0000
Revision:
1:33de962673fd
Parent:
0:1e3f8cf13bb8
changed include to Arduino.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eduardoG26 0:1e3f8cf13bb8 1 /*
eduardoG26 0:1e3f8cf13bb8 2 The MIT License (MIT)
eduardoG26 0:1e3f8cf13bb8 3
eduardoG26 0:1e3f8cf13bb8 4 Copyright (c) 2013 thomasfredericks
eduardoG26 0:1e3f8cf13bb8 5 Copyright (c) 2015 Eduardo de Mier
eduardoG26 0:1e3f8cf13bb8 6
eduardoG26 0:1e3f8cf13bb8 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
eduardoG26 0:1e3f8cf13bb8 8 this software and associated documentation files (the "Software"), to deal in
eduardoG26 0:1e3f8cf13bb8 9 the Software without restriction, including without limitation the rights to
eduardoG26 0:1e3f8cf13bb8 10 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
eduardoG26 0:1e3f8cf13bb8 11 the Software, and to permit persons to whom the Software is furnished to do so,
eduardoG26 0:1e3f8cf13bb8 12 subject to the following conditions:
eduardoG26 0:1e3f8cf13bb8 13
eduardoG26 0:1e3f8cf13bb8 14 The above copyright notice and this permission notice shall be included in all
eduardoG26 0:1e3f8cf13bb8 15 copies or substantial portions of the Software.
eduardoG26 0:1e3f8cf13bb8 16
eduardoG26 0:1e3f8cf13bb8 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
eduardoG26 0:1e3f8cf13bb8 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
eduardoG26 0:1e3f8cf13bb8 19 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
eduardoG26 0:1e3f8cf13bb8 20 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
eduardoG26 0:1e3f8cf13bb8 21 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
eduardoG26 0:1e3f8cf13bb8 22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
eduardoG26 0:1e3f8cf13bb8 23 */
eduardoG26 0:1e3f8cf13bb8 24
eduardoG26 0:1e3f8cf13bb8 25 #include "Metro.h"
eduardoG26 1:33de962673fd 26 #include "Arduino.h"
eduardoG26 0:1e3f8cf13bb8 27
eduardoG26 0:1e3f8cf13bb8 28 Metro::Metro(unsigned long interval_millis)
eduardoG26 0:1e3f8cf13bb8 29 {
eduardoG26 0:1e3f8cf13bb8 30 this->autoreset = 0;
eduardoG26 0:1e3f8cf13bb8 31 interval(interval_millis);
eduardoG26 0:1e3f8cf13bb8 32 reset();
eduardoG26 0:1e3f8cf13bb8 33 }
eduardoG26 0:1e3f8cf13bb8 34
eduardoG26 0:1e3f8cf13bb8 35 // New creator so I can use either the original check behavior or benjamin.soelberg's
eduardoG26 0:1e3f8cf13bb8 36 // suggested one (see below).
eduardoG26 0:1e3f8cf13bb8 37 // autoreset = 0 is benjamin.soelberg's check behavior
eduardoG26 0:1e3f8cf13bb8 38 // autoreset != 0 is the original behavior
eduardoG26 0:1e3f8cf13bb8 39
eduardoG26 0:1e3f8cf13bb8 40 Metro::Metro(unsigned long interval_millis, uint8_t autoreset)
eduardoG26 0:1e3f8cf13bb8 41 {
eduardoG26 0:1e3f8cf13bb8 42 this->autoreset = autoreset; // Fix by Paul Bouchier
eduardoG26 0:1e3f8cf13bb8 43 interval(interval_millis);
eduardoG26 0:1e3f8cf13bb8 44 reset();
eduardoG26 0:1e3f8cf13bb8 45 }
eduardoG26 0:1e3f8cf13bb8 46
eduardoG26 0:1e3f8cf13bb8 47 void Metro::interval(unsigned long interval_millis)
eduardoG26 0:1e3f8cf13bb8 48 {
eduardoG26 0:1e3f8cf13bb8 49 this->interval_millis = interval_millis;
eduardoG26 0:1e3f8cf13bb8 50 }
eduardoG26 0:1e3f8cf13bb8 51
eduardoG26 0:1e3f8cf13bb8 52 // Benjamin.soelberg's check behavior:
eduardoG26 0:1e3f8cf13bb8 53 // When a check is true, add the interval to the internal counter.
eduardoG26 0:1e3f8cf13bb8 54 // This should guarantee a better overall stability.
eduardoG26 0:1e3f8cf13bb8 55
eduardoG26 0:1e3f8cf13bb8 56 // Original check behavior:
eduardoG26 0:1e3f8cf13bb8 57 // When a check is true, add the interval to the current millis() counter.
eduardoG26 0:1e3f8cf13bb8 58 // This method can add a certain offset over time.
eduardoG26 0:1e3f8cf13bb8 59
eduardoG26 0:1e3f8cf13bb8 60 char Metro::check()
eduardoG26 0:1e3f8cf13bb8 61 {
eduardoG26 0:1e3f8cf13bb8 62 if (millis() - this->previous_millis >= this->interval_millis) {
eduardoG26 0:1e3f8cf13bb8 63 // As suggested by benjamin.soelberg@gmail.com, the following line
eduardoG26 0:1e3f8cf13bb8 64 // this->previous_millis = millis();
eduardoG26 0:1e3f8cf13bb8 65 // was changed to
eduardoG26 0:1e3f8cf13bb8 66 // this->previous_millis += this->interval_millis;
eduardoG26 0:1e3f8cf13bb8 67
eduardoG26 0:1e3f8cf13bb8 68 // If the interval is set to 0 we revert to the original behavior
eduardoG26 0:1e3f8cf13bb8 69 if (this->interval_millis <= 0 || this->autoreset ) {
eduardoG26 0:1e3f8cf13bb8 70 this->previous_millis = millis();
eduardoG26 0:1e3f8cf13bb8 71 } else {
eduardoG26 0:1e3f8cf13bb8 72 this->previous_millis += this->interval_millis;
eduardoG26 0:1e3f8cf13bb8 73 }
eduardoG26 0:1e3f8cf13bb8 74
eduardoG26 0:1e3f8cf13bb8 75 return 1;
eduardoG26 0:1e3f8cf13bb8 76 }
eduardoG26 0:1e3f8cf13bb8 77
eduardoG26 0:1e3f8cf13bb8 78
eduardoG26 0:1e3f8cf13bb8 79
eduardoG26 0:1e3f8cf13bb8 80 return 0;
eduardoG26 0:1e3f8cf13bb8 81
eduardoG26 0:1e3f8cf13bb8 82 }
eduardoG26 0:1e3f8cf13bb8 83
eduardoG26 0:1e3f8cf13bb8 84 void Metro::reset()
eduardoG26 0:1e3f8cf13bb8 85 {
eduardoG26 0:1e3f8cf13bb8 86
eduardoG26 0:1e3f8cf13bb8 87 this->previous_millis = millis();
eduardoG26 0:1e3f8cf13bb8 88
eduardoG26 0:1e3f8cf13bb8 89 }
eduardoG26 0:1e3f8cf13bb8 90