daily_automated
This commit is contained in:
65
trunk/Arduino/libraries/Timer/Event.cpp
Normal file
65
trunk/Arduino/libraries/Timer/Event.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
Code by Simon Monk
|
||||
http://www.simonmonk.org
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
// For Arduino 1.0 and earlier
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
Event::Event(void)
|
||||
{
|
||||
eventType = EVENT_NONE;
|
||||
}
|
||||
|
||||
void Event::update(void)
|
||||
{
|
||||
unsigned long now = millis();
|
||||
update(now);
|
||||
}
|
||||
|
||||
void Event::update(unsigned long now)
|
||||
{
|
||||
if (now - lastEventTime >= period)
|
||||
{
|
||||
switch (eventType)
|
||||
{
|
||||
case EVENT_EVERY:
|
||||
(*callback)();
|
||||
break;
|
||||
|
||||
case EVENT_OSCILLATE:
|
||||
pinState = ! pinState;
|
||||
digitalWrite(pin, pinState);
|
||||
break;
|
||||
}
|
||||
lastEventTime = now;
|
||||
count++;
|
||||
}
|
||||
if (repeatCount > -1 && count >= repeatCount)
|
||||
{
|
||||
eventType = EVENT_NONE;
|
||||
}
|
||||
}
|
||||
49
trunk/Arduino/libraries/Timer/Event.h
Normal file
49
trunk/Arduino/libraries/Timer/Event.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
Code by Simon Monk
|
||||
http://www.simonmonk.org
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#ifndef Event_h
|
||||
#define Event_h
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#define EVENT_NONE 0
|
||||
#define EVENT_EVERY 1
|
||||
#define EVENT_OSCILLATE 2
|
||||
|
||||
class Event
|
||||
{
|
||||
|
||||
public:
|
||||
Event(void);
|
||||
void update(void);
|
||||
void update(unsigned long now);
|
||||
int8_t eventType;
|
||||
unsigned long period;
|
||||
int repeatCount;
|
||||
uint8_t pin;
|
||||
uint8_t pinState;
|
||||
void (*callback)(void);
|
||||
unsigned long lastEventTime;
|
||||
int count;
|
||||
};
|
||||
|
||||
#endif
|
||||
29
trunk/Arduino/libraries/Timer/ReadMe.txt
Normal file
29
trunk/Arduino/libraries/Timer/ReadMe.txt
Normal file
@@ -0,0 +1,29 @@
|
||||
1.0 by Simon Monk
|
||||
Library as downloaded 02Feb2012 22:55 UTC from http://srmonk.blogspot.com/2012/01/arduino-timer-library.html
|
||||
|
||||
1.1 by Jack Christensen
|
||||
Changed data types of variables and functions:
|
||||
o event types and indexes changed from int to int8_t.
|
||||
o periods and durations changed from lon to unsigned long.
|
||||
o update() and stop() functions typed as void, since they return nothing.
|
||||
o pin numbers and pin values changed from int to uint8_t, this agrees with digitalWrite().
|
||||
o added return values to Timer::pulse() and Timer::oscillate(uint8_t, unsigned long, uint8_t).
|
||||
o changed test in Event::update() to use subtraction to avoid rollover issues.
|
||||
o Updated keywords.txt file to include all functions.
|
||||
|
||||
1.2 by Damian Philipp
|
||||
o Added a range check to Timer::stop() to avoid memory corruption.
|
||||
o Added constants to <Timer.h>:
|
||||
- NO_TIMER_AVAILABLE: Signals that while an event was to be queued, no free timer could be found.
|
||||
- TIMER_NOT_AN_EVENT: Can be used to flag a variable that *might* contain a timer ID as
|
||||
*not* containing a timer ID
|
||||
o Replaced a bunch of magic numbers in <Timer.cpp> by the above constants
|
||||
o Added several comments
|
||||
o Added Timer::pulseImmediate(). pulseImmediate sets the pin to the specified value for the given
|
||||
duration. After the duration, the pin is set to !value.
|
||||
|
||||
1.3 by Jack Christensen
|
||||
o Added "blink2" example illustrating flashing two LEDs at different rates.
|
||||
o 19Oct2013: This is the last v1.x release. It will continue to be available on GitHub
|
||||
as a branch named v1.3. Future development will continue with Sandy Walsh's v2.0 which
|
||||
can pass context (timer ID, etc.) to the callback functions.
|
||||
138
trunk/Arduino/libraries/Timer/Timer.cpp
Normal file
138
trunk/Arduino/libraries/Timer/Timer.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
Code by Simon Monk
|
||||
http://www.simonmonk.org
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
// For Arduino 1.0 and earlier
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include "Timer.h"
|
||||
|
||||
Timer::Timer(void)
|
||||
{
|
||||
}
|
||||
|
||||
int8_t Timer::every(unsigned long period, void (*callback)(), int repeatCount)
|
||||
{
|
||||
int8_t i = findFreeEventIndex();
|
||||
if (i == -1) return -1;
|
||||
|
||||
_events[i].eventType = EVENT_EVERY;
|
||||
_events[i].period = period;
|
||||
_events[i].repeatCount = repeatCount;
|
||||
_events[i].callback = callback;
|
||||
_events[i].lastEventTime = millis();
|
||||
_events[i].count = 0;
|
||||
return i;
|
||||
}
|
||||
|
||||
int8_t Timer::every(unsigned long period, void (*callback)())
|
||||
{
|
||||
return every(period, callback, -1); // - means forever
|
||||
}
|
||||
|
||||
int8_t Timer::after(unsigned long period, void (*callback)())
|
||||
{
|
||||
return every(period, callback, 1);
|
||||
}
|
||||
|
||||
int8_t Timer::oscillate(uint8_t pin, unsigned long period, uint8_t startingValue, int repeatCount)
|
||||
{
|
||||
int8_t i = findFreeEventIndex();
|
||||
if (i == NO_TIMER_AVAILABLE) return NO_TIMER_AVAILABLE;
|
||||
|
||||
_events[i].eventType = EVENT_OSCILLATE;
|
||||
_events[i].pin = pin;
|
||||
_events[i].period = period;
|
||||
_events[i].pinState = startingValue;
|
||||
digitalWrite(pin, startingValue);
|
||||
_events[i].repeatCount = repeatCount * 2; // full cycles not transitions
|
||||
_events[i].lastEventTime = millis();
|
||||
_events[i].count = 0;
|
||||
return i;
|
||||
}
|
||||
|
||||
int8_t Timer::oscillate(uint8_t pin, unsigned long period, uint8_t startingValue)
|
||||
{
|
||||
return oscillate(pin, period, startingValue, -1); // forever
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will generate a pulse of !startingValue, occuring period after the
|
||||
* call of this method and lasting for period. The Pin will be left in !startingValue.
|
||||
*/
|
||||
int8_t Timer::pulse(uint8_t pin, unsigned long period, uint8_t startingValue)
|
||||
{
|
||||
return oscillate(pin, period, startingValue, 1); // once
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will generate a pulse of startingValue, starting immediately and of
|
||||
* length period. The pin will be left in the !startingValue state
|
||||
*/
|
||||
int8_t Timer::pulseImmediate(uint8_t pin, unsigned long period, uint8_t pulseValue)
|
||||
{
|
||||
int8_t id(oscillate(pin, period, pulseValue, 1));
|
||||
// now fix the repeat count
|
||||
if (id >= 0 && id < MAX_NUMBER_OF_EVENTS) {
|
||||
_events[id].repeatCount = 1;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
void Timer::stop(int8_t id)
|
||||
{
|
||||
if (id >= 0 && id < MAX_NUMBER_OF_EVENTS) {
|
||||
_events[id].eventType = EVENT_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::update(void)
|
||||
{
|
||||
unsigned long now = millis();
|
||||
update(now);
|
||||
}
|
||||
|
||||
void Timer::update(unsigned long now)
|
||||
{
|
||||
for (int8_t i = 0; i < MAX_NUMBER_OF_EVENTS; i++)
|
||||
{
|
||||
if (_events[i].eventType != EVENT_NONE)
|
||||
{
|
||||
_events[i].update(now);
|
||||
}
|
||||
}
|
||||
}
|
||||
int8_t Timer::findFreeEventIndex(void)
|
||||
{
|
||||
for (int8_t i = 0; i < MAX_NUMBER_OF_EVENTS; i++)
|
||||
{
|
||||
if (_events[i].eventType == EVENT_NONE)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return NO_TIMER_AVAILABLE;
|
||||
}
|
||||
67
trunk/Arduino/libraries/Timer/Timer.h
Normal file
67
trunk/Arduino/libraries/Timer/Timer.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
Code by Simon Monk
|
||||
http://www.simonmonk.org
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#ifndef Timer_h
|
||||
#define Timer_h
|
||||
|
||||
#include <inttypes.h>
|
||||
#include "Event.h"
|
||||
|
||||
#define MAX_NUMBER_OF_EVENTS (10)
|
||||
|
||||
#define TIMER_NOT_AN_EVENT (-2)
|
||||
#define NO_TIMER_AVAILABLE (-1)
|
||||
|
||||
class Timer
|
||||
{
|
||||
|
||||
public:
|
||||
Timer(void);
|
||||
|
||||
int8_t every(unsigned long period, void (*callback)(void));
|
||||
int8_t every(unsigned long period, void (*callback)(void), int repeatCount);
|
||||
int8_t after(unsigned long duration, void (*callback)(void));
|
||||
int8_t oscillate(uint8_t pin, unsigned long period, uint8_t startingValue);
|
||||
int8_t oscillate(uint8_t pin, unsigned long period, uint8_t startingValue, int repeatCount);
|
||||
|
||||
/**
|
||||
* This method will generate a pulse of !startingValue, occuring period after the
|
||||
* call of this method and lasting for period. The Pin will be left in !startingValue.
|
||||
*/
|
||||
int8_t pulse(uint8_t pin, unsigned long period, uint8_t startingValue);
|
||||
|
||||
/**
|
||||
* This method will generate a pulse of pulseValue, starting immediately and of
|
||||
* length period. The pin will be left in the !pulseValue state
|
||||
*/
|
||||
int8_t pulseImmediate(uint8_t pin, unsigned long period, uint8_t pulseValue);
|
||||
void stop(int8_t id);
|
||||
void update(void);
|
||||
void update(unsigned long now);
|
||||
|
||||
protected:
|
||||
Event _events[MAX_NUMBER_OF_EVENTS];
|
||||
int8_t findFreeEventIndex(void);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
29
trunk/Arduino/libraries/Timer/examples/blink2/blink2.ino
Normal file
29
trunk/Arduino/libraries/Timer/examples/blink2/blink2.ino
Normal file
@@ -0,0 +1,29 @@
|
||||
//Flash two LEDs at different rates using Simon Monk's Timer library
|
||||
//http://www.doctormonk.com/2012/01/arduino-timer-library.html
|
||||
//
|
||||
//Jack Christensen 30Sep2013
|
||||
//
|
||||
//Beerware license: Free for any and all purposes, but if you find it
|
||||
//useful AND we actually meet someday, you can buy me a beer!
|
||||
|
||||
#include "Timer.h" //http://github.com/JChristensen/Timer
|
||||
|
||||
const int LED1 = 8; //connect one LED to this pin (with appropriate current-limiting resistor of course)
|
||||
const int LED2 = 9; //connect another LED to this pin (don't forget the resistor)
|
||||
const unsigned long PERIOD1 = 1000; //one second
|
||||
const unsigned long PERIOD2 = 10000; //ten seconds
|
||||
Timer t; //instantiate the timer object
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
pinMode(LED1, OUTPUT);
|
||||
pinMode(LED2, OUTPUT);
|
||||
t.oscillate(LED1, PERIOD1, HIGH);
|
||||
t.oscillate(LED2, PERIOD2, HIGH);
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
t.update();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "Timer.h"
|
||||
|
||||
Timer t;
|
||||
|
||||
int ledEvent;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
int tickEvent = t.every(2000, doSomething);
|
||||
Serial.print("2 second tick started id=");
|
||||
Serial.println(tickEvent);
|
||||
|
||||
pinMode(13, OUTPUT);
|
||||
ledEvent = t.oscillate(13, 50, HIGH);
|
||||
Serial.print("LED event started id=");
|
||||
Serial.println(ledEvent);
|
||||
|
||||
int afterEvent = t.after(10000, doAfter);
|
||||
Serial.print("After event started id=");
|
||||
Serial.println(afterEvent);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
t.update();
|
||||
}
|
||||
|
||||
void doSomething()
|
||||
{
|
||||
Serial.print("2 second tick: millis()=");
|
||||
Serial.println(millis());
|
||||
}
|
||||
|
||||
|
||||
void doAfter()
|
||||
{
|
||||
Serial.println("stop the led event");
|
||||
t.stop(ledEvent);
|
||||
t.oscillate(13, 500, HIGH, 5);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "Timer.h"
|
||||
|
||||
Timer t;
|
||||
int pin = 13;
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(pin, OUTPUT);
|
||||
t.pulse(pin, 10 * 1000, HIGH); // 10 seconds
|
||||
// t.pulse(pin, 10 * 60 * 1000, HIGH); // 10 minutes
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
t.update();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "Timer.h"
|
||||
|
||||
Timer t;
|
||||
int pin = 13;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(pin, OUTPUT);
|
||||
t.oscillate(pin, 100, LOW);
|
||||
t.every(1000, takeReading);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
t.update();
|
||||
}
|
||||
|
||||
void takeReading()
|
||||
{
|
||||
Serial.println(analogRead(0));
|
||||
}
|
||||
31
trunk/Arduino/libraries/Timer/keywords.txt
Normal file
31
trunk/Arduino/libraries/Timer/keywords.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For Timer Library
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
Timer KEYWORD1
|
||||
Event KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
every KEYWORD2
|
||||
after KEYWORD2
|
||||
oscillate KEYWORD2
|
||||
pulse KEYWORD2
|
||||
pulseImmediate KEYWORD2
|
||||
stop KEYWORD2
|
||||
update KEYWORD2
|
||||
findFreeEventIndex KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
Reference in New Issue
Block a user