GithubHelp home page GithubHelp logo

entykey / arduinoasync Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matheusalvesa/arduinoasync

1.0 0.0 0.0 12 KB

A simple Arduino library that allows asynchronous function execution

C++ 100.00%

arduinoasync's Introduction

Arduino Async

This is a very simple library to run asynchronous functions on your sketch.

Installation

  • Download the master branch on .zip format.
  • Unzip, then modify the Folder name to "Async"
  • Paste the modified folder on your Library folder. Typically: C:\Program Files (x86)\Arduino\libraries

How to use

First of all import the lib:

#include <async.h>

Instantiate the Async Class:

Async asyncEngine = Async();       // Allocate on Stack
      /* or */
Async *asyncEngine = new Async();  // Allocate on Heep

Note: The examples above assume that you allocate the Async object on the Stack. If you allocate on Heep, use pointer dereferencing: "asyncEngine->"

Add the run() on the main loop function:

asyncEngine.run();

Execute every 10 milliseconds:

// id can be used later to stop the execution loop
short id = asyncEngine.setInterval(blinkLed, 10);

blinkLed code:

/*
  Blink the internal led every second.
	
  Using millis to avoid blocking code.
  Always avoid using delay function.
*/
void blinkLed() {
  static unsigned long start = millis();

  if((millis() - start) >= 500 && (millis() - start) < 1000) {
    digitalWrite(13, HIGH);
  }

  if((millis() - start) >= 1000) {
    digitalWrite(13, LOW);
    start = millis();
  }
}

If you just want to delay a execution of a function use setTimeout

// Delays the execution of a function by 10 seconds
asyncEngine.setTimeout(myFunction, 10000);

You can Stop the execution of a function previously added by calling clearInterval:

asyncEngine.clearInterval(id);

This full example can be found on examples/blink_led.ino.

References

Constructor:

/*
  sizePool is the limit of functions that the engine can handle.
  Avoid using big numbers because of low memory available on Atmega.

  If you add more functions that sizePool the last functions added will be ignored.
*/
Async(unsigned short sizePool = 10)

setTimeout:

/*
  First param is the function that will be executed.
  Second param is the time to be waited before execute.

  The returned int value can be used to cancel the execution.
*/
short setTimeout(
  void (*fun)(void) = nullptr,
  unsigned long time = 0
)

setInterval:

/*
  First param is the function that will be executed.
  Second param is the time loop.

  The returned int value can be used to cancel the execution.
*/
short setInterval(
  void (*fun)(void) = nullptr,
  unsigned long time = 0
)

clearInterval:

/*
  Stop executing the function with the id passed
*/
bool clearInterval(short id = -1)

run:

/*
  Must be placed on the main loop.
*/
void run()

arduinoasync's People

Contributors

matheusalvesa avatar

Stargazers

 avatar

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.