Contiki 2.5
button-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, STMicroelectronics.
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
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki OS
31  *
32  * $Id: button-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $
33  */
34 /*---------------------------------------------------------------------------*/
35 /**
36 * \file
37 * Button sensor.
38 * \author
39 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
40 */
41 /*---------------------------------------------------------------------------*/
42 
43 #include "dev/button-sensor.h"
44 #include "hal.h"
45 #include "hal/micro/micro-common.h"
47 
48 #include BOARD_HEADER
49 
50 #define DEBOUNCE 1
51 
52 #if DEBOUNCE
53 static struct timer debouncetimer;
54 #endif
55 
56 #define FALSE 0
57 #define TRUE 1
58 
59 uint8_t button_flags = 0;
60 
61 #define BUTTON_ACTIVE_FLG 0x01
62 #define BUTTON_PRESSED_FLG 0x02
63 
64 #define BUTTON_HAS_BEEN_PRESSED() (button_flags & BUTTON_PRESSED_FLG)
65 #define BUTTON_HAS_BEEN_RELEASED() (!(button_flags & BUTTON_PRESSED_FLG))
66 #define BUTTON_SET_PRESSED() (button_flags |= BUTTON_PRESSED_FLG)
67 #define BUTTON_SET_RELEASED() (button_flags &= ~BUTTON_PRESSED_FLG)
68 
69 /*---------------------------------------------------------------------------*/
70 static void
71 init(void)
72 {
73  #if DEBOUNCE
74  timer_set(&debouncetimer, 0);
75  #endif
76 
77  /* Configure GPIO for BUTTONSs */
78  halInitButton();
79 
80 }
81 /*---------------------------------------------------------------------------*/
82 static void
83 activate(void)
84 {
85  button_flags |= BUTTON_ACTIVE_FLG;
86 }
87 /*---------------------------------------------------------------------------*/
88 static void
89 deactivate(void)
90 {
91  button_flags &= ~BUTTON_ACTIVE_FLG;
92 }
93 /*---------------------------------------------------------------------------*/
94 static int
95 active(void)
96 {
97  return (button_flags & BUTTON_ACTIVE_FLG)? 1 : 0;
98 }
99 /*---------------------------------------------------------------------------*/
100 static int
101 value(int type)
102 {
103  if(!active()){
104  return 0;
105  }
106 
107 
108 #if DEBOUNCE
109  if(timer_expired(&debouncetimer)) {
110 
111  if(halGetButtonStatus(BUTTON_S1) == BUTTON_PRESSED){
112 
113  timer_set(&debouncetimer, CLOCK_SECOND / 10);
114  if(BUTTON_HAS_BEEN_RELEASED()){ // Button has been previously released.
115  sensors_changed(&button_sensor);
116  }
117  BUTTON_SET_PRESSED();
118 
119  return 1;
120  }
121  else {
122  BUTTON_SET_RELEASED();
123  return 0;
124  }
125  }
126  else {
127  return 0;
128  }
129 #else
130  if(halGetButtonStatus(BUTTON_S1) == BUTTON_PRESSED){
131  sensors_changed(&button_sensor);
132  return 1;
133  }
134  else {
135  return 0;
136  }
137 #endif
138 
139 }
140 /*---------------------------------------------------------------------------*/
141 static int
142 configure(int type, int value)
143 {
144  switch(type){
145  case SENSORS_HW_INIT:
146  init();
147  return 1;
148  case SENSORS_ACTIVE:
149  if(value)
150  activate();
151  else
152  deactivate();
153  return 1;
154  }
155 
156  return 0;
157 }
158 /*---------------------------------------------------------------------------*/
159 static int
160 status(int type)
161 {
162  switch(type) {
163 
164  case SENSORS_READY:
165  return active();
166  }
167 
168  return 0;
169 }
170 /*---------------------------------------------------------------------------*/
171 #if 0
172 void BUTTON_S1_ISR(void)
173 {
174 
175  ENERGEST_ON(ENERGEST_TYPE_IRQ);
176 
177  //sensors_handle_irq(IRQ_BUTTON);
178 
179  if(INT_GPIOFLAG & BUTTON_S1_FLAG_BIT) {
180 
181 #if DEBOUNCE
182  if(timer_expired(&debouncetimer)) {
183  timer_set(&debouncetimer, CLOCK_SECOND / 5);
184  sensors_changed(&button_sensor);
185  }
186 #else
187  sensors_changed(&button_sensor);
188 #endif
189 
190  }
191 
192  INT_GPIOFLAG = BUTTON_S1_FLAG_BIT;
193 
194  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
195 }
196 #endif
197 /*---------------------------------------------------------------------------*/
198 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
199  value, configure, status);
200