Code style fixes: sensinode examples

This commit is contained in:
George Oikonomou 2012-12-16 15:29:45 +00:00
parent 594ba9a0ed
commit 64a95dcb59
19 changed files with 205 additions and 251 deletions

View file

@ -10,61 +10,36 @@
#include "contiki.h" #include "contiki.h"
#include "dev/leds.h" #include "dev/leds.h"
#include <stdio.h> /* For printf() */ #include <stdio.h>
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* We declare the two processes */
PROCESS(hello_world_process, "Hello world process"); PROCESS(hello_world_process, "Hello world process");
PROCESS(blink_process, "LED blink process"); PROCESS(blink_process, "LED blink process");
/* We require the processes to be started automatically */
AUTOSTART_PROCESSES(&hello_world_process, &blink_process); AUTOSTART_PROCESSES(&hello_world_process, &blink_process);
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Implementation of the first process */ /* Implementation of the first process */
PROCESS_THREAD(hello_world_process, ev, data) PROCESS_THREAD(hello_world_process, ev, data)
{ {
/* variables are declared static to ensure their values are maintained
between subsequent calls.
All the code between PROCESS_THREAD and PROCESS_BEGIN() runs each time
the process is invoked. */
static struct etimer timer; static struct etimer timer;
static int count; static int count;
/* any process must start with this. */
PROCESS_BEGIN(); PROCESS_BEGIN();
/* set the etimer module to generate an event in one second.
CLOCK_CONF_SECOND is #define as 128 */
etimer_set(&timer, CLOCK_CONF_SECOND * 4); etimer_set(&timer, CLOCK_CONF_SECOND * 4);
count = 0; count = 0;
/* Don't declare variables after PROCESS_BEGIN.
* While it will compile fine with TARGET=native (gcc is happy),
* SDCC doesn't like it. Soon as you try TARGET=sensinode you will get:
* syntax error: token -> 'int' ;
* Try uncommenting the line below and observe the results */
/* int whoops = 0;
* whoops = 0; */
while (1)
{
/* wait here for an event to happen */
PROCESS_WAIT_EVENT();
/* This achieves the same
* PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER); */
/* if the event is the timer event as expected... */ while(1) {
if(ev == PROCESS_EVENT_TIMER)
{ PROCESS_WAIT_EVENT();
/* do the process work */
if(ev == PROCESS_EVENT_TIMER) {
printf("Sensor says no... #%d\r\n", count); printf("Sensor says no... #%d\r\n", count);
count++; count++;
/* reset the timer so it will generate an other event
* the exact same time after it expired (periodicity guaranteed) */
etimer_reset(&timer); etimer_reset(&timer);
} }
/* and loop */
} }
/* any process must end with this, even if it is never reached. */
PROCESS_END(); PROCESS_END();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -74,16 +49,12 @@ PROCESS_THREAD(blink_process, ev, data)
static struct etimer timer; static struct etimer timer;
PROCESS_BEGIN(); PROCESS_BEGIN();
while (1) while(1) {
{
/* we set the timer from here every time */
etimer_set(&timer, CLOCK_CONF_SECOND); etimer_set(&timer, CLOCK_CONF_SECOND);
/* and wait until the event we receive is the one we're waiting for */
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
printf("Blink... (state %0.2X).\r\n", leds_get()); printf("Blink... (state %0.2X).\r\n", leds_get());
/* update the LEDs */
leds_toggle(LEDS_GREEN); leds_toggle(LEDS_GREEN);
} }
PROCESS_END(); PROCESS_END();

View file

@ -58,14 +58,18 @@
PROCESS(example_broadcast_process, "BROADCAST example"); PROCESS(example_broadcast_process, "BROADCAST example");
AUTOSTART_PROCESSES(&example_broadcast_process); AUTOSTART_PROCESSES(&example_broadcast_process);
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from) static void
broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from)
{ {
leds_toggle(LEDS_RED); leds_toggle(LEDS_RED);
PRINTF("broadcast message received from %02x.%02x\n", from->u8[0], from->u8[1]); PRINTF("broadcast message received from %02x.%02x\n", from->u8[0],
PRINTF("Size=0x%02x: '0x%04x'\n", packetbuf_datalen(), *(uint16_t *) packetbuf_dataptr()); from->u8[1]);
PRINTF("Size=0x%02x: '0x%04x'\n", packetbuf_datalen(),
*(uint16_t *)packetbuf_dataptr());
} }
/*---------------------------------------------------------------------------*/
static void print_rime_stats() static void
print_rime_stats()
{ {
PRINTF("\nNetwork Stats\n"); PRINTF("\nNetwork Stats\n");
PRINTF(" TX=%lu , RX=%lu\n", rimestats.tx, rimestats.rx); PRINTF(" TX=%lu , RX=%lu\n", rimestats.tx, rimestats.rx);
@ -90,7 +94,7 @@ PROCESS_THREAD(example_broadcast_process, ev, data)
PRINTF("Start\n"); PRINTF("Start\n");
broadcast_open(&broadcast, BROADCAST_CHANNEL, &bc_rx); broadcast_open(&broadcast, BROADCAST_CHANNEL, &bc_rx);
PRINTF("Open Broadcast Connection, channel %u\n", BROADCAST_CHANNEL); PRINTF("Open Broadcast Connection, channel %u\n", BROADCAST_CHANNEL);
// leds_off(LEDS_ALL);
while(1) { while(1) {
/* Delay 2-4 seconds */ /* Delay 2-4 seconds */
@ -98,7 +102,8 @@ PROCESS_THREAD(example_broadcast_process, ev, data)
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
leds_on(LEDS_GREEN); leds_on(LEDS_GREEN);
packetbuf_copyfrom(&counter, sizeof(counter)); packetbuf_copyfrom(&counter, sizeof(counter));
PRINTF("Sending %u bytes: 0x%04x\n", packetbuf_datalen(), *(uint16_t *) packetbuf_dataptr()); PRINTF("Sending %u bytes: 0x%04x\n", packetbuf_datalen(),
*(uint16_t *)packetbuf_dataptr());
if(broadcast_send(&broadcast) == 0) { if(broadcast_send(&broadcast) == 0) {
PRINTF("Error Sending\n"); PRINTF("Error Sending\n");
} }

View file

@ -181,7 +181,8 @@ broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from)
/* Convert RSSI to the loc. eng. format */ /* Convert RSSI to the loc. eng. format */
parameters.rssi[from->u8[1] - 1] = (-2 * rssi); parameters.rssi[from->u8[1] - 1] = (-2 * rssi);
/* Raw dump the packetbuf into the ref_coords struct */ /* Raw dump the packetbuf into the ref_coords struct */
memcpy(&ref_coords[from->u8[1] - 1], packetbuf_dataptr(), 2 * sizeof(uint8_t)); memcpy(&ref_coords[from->u8[1] - 1], packetbuf_dataptr(),
2 * sizeof(uint8_t));
} }
return; return;
@ -194,7 +195,8 @@ broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from)
*/ */
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
set_imaginary_ref_nodes() { set_imaginary_ref_nodes()
{
ref_coords[0].x = 1; ref_coords[0].x = 1;
ref_coords[0].y = 5; ref_coords[0].y = 5;
parameters.rssi[0] = SAMPLE_RSSI; parameters.rssi[0] = SAMPLE_RSSI;
@ -266,7 +268,9 @@ PROCESS_THREAD(blindnode_bcast_rec, ev, data)
parameters.n = n; parameters.n = n;
calculate(); calculate();
n++; n++;
if(n==32) { n=0; } if(n == 32) {
n = 0;
}
/* Send our calculated location to some monitoring node */ /* Send our calculated location to some monitoring node */
packetbuf_copyfrom(&coords, 2 * sizeof(uint8_t)); packetbuf_copyfrom(&coords, 2 * sizeof(uint8_t));

View file

@ -47,4 +47,3 @@ netstack_init(void)
NETSTACK_RADIO.init(); NETSTACK_RADIO.init();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

View file

@ -14,67 +14,44 @@
#include "contiki.h" #include "contiki.h"
//#include "dev/leds.h" //#include "dev/leds.h"
#include <limits.h> #include <limits.h>
#include <stdio.h> /* For printf() */ #include <stdio.h>
#include "event-post.h" #include "event-post.h"
/* This is our event type */ /* This is our event type */
static process_event_t event_data_ready; static process_event_t event_data_ready;
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Declare the two processes here */ /* Declare the two processes here */
PROCESS(sensor_process, "Sensor process"); PROCESS(sensor_process, "Sensor process");
PROCESS(print_process, "Print process"); PROCESS(print_process, "Print process");
/* Tell Contiki that we want them to start automatically */
AUTOSTART_PROCESSES(&sensor_process, &print_process); AUTOSTART_PROCESSES(&sensor_process, &print_process);
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Implementation "Sensor Process" */
PROCESS_THREAD(sensor_process, ev, data) PROCESS_THREAD(sensor_process, ev, data)
{ {
/* static variables to preserve values across consecutive calls of this
* process. */
/* Set an etimer */
static struct etimer timer; static struct etimer timer;
/* And the 'sensor' monitoring variable */
static struct event_struct es; static struct event_struct es;
PROCESS_BEGIN(); PROCESS_BEGIN();
/* Set some near-the-limit initial values */
/* signed primitives */
es.s_val = SHRT_MAX - 2; es.s_val = SHRT_MAX - 2;
es.i_val = INT_MAX - 2; es.i_val = INT_MAX - 2;
es.l_val = LONG_MAX - 2; es.l_val = LONG_MAX - 2;
/* sizeof(long long) == sizeof(long) on sensinodes - see other examples*/
es.ll_val = LONG_MAX - 2; es.ll_val = LONG_MAX - 2;
/* and some typedef-ed unsigned variables */
es.u8_val = UCHAR_MAX - 2; es.u8_val = UCHAR_MAX - 2;
es.u16_val = USHRT_MAX - 2; es.u16_val = USHRT_MAX - 2;
es.u32_val = ULONG_MAX - 2; es.u32_val = ULONG_MAX - 2;
/* allocate the required event */
event_data_ready = process_alloc_event(); event_data_ready = process_alloc_event();
/* process_event_t is actually a u_char. What did the OS allocate for us? */
printf("Contiki allocated event ID %d.\r\n", event_data_ready); printf("Contiki allocated event ID %d.\r\n", event_data_ready);
/* Set a timer here. We will generate an event every times this timer expires
* etimer_set accepts clock ticks as its 2nd argument.
* CLOCK_CONF_SECOND is the number of ticks per second.
* This CLOCK_CONF_SECOND * N = N seconds */
etimer_set(&timer, CLOCK_CONF_SECOND * 2); etimer_set(&timer, CLOCK_CONF_SECOND * 2);
while (1) while(1) {
{
printf("Sensor process: Wait for timer event...\r\n"); printf("Sensor process: Wait for timer event...\r\n");
/* Wait on our timer */
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
/* blip */
/* leds_toggle(LEDS_BLUE); */
/* Set the 'sensor' value before throwing the event */
printf("Sensor Process: Incrementing values...\r\n"); printf("Sensor Process: Incrementing values...\r\n");
es.s_val++; es.s_val++;
es.i_val++; es.i_val++;
@ -84,13 +61,9 @@ PROCESS_THREAD(sensor_process, ev, data)
es.u16_val++; es.u16_val++;
es.u32_val++; es.u32_val++;
/* Post our event.
* N.B. es is declared static.
* Try passing a volatile variable and observe the results... */
printf("Sensor Process: Generating 'Data Ready' event.\r\n"); printf("Sensor Process: Generating 'Data Ready' event.\r\n");
process_post(&print_process, event_data_ready, &es); process_post(&print_process, event_data_ready, &es);
/* reset the timer so we can wait on it again */
etimer_reset(&timer); etimer_reset(&timer);
} }
@ -105,16 +78,10 @@ PROCESS_THREAD(print_process, ev, data)
PROCESS_BEGIN(); PROCESS_BEGIN();
while (1) while(1) {
{
/* Stop here and wait until "event_data_ready" occurs */
PROCESS_WAIT_EVENT_UNTIL(ev == event_data_ready); PROCESS_WAIT_EVENT_UNTIL(ev == event_data_ready);
/* When the event occurs, the incoming data will be stored in
* process_data_t data (careful, this is void *)
*
* Print away...
* es is volatile, we need to set it = data again and dereference it. */
sd = data; sd = data;
printf("Print Process - Data Ready:\r\n"); printf("Print Process - Data Ready:\r\n");
printf(" s: %d\r\n", sd->s_val); printf(" s: %d\r\n", sd->s_val);
@ -125,7 +92,6 @@ PROCESS_THREAD(print_process, ev, data)
printf(" u16: %u\r\n", sd->u16_val); printf(" u16: %u\r\n", sd->u16_val);
printf(" u32: %lu\r\n", sd->u32_val); printf(" u32: %lu\r\n", sd->u32_val);
/* aaaaand back to waiting for the next event */
} }
PROCESS_END(); PROCESS_END();
} }

View file

@ -110,7 +110,9 @@
#define SEND_BATTERY_INFO 0 #define SEND_BATTERY_INFO 0
#if SEND_BATTERY_INFO #if SEND_BATTERY_INFO
#include "sensors-example.h" #include "sensors-example.h"
static void bc_rx(struct broadcast_conn *c, const rimeaddr_t *from) { static void
bc_rx(struct broadcast_conn *c, const rimeaddr_t *from)
{
return; return;
} }
@ -217,7 +219,8 @@ PROCESS_THREAD(sensors_test_process, ev, data)
sane = ((rv * 0.61065 - 773) / 2.45); sane = ((rv * 0.61065 - 773) / 2.45);
dec = sane; dec = sane;
frac = sane - dec; frac = sane - dec;
PRINTF(" Temp=%d.%02u C (%d)\n", dec, (unsigned int)(frac*100), rv); PRINTF(" Temp=%d.%02u C (%d)\n", dec, (unsigned int)(frac * 100),
rv);
} }
/* /*
* Accelerometer: Freescale Semiconductor MMA7340L * Accelerometer: Freescale Semiconductor MMA7340L
@ -305,7 +308,8 @@ PROCESS_THREAD(sensors_test_process, ev, data)
sane = (float)(rv * 0.4071); sane = (float)(rv * 0.4071);
dec = sane; dec = sane;
frac = sane - dec; frac = sane - dec;
PRINTF(" Light=%d.%02ulux (%d)\n", dec, (unsigned int)(frac*100), rv); PRINTF(" Light=%d.%02ulux (%d)\n", dec, (unsigned int)(frac * 100),
rv);
} }
/* /*
* Power Supply Voltage. * Power Supply Voltage.

View file

@ -47,4 +47,3 @@ netstack_init(void)
NETSTACK_RADIO.init(); NETSTACK_RADIO.init();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

View file

@ -48,7 +48,8 @@ AUTOSTART_PROCESSES(&clock_test_process);
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#if TEST_RTIMER #if TEST_RTIMER
void void
rt_callback(struct rtimer *t, void *ptr) { rt_callback(struct rtimer *t, void *ptr)
{
rt_now = RTIMER_NOW(); rt_now = RTIMER_NOW();
ct = clock_time(); ct = clock_time();
printf("Task called at %u (clock = %u)\n", rt_now, ct); printf("Task called at %u (clock = %u)\n", rt_now, ct);
@ -88,8 +89,8 @@ PROCESS_THREAD(clock_test_process, ev, data)
rt_now = RTIMER_NOW(); rt_now = RTIMER_NOW();
rt_for = rt_now + RTIMER_SECOND; rt_for = rt_now + RTIMER_SECOND;
printf("Now=%u (clock = %u) - For=%u\n", rt_now, ct, rt_for); printf("Now=%u (clock = %u) - For=%u\n", rt_now, ct, rt_for);
if (rtimer_set(&rt, rt_for, 1, if(rtimer_set(&rt, rt_for, 1, (rtimer_callback_t) rt_callback, NULL) !=
(void (*)(struct rtimer *, void *))rt_callback, NULL) != RTIMER_OK) { RTIMER_OK) {
printf("Error setting\n"); printf("Error setting\n");
} }
@ -99,7 +100,8 @@ PROCESS_THREAD(clock_test_process, ev, data)
#endif #endif
#if TEST_ETIMER #if TEST_ETIMER
printf("Clock tick and etimer test, 1 sec (%u clock ticks):\n", CLOCK_SECOND); printf("Clock tick and etimer test, 1 sec (%u clock ticks):\n",
CLOCK_SECOND);
i = 0; i = 0;
while(i < 10) { while(i < 10) {
etimer_set(&et, CLOCK_SECOND); etimer_set(&et, CLOCK_SECOND);

View file

@ -182,7 +182,8 @@ PROCESS_THREAD(udp_client_process, ev, data)
UIP_HTONS(l_conn->lport), UIP_HTONS(l_conn->rport)); UIP_HTONS(l_conn->lport), UIP_HTONS(l_conn->rport));
#if UIP_CONF_ROUTER #if UIP_CONF_ROUTER
uip_ip6addr(&ipaddr,0x2001,0x630,0x301,0x6453,0x0215,0x2000,0x0002,0x0302); uip_ip6addr(&ipaddr, 0x2001, 0x630, 0x301, 0x6453, 0x0215, 0x2000, 0x0002,
0x0302);
g_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); g_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL);
if(!g_conn) { if(!g_conn) {
PRINTF("udp_new g_conn error.\n"); PRINTF("udp_new g_conn error.\n");

View file

@ -110,7 +110,8 @@ PROCESS_THREAD(ping6_process, ev, data)
PRINTF("ping6 running.\n"); PRINTF("ping6 running.\n");
PRINTF("Button 1: 5 pings 16 byte payload.\n"); PRINTF("Button 1: 5 pings 16 byte payload.\n");
uip_ip6addr(&dest_addr,0x2001,0x470,0x55,0,0x0215,0x2000,0x0002,0x0302); uip_ip6addr(&dest_addr, 0x2001, 0x470, 0x55, 0, 0x0215, 0x2000, 0x0002,
0x0302);
count = 0; count = 0;
/* Check if we have buttons */ /* Check if we have buttons */

