SofaPirate / Plaquette

Object-oriented Arduino library for creative physical computing
https://sofapirate.github.io/Plaquette/
GNU General Public License v3.0
15 stars 4 forks source link

Add support for event-based triggers #79

Open sofian opened 1 year ago

sofian commented 1 year ago

Some of the nodes in Plaquette resonate better with an event-based approach than the current frame-by-frame approach. In particular DigitalNodes such as Metro, Alarm, PeakDetector which emit a "1" only at certain frames; or others where we could look at rose(), fell() or changed() conditions.

It would be a great feature to provide support for such events. There are a number of ways on how to do this.

Option 1: Global event function(s)

Allowing use of functions similar to Arduino's serialEvent() or Processing's mouseClicked() eg.

void roseEvent(Node& node) {
  if (node == button) ...
}

or

void event(Node& node, EventType type) {
  if (node == button && type == ROSE) ...
...
}

To prevent functions from firing all the time, Plaquette could maintain a list of nodes which it listens to eg. Plaquette.listen(button); or something like this.

Option 2: Plaquette-level callbacks

Similar but one directly assigns their own callbacks eg.

Plaquette.listen(button, onButtonPressed, ROSE);
...

void onButtonPressed() {
...
}

Option 3: Node-level callbacks

Similar but callbacks are retained by the nodes themselves:

button.attach(onButtonChanged);
...
void onButtonChanged() {
  if (button.rose()) ...
}