Contiki 3.x
tmp102-test.c
1 #include <stdio.h>
2 #include "contiki.h"
3 #include "dev/i2cmaster.h" // Include IC driver
4 #include "dev/tmp102.h" // Include sensor driver
5 
6 #define TMP102_READ_INTERVAL (CLOCK_SECOND/2) // Poll the sensor every 500 ms
7 
8 PROCESS (temp_process, "Test Temperature process");
9 AUTOSTART_PROCESSES (&temp_process);
10 /*---------------------------------------------------------------------------*/
11 static struct etimer et;
12 
13 PROCESS_THREAD (temp_process, ev, data)
14 {
15  PROCESS_BEGIN ();
16 
17  {
18  int16_t tempint;
19  uint16_t tempfrac;
20  int16_t raw;
21  uint16_t absraw;
22  int16_t sign;
23  char minus = ' ';
24 
25  tmp102_init();
26 
27  while (1)
28  {
29  etimer_set(&et, TMP102_READ_INTERVAL); // Set the timer
30  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); // wait for its expiration
31 
32  sign = 1;
33 
34  raw = tmp102_read_temp_raw(); // Reading from the sensor
35 
36  absraw = raw;
37  if (raw < 0) { // Perform 2C's if sensor returned negative data
38  absraw = (raw ^ 0xFFFF) + 1;
39  sign = -1;
40  }
41  tempint = (absraw >> 8) * sign;
42  tempfrac = ((absraw>>4) % 16) * 625; // Info in 1/10000 of degree
43  minus = ((tempint == 0) & (sign == -1)) ? '-' : ' ' ;
44  printf ("Temp = %c%d.%04d\n", minus, tempint, tempfrac);
45  }
46  }
47  PROCESS_END ();
48 }
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
static struct etimer et
NIC receiver thread.
Definition: lanc111.c:1133
A timer.
Definition: etimer.h:76
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
void tmp102_init(void)
Initialiser for the TMP102 sensor driver.
Definition: tmp102.c:59
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120