Contiki 3.x
test-adxl345.c
1 #include <stdio.h>
2 #include "contiki.h"
3 #include "serial-shell.h"
4 #include "adxl345.h"
5 
6 #define LED_INT_ONTIME CLOCK_SECOND/2
7 #define ACCM_READ_INTERVAL CLOCK_SECOND
8 
9 static process_event_t ledOff_event;
10 /*---------------------------------------------------------------------------*/
11 PROCESS(accel_process, "Test Accel process");
12 PROCESS(led_process, "LED handling process");
13 AUTOSTART_PROCESSES(&accel_process, &led_process);
14 /*---------------------------------------------------------------------------*/
15 /* As several interrupts can be mapped to one interrupt pin, when interrupt
16  strikes, the adxl345 interrupt source register is read. This function prints
17  out which interrupts occurred. Note that this will include all interrupts,
18  even those mapped to 'the other' pin, and those that will always signal even if
19  not enabled (such as watermark). */
20 
21 void
22 print_int(uint16_t reg){
23 #define ANNOYING_ALWAYS_THERE_ANYWAY_OUTPUT 0
24 #if ANNOYING_ALWAYS_THERE_ANYWAY_OUTPUT
25  if(reg & ADXL345_INT_OVERRUN) {
26  printf("Overrun ");
27  }
28  if(reg & ADXL345_INT_WATERMARK) {
29  printf("Watermark ");
30  }
31  if(reg & ADXL345_INT_DATAREADY) {
32  printf("DataReady ");
33  }
34 #endif
35  if(reg & ADXL345_INT_FREEFALL) {
36  printf("Freefall ");
37  }
38  if(reg & ADXL345_INT_INACTIVITY) {
39  printf("InActivity ");
40  }
41  if(reg & ADXL345_INT_ACTIVITY) {
42  printf("Activity ");
43  }
44  if(reg & ADXL345_INT_DOUBLETAP) {
45  printf("DoubleTap ");
46  }
47  if(reg & ADXL345_INT_TAP) {
48  printf("Tap ");
49  }
50  printf("\n");
51 }
52 
53 /*---------------------------------------------------------------------------*/
54 /* accelerometer free fall detection callback */
55 
56 void
57 accm_ff_cb(u8_t reg){
58  L_ON(LEDS_B);
59  process_post(&led_process, ledOff_event, NULL);
60  printf("~~[%u] Freefall detected! (0x%02X) -- ", ((u16_t) clock_time())/128, reg);
61  print_int(reg);
62 }
63 /*---------------------------------------------------------------------------*/
64 /* accelerometer tap and double tap detection callback */
65 
66 void
67 accm_tap_cb(u8_t reg){
68  process_post(&led_process, ledOff_event, NULL);
69  if(reg & ADXL345_INT_DOUBLETAP){
70  L_ON(LEDS_G);
71  printf("~~[%u] DoubleTap detected! (0x%02X) -- ", ((u16_t) clock_time())/128, reg);
72  } else {
73  L_ON(LEDS_R);
74  printf("~~[%u] Tap detected! (0x%02X) -- ", ((u16_t) clock_time())/128, reg);
75  }
76  print_int(reg);
77 }
78 /*---------------------------------------------------------------------------*/
79 /* When posted an ledOff event, the LEDs will switch off after LED_INT_ONTIME.
80  static process_event_t ledOff_event;
81  ledOff_event = process_alloc_event();
82  process_post(&led_process, ledOff_event, NULL);
83 */
84 
85 static struct etimer ledETimer;
86 PROCESS_THREAD(led_process, ev, data) {
87  PROCESS_BEGIN();
88  while(1){
89  PROCESS_WAIT_EVENT_UNTIL(ev == ledOff_event);
90  etimer_set(&ledETimer, LED_INT_ONTIME);
92  L_OFF(LEDS_R + LEDS_G + LEDS_B);
93  }
94  PROCESS_END();
95 }
96 
97 /*---------------------------------------------------------------------------*/
98 /* Main process, setups */
99 
100 static struct etimer et;
101 
102 PROCESS_THREAD(accel_process, ev, data) {
103  PROCESS_BEGIN();
104  {
105  int16_t x, y, z;
106 
107  serial_shell_init();
108  shell_ps_init();
109  shell_file_init(); // for printing out files
110  shell_text_init(); // for binprint
111 
112  /* Register the event used for lighting up an LED when interrupt strikes. */
113  ledOff_event = process_alloc_event();
114 
115  /* Start and setup the accelerometer with default values, eg no interrupts enabled. */
116  accm_init();
117 
118  /* Register the callback functions for each interrupt */
119  ACCM_REGISTER_INT1_CB((void *)accm_ff_cb);
120  ACCM_REGISTER_INT2_CB((void *)accm_tap_cb);
121 
122  /* Set what strikes the corresponding interrupts. Several interrupts per pin is
123  possible. For the eight possible interrupts, see adxl345.h and adxl345 datasheet. */
124  accm_set_irq(ADXL345_INT_FREEFALL, ADXL345_INT_TAP + ADXL345_INT_DOUBLETAP);
125 
126  while (1) {
127  x = accm_read_axis(X_AXIS);
128  y = accm_read_axis(Y_AXIS);
129  z = accm_read_axis(Z_AXIS);
130  printf("x: %d y: %d z: %d\n", x, y, z);
131 
132  etimer_set(&et, ACCM_READ_INTERVAL);
134  }
135  }
136  PROCESS_END();
137 }
138 
139 /*---------------------------------------------------------------------------*/
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
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 process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
process_event_t process_alloc_event(void)
Allocate a global event number.
Definition: process.c:93
A brief description of what this file is.
#define NULL
The null pointer.
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120