New library: Basic callback framework

Hi all,

We have developed a simple callback framework that allows calling a function when an user defined event is triggered.

(it is the callback_based branch that you should look at)

We thank your feedback!

Thanks!

I extracted this from your Github link

It is quite common to associate a function call to an event. For example, if pin X of the board goes UP one would like to turn a LED on.

I have to say I have a very jaundiced view of libraries. And I don't immediately see the value of using a library in place of

if (digitalRead(pinX) == HIGH) {
   digitalWrite(ledPin, HIGH);
}

or

digitalWrite(ledPin, digitalRead(pinX));

I prefer the idea of teaching people to fish rather than giving them a present of a nice salmon.

...R

Robin2:
I extracted this from your Github link
I have to say I have a very jaundiced view of libraries. And I don't immediately see the value of using a library in place of

if (digitalRead(pinX) == HIGH) {

digitalWrite(ledPin, HIGH);
}



or


digitalWrite(ledPin, digitalRead(pinX));




I prefer the idea of teaching people to fish rather than giving them a present of a nice salmon.

...R

Dea ...R,

Of course, for such a case it doesn't improve anything. Callbacks are useful in situation when you have to process events of very different nature and/or with static information.

Consider the following scenario and check the code you get if you implemented a button interface, where the button is associated to a varibale that it can increase. If the user presses the button for a while the increments increases so that with short button pressed the variable increases slowly and with longer pressings it increases much faster. Now add the fact that you want that a double tap of the button sets the variable to a default value, and that a triple tap (or any other sequence) stores the current value as default. Now imagine you have 5 buttons.

Maybe with this scenario you can appreciate the value of event callbacks.

If you look at the CmdMessenger library you will see another example where callbacks simplify your code extremely.

These libraries are useful to teach the logic of programming with physical constructions. Abstractions are not inherently bad, they fulfill a role, such as down-to-the-basics code... I could argue that digitalWrite is already nice salmon...you see it all depends on the objective.

Thanks for the feedback anyways. I am interested on feedback about the code efficiency as well. For example, to reduce its memory footprint.

dwengo:
I could argue that digitalWrite is already nice salmon…

Nice point …

Because of that I took a look at your code example (repeated here for readers’ conveninence)

#include <EventManager.h>

class Publisher: public EventPublisher
{
  uint8_t pin;
  unsigned int sinceLast;

  public:
    // This is the constructor. It is called
    // whenever we create an instance of this class.
    // It sets the parameters.
    Publisher(uint8_t P): pin(P), sinceLast(0){};

    // Here we say if the event was triggered,
    // We check for a LOW in pin.
    // We check every 200 ms to allow human input
    bool isTriggered()
    {
       bool is_down = false;
       if (millis()- sinceLast > 200)
       {
          sinceLast = millis();
          is_down   =  (digitalRead(pin) == LOW);
       };
      return is_down;
    }
};

// The pin with the led
const int ledPin = 13;

// Here we define the callback (assume we have a led on ledPin).
// All callbacks should receive a pointer to Event.
// Here we do not use it because this is a silly example
void writeHIGHtoPin(const Event * ev)
{
  static bool state = false; // just to remember the button state
  if (state)
  {
    digitalWrite(ledPin, LOW);
    state = false;
  }
  else
  {
    digitalWrite(ledPin, HIGH);
    state = true;
  }
}

// The pin with the button
const int buttonPin = 2;
// We create an instance of the class
// saying that it should pulish events on buttonPin
Publisher pin_is_low(buttonPin);

// We create the callback
Callback ledOn = writeHIGHtoPin;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  // We bind the callback and the event publisher
  dwenguinoManager.bind(ledOn, pin_is_low);
}

void loop()
{
  dwenguinoManager.update();
}

and I confess I can’t understand it.

I am not a C++ expert (I avoid it if I can) and I don’t understand the role of the class in the example. And I don’t understand the way in which the example is using the library.

And, dare I say it, I can understand Arduino code better than many who post questions on this Forum.

I think a small (or not so small) text book is required which explains the library in a lot more detail and explains what it can be useful for and what circumstances it would not be appropriate for.

…R

Robin2:
Nice point …

Because of that I took a look at your code example (repeated here for readers’ conveninence)

#include <EventManager.h>

class Publisher: public EventPublisher
{
  uint8_t pin;
  unsigned int sinceLast;

public:
    // This is the constructor. It is called
    // whenever we create an instance of this class.
    // It sets the parameters.
    Publisher(uint8_t P): pin(P), sinceLast(0){};

// Here we say if the event was triggered,
    // We check for a LOW in pin.
    // We check every 200 ms to allow human input
    bool isTriggered()
    {
      bool is_down = false;
      if (millis()- sinceLast > 200)
      {
          sinceLast = millis();
          is_down  =  (digitalRead(pin) == LOW);
      };
      return is_down;
    }
};

// The pin with the led
const int ledPin = 13;

// Here we define the callback (assume we have a led on ledPin).
// All callbacks should receive a pointer to Event.
// Here we do not use it because this is a silly example
void writeHIGHtoPin(const Event * ev)
{
  static bool state = false; // just to remember the button state
  if (state)
  {
    digitalWrite(ledPin, LOW);
    state = false;
  }
  else
  {
    digitalWrite(ledPin, HIGH);
    state = true;
  }
}

// The pin with the button
const int buttonPin = 2;
// We create an instance of the class
// saying that it should pulish events on buttonPin
Publisher pin_is_low(buttonPin);

// We create the callback
Callback ledOn = writeHIGHtoPin;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  // We bind the callback and the event publisher
  dwenguinoManager.bind(ledOn, pin_is_low);
}

void loop()
{
  dwenguinoManager.update();
}



and I confess I can't understand it.

I am not a C++ expert (I avoid it if I can) and I don't understand the role of the class in the example. And I don't understand the way in which the example is using the library.

And, dare I say it, I can understand Arduino code better than many who post questions on this Forum.

I think a small (or not so small) text book is required which explains the library in a lot more detail and explains what it can be useful for and what circumstances it would not be appropriate for.

...R

Thank you for your answer. If you can’t read C++ there isn’t much I can do about it.

I will see if I can improve the already verbose documented example, I will welcome some comments from somebody confident with C++.

dwengo:
Thank you for your answer. If you can't read C++ there isn't much I can do about it.

That is not an unusual answer from a certain sector of the programming fraternity.

But it is not very realistic unless your code is intended only for experts.
My own interest lies in helping people to use the Arduino.

...R

Robin2:
That is not an unusual answer from a certain sector of the programming fraternity.

But it is not very realistic unless your code is intended only for experts.
My own interest lies in helping people to use the Arduino.

...R

Your reluctance to learn is getting a bit old. No wonder you can't see the use for these things.
You continually push your own ideas onto people; not saying they are bad, but sometimes heavily misguided by your own lack of understanding.

Do yourself a favor, make a cup of tea and do some light reading.
http://www.cplusplus.com/doc/tutorial/

This will also help you understand the actual Arduino core, which is built using C++. (only 6 of the 24 source files are C)

pYro_65:
Do yourself a favor, make a cup of tea and do some light reading.

I am hoping the OP will make the use of his library easily understandable by a newcomer to the Arduino system who knows even less than I do.

I frequently refer to the cplusplus website.

...R