Go Down

Topic: Reactduino - A non-blocking async framework for Arduino (Read 343 times) previous topic - next topic

AndrewCarterUK

Hi All,

I use the Arduino software framework extensively and I've been wanting to make something like this for a while.

My background is web development, and in this arena it's quite common for languages (such as Javascript) to operate on an event loop using callbacks to prevent blocking operations.

Blocking operations, such as delay(), are a problem in embedded software development too - and I think my project Reactduino can help here.

The example I use in the project README is if you want to make a program which echoes data back to the serial port, and you want to flash the LED whenever data is processed.

Normally, you might end up with a verbose sketch like the one below. You can't use the delay function to blink the LED, because this would slow down the rate at which you could read data off the serial port.

Code: [Select]
#include <Arduino.h>

uint32_t start;
bool blink = false;

void setup()
{
    Serial.begin(9600);
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
    if (Serial.available() > 0) {
        Serial.write(Serial.read());

        blink = true;
        start = millis();
        digitalWrite(LED_BUILTIN, HIGH);
    }

    if (blink && millis() - start > 1000) {
        blink = false;
        digitalWrite(LED_BUILTIN, LOW);
    }

    yield();
}


With Reactduino, the same program looks like this:

Code: [Select]
#include <Reactduino.h>

Reactduino app([] () {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

  app.onAvailable(&Serial, [] () {
    static reaction led_off = INVALID_REACTION;

    Serial.write(Serial.read());
    digitalWrite(LED_BUILTIN, HIGH);

    app.free(led_off); // Cancel previous timer (if set)

    led_off = app.delay(1000, [] () { digitalWrite(LED_BUILTIN, LOW); });
  });
});


For reference, this is what blink looks like:

Code: [Select]
#include <Reactduino.h>

Reactduino app([] () {
  pinMode(LED_BUILTIN, OUTPUT);

  app.repeat(1000, [] () {
      static bool state = false;
      digitalWrite(LED_BUILTIN, state = !state);
  });
});


The project is in it's early days, and I've got some more features planned. All the documentation can be found on the GitHub.

I'd be interested to here your thoughts!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy