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");
AUTOSTART_PROCESSES(&process_1,&process_2);

//***********************************************************
PROCESS_THREAD(process_1, ev, data)
{
PROCESS_BEGIN();
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_END();
}
//***********************************************************
/*Process_2 will be blocked until ourEventIsReady is passed to 'ev'*/
PROCESS_THREAD(process_2, ev, data)
{
PROCESS_BEGIN();
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.
*/

PROCESS_END();
}
/*
As can be seen I didnt use any while() loop.
*/


The output looks like this:





comments