EventFuse Library for Arduino
Author:  David Knaack
Contact: davidknaack@gmail.com


Navigation


History

 0.1 2009-06-16: Initial Release
 0.2 2009-07-20: Interface update
 0.3 2012-04-27: Fix for INF_REPEAT issue


Description

EventFuse is a library for the Arduino.

It is useful for triggering actions after a specific number of events have occured. The user may set a 'fuse' of a specific length and a callback function that will be executed when the fuse runs out. Fuses may be set to repeat a specific number of times or to be unlimited. Fuses may be paused, resumed and deleted.

Each time the user calls the 'burn' function of the Fuses object all active fuses will be shortened by the specified quantity (usually 1). The callback function will then be called for any fuse that reaches 0 or less.

In the callback function the user may manipulate the fuse settings by setting a new fuse length or repeat count or disabling the fuse.

The burn function may be called from a timer routine to provide a time-based countdown feature, or it can be called in response to other programmed events.


Download, install and import

Download v0.3 from GitHub or EventFuse.zip (v0.2, see GitHub change log for updates)

Put the EventFuse folder in the Arduino "libraries" folder.

In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch->Import Library->EventFuse".


Setup

The library declares a default instance of the EventFuse class called eventFuse. Additional instances may be created if desired.


Methods

FuseID newFuse(int userData, int fuseLen, unsigned int repeatCount, eventFuseCallback_t fuseCallback);

FuseID newFuse(int fuseLen, unsigned int repeatCount, eventFuseCallback_t fuseCallback);

Set a fuse of length fuseLen units that will repeat repeatCount times before it is disabled and that will call fuseCallback each time the length reaches zero or less.

void resetFuse(FuseID fuse, int userData, int fuseLen, unsigned int repeatCount, eventFuseCallback_t fuseCallback);

void resetFuse(FuseID fuse, int fuseLen, unsigned int repeatCount, eventFuseCallback_t fuseCallback);

Update the settings on an existing fuse. May also be used to create a fuse with the specified ID, but can potentially overwrite an existing fuse.

void burn(int len);

Reduce the length of all fuses by len. The value may be negative to increase the length of the fuse, but be aware that no range checking is performed, it is possible for fuseLen to wrap. After the lengths have been adjusted the callback for each fuse with a length of zero or less will executed, the repeatCount reduced and the fuse length reset. When the repeatCount reaches zero the fuse state will be set to fsUnallocated. The repeat count for fuses with a repeat count of INF_REPEAT will not be adjusted.

Take care to avoid coding a callback routine such that another call to burn() can be made. This can occur if burn() is called by an interrupt-driven event.

For example, in the second example below if the toggleOutput() function were to call delay(2) the MsTimer2 interrupt callback would trigger tick() which then calls burn(). In the burn() function the toggleOutput() callback is called again and the process repeats.


Example

LEDFade

  1. /*
  2.  *
  3.  * Description:
  4.  * Fade an LED by combining the output of two
  5.  * fuses with similar intervals. The combined
  6.  * output exhibits a beat pattern that varies
  7.  * the output pulse width.
  8.  *
  9.  */
  10. #include <EventFuse.h>
  11.  
  12. int ledPin = 13;       // LED output pin
  13. boolean output = LOW;  // Output state
  14.  
  15. void FuseEvent(FuseID fuse, int userData){
  16.   output = !output;
  17.   digitalWrite( ledPin, output );
  18. }
  19.  
  20. void setup() {
  21.   pinMode(ledPin, OUTPUT);
  22.  
  23.   // Set up the two fade fuses
  24.   eventFuse.newFuse( 150, INF_REPEAT, FuseEvent );
  25.   eventFuse.newFuse( 152, INF_REPEAT, FuseEvent );
  26. }
  27.  
  28. void loop(){
  29.   delayMicroseconds(100);
  30.   eventFuse.burn(1);
  31. }