View file

@ -104,9 +104,10 @@ static void
print_stats() print_stats()
{ {
PRINTF("tl=%lu, ts=%lu, bs=%lu, bc=%lu\n", PRINTF("tl=%lu, ts=%lu, bs=%lu, bc=%lu\n",
rimestats.toolong, rimestats.tooshort, rimestats.badsynch, rimestats.badcrc); rimestats.toolong, rimestats.tooshort, rimestats.badsynch,
PRINTF("llrx=%lu, lltx=%lu, rx=%lu, tx=%lu\n", rimestats.badcrc);
rimestats.llrx, rimestats.lltx, rimestats.rx, rimestats.tx); PRINTF("llrx=%lu, lltx=%lu, rx=%lu, tx=%lu\n", rimestats.llrx,
rimestats.lltx, rimestats.rx, rimestats.tx);
} }
#else #else
#define print_stats() #define print_stats()
@ -145,7 +146,8 @@ create_dag()
print_local_addresses(); print_local_addresses();
dag = rpl_set_root(RPL_DEFAULT_INSTANCE, &uip_ds6_get_global(ADDR_PREFERRED)->ipaddr); dag = rpl_set_root(RPL_DEFAULT_INSTANCE,
&uip_ds6_get_global(ADDR_PREFERRED)->ipaddr);
if(dag != NULL) { if(dag != NULL) {
uip_ip6addr(&ipaddr, 0x2001, 0x630, 0x301, 0x6453, 0, 0, 0, 0); uip_ip6addr(&ipaddr, 0x2001, 0x630, 0x301, 0x6453, 0, 0, 0, 0);
rpl_set_prefix(dag, &ipaddr, 64); rpl_set_prefix(dag, &ipaddr, 64);