Wednesday, June 12, 2013

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 7

Part 1 in this series can be found here.
Part 2 can be found here.
Part 3 can be found here.
Part 4 can be found here.
Part 5 can be found here.
Part 6 can be found here.

This post will discuss how to pass a variable from one process to another. This is also the method how to unblock another process from one process. We need this code to invoke that:

process_post(&process_2, the_event_signaller_thing, &any_variable_we_want_to_pass);

"the_event_signaller_thing"will have to be declared as type: static process_event_t and later allocated from process_alloc_event();

Here is a fully functional code for you to copy paste and play around:

#include "contiki.h"
#include < stdio.h >

static process_event_t ourEventIsReady;
/*This will be used to signal the event*/

PROCESS(process_1, "This will pass the variable and post an event");
PROCESS(process_2, "This will receive the variable and wake up");

PROCESS_THREAD(process_1, ev, data)
static int ourVariable = 99;
/*Make sure this is static or you will get a value
of 0 each time you compile. Luckily it took me no more
than 2 minutes to figure my error*/

ourEventIsReady = process_alloc_event(); 
//Allocate the event

process_post(&process_2, ourEventIsReady, &ourVariable);
//And then send it to process_2..

/*Process_2 will be blocked until ourEventIsReady is passed to 'ev'*/
PROCESS_THREAD(process_2, ev, data)
PROCESS_WAIT_EVENT_UNTIL(ev == ourEventIsReady);
printf("Variable is %d \n", (*(int*)data));
(*(int*)) is necesaary to convert 'data' into a proper integer.
Argument 'data' type is actually a process_type_t, so 
it will give a warning and display the pointer
if we dont include the *(int*) thing first.

As can be seen I didnt use any while() loop.

The output looks like this: