Contiki-NG
ht-sensor.c
1 /*
2  * Copyright (c) 2015 NXP B.V.
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 NXP B.V. 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 NXP B.V. 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 NXP B.V. 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  * Author: Theo van Daele <theo.van.daele@nxp.com>
32  *
33  */
34 #include "contiki.h"
35 #include "sys/etimer.h"
36 #include "lib/sensors.h"
37 #include "ht-sensor.h"
38 #include <stdlib.h>
39 #include <HtsDriver.h>
40 
41 /*---------------------------------------------------------------------------*/
42 /* LOCAL DEFINITIONS */
43 /*---------------------------------------------------------------------------*/
44 /* #define DEBUG */
45 #ifdef DEBUG
46 #include <stdio.h>
47 #define PRINTF(...) printf(__VA_ARGS__)
48 #else
49 #define PRINTF(...)
50 #endif
51 
52 typedef enum {
53  HT_SENSOR_STATUS_NOT_INIT = 0,
54  HT_SENSOR_STATUS_INIT,
55  HT_SENSOR_STATUS_NOT_ACTIVE = HT_SENSOR_STATUS_INIT,
56  HT_SENSOR_STATUS_ACTIVE
57 } ht_sensor_status_t;
58 
59 /* Absolute delta in light or humidity level needed to generate event */
60 #define DELTA_TEMP_SENSOR_VALUE 1
61 #define DELTA_HUM_SENSOR_VALUE 1
62 
63 /*---------------------------------------------------------------------------*/
64 /* LOCAL DATA DEFINITIONS */
65 /*---------------------------------------------------------------------------*/
66 const struct sensors_sensor ht_sensor;
67 volatile static ht_sensor_status_t ht_sensor_status = HT_SENSOR_STATUS_NOT_INIT;
68 static int prev_temp_event_val = 0;
69 static int prev_hum_event_val = 0;
70 static int temp_sensor_value = 0;
71 static int hum_sensor_value = 0;
72 
73 /*---------------------------------------------------------------------------*/
74 /* LOCAL FUNCTION PROTOTYPES */
75 /*---------------------------------------------------------------------------*/
76 PROCESS(HTSensorSampling, "Humidity/Temperature sensor");
77 
78 /*---------------------------------------------------------------------------*/
79 /* PUBLIC FUNCTIONS */
80 /*---------------------------------------------------------------------------*/
81 static int
82 configure(int type, int value)
83 {
84  if(type == SENSORS_HW_INIT) {
85  PRINTF("SENSORS_HW_INIT\n");
86  ht_sensor_status = HT_SENSOR_STATUS_INIT;
87  process_start(&HTSensorSampling, NULL);
88  return 1;
89  } else if(type == SENSORS_ACTIVE) {
90  if(ht_sensor_status != HT_SENSOR_STATUS_NOT_INIT) {
91  if(value) {
92  /* ACTIVATE SENSOR */
93  vHTSreset();
94  prev_temp_event_val = 0;
95  prev_hum_event_val = 0;
96  /* Activate ht sensor. Start sampling */
97  PRINTF("HT SENSOR ACTIVATED\n");
98  ht_sensor_status = HT_SENSOR_STATUS_ACTIVE;
99  process_post(&HTSensorSampling, PROCESS_EVENT_MSG, (void *)&ht_sensor_status);
100  } else {
101  /* DE-ACTIVATE SENSOR */
102  PRINTF("HT SENSOR DE-ACTIVATED\n");
103  ht_sensor_status = HT_SENSOR_STATUS_NOT_ACTIVE;
104  process_post(&HTSensorSampling, PROCESS_EVENT_MSG, (void *)&ht_sensor_status);
105  }
106  return 1;
107  } else {
108  /* HT sensor must be intialised before being (de)-activated */
109  PRINTF("ERROR: NO HW_INIT HT SENSOR\n");
110  return 0;
111  }
112  } else {
113  /* Non valid type */
114  return 0;
115  }
116 }
117 /*---------------------------------------------------------------------------*/
118 static int
119 status(int type)
120 {
121  if(type == SENSORS_ACTIVE) {
122  return ht_sensor_status == HT_SENSOR_STATUS_ACTIVE;
123  } else if(type == SENSORS_READY) {
124  return ht_sensor_status != HT_SENSOR_STATUS_NOT_INIT;
125  }
126  return 0;
127 }
128 /*---------------------------------------------------------------------------*/
129 static int
130 value(int type)
131 {
132  /* type: HT_SENSOR_TEMP is to return temperature
133  !=HT_SENSOR_TEMP is to return humidity */
134  if(type == HT_SENSOR_TEMP) {
135  return temp_sensor_value;
136  } else {
137  return hum_sensor_value;
138  }
139 }
140 /*---------------------------------------------------------------------------*/
141 /* LOCAL FUNCTIONS */
142 /*---------------------------------------------------------------------------*/
143 /* Process to get ht sensor value.
144  ht sensor is sampled. Sampling stopped when sensor is de-activated.
145  Event is generated if temp and/or hum value changed at least the value DELTA_TEMP_SENSOR_VALUE
146  or DELTA_HUM_SENSOR_VALUE since last event. */
147 PROCESS_THREAD(HTSensorSampling, ev, data)
148 {
149  PROCESS_BEGIN();
150  static struct etimer et;
151 
152  etimer_set(&et, CLOCK_SECOND);
153  while(1) {
154  PROCESS_WAIT_EVENT_UNTIL((ev == PROCESS_EVENT_TIMER) || (ev == PROCESS_EVENT_MSG));
155  if(ev == PROCESS_EVENT_TIMER) {
156  /* Handle sensor reading. */
157  vHTSstartReadTemp();
158  temp_sensor_value = u16HTSreadTempResult();
159  PRINTF("Temperature sample: %d\n", temp_sensor_value);
160  vHTSstartReadHumidity();
161  hum_sensor_value = u16HTSreadHumidityResult();
162  PRINTF("Humidity sample: %d\n", hum_sensor_value);
163  if((abs(temp_sensor_value - prev_temp_event_val) > DELTA_TEMP_SENSOR_VALUE) ||
164  (abs(hum_sensor_value - prev_hum_event_val) > DELTA_HUM_SENSOR_VALUE)) {
165  prev_temp_event_val = temp_sensor_value;
166  prev_hum_event_val = hum_sensor_value;
167  sensors_changed(&ht_sensor);
168  }
169  etimer_reset(&et);
170  } else {
171  /* ev == PROCESS_EVENT_MSG */
172  if(*(int *)data == HT_SENSOR_STATUS_NOT_ACTIVE) {
173  /* Stop sampling */
174  etimer_stop(&et);
175  } else if((*(int *)data == HT_SENSOR_STATUS_ACTIVE)) {
176  /* restart sampling */
177  etimer_restart(&et);
178  }
179  }
180  }
181  PROCESS_END();
182 }
183 
184 /*---------------------------------------------------------------------------*/
185 /* Sensor defintion for sensor module */
186 SENSORS_SENSOR(ht_sensor, HT_SENSOR, value, configure, status);
187 /*---------------------------------------------------------------------------*/
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:243
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
Definition: etimer.c:199
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
Event timer header file.
A timer.
Definition: etimer.h:76
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99