LEDFadeInt

  1. /*
  2.  *
  3.  * Description:
  4.  * Fade an LED by combining the output of two
  5.  * fuses with similar intervals. The combined
  6.  * output exhibits a beat pattern that varies
  7.  * the output pulse width.
  8.  *
  9.  * Use the interrupt based MsTimer2 library as
  10.  * a source for a 1mS event for the burn function.
  11.  *
  12.  */
  13.  
  14. #include <EventFuse.h>
  15. #include <MsTimer2.h>
  16.  
  17. int ledPin = 13;       // LED output pin
  18. boolean output = LOW;  // Output state
  19.  
  20. void ToggleOutput(FuseID fuse, int userData){
  21.   output = !output;
  22.   digitalWrite( ledPin, output );
  23. }
  24.  
  25. void tick(){
  26.   eventFuse.burn(1);
  27. }
  28.  
  29. void setup() {
  30.   pinMode(ledPin, OUTPUT);
  31.   eventFuse.newFuse( 20, INF_REPEAT, ToggleOutput );
  32.   eventFuse.newFuse( 21, INF_REPEAT, ToggleOutput );
  33.  
  34.   MsTimer2::set( 1, tick );
  35.   MsTimer2::start();
  36. }
  37.  
  38. void loop(){
  39. }

SerialInterval

  1. // This example sends a Serial message every 250 milliseconds
  2.  
  3. #include <EventFuse.h> // Include the EventFuse library
  4.  
  5. void sendMessage(FuseID fuse, int userData){
  6.   // Output all the analog readings separated by a space character
  7.   for (int i = 0; i < 6; i++ ) {
  8.     Serial.print (analogRead(i));
  9.     Serial.print(' ',BYTE);
  10.   }
  11.  
  12.   // Terminate message with a linefeed and a carriage return
  13.   Serial.print('\r\n', BYTE);
  14. }
  15.  
  16. void setup() {
  17.   Serial.begin(115200); // Start the Serial communication
  18.   eventFuse.newFuse( 250, INF_REPEAT, sendMessage );
  19. }
  20.  
  21. void loop() {
  22.   delay(1);
  23.   eventFuse.burn(1);
  24. }

LampTimer

  1. /*
  2.  *
  3.  * Description:
  4.  * EventFuse example demonstrating control of
  5.  * multiple independent switched outputs. Each
  6.  * output can be configured with independent
  7.  * on and off durations with a minimum of 1 second
  8.  * and a maximum of about 18 hours (2^16 seconds).
  9.  *
  10.  */
  11.  
  12. #include <EventFuse.h>
  13. #include <MsTimer2.h>
  14.  
  15. #define OutputCount 4
  16. // These would be better handled as enums,
  17. // but that requires a seperate .h file.
  18. #define OffTime 0
  19. #define OnTime 1
  20. #define OutputPin 2
  21.  
  22. // The outputs array defines how long each output will
  23. // be turned off, on, and what pin to use for that output.
  24. // The off and on values are in units of 'ticks'. The length
  25. // of a tick is controlled by the setup of MsTimer2.
  26.                              // off   on  pin
  27. byte outputs[OutputCount][3] ={{  5,  10,  13},   // Output A
  28.                                { 15,  20,  12},   // Output B
  29.                                {  2,  12,  11},   // Output C
  30.                                { 10,   2,  10},}; // Output D
  31.  
  32. void OutputHandler(FuseID fuseID, int outputID){
  33.   // look up the pin associated with this output
  34.   byte pin = outputs[outputID][OutputPin];
  35.  
  36.   // get and invert the current pin state and write
  37.   // it back to the port to invert the current pin state.
  38.   int state = 1&~digitalRead(pin);
  39.   digitalWrite( pin, state );
  40.  
  41.   // Reset the fuse length with a new interval. The current state
  42.   // of the pin is used to determine which interval should be used.
  43.   eventFuse[fuseID].fuseLen = outputs[outputID][state];
  44. }
  45.  
  46. void timerTick(){
  47.   eventFuse.burn(1);
  48. }
  49.  
  50. void setup() {
  51.   // Set up and init all outputs to off
  52.   for(byte i = 0; i<OutputCount; i++){
  53.     pinMode( outputs[i][OutputPin], OUTPUT);
  54.     digitalWrite( outputs[i][OutputPin], LOW );
  55.  
  56.     // Set up an event fuse for this output.
  57.     eventFuse.newFuse( i, outputs[i][OffTime], INF_REPEAT, OutputHandler );
  58.   }
  59.  
  60.   // Set MsTimer2 for one second per tick.
  61.   MsTimer2::set(1000, timerTick );
  62.   MsTimer2::start();
  63. }
  64.  
  65. void loop(){
  66. }


Information about this page

Last Modified: April 27, 2012, at 10:06 PM
By: DaveK

Share