[
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2016 Jonathan Bartlett\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "Eventually\n==========\nAn Arduino Event-based Programming Library\n------------------------------------------\n\nArduino programming is a bit of a bait-and-switch.  At first, people are like, \"look, it's super-easy - see my blink program!\"  Then, when it comes to writing anything with even the most minute complexity - with states and input and output - one has to switch to a horrid event-programming mindset with thousands of state variables everywhere.\n\nThen, when you have to debounce the buttons, it gets even worse!\n\nThe goal of Eventually is to make a more event-oriented environment for Arduino programming.  To make it *actually* easy to use to build worthwhile projects.\n\n### Writing a Simple Eventually Program\n\nTo show the program in action, we will write a program that blinks a light,when a button is pushed.\n\nTo use Eventually, you need to download the library and then include it in your code (Sketch -> Include Library).  That should put the following line at the top of your program:\n\n    #include <Eventually.h>\n\nNext, you need to include a global variable for the event manager.  I usually call this \"mgr\":\n\n    EvtManager mgr;\n\nEventually programs do not use the loop() function like other Arduino programs.  Instead, remove the auto-generated loop() function and in its place put in this line of code (it SHOULD NOT be within a function) to use our event manager instead of the normal Arduino loop (notice that there is NO SEMICOLON at the end):\n\n    USE_EVENTUALLY_LOOP(mgr)\n\nNow, during our setup() function, instead of just doing pinModes, you also need to setup one or more listeners.  The two primary types of listeners for Eventually are EvtPinListener and EvtTimeListener.  We are going to use both.  We will use the pin listener to tell us when the button is pushed and the time listener to blink the light.  Therefore, our setup function will look like this:\n\n    #define LIGHT_PIN 5\n    #define BUTTON_PIN 3\n\n    void setup() {\n      /* Set the pin modes */\n      pinMode(LIGHT_PIN, OUTPUT);\n      pinMode(BUTTON_PIN, INPUT);\n\n      /* Add a button listener */\n      mgr.addListener(new EvtPinListener(BUTTON_PIN, (EvtAction)start_blinking));\n    }\n\nAlternatively, if our BUTTON_PIN uses INPUT_PULLUP:\n\n  void setup() {\n\n    // ...\n\n    /* Button is LOW when pushed, so use INPUT_PULLUP */\n    pinMode(BUTTON_PIN, INPUT_PULLUP);\n\n    /* Add a button listener, listening for LOW */\n    /* (To use this constructor, we also have to specify the debounce delay) */\n    mgr.addListener(new EvtPinListener(BUTTON_PIN, 40, LOW, (EvtAction)start_blinking));\n\n  }\n\nThis creates a listener for the given button pin, and will call start_blinking when the button is pushed.  Note that by default, EvtPinListeners will automatically debounce the pin, and also make sure that it starts in the opposite state before beginning listening (though both of these are configurable).\n\nAlso note that we put (EvtAction) in front of our function name.  This makes sure the compiler knows that this will be used as an event action.  It may not compile without this marking.\n\nNow, when the button is pushed, we need start_blinking to install a new time-based listener to blink the light, and another button listener to stop the blinking.  So the code will look like this:\n\n    bool start_blinking() {\n      mgr.resetContext(); \n      mgr.addListener(new EvtTimeListener(500, true, (EvtAction)blink_pin));\n      mgr.addListener(new EvtPinListener(BUTTON_PIN, (EvtAction)stop_blinking));\n      return true;\n    }\n\nThis starts by calling resetContext(), which will uninstall all current listeners to prepare for a new set.  Then, it adds a time listener.  The first parameter to the EvtTimeListener constructor is the number of milliseconds to wait until firing.  In this case, 500 (half a second).  The second parameter is whether or not to continually fire.  If it is set to false, it will fire once and then be done.  The last parameter is the function to call.\n\nThen it adds a pin listener to call the stop_blinking function when pushed.  All triggered actions need to return true or false.  \"true\" stops the current action chain and is usually what you want.\n\nNow, let's look at the blink_pin function:\n\n    bool blink_state = LOW;\n    bool blink_pin() {\n      blink_state = !blink_state;\n      digitalWrite(LIGHT_PIN, blink_state);\n      return false;\n    }\n\nThis creates a global variable that tells us the blink state, and whenever this is called, it alternates the blink state and writes that to the pin.  This one returns false because, since the blinking doesn't affect the action of other pins, the event chain should continue.\n\nFinally, we need a stop_blinking() function.  All this will do is create a new listener to wait for it to start again:\n\n    bool stop_blinking() {\n      mgr.resetContext();\n      mgr.addListener(new EvtPinListener(BUTTON_PIN, (EvtAction)start_blinking));\n      return true;\n    }\n\nNow you are ready to go!\n\n### C++ Issues\n\nOne of the goals of this library was to make it use the flexibility of C++ without the user having to know a lot of C++.  If you don't know C++, just use the listeners in the basic way mentioned.  However, if you want to do more advanced things, this section covers the C++ issues.  \n\nOne main goal is to take advantage of the flexibility available with C++ pointers but without exposing the user to them.  Therefore, listeners are always created with the \"new\" keyword, but they are immediately added to the manager (technically, the context) which then manages it.  Once the listener is added to the manager/context, then the responsibility for its memory management goes to the manager/context.  Note that at this time, there is no ability for user code to detect when a listener gets destroyed, so if you add data to a listener (see section below), then it should not rely on the listener to destroy the data at the right time.\n\n### Writing Your Own Listeners\n\nIf you need a more advanced listener, you can write your own fairly easily.\nYou need to create a subclass of EvtListener and add (a) a constructor, (b) a setupListener() method, and (c) an isEventTriggered() method.\n\nThe constructor simply takes whatever arguments control the listener's behavior.  The setupListener() method gets called when the listener is added to the chain.  This should handle doing things such as sampling the current time or pin states that you will need to determine if the event is triggered.\n\nThe isEventTriggered() method returns true if the event is triggered.\n\nBelow is a simple class that simply always fires:\n\n    class EvtAlwaysFiresListener : public EvtListener {\n      public:\n      EvtAlwaysFiresListener();\n      void setupListener();\n      bool isEventTriggered();\n    }\n    EvtAlwaysFiresListener::EvtAlwaysFiresListener() {\n      // Nothing to construct\n    }\n\n    void EvtAlwaysFiresListener::setupListener() {\n      // Nothing to setup\n    }\n\n    bool EvtAlwaysFiresListener::isEventTriggered() {\n      return true;\n    }\n\n### Adding Data to Listeners\n\nThis library also has the ability to add small bits of data to listeners.  Each listener had a \"data\" member, which allows arbitrary data stored into it (type is a pointer to void, and the value defaults to 0).  The data variable is completely unmanaged, so use it for whatever you want.  It doesn't have to store a pointer, in fact, it is probably easier to manage if it doesn't.\n\nSo, for instance, we can rewrite the blink_pin function to not rely on a global variable, like this:\n\n    bool blink_pin(EvtListener *lstn) {\n      lstn->data = !lstn->data;\n      digitalWrite(LIGHT_PIN, lstn->data);\n      return false;\n    }\n\nThis uses the data variable to store the pin state.  Note that our action function changed signature as well.  The actual signature for the action function is (EvtListener *, EvtContext *).  However, the C++ stack will allow you to have the function with fewer arguments, as long as they are in the right order.  That is why we have to put (EvtAction) in front of our action functions - to get it to the right type.  Note that if we want access to more parts of our listener, we can also use the subclass type instead of just EvtListener if we know which one it will be.\n\n### Future Expansion\n\nThe two biggest features I want to add are (a) support for multiple contexts, and (b) declarative creation of a state machine.  There is some code in there for (a), but it is not ready to use.\n"
  },
  {
    "path": "examples/Blinky/Blinky.ino",
    "content": "#include <Eventually.h>\n\n/*\n * This is the standard blinky lights written using Eventually.\n * Just set LIGHT_PIN to whatever pin you have your LED attached to.\n */\n\n#define LIGHT_PIN 5\n\nEvtManager mgr;\nbool state = LOW;\n\nvoid setup() {\n  pinMode(LIGHT_PIN, OUTPUT);\n  mgr.addListener(new EvtTimeListener(1000, true, (EvtAction)blinkme)); \n}\n\nbool blinkme() {\n  state = !state; // Switch light states\n  digitalWrite(LIGHT_PIN, state); // Display the state\n  return false; // Allow the event chain to continue\n}\n\nUSE_EVENTUALLY_LOOP(mgr) // Use this instead of your loop() function.\n"
  },
  {
    "path": "examples/ListenerWithData/ListenerWithData.ino",
    "content": "#include <Eventually.h>\n\n/* \n * This is the same as the \"simple\" example, except\n * that it shows how to store data in the listener itself.\n */\n\n#define LIGHT_PIN 5\n#define BUTTON_PIN 3\n\nbool speed = LOW;\nEvtManager mgr;\n\nbool blink(EvtListener *l) {\n  Serial.write(\"hello\");\n  int data = (int)l->extraData;\n  Serial.write(data);\n  if(data == 0) {\n    data = 1;\n  } else {\n    data = 0;\n  }  \n  Serial.write(data);\n  Serial.write(\"done\");\n  digitalWrite(LIGHT_PIN, data);\n  l->extraData = (void *)data;\n  return false;\n}\n\nbool set_speed() {\n  mgr.resetContext();\n  mgr.addListener(new EvtPinListener(BUTTON_PIN, (EvtAction)set_speed));\n  speed = !speed; // Change speeds\n  if(speed == HIGH) {\n    mgr.addListener(new EvtTimeListener(250, true, (EvtAction)blink));\n  } else {\n    mgr.addListener(new EvtTimeListener(1000, true, (EvtAction)blink));\n  }\n\n  return true;\n}\n\nvoid setup() {\n  pinMode(LIGHT_PIN, OUTPUT);\n  pinMode(BUTTON_PIN, INPUT);\n  set_speed();\n}\n\nUSE_EVENTUALLY_LOOP(mgr)\n"
  },
  {
    "path": "examples/SimpleButtonBlink/SimpleButtonBlink.ino",
    "content": "#include <Eventually.h>\n\n/* \n * This just shows a simple way of using the library.\n * Pushing a button moves the Arduino back-and-forth\n * from a fast blink to a slow blink.\n */\n\n#define LIGHT_PIN 5\n#define BUTTON_PIN 3\n\nbool speed = LOW;\nEvtManager mgr;\nbool pin_state = LOW;\n\nbool blink() {\n  pin_state = !pin_state;\n  digitalWrite(LIGHT_PIN, pin_state);\n  return false;\n}\n\nbool set_speed() {\n  mgr.resetContext();\n  mgr.addListener(new EvtPinListener(BUTTON_PIN, (EvtAction)set_speed));\n  speed = !speed; // Change speeds\n  if(speed == HIGH) {\n    mgr.addListener(new EvtTimeListener(250, true, (EvtAction)blink));\n  } else {\n    mgr.addListener(new EvtTimeListener(1000, true, (EvtAction)blink));\n  }\n\n  return true;\n}\n\nvoid setup() {\n  pinMode(LIGHT_PIN, OUTPUT);\n  pinMode(BUTTON_PIN, INPUT);\n  set_speed();\n}\n\nUSE_EVENTUALLY_LOOP(mgr)\n"
  },
  {
    "path": "examples/TennisGame/TennisGame.ino",
    "content": "#include <Eventually.h>\n\n/*\n * This is a simple Tennis game using the Eventually library.\n * To use, wire up two buttons (one for each player, and then\n * a series of lights to be used for the ball.  You can\n * change PLAYER_BUTTON_1 and PLAYER_BUTTON_2 to whatever you like.\n * The ball lights need to be in a sequential series of pins starting\n * with BALL_PIN_START and ending with BALL_PIN_END.  The code\n * will auto-adapt to however many pins you use, as long as you tell\n * it where the start and end points are.\n */\n\n#define PLAYER_BUTTON_1 3\n#define PLAYER_BUTTON_2 2\n\n#define BALL_PIN_START 5\n#define BALL_PIN_END 9\n\n#define BALL_SPEED 1000\n#define TURN_DELAY 1000\n\nEvtManager mgr;\n\nint currentPlayer;\nint numBallPositions = BALL_PIN_END - BALL_PIN_START + 1;\nint lastBallPosition = BALL_PIN_END - BALL_PIN_START;\nint currentBallPosition;\n\nvoid setup() {\n  pinMode(PLAYER_BUTTON_1, INPUT);\n  pinMode(PLAYER_BUTTON_2, INPUT);\n  for(int i = BALL_PIN_START; i <= BALL_PIN_END; i++) {\n    pinMode(i, OUTPUT);\n  }\n\n  currentPlayer = 1;\n  readyPlayer();\n}\n\nvoid readyPlayer() {\n  mgr.resetContext();\n\n  if(currentPlayer == 1) {\n    currentBallPosition = 0;\n  } else {\n    currentBallPosition = lastBallPosition;\n  }\n  showBall(currentBallPosition);\n\n  mgr.addListener(new EvtPinListener(PLAYER_BUTTON_1, (EvtAction)startNextPlayer));\n}\n\nvoid showBall(int offset) {\n  for(int i = 0; i + BALL_PIN_START <= BALL_PIN_END; i++) {\n    if(i == offset) {\n      digitalWrite(BALL_PIN_START + i, HIGH);\n    } else {\n      digitalWrite(BALL_PIN_START + i, LOW);\n    }\n  }\n}\n\nvoid showAll() {\n  for(int i = 0; i + BALL_PIN_START <= BALL_PIN_END; i++) {\n    digitalWrite(BALL_PIN_START + i, HIGH);\n  }\n}\n\nbool startNextPlayer() {\n  mgr.resetContext();\n\n  mgr.addListener(new EvtTimeListener(BALL_SPEED, true, (EvtAction)moveBall));\n\n  if(currentPlayer == 1) {\n    currentPlayer = 2;\n    mgr.addListener(new EvtPinListener(PLAYER_BUTTON_2, (EvtAction)swingRacket));\n    currentBallPosition = 0;\n  } else {\n    currentPlayer = 1;\n    mgr.addListener(new EvtPinListener(PLAYER_BUTTON_1, (EvtAction)swingRacket));\n    currentBallPosition = lastBallPosition;\n  }\n\n  return true;\n}\n\n\nbool moveBall() {\n  if(currentPlayer == 1) {\n    currentBallPosition--;\n    if(currentBallPosition < 0) {\n      return missedBall();\n    }\n  } else {\n    currentBallPosition++;\n    if(currentBallPosition > lastBallPosition) {\n      return missedBall();\n    }\n  }\n\n  showBall(currentBallPosition);\n  return false;\n}\n\n\nbool swingRacket() {\n  if((currentPlayer == 1 && currentBallPosition == 0) || (currentPlayer == 2 && currentBallPosition == lastBallPosition)) {\n    return hitBall();\n  } else {\n    return missedBall();\n  }\n}\n\nbool hitBall() {\n  // If we hit, it moves to be the next player's ball\n  return startNextPlayer();\n}\n\nbool missedBall() {\n  mgr.resetContext();\n  showAll();\n  currentPlayer = 1;\n  mgr.addListener(new EvtTimeListener(TURN_DELAY, false, (EvtAction)readyPlayer));\n\n  return true;\n}\n\nUSE_EVENTUALLY_LOOP(mgr)\n"
  },
  {
    "path": "library.properties",
    "content": "name=Eventually\nversion=0.1.5\nauthor=Jonathan Bartlett <jonathan@bartlettpublishing.com>\nmaintainer=Jonathan Bartlett <jonathan@bartlettpublishing.com>\nsentence=Event-based programming library for Arduino\nparagraph=This library is meant to make Arduino programming tasks much more simplified by using an event-driven model rather than the standard looping model.\ncategory=Other\nurl=http://www.github.com/johnnyb/Eventually\narchitectures=*\nincludes=Eventually.h\n"
  },
  {
    "path": "src/Eventually.cpp",
    "content": "/*\n * This program is copyright 2016 by Jonathan Bartlett.  See LICENSING\n * file for information on usage (MIT License).  \n * Be sure to check out my books at www.bplearning.net!\n */\n\n#include <Eventually.h>\n\n/* *** EVT MANAGER *** */\nEvtManager::EvtManager() {\n  contextStack = new EvtContext[EVENTUALLY_MAX_CONTEXTS];\n  contextStack[contextOffset].setupContext();\n}\n\nvoid EvtManager::addListener(EvtListener *lstn) {\n  contextStack[contextOffset].addListener(lstn);\n}\n\nvoid EvtManager::removeListener(EvtListener *lstn) {\n    contextStack[contextOffset].removeListener(lstn);\n}\n\nEvtContext *EvtManager::currentContext () {\n  return &contextStack[contextOffset];\n}\n\nEvtContext *EvtManager::pushContext() {\n  contextOffset++;\n  contextStack[contextOffset].setupContext();\n  return &contextStack[contextOffset];\n}\n\nEvtContext *EvtManager::resetContext() {\n  contextStack[contextOffset].setupContext();\n  return &contextStack[contextOffset];\n}\n\nEvtContext *EvtManager::popContext() {\n  contextOffset--;\n  return &contextStack[contextOffset];\n}\n\nvoid EvtManager::loopIteration() {\n  contextStack[contextOffset].loopIteration();\n}\n\n/* *** EVT CONTEXT *** */\n\nEvtContext::EvtContext() {\n}\n\nvoid EvtContext::loopIteration() {\n  for(int i = 0; i < listenerCount; i++) {\n    if(listeners[i]) { // Make sure it isn't deleted  \n      if(listeners[i]->isEventTriggered()) { // If we are triggered, run the action\n        if(listeners[i]->performTriggerAction(this)) { // If the action returns true, stop the chain\n          return;\n        }\n      }\n    }\n  }\n}\n\nvoid EvtContext::setupContext() {\n  if(data){\n    delete data;\n  }\n  if(listeners) {\n    for(int i = 0; i < listenerCount; i++) {\n      if(listeners[i]) {\n        delete listeners[i];\n      }\n    }\n    delete listeners;\n  }\n\n  listeners = new EvtListener *[EVENTUALLY_MAX_LISTENERS];\n  listenerCount = 0;\n}\n  \nvoid EvtContext::addListener(EvtListener *lstn) {\n  int i = 0;\n\n  // find the first empty slot\n  for(; i < listenerCount; i++) {\n    if(listeners[i] == 0) {\n      break;\n    }\n  }\n\n  // if we didn't find one, expand\n  if(i == listenerCount) {\n    listenerCount++;\n  }\n  \n  listeners[i] = lstn;\n  lstn->setupListener();\n}\n\nvoid EvtContext::removeListener(EvtListener *lstn) {\n  for(int i = 0; i < listenerCount; i++) {\n    if(listeners[i] == lstn) {\n      delete lstn;\n      listeners[i] = 0;      \n    }\n  }\n}\n\n/* *** EVT LISTENER *** */\n\nvoid EvtListener::setupListener() {\n  \n}\n\nbool EvtListener::isEventTriggered() {\n  return false;\n}\n\nbool EvtListener::performTriggerAction(EvtContext *ctx) {\n  return (*triggerAction)(this, ctx);\n}\n\n/* *** EVT PIN LISTENER *** */\n\nEvtPinListener::EvtPinListener() {\n  \n}\n\nEvtPinListener::EvtPinListener(int pin, int debounce, bool targetValue, EvtAction action) {\n  this->pin = pin;\n  this->debounce = debounce;\n  this->targetValue = targetValue;\n  this->triggerAction = action;\n}\n\nEvtPinListener::EvtPinListener(int pin, int debounce, EvtAction action) {\n  this->pin = pin;\n  this->debounce = debounce;\n  this->triggerAction = action;\n}\n\nEvtPinListener::EvtPinListener(int pin, EvtAction action) {\n  this->pin = pin;\n  this->triggerAction = action;\n}\n\nvoid EvtPinListener::setupListener() {\n  startState = digitalRead(pin);\n}\n\nbool EvtPinListener::isEventTriggered() {\n  bool val = digitalRead(pin); \n\n  // Debounce check if we were triggered earlier\n  if(firstNoticed) {\n    unsigned long curMillis = millis();\n    if(curMillis > firstNoticed + debounce) {\n      // Debounce time expired, check again\n\n      // Reset Watcher\n      firstNoticed = 0;\n\n      // Check\n      if(val == targetValue) {\n        return true;\n      } else {\n        return false;\n      }\n    } else {\n      // Waiting for debouncer to finish\n      return false;\n    }\n  }\n  \n  if(mustStartOpposite && (startState == targetValue)) {\n    /* This is a waiting loop to wait for the pin to change to the opposite state before sensing */\n    /* Q - do I need to debounce mustStartOpposite? */\n    if(val == startState) {\n      // Do nothing\n    } else {\n      startState = val;\n    }\n\n    return false;\n  } else {\n    /* This is the real deal */\n    if(val == targetValue) {\n      if(debounce == 0) {\n        return true;\n      } else {\n        firstNoticed = millis();\n        return false;\n      }\n    } else {\n      return false;\n    }\n  }\n}\n\n/* *** EVT TIME LISTENER *** */\nEvtTimeListener::EvtTimeListener() {\n  \n}\n\nEvtTimeListener::EvtTimeListener(unsigned long time, bool multiFire, EvtAction t) {\n  this->millis = time;\n  this->triggerAction = t;\n  this->multiFire = multiFire;\n}\n\nvoid EvtTimeListener::setupListener() {\n  startMillis = ::millis();\n}\n\nbool EvtTimeListener::isEventTriggered() {\n  unsigned long curTime = ::millis();\n  bool shouldFire = false;\n  if(curTime >= startMillis) {\n    /* Normal */\n    if(curTime - startMillis > this->millis) {\n      shouldFire = true;\n    }\n  } else {\n    /* Wrap-Around! */\n    if(((ULONG_MAX - startMillis) + curTime) > this->millis) {\n      shouldFire = true;\n    }\n  }\n\n  return shouldFire;  \n}\n\nbool EvtTimeListener::performTriggerAction(EvtContext *c) {\n  bool returnval = (*triggerAction)(this, c);\n  if(multiFire) {\n    // On multifire, setup to receive the event again\n    setupListener();\n    // On multifire, we shouldn't stop the event chain no matter what, since we are just restarting in this context\n    return false;\n  } else {\n\t  // remove this listener from the chain\n\t  // else it will keep triggering each loop\n\t  c->removeListener(this);\n\n    return returnval;\n  }\n}\n"
  },
  {
    "path": "src/Eventually.h",
    "content": "/*\n * This program is copyright 2016 by Jonathan Bartlett.  See LICENSING\n * file for information on usage (MIT License).  \n * Be sure to check out my books at www.bplearning.net!\n */\n\n#ifndef EVENTUALLY_H\n#define EVENTUALLY_H\n\n#include <limits.h>\n#include <Arduino.h>\n\n#define EVENTUALLY_MAX_CONTEXTS 10\n#define EVENTUALLY_MAX_LISTENERS 20\n\nclass EvtManager;\nclass EvtContext;\nclass EvtListener;\n\ntypedef bool (*EvtAction)(EvtListener *, EvtContext *);\n\nclass EvtManager {\n\n  public:\n\n  EvtManager();\n  void loopIteration();\n  EvtContext *pushContext();\n  EvtContext *resetContext();\n  EvtContext *popContext();\n  EvtContext *currentContext();\n  void addListener(EvtListener *lstn);\n  void removeListener(EvtListener *lstn);\n\n  private:\n  EvtContext *contextStack = 0;\n  int contextOffset = 0;\n  int contextDepth = 0;\n};\n\n// Note - should probably expand the number of available listeners by chaining contexts\nclass EvtContext {\n  public:\n  void *data = 0;\n\n  EvtContext();\n  void setupContext();\n  void loopIteration();\n  void addListener(EvtListener *lstn);\n  void removeListener(EvtListener *lstn);\n\n  private:\n  EvtListener **listeners = 0;\n  int listenerCount;\n};\n\n\nclass EvtListener {\n  public:\n  void *extraData = 0; // Anything you want to store here\n  EvtAction triggerAction;\n\n  virtual void setupListener();\n  virtual bool isEventTriggered();\n  virtual bool performTriggerAction(EvtContext *); // return false if I should stop the current chain\n\n  protected:\n};\n\nclass EvtPinListener : public EvtListener {\n  public:\n  EvtPinListener();\n  EvtPinListener(int pin, EvtAction trigger);\n  EvtPinListener(int pin, int debounce, EvtAction action);\n  EvtPinListener(int pin, int debounce, bool targetValue, EvtAction action);\n  int pin = 0;\n  int debounce = 40;  \n  bool targetValue = HIGH;\n  bool mustStartOpposite = true;\n  bool startState;\n  unsigned long firstNoticed = 0;\n\n  void setupListener();\n  bool isEventTriggered();\n};\n\nclass EvtTimeListener : public EvtListener {\n  public:\n  EvtTimeListener();\n  EvtTimeListener(unsigned long time, bool multiFire, EvtAction trigger);\n  unsigned long millis;\n  void setupListener();\n  bool isEventTriggered();\n  bool performTriggerAction(EvtContext *);\n  private:\n  unsigned long startMillis;\n  bool multiFire = false;\n  int numFires = 0;\n};\n\n#define USE_EVENTUALLY_LOOP(mgr) void loop() { mgr.loopIteration(); }\n\n#endif\n"
  }
]