Cosa: An Object-Oriented Platform for Arduino programming

Ferio:
...how I can subscribe to an event (for example of an ExternalInterruptPin) when subclassing is not an option? The class pushes an event but I do not see how I it can be handled from outside.

Hi Ferio. Thanks for your interest in this project. The short answer to your question is that you can always handle the event directly instead of calling dispatch(). Below is snippet from CosaFSMBlink which uses the dispatch-method.

void loop()
{
  // The basic event dispatcher
  Event event;
  Event::queue.await(&event);
  event.dispatch();
}

And below is a snippet from the CosaNEXAreceiver sketch which handles the event instead of calling the on_event() method.

void loop()
{
  // Wait for the next event
  Event event;
  Event::queue.await(&event);
  uint8_t type = event.get_type();
  Event::Handler* handler = event.get_target();

  // Check that the event is an read completed and from the correct source
  if ((type != Event::READ_COMPLETED_TYPE) || (handler != &receiver)) return;
  ...
}

The flow of control is:

  • The event.await() will perform a sleep while waiting for events
  • The ExternalInterruptPin::on_interrupt() method is called when an interrupt occurs. The default implementation will push a changed event onto the event queue
  • The event.await() returns with the event
  • If event.dispatch() is called the event target on_event() method is called, otherwise decode the event type and target

You can find more details about this on the blog, Cosa: The Analog Pin Classes; An Introduction to Event Driven Programming and Cosa: Object-Oriented Interrupt Handling

Cheers!