Contiki-Inga 3.x
sensors.h
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 #ifndef SENSORS_H_
33 #define SENSORS_H_
34 
35 #include "contiki.h"
36 
37 /** \defgroup sensor-interface Sensor interface
38  *
39  * \section using_sensor Using a sensor
40  *
41  * Each sensor has a set of functions for controlling it and query it for its state.
42  * Some sensors also generate an events when the sensors change.
43  * A sensor must be activated before it generates events or relevant values.
44  *
45  * - SENSORS_ACTIVATE(sensor) - activate the sensor
46  * - SENSORS_DEACTIVATE(sensor) - deactivate the sensor
47  * - sensor.value(0) - query the sensor for its last value
48  *
49  * - sensors_event - event sent when a sensor has changed (the data argument will referer to the actual sensor)
50  *
51  * Example for querying the button:
52 \code
53 SENSORS_ACTIVATE(button_sensor) // activate the button sensor
54 button_sensor::value(0) // the button has been pressed or not
55 \endcode
56  *
57  * \section defining_sensor Defining new sensors
58  *
59  * Platform designers may need to define new sensors. This sections describes
60  * the steps required.
61  *
62  * Each sensor requires 3 functions; one for quering data,
63  * one for configuring the sensor, and one for requesting status.
64  *
65 \code
66 static int my_value(int type) {...}
67 static int my_configure(int type, int value) {...}
68 static int my_status(int type) {...}
69 \endcode
70  *
71  * These functions have to adapt the interface described in \ref sensors_sensor.
72  *
73  * To create a contiki sensor of them use the SENSORS_SENSOR() macro.
74 \code
75 SENSORS_SENSOR(my_sensor, "MY_TYPE", my_value, my_configure, my_status);
76 \endcode
77  *
78  * Finally add the sensor to your platforms sensor list.
79 \code
80 SENSORS(..., &my_sensor, ...);
81 \endcode
82  *
83  * @{ */
84 
85 /** \name Default constants for the configure/status API
86  * @{ */
87 /** internal - used only for (hardware) initialization. */
88 #define SENSORS_HW_INIT 128
89 /** Configuration: activates/deactives sensor: 0 -> turn off, 1 -> turn on.
90  * Status query: Returns 1 if sensor is active.
91  */
92 #define SENSORS_ACTIVE 129
93 /** Status query: Returns 1 if sensor is ready */
94 #define SENSORS_READY 130
95 /** @} */
96 
97 /** Activates sensor */
98 #define SENSORS_ACTIVATE(sensor) (sensor).configure(SENSORS_ACTIVE, 1)
99 /** Deactivates sensor */
100 #define SENSORS_DEACTIVATE(sensor) (sensor).configure(SENSORS_ACTIVE, 0)
101 
102 /** Defines new sensor
103  * @param name of the sensor
104  * @param String type name of the sensor
105  * @param pointer to sensors value function
106  * @param pointer to sensors configure function
107  * @param pointer to sensors status function
108  */
109 #define SENSORS_SENSOR(name, type, value, configure, status) \
110 const struct sensors_sensor name = { type, value, configure, status }
111 
112 /** Provides the number of available sensors. */
113 #define SENSORS_NUM (sizeof(sensors) / sizeof(struct sensors_sensor *))
114 
115 /** Setup defined sensors.
116  * @param comma-separated list of pointers to defined sensors.
117  *
118 \code
119 SENSORS_SENSOR(acc_sensor, "ACC", acc_val, acc_cfg, acc_status);
120 SENSORS_SENSOR(battery_sensor, "ACC", battery_val, battery_cfg, battery_status);
121 SENSORS(accsensor, battery_sensor);
122 \endcode
123  */
124 #define SENSORS(...) \
125 const struct sensors_sensor *sensors[] = {__VA_ARGS__, NULL}; \
126 unsigned char sensors_flags[SENSORS_NUM]
127 
128 /**
129  * Sensor defintion structure.
130  */
132  /** String name associated with sensor class. */
133  char * type;
134  /** Get sensor value.
135  *
136  * @param type Determines which value to read.
137  * Available types depend on the respective sensor implementation.
138  * @return Integer sensor value associated with given type
139  */
140  int (* value) (int type);
141  /** Configure sensor.
142  *
143  * @param type Configuration type
144  * Beside standard types respective sensor implementations
145  * may define additional types.
146  * @param value Value to set for configuration type.
147  * @retval 1 if configuration succeeded
148  * @retval 0 if configuration failed or type is unknown
149  */
150  int (* configure) (int type, int value);
151  /** Get status of sensor.
152  *
153  * @param type One of (\ref SENSORS_ACTIVE, \ref SENSORS_READY) or sensor-specific ones.
154  * @return Return value defined for particular type
155  * \retval 0 if type is unknown
156  */
157  int (* status) (int type);
158 };
159 
160 /** Returns sensor associated with type name if existing
161  * @param type Type name (string)
162  * @return pointer to sensor
163  */
164 const struct sensors_sensor *sensors_find(const char *type);
165 /** Returns next sensor in sensors list
166  * @return pointer to sensor
167  */
168 const struct sensors_sensor *sensors_next(const struct sensors_sensor *s);
169 /** Returns first sensor in sensors list
170  * @return pointer to sensor
171  */
172 const struct sensors_sensor *sensors_first(void);
173 
174 /** To be called from sensor readout interrupt.
175  * Polls sensor process.
176  * @param s Reference to the sensor that updated
177  */
178 void sensors_changed(const struct sensors_sensor *s);
179 
180 extern process_event_t sensors_event;
181 
182 PROCESS_NAME(sensors_process);
183 
184 /** @} */
185 
186 #endif /* SENSORS_H_ */