Contiki-Inga 3.x
sensors.c
1 /*
2  * Copyright (c) 2009, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 
34 #include <string.h>
35 
36 #include "contiki.h"
37 
38 #include "lib/sensors.h"
39 
40 const extern struct sensors_sensor *sensors[];
41 extern unsigned char sensors_flags[];
42 
43 #define FLAG_CHANGED 0x80
44 
45 process_event_t sensors_event;
46 
47 static unsigned char num_sensors;
48 
49 PROCESS(sensors_process, "Sensors");
50 
51 /*---------------------------------------------------------------------------*/
52 static int
53 get_sensor_index(const struct sensors_sensor *s)
54 {
55  int i;
56  for(i = 0; i < num_sensors; ++i) {
57  if(sensors[i] == s) {
58  return i;
59  }
60  }
61  return i;
62 }
63 /*---------------------------------------------------------------------------*/
64 const struct sensors_sensor *
66 {
67  return sensors[0];
68 }
69 /*---------------------------------------------------------------------------*/
70 const struct sensors_sensor *
71 sensors_next(const struct sensors_sensor *s)
72 {
73  return sensors[get_sensor_index(s) + 1];
74 }
75 /*---------------------------------------------------------------------------*/
76 void
78 {
79  sensors_flags[get_sensor_index(s)] |= FLAG_CHANGED;
80  process_poll(&sensors_process);
81 }
82 /*---------------------------------------------------------------------------*/
83 const struct sensors_sensor *
84 sensors_find(const char *prefix)
85 {
86  int i;
87  unsigned short len;
88 
89  /* Search through all processes and search for the specified process
90  name. */
91  len = strlen(prefix);
92 
93  for(i = 0; i < num_sensors; ++i) {
94  if(strncmp(prefix, sensors[i]->type, len) == 0) {
95  return sensors[i];
96  }
97  }
98  return NULL;
99 }
100 /*---------------------------------------------------------------------------*/
101 PROCESS_THREAD(sensors_process, ev, data)
102 {
103  static int i;
104  static int events;
105 
106  PROCESS_BEGIN();
107 
108  sensors_event = process_alloc_event();
109 
110  for(i = 0; sensors[i] != NULL; ++i) {
111  sensors_flags[i] = 0;
112  sensors[i]->configure(SENSORS_HW_INIT, 0);
113  }
114  num_sensors = i;
115 
116  while(1) {
117 
119 
120  do {
121  events = 0;
122  for(i = 0; i < num_sensors; ++i) {
123  if(sensors_flags[i] & FLAG_CHANGED) {
124  if(process_post(PROCESS_BROADCAST, sensors_event, (void *)sensors[i]) == PROCESS_ERR_OK) {
125  PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event);
126  }
127  sensors_flags[i] &= ~FLAG_CHANGED;
128  events++;
129  }
130  }
131  } while(events);
132  }
133 
134  PROCESS_END();
135 }
136 /*---------------------------------------------------------------------------*/