Contiki-NG
pot-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 "pot-sensor.h"
38 #include <stdlib.h>
39 #include <GenericBoard.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  POT_STATUS_NOT_INIT = 0,
54  POT_STATUS_INIT,
55  POT_STATUS_NOT_ACTIVE = POT_STATUS_INIT,
56  POT_STATUS_ACTIVE
57 } pot_status_t;
58 
59 /* Absolute delta in pot level needed to generate event */
60 #define DELTA_POT_VALUE 1
61 
62 /*---------------------------------------------------------------------------*/
63 /* LOCAL DATA DEFINITIONS */
64 /*---------------------------------------------------------------------------*/
65 const struct sensors_sensor pot_sensor;
66 volatile static pot_status_t pot_status = POT_STATUS_NOT_INIT;
67 static int prev_pot_event_val = 0;
68 static int pot_value = 0;
69 
70 /*---------------------------------------------------------------------------*/
71 /* LOCAL FUNCTION PROTOTYPES */
72 /*---------------------------------------------------------------------------*/
73 PROCESS(POTSampling, "POT");
74 
75 /*---------------------------------------------------------------------------*/
76 /* PUBLIC FUNCTIONS */
77 /*---------------------------------------------------------------------------*/
78 static int
79 configure(int type, int value)
80 {
81  if(type == SENSORS_HW_INIT) {
82  pot_status = POT_STATUS_INIT;
83  bPotEnable();
84  process_start(&POTSampling, NULL);
85  return 1;
86  } else if(type == SENSORS_ACTIVE) {
87  if(pot_status != POT_STATUS_NOT_INIT) {
88  if(value) {
89  /* ACTIVATE SENSOR */
90  bPotEnable();
91  prev_pot_event_val = 0;
92  /* Activate POT. */
93  PRINTF("POT ACTIVATED\n");
94  pot_status = POT_STATUS_ACTIVE;
95  process_post(&POTSampling, PROCESS_EVENT_MSG, (void *)&pot_status);
96  } else {
97  /* DE-ACTIVATE SENSOR */
98  bPotDisable();
99  PRINTF("POT DE-ACTIVATED\n");
100  pot_status = POT_STATUS_NOT_ACTIVE;
101  process_post(&POTSampling, PROCESS_EVENT_MSG, (void *)&pot_status);
102  }
103  return 1;
104  } else {
105  /*
106  POT must be intialised before being (de)-activated */
107  PRINTF("ERROR: NO HW_INIT POT\n");
108  return 0;
109  }
110  } else {
111  /* Non valid type */
112  return 0;
113  }
114 }
115 /*---------------------------------------------------------------------------*/
116 static int
117 status(int type)
118 {
119  if(type == SENSORS_ACTIVE) {
120  return pot_status == POT_STATUS_ACTIVE;
121  } else if(type == SENSORS_READY) {
122  return pot_status != POT_STATUS_NOT_INIT;
123  }
124  return 0;
125 }
126 /*---------------------------------------------------------------------------*/
127 static int
128 value(int type)
129 {
130  /* type: Not defined for the pot interface */
131  return pot_value;
132 }
133 /*---------------------------------------------------------------------------*/
134 /* LOCAL FUNCTIONS */
135 /*---------------------------------------------------------------------------*/
136 /* Process to get POT_SENSOR value.
137  POT is sampled. Sampling stopped when POT is de-activated.
138  Event is generated if pot value changed at least the value DELTA_POT_VALUE
139  since last event. */
140 PROCESS_THREAD(POTSampling, ev, data)
141 {
142  PROCESS_BEGIN();
143  static struct etimer et;
144 
145  etimer_set(&et, CLOCK_SECOND / 10);
146  while(1) {
147  PROCESS_WAIT_EVENT_UNTIL((ev == PROCESS_EVENT_TIMER) || (ev == PROCESS_EVENT_MSG));
148  if(ev == PROCESS_EVENT_TIMER) {
149  /* Handle sensor reading. */
150  PRINTF("POT sample\n");
151  pot_value = u16ReadPotValue();
152  PRINTF("POT = %d\n", pot_value);
153  if(abs(pot_value - prev_pot_event_val) > DELTA_POT_VALUE) {
154  prev_pot_event_val = pot_value;
155  sensors_changed(&pot_sensor);
156  }
157  etimer_reset(&et);
158  } else {
159  /* ev == PROCESS_EVENT_MSG */
160  if(*(int *)data == POT_STATUS_NOT_ACTIVE) {
161  /* Stop sampling */
162  etimer_stop(&et);
163  } else if((*(int *)data == POT_STATUS_ACTIVE)) {
164  /* restart sampling */
165  etimer_restart(&et);
166  }
167  }
168  }
169  PROCESS_END();
170 }
171 
172 /*---------------------------------------------------------------------------*/
173 /* Sensor defintion for sensor module */
174 SENSORS_SENSOR(pot_sensor, POT_SENSOR, value, configure, status);
175 /*---------------------------------------------------------------------------*/
176 
#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