Git Product home page Git Product logo

eventually's People

Contributors

benjenkinson avatar johnnyb avatar matthewturner avatar

Stargazers

 avatar  avatar  avatar  avatar

eventually's Issues

extraData

extraData was removed from EvtListener (probably to save some memory). However it is very useful to have the option to assign extraData to a listener (e.g. an object uses a listener and in the callback the object has to be assigned. extraData can be used to store a pointer to the object that owns the listener)

I would propose to reintroduce extraData to EvtListener that was removed here: 0a3f2e4#diff-2f154c55f0e4e9f7f0ecadefc29fde25da27e0fe45a7661e8c40c0caff81cbca

Adding a listener twice

Is it really wanted that the same listener can be added twice?

EvtManager mgr(true);  // true to manage memory
EvtOnceListener readShtListener((EvtAction)[](IEvtListener *lst, IEvtContext *ctx) {
  readSht();
  return false;  // do not stop the current action chain, execute other events
});

      Serial.println("listenerCount = " + mgr.listenerCount());
      mgr.addListener(&readShtListener);
      Serial.println("listenerCount = " + mgr.listenerCount());
      mgr.addListener(&readShtListener);
      Serial.println("listenerCount = " + mgr.listenerCount());

outputs

listenerCount = 1
listenerCount = 2
listenerCount = 3

Just for information:

#ifndef EvtOnceListener_h
#define EvtOnceListener_h

#include "EvtListener.h"

class EvtOnceListener : public EvtListener {
 public:
  EvtOnceListener();
  EvtOnceListener(EvtAction triggerAction);
  ~EvtOnceListener();
  void reset();
  bool isEventTriggered();
  bool performTriggerAction(IEvtContext *);
};

EvtOnceListener::EvtOnceListener() {
  // Nothing to construct
}

EvtOnceListener::EvtOnceListener(EvtAction triggerAction) {
  _triggerAction = triggerAction;
}

EvtOnceListener::~EvtOnceListener() {
  // Nothing to destruct
}

void EvtOnceListener::reset() {
  // Nothing to setup
}

/**
 * @brief  Evaluate if action shall be triggered / executed (performTriggerAction())
 * @author Holger Mueller
 * @date   2024-02-20
 *
 * @return returns true if the event is triggered.
 */
bool EvtOnceListener::isEventTriggered() {
  return _enabled;
}

/**
 * @brief  Perform the triggered action and disable further triggers
 * @author Holger Mueller
 * @date   2024-02-21
 * 
 * @param  ctx EvtManager context
 * @return Return value of the action. 
 */
bool EvtOnceListener::performTriggerAction(IEvtContext *ctx) {
  Serial.println("EvtOnceListener::performTriggerAction execution started"); // TODO: delme
  bool returnVal = (*_triggerAction)(this, ctx);
  disable();
  Serial.println("EvtOnceListener::performTriggerAction execution ended"); // TODO: delme
  return returnVal;
}

#endif

How to work with classes?

Hi, I'm working on a complex project where I need to work with classes. So I tried the SimpleButtonBlink example, but since resetContext() does not exist anymore I replaced it with reset(), changed pins and made a couple more changes.

#include <Eventually.h>

#define LIGHT_PIN  13
#define BUTTON_PIN 3

bool speed = LOW;
bool pin_state = LOW;
EvtManager mgr;

bool blink()
{
  pin_state = !pin_state;
  digitalWrite(LIGHT_PIN, pin_state);
  return false;
}

bool run()
{
  mgr.reset();
  // Won't this call overflow the stack???
  mgr.addListener(new EvtPinListener(BUTTON_PIN, 20, LOW, (EvtAction)&run));
  mgr.addListener(new EvtTimeListener(speed == HIGH ? 100 : 500, true, (EvtAction)&blink));
  speed = !speed;
}

void setup()
{
  pinMode(LIGHT_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  run();
}

void loop()
{
  mgr.loopIteration();
}

So far so good, worked without problems. Because I need classes in my project, next step was to create a Blinker class to encapsulate the main functionality:

#include <Eventually.h>

#define LIGHT_PIN  13
#define BUTTON_PIN 3

EvtManager mgr;

class Blinker
{
  private:
    bool pin_state = HIGH;
    bool speed = HIGH;
    uint8_t lightPin;
    uint8_t btnPin;
    bool blink();

  public:
    Blinker(uint8_t led, uint8_t button);
    bool run();
};

Blinker::Blinker(uint8_t led, uint8_t button)
{
  lightPin = led;
  btnPin = button;
}

bool Blinker::blink()
{
  pin_state = !pin_state;
  digitalWrite(lightPin, pin_state);
  return false;
}

bool Blinker::run()
{
  mgr.reset();
  // Won't this call overflow the stack???
  mgr.addListener(new EvtPinListener(BUTTON_PIN, 20, LOW, (EvtAction)&run));
  mgr.addListener(new EvtTimeListener(speed == HIGH ? 100 : 500, true, (EvtAction)&blink));
  speed = !speed;
}

Blinker blinker = Blinker(LIGHT_PIN, BUTTON_PIN);

void setup()
{
  blinker.run();
}

void loop()
{
  mgr.loopIteration();
}

But this code doesn't work. The private fields do not seem to retain state, but that's just a guess. I made several other attempts, including passing a pointer to mgr and creating the listeners as variables inside the class (equivalent to the "consider declaring the listeners as global variables..." part in the README). No luck.

It might doing something terribly wrong here, but after several attempts I still have no clue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.