Contiki-NG
sht11-sensor.c
1/*
2 * Copyright (c) 2005, 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#include <stdlib.h>
34
35#include "contiki.h"
36#include "lib/sensors.h"
37#include "dev/sensor/sht11/sht11.h"
38#include "dev/sensor/sht11/sht11-sensor.h"
39
40const struct sensors_sensor sht11_sensor;
41
42enum {
43 ON, OFF
44};
45static uint8_t state = OFF;
46
47/*---------------------------------------------------------------------------*/
48static int
49value(int type)
50{
51 switch(type) {
52 /* Photosynthetically Active Radiation. */
53 case SHT11_SENSOR_TEMP:
54 return sht11_temp();;
55
56 /* Total Solar Radiation. */
57 case SHT11_SENSOR_HUMIDITY:
58 return sht11_humidity();
59
60 case SHT11_SENSOR_BATTERY_INDICATOR:
61 return sht11_sreg() & 0x40? 1: 0;
62}
63 return 0;
64}
65/*---------------------------------------------------------------------------*/
66static int
67status(int type)
68{
69 switch(type) {
70 case SENSORS_ACTIVE:
71 case SENSORS_READY:
72 return (state == ON);
73 }
74 return 0;
75}
76
77/*---------------------------------------------------------------------------*/
78static int
79configure(int type, int c)
80{
81 switch(type) {
82 case SENSORS_ACTIVE:
83 if(c) {
84 if(!status(SENSORS_ACTIVE)) {
85 rtimer_clock_t t0;
86 sht11_init();
87 state = ON;
88
89 /* For for about 11 ms before the SHT11 can be used. */
90 t0 = RTIMER_NOW();
91 while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + RTIMER_SECOND / 100));
92 }
93 } else {
94 sht11_off();
95 state = OFF;
96 }
97 }
98 return 0;
99}
100/*---------------------------------------------------------------------------*/
101SENSORS_SENSOR(sht11_sensor, "sht11",
102 value, configure, status);
#define RTIMER_SECOND
Number of rtimer ticks for 1 second.
Definition: rtimer.h:112
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:185