Added Metro timer library

This commit is contained in:
Mike C 2013-04-08 12:45:22 -04:00
parent 7ba91945ee
commit 8c8d1451f9
12 changed files with 341 additions and 0 deletions

73
Libraries/Metro/Metro.cpp Executable file
View file

@ -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();
}

26
Libraries/Metro/Metro.h Executable file
View file

@ -0,0 +1,26 @@
#ifndef Metro_h
#define Metro_h
#include <inttypes.h>
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

BIN
Libraries/Metro/Metro.pdf Executable file

Binary file not shown.

7
Libraries/Metro/about.txt Executable file
View file

@ -0,0 +1,7 @@
VERSION 2.3.2
¥ Added this->autoreset = 0; to Metro(unsigned long interval_millis) as suggested by Paul Bouchier
VERSION 2.3.1
¥ Updated for Arduino 1.0

View file

@ -0,0 +1,36 @@
/*
This code will blink an LED attached to pin 13 on and off.
It will stay on for 0.25 seconds.
It will stay off for 1 second.
*/
#include <Metro.h> //Include Metro library
#define LED 13 // Define the led's pin
//Create a variable to hold theled's current state
int state = HIGH;
// Instanciate a metro object and set the interval to 250 milliseconds (0.25 seconds).
Metro ledMetro = Metro(250);
void setup()
{
pinMode(LED,OUTPUT);
digitalWrite(LED,state);
}
void loop()
{
if (ledMetro.check() == 1) { // check if the metro has passed its interval .
if (state==HIGH) {
state=LOW;
ledMetro.interval(250); // if the pin is HIGH, set the interval to 0.25 seconds.
}
else {
ledMetro.interval(1000); // if the pin is LOW, set the interval to 1 second.
state=HIGH;
}
digitalWrite(LED,state);
}
}

Binary file not shown.

View file

@ -0,0 +1,51 @@
// This code will blink output 13 every 250 ms
// abd will blink output 9 every 125 ms
#include <Metro.h> // Include Metro library
#define LED0 13 // Define a LED pin
#define LED1 9 // Define another LED pin
// Create variables to hold the LED states
int state0 = HIGH;
int state1 = HIGH;
// Instantiate a metro object and set the interval to 250 milliseconds (0.25 seconds).
Metro metro0 = Metro(500);
// Instantiate another metro object and set the interval to 125 milliseconds (0.125 seconds).
Metro metro1 = Metro(125);
void setup()
{
pinMode(LED0,OUTPUT);
digitalWrite(LED0,state0);
pinMode(LED1,OUTPUT);
digitalWrite(LED1,state1);
}
void loop()
{
if (metro0.check() == 1) { // check if the metro has passed its interval .
if (state0==HIGH) {
state0=LOW;
} else {
state0=HIGH;
}
digitalWrite(LED0,state0);
}
if (metro1.check() == 1) { // check if the metro has passed its interval .
if (state1==HIGH) {
state1=LOW;
} else {
state1=HIGH;
}
digitalWrite(LED1,state1);
}
}

View file

@ -0,0 +1,24 @@
// This example sends a Serial message every 250 milliseconds
#include <Metro.h> // Include the Metro library
Metro serialMetro = Metro(250); // Instantiate an instance
void setup() {
Serial.begin(115200); // Start the Serial communication
}
void loop() {
if (serialMetro.check() == 1) { // check if the metro has passed it's interval .
// Output all the analog readings seperated by a space character
for (int i = 0; i < 6; i++ ) {
Serial.print (analogRead( i) );
Serial.print(32,BYTE);
}
// Terminate message with a linefeed and a carriage return
Serial.print(13,BYTE);
Serial.print(10,BYTE);
}
}

25
Libraries/Metro/keywords.txt Executable file
View file

@ -0,0 +1,25 @@
#######################################
# Syntax Coloring Map For Test
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Metro KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
check KEYWORD2
interval KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1)
#######################################

View file

@ -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();
}

View file

@ -0,0 +1,26 @@
#ifndef Metro_h
#define Metro_h
#include <inttypes.h>
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

Binary file not shown.