Contiki 2.6
|
00001 /* 00002 * Copyright (c) 2010, STMicroelectronics. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above 00011 * copyright notice, this list of conditions and the following 00012 * disclaimer in the documentation and/or other materials provided 00013 * with the distribution. 00014 * 3. The name of the author may not be used to endorse or promote 00015 * products derived from this software without specific prior 00016 * written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 00019 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00020 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00022 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00023 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00024 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 * This file is part of the Contiki OS 00031 * 00032 * $Id: button-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $ 00033 */ 00034 /*---------------------------------------------------------------------------*/ 00035 /** 00036 * \file 00037 * Button sensor. 00038 * \author 00039 * Salvatore Pitrulli <salvopitru@users.sourceforge.net> 00040 */ 00041 /*---------------------------------------------------------------------------*/ 00042 00043 #include "dev/button-sensor.h" 00044 #include "hal.h" 00045 #include "hal/micro/micro-common.h" 00046 #include "hal/micro/cortexm3/micro-common.h" 00047 00048 #include BOARD_HEADER 00049 00050 #define DEBOUNCE 1 00051 00052 #if DEBOUNCE 00053 static struct timer debouncetimer; 00054 #endif 00055 00056 #define FALSE 0 00057 #define TRUE 1 00058 00059 uint8_t button_flags = 0; 00060 00061 #define BUTTON_ACTIVE_FLG 0x01 00062 #define BUTTON_PRESSED_FLG 0x02 00063 00064 #define BUTTON_HAS_BEEN_PRESSED() (button_flags & BUTTON_PRESSED_FLG) 00065 #define BUTTON_HAS_BEEN_RELEASED() (!(button_flags & BUTTON_PRESSED_FLG)) 00066 #define BUTTON_SET_PRESSED() (button_flags |= BUTTON_PRESSED_FLG) 00067 #define BUTTON_SET_RELEASED() (button_flags &= ~BUTTON_PRESSED_FLG) 00068 00069 /*---------------------------------------------------------------------------*/ 00070 static void 00071 init(void) 00072 { 00073 #if DEBOUNCE 00074 timer_set(&debouncetimer, 0); 00075 #endif 00076 00077 /* Configure GPIO for BUTTONSs */ 00078 halInitButton(); 00079 00080 } 00081 /*---------------------------------------------------------------------------*/ 00082 static void 00083 activate(void) 00084 { 00085 button_flags |= BUTTON_ACTIVE_FLG; 00086 } 00087 /*---------------------------------------------------------------------------*/ 00088 static void 00089 deactivate(void) 00090 { 00091 button_flags &= ~BUTTON_ACTIVE_FLG; 00092 } 00093 /*---------------------------------------------------------------------------*/ 00094 static int 00095 active(void) 00096 { 00097 return (button_flags & BUTTON_ACTIVE_FLG)? 1 : 0; 00098 } 00099 /*---------------------------------------------------------------------------*/ 00100 static int 00101 value(int type) 00102 { 00103 if(!active()){ 00104 return 0; 00105 } 00106 00107 00108 #if DEBOUNCE 00109 if(timer_expired(&debouncetimer)) { 00110 00111 if(halGetButtonStatus(BUTTON_S1) == BUTTON_PRESSED){ 00112 00113 timer_set(&debouncetimer, CLOCK_SECOND / 10); 00114 if(BUTTON_HAS_BEEN_RELEASED()){ // Button has been previously released. 00115 sensors_changed(&button_sensor); 00116 } 00117 BUTTON_SET_PRESSED(); 00118 00119 return 1; 00120 } 00121 else { 00122 BUTTON_SET_RELEASED(); 00123 return 0; 00124 } 00125 } 00126 else { 00127 return 0; 00128 } 00129 #else 00130 if(halGetButtonStatus(BUTTON_S1) == BUTTON_PRESSED){ 00131 sensors_changed(&button_sensor); 00132 return 1; 00133 } 00134 else { 00135 return 0; 00136 } 00137 #endif 00138 00139 } 00140 /*---------------------------------------------------------------------------*/ 00141 static int 00142 configure(int type, int value) 00143 { 00144 switch(type){ 00145 case SENSORS_HW_INIT: 00146 init(); 00147 return 1; 00148 case SENSORS_ACTIVE: 00149 if(value) 00150 activate(); 00151 else 00152 deactivate(); 00153 return 1; 00154 } 00155 00156 return 0; 00157 } 00158 /*---------------------------------------------------------------------------*/ 00159 static int 00160 status(int type) 00161 { 00162 switch(type) { 00163 00164 case SENSORS_READY: 00165 return active(); 00166 } 00167 00168 return 0; 00169 } 00170 /*---------------------------------------------------------------------------*/ 00171 #if 0 00172 void BUTTON_S1_ISR(void) 00173 { 00174 00175 ENERGEST_ON(ENERGEST_TYPE_IRQ); 00176 00177 //sensors_handle_irq(IRQ_BUTTON); 00178 00179 if(INT_GPIOFLAG & BUTTON_S1_FLAG_BIT) { 00180 00181 #if DEBOUNCE 00182 if(timer_expired(&debouncetimer)) { 00183 timer_set(&debouncetimer, CLOCK_SECOND / 5); 00184 sensors_changed(&button_sensor); 00185 } 00186 #else 00187 sensors_changed(&button_sensor); 00188 #endif 00189 00190 } 00191 00192 INT_GPIOFLAG = BUTTON_S1_FLAG_BIT; 00193 00194 ENERGEST_OFF(ENERGEST_TYPE_IRQ); 00195 } 00196 #endif 00197 /*---------------------------------------------------------------------------*/ 00198 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, 00199 value, configure, status); 00200