Featured

Archive

Post Page Advertisement [Top]

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

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

Now we have got our 'Hello World' process running nicely on a mote. Now we are ready to make things more interesting. Let's now focus on the stuff that comes between PROCESS_BEGIN() and PROCESS_END().

Presented next is a modified snippet of the source code I took from this website. This is another example of a 'Hello World' process, but it introduces some new concepts related to process blocking and then unblocking via a timer (I am trying to reduce the technical jargon so do excuse me if it may not sound correct).

PROCESS_THREAD(hello_world_process, ev, data)
{
/* variables are declared static to ensure their values are kept
each time the kernel switches back to this process */
static struct etimer timer; //Code to declare a timer. MEMORIZE
static int count = 0; //Just a counter
PROCESS_BEGIN();
/*Set the length of the timer to one second
In Contiki (CLOCK_CONF_SECOND is equal to 1 second. */
etimer_set(&timer, CLOCK_CONF_SECOND);
while (1)
{
// This will block the process until a kind of event happens
PROCESS_WAIT_EVENT();
// This specifies the event that we are waiting for to be a timer
if(ev == PROCESS_EVENT_TIMER)
{
// do the process work
printf("Hello, world #%i\n", count);
count ++;
/*By adding 'etimer_reset(&timer);' in the end of the functional code
the process will runs again and again. This will reset the timer.
This is done to loop the event generation mechanism. As can be seen, the
while() loop is just for the event listener.*/
etimer_reset(&timer);
}
} //keep doing this
PROCESS_END();
}

An alternative way to reset the timer is just include the initialisation of the timer variable inside the while() loop.
This way we dont have to worry about putting etimer_reset(&timer) at the end of the code. This way the while() loop will take care of both the event listener and the event generator.

while (1)
{
etimer_set(&timer, CLOCK_CONF_SECOND);
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
/* The above statement is able to replace the two earlier statements of:
PROCESS_WAIT_EVENT();
if(ev == PROCESS_EVENT_TIMER)
{}
So consider it an alternative. I got confused when I first learnt this so
I hope this makes it easier for the readers.
*/