Getting started

Installation

There are two possible ways:

warning If you work with the Arduino IDE, the files must be located at the same folder level than your sketch!

Coloring keywords on the Arduino IDE

Some keywords of this project are colored in the Arduino IDE. If it does not, restart the IDE.

Upgrade my installation

To install a new version of the lib, follow this procedure (very easy):

Classes

Classes you should take a look:

Set up my first sketch

Copy / paste this code on your sketch:

#include <os48.h> //The only file you have to include

using namespace os48; //This line is necessary

//Scheduler is a singleton, you don't have to create an instance because only one can exist.
Scheduler* scheduler = Scheduler::get(); //Get the instance

//Declare the task pointers as global vars to use them in the task functions.
Task* task1 = NULL; 
Task* task2 = NULL;

void setup() {
  Serial.begin(9600);
  Serial.println("Creating tasks...");

  task1 = scheduler->createTask(&func1, 60); //Creates a task associated to the 'func1' function with 60 bytes of stack memory.
  task2 = scheduler->createTask(&func2, 60);

  Serial.println("Starting...");
  scheduler->start(); //Starts the scheduler. At this point you enter in a multi tasking context.

  //...
  //Nothing will be executed here
}

void func1()
{
  for(;;)
  {    
    OS48_NO_CS_BLOCK //Ensures that another task has no chance to interrupt the current task between the braces.
    {
      Serial.println("I am task 1");
    }

    //task() is a function returing the instance of the current running task. 
    task()->sleep(1000); //The current task will sleep during 1s.
  }
}

void func2()
{
  task()->sleep(500);  

  for(;;)
  {    
    OS48_NO_CS_BLOCK
    {
      Serial.println("I am task 2");
    }
    task()->sleep(1000);    
  }
}

void loop() {} //Useless now but you have to let it defined.

Upload the sketch on your microntroller and open the serial console. You can see a combination of task1 and task2 displayed. Both tasks are executed in parallel on two timelines. Take a look to the comments if something is hard to understand.