Contiki-Inga 3.x
pressure-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
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 
30 /**
31  * \file
32  * Pressure sensor interface implemention
33  * \author
34  * Georg von Zengen
35  */
36 
37 #include "contiki.h"
38 #include "lib/sensors.h"
39 #include "dev/bmp085.h"
40 #include "dev/pressure-sensor.h"
41 
42 #define CFG_READY_ 0
43 #define CFG_ACTIVE_ 1
44 #define CFG_MODE_ 3
45 
46 const struct sensors_sensor pressure_sensor;
47 static uint8_t config = 0x00;
48 static uint32_t pressure;
49 /*----------------------------------------------------------------------------*/
50 static int
51 value(int type)
52 {
53  // if not activated, do not query data
54  if (!(config & (1 << CFG_ACTIVE_))) return -1;
55 
56  switch (type) {
57  case PRESS_H:
58  // read with selected mode
59  pressure = bmp085_read_pressure((config & (0x3 << CFG_MODE_)) >> CFG_MODE_);
60  return (uint16_t) ((pressure >> 16) & 0xFFFF);
61  case PRESS_L:
62  return (uint16_t) (pressure & 0xFFFF);
63  case TEMP:
64  return (int16_t) bmp085_read_temperature();
65  }
66  return 0;
67 }
68 /*----------------------------------------------------------------------------*/
69 static int
70 status(int type)
71 {
72  switch (type) {
73  case SENSORS_READY:
74  return (config & (1 << CFG_READY_)) >> CFG_READY_;
75  break;
76  case SENSORS_ACTIVE:
77  return (config & (1 << CFG_ACTIVE_)) >> CFG_ACTIVE_;
78  break;
79  }
80  return 0;
81 }
82 /*----------------------------------------------------------------------------*/
83 static int
84 configure(int type, int c)
85 {
86  switch (type) {
87  case SENSORS_HW_INIT:
88  if (bmp085_available()) {
89  config |= (1 << CFG_READY_);
90  return 1;
91  } else {
92  config &= (1 << CFG_READY_);
93  return 0;
94  }
95  break;
96  case SENSORS_ACTIVE:
97  if (c) {
98  config |= (1 << CFG_ACTIVE_);
99  return (bmp085_init() == 0) ? 1 : 0;
100  } else {
101  config &= ~(1 << CFG_ACTIVE_);
102  return (bmp085_deinit() == 0) ? 1 : 0;
103  }
104  break;
105  default:
106  break;
107 
109  switch (c) {
110  case PRESSURE_MODE_ULTRA_LOW_POWER:
111  config &= ~(0x3 << CFG_MODE_);
112  config |= (BMP085_ULTRA_LOW_POWER << CFG_MODE_);
113  break;
114  case PRESSURE_MODE_STANDARD:
115  config &= ~(0x3 << CFG_MODE_);
116  config |= (BMP085_STANDARD << CFG_MODE_);
117  break;
118  case PRESSURE_MODE_HIGH_RES:
119  config &= ~(0x3 << CFG_MODE_);
120  config |= (BMP085_HIGH_RESOLUTION << CFG_MODE_);
121  break;
122  case PRESSURE_MODE_ULTRA_HIGH_RES:
123  config &= ~(0x3 << CFG_MODE_);
124  config |= (BMP085_ULTRA_HIGH_RES << CFG_MODE_);
125  break;
126  }
127  break;
128  }
129  return 0;
130 }
131 /*----------------------------------------------------------------------------*/
132 SENSORS_SENSOR(pressure_sensor, PRESSURE_SENSOR, value, configure, status);