Contiki-NG
light-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 "light-sensor.h"
38 #include <AppHardwareApi.h>
39 #include <AlsDriver.h>
40 #include <stdlib.h>
41 
42 /*---------------------------------------------------------------------------*/
43 /* LOCAL DEFINITIONS */
44 /*---------------------------------------------------------------------------*/
45 /* #define DEBUG */
46 #ifdef DEBUG
47 #include <stdio.h>
48 #define PRINTF(...) printf(__VA_ARGS__)
49 #else
50 #define PRINTF(...)
51 #endif
52 
53 typedef enum {
54  LIGHT_SENSOR_STATUS_NOT_INIT = 0,
55  LIGHT_SENSOR_STATUS_INIT,
56  LIGHT_SENSOR_STATUS_NOT_ACTIVE = LIGHT_SENSOR_STATUS_INIT,
57  LIGHT_SENSOR_STATUS_ACTIVE
58 } light_sensor_status_t;
59 
60 /* Absolute delta in light level needed to generate event */
61 #define DELTA_LIGHT_SENSOR_VALUE 1
62 
63 /*---------------------------------------------------------------------------*/
64 /* LOCAL DATA DEFINITIONS */
65 /*---------------------------------------------------------------------------*/
66 const struct sensors_sensor light_sensor;
67 volatile static light_sensor_status_t light_sensor_status = LIGHT_SENSOR_STATUS_NOT_INIT;
68 static int prev_light_event_val = 0;
69 static int light_sensor_value = 0;
70 
71 /*---------------------------------------------------------------------------*/
72 /* LOCAL FUNCTION PROTOTYPES */
73 /*---------------------------------------------------------------------------*/
74 static int adjust(int input1, int input2);
75 PROCESS(LightSensorSampling, "Light sensor");
76 
77 /*---------------------------------------------------------------------------*/
78 /* PUBLIC FUNCTIONS */
79 /*---------------------------------------------------------------------------*/
80 static int
81 configure(int type, int value)
82 {
83  if(type == SENSORS_HW_INIT) {
84  PRINTF("SENSORS_HW_INIT\n");
85  light_sensor_status = LIGHT_SENSOR_STATUS_INIT;
86  process_start(&LightSensorSampling, NULL);
87  return 1;
88  } else if(type == SENSORS_ACTIVE) {
89  if(light_sensor_status != LIGHT_SENSOR_STATUS_NOT_INIT) {
90  if(value) {
91  /* ACTIVATE SENSOR */
92  vALSreset();
93  prev_light_event_val = 0;
94  /* Activate light sensor. Use channel 0. (Channel 1 = IR). Start sampling */
95  PRINTF("LIGHT SENSOR ACTIVATED\n");
96  light_sensor_status = LIGHT_SENSOR_STATUS_ACTIVE;
97  process_post(&LightSensorSampling, PROCESS_EVENT_MSG, (void *)&light_sensor_status);
98  } else {
99  /* DE-ACTIVATE SENSOR */
100  vALSpowerDown();
101  PRINTF("LIGHT SENSOR DE-ACTIVATED\n");
102  light_sensor_status = LIGHT_SENSOR_STATUS_NOT_ACTIVE;
103  process_post(&LightSensorSampling, PROCESS_EVENT_MSG, (void *)&light_sensor_status);
104  }
105  return 1;
106  } else {
107  /* Light sensor must be intialised before being (de)-activated */
108  PRINTF("ERROR: NO HW_INIT LIGHT SENSOR\n");
109  return 0;
110  }
111  } else {
112  /* Non valid type */
113  return 0;
114  }
115 }
116 /*---------------------------------------------------------------------------*/
117 static int
118 status(int type)
119 {
120  if(type == SENSORS_ACTIVE) {
121  return light_sensor_status == LIGHT_SENSOR_STATUS_ACTIVE;
122  } else if(type == SENSORS_READY) {
123  return light_sensor_status != LIGHT_SENSOR_STATUS_NOT_INIT;
124  }
125  return 0;
126 }
127 /*---------------------------------------------------------------------------*/
128 static int
129 value(int type)
130 {
131  /* type: Not defined for the light sensor interface */
132  return light_sensor_value;
133 }
134 /*---------------------------------------------------------------------------*/
135 /* LOCAL FUNCTIONS */
136 /*---------------------------------------------------------------------------*/
137 /* Process to get light sensor value.
138  Light sensor is sampled. Sampling stopped when sensor is de-activated.
139  Event is generated if light value changed at least the value DELTA_LIGHT_SENSOR_VALUE
140  since last event. */
141 PROCESS_THREAD(LightSensorSampling, ev, data)
142 {
143  PROCESS_BEGIN();
144  static struct etimer et;
145  int channel0_value, channel1_value;
146 
147  etimer_set(&et, CLOCK_SECOND / 10);
148  while(1) {
149  PROCESS_WAIT_EVENT_UNTIL((ev == PROCESS_EVENT_TIMER) || (ev == PROCESS_EVENT_MSG));
150  if(ev == PROCESS_EVENT_TIMER) {
151  /* Handle sensor reading. */
152  PRINTF("Light sensor sample\n");
153  vALSstartReadChannel(0);
154  channel0_value = u16ALSreadChannelResult();
155  PRINTF("Channel 0 = %d\n", channel0_value);
156  vALSstartReadChannel(1);
157  channel1_value = u16ALSreadChannelResult();
158  PRINTF("Channel 1 = %d\n", channel1_value);
159  light_sensor_value = adjust(channel0_value, channel1_value);
160  PRINTF("Light output = %d\n", light_sensor_value);
161  if(abs(light_sensor_value - prev_light_event_val) > DELTA_LIGHT_SENSOR_VALUE) {
162  prev_light_event_val = light_sensor_value;
163  sensors_changed(&light_sensor);
164  }
165  etimer_reset(&et);
166  } else {
167  /* ev == PROCESS_EVENT_MSG */
168  if(*(int *)data == LIGHT_SENSOR_STATUS_NOT_ACTIVE) {
169  /* Stop sampling */
170  etimer_stop(&et);
171  } else if((*(int *)data == LIGHT_SENSOR_STATUS_ACTIVE)) {
172  /* restart sampling */
173  etimer_restart(&et);
174  }
175  }
176  }
177  PROCESS_END();
178 }
179 
180 /*---------------------------------------------------------------------------*/
181 /* Sensor defintion for sensor module */
182 SENSORS_SENSOR(light_sensor, LIGHT_SENSOR, value, configure, status);
183 /*---------------------------------------------------------------------------*/
184 
185 /*---------------------------------------------------------------------------*/
186 /* adjust() converts the 2 measured light level into 1 ambient light level. */
187 /* See manual JN-RM-2003.pdf */
188 /* Approximation is used: output[Lux] = 0.39*(ch0-ch1) */
189 /*---------------------------------------------------------------------------*/
190 static int
191 adjust(int ch0, int ch1)
192 {
193  if(ch0 > ch1) {
194  return (39 * (ch0 - ch1)) / 100;
195  } else {
196  return 0;
197  }
198 }
#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:1107
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