Contiki-NG
button-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 "button-sensor.h"
38#include <AppHardwareApi.h>
39
40/*---------------------------------------------------------------------------*/
41/* LOCAL DEFINITIONS */
42/*---------------------------------------------------------------------------*/
43/* #define DEBUG */
44#ifdef DEBUG
45#include <stdio.h>
46#define PRINTF(...) printf(__VA_ARGS__)
47#else
48#define PRINTF(...)
49#endif
50
51typedef enum {
52 APP_E_BUTTON_SW0 = 0,
53#if SENSOR_BOARD_DR1199
54 APP_E_BUTTON_SW1,
55 APP_E_BUTTON_SW2,
56 APP_E_BUTTON_SW3,
57 APP_E_BUTTON_SW4,
58#endif /* SENSOR_BOARD_DR1199 */
59 APP_E_BUTTON_NUM /* Number of buttons */
60} app_e_button_t;
61
62/* Mapping of DIO port connections to buttons. Use as mask to get button value */
63#define APP_PORT_BUTTON_SW0 (8)
64#if SENSOR_BOARD_DR1199
65#define APP_PORT_BUTTON_SW1 (11)
66#define APP_PORT_BUTTON_SW2 (12)
67#define APP_PORT_BUTTON_SW3 (17)
68#define APP_PORT_BUTTON_SW4 (1)
69#endif /* SENSOR_BOARD_DR1199 */
70
71/* Definition of port masks based on button mapping */
72#if SENSOR_BOARD_DR1199
73#define APP_BUTTONS_DIO_MASK ((1 << APP_PORT_BUTTON_SW0) | \
74 (1 << APP_PORT_BUTTON_SW1) | \
75 (1 << APP_PORT_BUTTON_SW2) | \
76 (1 << APP_PORT_BUTTON_SW3) | \
77 (1 << APP_PORT_BUTTON_SW4))
78#else /* SENSOR_BOARD_DR1199 */
79#define APP_BUTTONS_DIO_MASK (1 << APP_PORT_BUTTON_SW0)
80#endif /* SENSOR_BOARD_DR1199 */
81
82#define KEY_SAMPLE_TIME (CLOCK_SECOND / 20)
83
84typedef enum {
85 BUTTONS_STATUS_NOT_INIT = 0,
86 BUTTONS_STATUS_INIT,
87 BUTTONS_STATUS_NOT_ACTIVE = BUTTONS_STATUS_INIT,
88 BUTTONS_STATUS_ACTIVE
89} buttons_status_t;
90
91/*---------------------------------------------------------------------------*/
92/* LOCAL DATA DEFINITIONS */
93/*---------------------------------------------------------------------------*/
94const struct sensors_sensor button_sensor;
95volatile static buttons_status_t buttons_status = BUTTONS_STATUS_NOT_INIT;
96static int key_value = 0;
97static uint8 key_map[] = { APP_PORT_BUTTON_SW0, /* APP_E_BUTTON_SW0 */
98#if SENSOR_BOARD_DR1199
99 APP_PORT_BUTTON_SW1, /* APP_E_BUTTON_SW1 */
100 APP_PORT_BUTTON_SW2, /* APP_E_BUTTON_SW2 */
101 APP_PORT_BUTTON_SW3, /* APP_E_BUTTON_SW3 */
102 APP_PORT_BUTTON_SW4 /* APP_E_BUTTON_SW4 */
103#endif /* SENSOR_BOARD_DR1199 */
104};
105
106/*---------------------------------------------------------------------------*/
107/* LOCAL FUNCTION PROTOTYPES */
108/*---------------------------------------------------------------------------*/
109PROCESS(key_sampling, "Key sample");
110static int get_key_value(void);
111
112/*---------------------------------------------------------------------------*/
113/* PUBLIC FUNCTIONS */
114/*---------------------------------------------------------------------------*/
115static int
116configure(int type, int value)
117{
118 if(type == SENSORS_HW_INIT) {
119 /* Called from sensor thread when started.
120 Configure DIO lines with buttons connected as input */
121 vAHI_DioSetDirection(APP_BUTTONS_DIO_MASK, 0);
122 /* Turn on pull-ups for DIO lines with buttons connected */
123 vAHI_DioSetPullup(APP_BUTTONS_DIO_MASK, 0);
124 PRINTF("HW_INIT BUTTONS (0x%x)\n", APP_BUTTONS_DIO_MASK);
125 /* Configure debounce timer. Do not run it yet. */
126 buttons_status = BUTTONS_STATUS_INIT;
127 process_start(&key_sampling, NULL);
128 return 1;
129 } else if(type == SENSORS_ACTIVE) {
130 if(buttons_status != BUTTONS_STATUS_NOT_INIT) {
131 if(value) {
132 /* Button sensor activated */
133 PRINTF("BUTTONS ACTIVATED\n");
134 buttons_status = BUTTONS_STATUS_ACTIVE;
135 } else {
136 /* Button sensor de-activated */
137 PRINTF("BUTTONS DE-ACTIVATED\n");
138 buttons_status = BUTTONS_STATUS_NOT_ACTIVE;
139 }
140 process_post(&key_sampling, PROCESS_EVENT_MSG, (void *)&buttons_status);
141 return 1;
142 } else {
143 /* Buttons must be intialised before being (de)-activated */
144 PRINTF("ERROR: NO HW_INIT BUTTONS\n");
145 return 0;
146 }
147 } else {
148 /* Non valid type */
149 return 0;
150 }
151}
152/*---------------------------------------------------------------------------*/
153static int
154status(int type)
155{
156 if(type == SENSORS_ACTIVE) {
157 return buttons_status == BUTTONS_STATUS_ACTIVE;
158 } else if(type == SENSORS_READY) {
159 return buttons_status != BUTTONS_STATUS_NOT_INIT;
160 }
161 return 0;
162}
163/*---------------------------------------------------------------------------*/
164static int
165value(int type)
166{
167 /* type: Not defined for the buttons interface
168 */
169 return key_value;
170}
171/*---------------------------------------------------------------------------*/
172/* LOCAL FUNCTIONS */
173/*---------------------------------------------------------------------------*/
174static int
175get_key_value(void)
176{
177 /* Function returns the actual key value. Pressed key will return '1' */
178 int io_value = ~u32AHI_DioReadInput() & APP_BUTTONS_DIO_MASK;
179 int k = 0;
180 int key = 0;
181
182 while(k < APP_E_BUTTON_NUM) {
183 if(io_value & (1 << key_map[k])) {
184 key |= (1 << k);
185 }
186 k++;
187 }
188 return key;
189}
190/* Process takes care of detecting key changes */
191/*---------------------------------------------------------------------------*/
192PROCESS_THREAD(key_sampling, ev, data)
193{
195 static struct etimer et;
196 static int previous_key_value = 0;
197 static char debounce_check = 0;
198 int current_key_value;
199
200 etimer_set(&et, CLOCK_SECOND / 50);
201 while(1) {
202 PROCESS_WAIT_EVENT_UNTIL((ev == PROCESS_EVENT_TIMER) || (ev == PROCESS_EVENT_MSG));
203 if(ev == PROCESS_EVENT_TIMER) {
204 /* Handle sensor reading. */
205 PRINTF("Key sample\n");
206 current_key_value = get_key_value();
207 if(debounce_check != 0) {
208 /* Check if key remained constant */
209 if(previous_key_value == current_key_value) {
210 sensors_changed(&button_sensor);
211 key_value = current_key_value;
212 debounce_check = 0;
213 } else {
214 /* Bouncing */
215 previous_key_value = current_key_value;
216 }
217 } else
218 /* Check for new key change */
219 if(current_key_value != previous_key_value) {
220 previous_key_value = current_key_value;
221 debounce_check = 1;
222 }
223 etimer_reset(&et);
224 } else {
225 /* ev == PROCESS_EVENT_MSG */
226 if(*(buttons_status_t *)data == BUTTONS_STATUS_NOT_ACTIVE) {
227 /* Stop sampling */
228 etimer_stop(&et);
229 } else if((*(buttons_status_t *)data == BUTTONS_STATUS_ACTIVE)) {
230 /* restart sampling */
231 etimer_restart(&et);
232 }
233 }
234 }
235 PROCESS_END();
236}
237
238/*---------------------------------------------------------------------------*/
239/* Sensor defintion for sensor module */
240SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, configure, status);
241/*---------------------------------------------------------------------------*/
Event timer header file.
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
Definition: etimer.c:199
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:243
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
static int value(int type, nrf_drv_gpiote_pin_t pin)
Return current state of a button.
static int status(int type, nrf_drv_gpiote_pin_t pin)
Get status of a given button.
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#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 PROCESS_END()
Define the end of a process.
Definition: process.h:131
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
A timer.
Definition: etimer.h:76
SENSORS & button_sensor
Exports global symbols for the sensor API.
Definition: z1-sensors.c:46