Usage

A Simple Example

This example is a producer-consumer problem which uses a lock. Pushing a button on port d is producing one element. A hard periodic task that runs every 20 ms debounces and checks if the user has pressed a button. Every time the user has pressed a new button, a new soft aperiodic task is spawned to deal with it (to consume).

unsigned char debounce_1;
unsigned char intProducedOnes; //# of elements produced that are waiting to be consumed.

void User_Init(void){
   DDRD = 0x00; //sets port D to an input, enables pullup
   PORTD = 0x00;
   IntProducedOnes;
   //check for a button push every ~20ms
   Schedule_Add_HP(taskCheckButton, 2, 38,NO_AFTER_TASK);
}

//checks for a button push, if so produce.
//The lock prevents the consumer and producer changing the variable at the same time
void taskCheckButton(){
unsigned char d;
   d = PIND;
   //if the user was not pushing a button 20ms ago, but is pressing a button now.
   if ((d != 0xff) && (debounce_1 == 0xff)){
      Lock(0);
      intProducedOnes++;
      Schedule_Add_SA(Consume);
      Unlock(0);
   }
   debounce_1 = d;
   CALL_OS; //each job must have the same ending statements
   Job_Return();
   RETURN_OS;
}

//check if there is anything to consume, if so consume it.
void Consume(void){
   if (intProducedOnes > 0){
      Lock(0);
      intProducedOnes--;
      //code that consumes the element
      Unlock(0);
   }

   CALL_OS;
   Job_Return();
   RETURN_OS;
}