Contiki 2.5
key.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Swedish Institute of Computer Science
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 are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in
12  * the documentation and/or other materials provided with the
13  * distribution.
14  * * Neither the name of the copyright holders nor the names of
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \file
32  *
33  * \brief
34  * This file provides joystick operations. Combined with ADC functions.
35  *
36  * \author
37  * Mike Vidales mavida404@gmail.com
38  *
39  */
40 
41 #include "key.h"
42 #include "uart.h"
43 #include "main.h"
44 #include "adc.h"
45 
46 /**
47  * \addtogroup lcd
48  * \{
49 */
50 
51 /*---------------------------------------------------------------------------*/
52 
53 /**
54  * \brief This will intialize the joystick and the ADC for button readings.
55 */
56 void
57 key_init(void)
58 {
59  /* Disable digital input buffer for joystick signal */
60  DIDR0 |= (1 << ADC1D);
61 
62  /* Enter is input w/pullup */
63  ENTER_DDR &= ~(1<<ENTER_PIN);
64  ENTER_PUR |= (1<<ENTER_PIN);
65 
66  /* Joystick is input wo/pullup (all though normal port function is overridden by ADC module when reading) */
67  KEY_DDR &= ~(1<<KEY_PIN);
68  KEY_PUR &= ~(1<<KEY_PIN);
69 
70  /* Get the ADC ready to use */
71  adc_init(ADC_CHAN_ADC1, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128);
72 }
73 
74 /*---------------------------------------------------------------------------*/
75 
76 /**
77  * \brief This will disable the ADC used for button readings.
78 */
79 void
81 {
82  /* Turn off the ADC */
83  adc_deinit();
84 }
85 
86 /*---------------------------------------------------------------------------*/
87 
88 /**
89  * \brief This will poll run key_task() to determine if a button has been pressed.
90  *
91  * \retval True if button is pressed
92  * \retval False if button is not pressed
93 */
94 uint8_t
95 is_button(void)
96 {
97  /* Return true if button has been pressed. */
98  if (key_task() == KEY_NO_KEY){
99  return false;
100  }
101  else{
102  return true;
103  }
104 }
105 
106 /*---------------------------------------------------------------------------*/
107 
108 /**
109  * \brief This function will wait for a user to press a button.
110  *
111  * \return retval Returns the global button state.
112 */
113 uint8_t
115 {
116  uint8_t retval;
117  while (!is_button())
118  ;
119 
120  retval = button;
121  button = KEY_STATE_DONE;
122  return retval;
123 }
124 
125 /*---------------------------------------------------------------------------*/
126 
127 /**
128  * \brief This will check the joystick state to return the current button status.
129  *
130  * \return button Current button state.
131 */
132 key_state_t
133 key_task(void)
134 {
135  key_state_t key_state;
136 
137  /* Check joystick state. Post event if any change since last */
138  key_state = key_state_get();
139  if (key_state == KEY_STATE_NO_KEY){
140  if (button == KEY_STATE_DONE){
141  button = KEY_STATE_NO_KEY;
142  }
143  return KEY_NO_KEY;
144  }
145 
146  /* Key_state is button press code */
147  if (button == KEY_STATE_DONE){
148  /*
149  * Button has already been used, don't return any more presses
150  * until the button is released/re-pressed
151  */
152  return KEY_NO_KEY;
153  }
154 
155  /* Button has been pressed for the first time */
156  button = key_state;
157  return button;
158 }
159 
160 /*---------------------------------------------------------------------------*/
161 
162 /**
163  * \brief This function will start the ADC conversion and read the current
164  * converstion value to determine the button position.
165  *
166  * \retval KEY_ENTER Enter button has been pressed.
167  * \retval KEY_UP Up Button has been pressed.
168  * \retval KEY_RIGHT Right Button has been pressed.
169  * \retval KEY_LEFT Left Button has been pressed.
170  * \retval KEY_DOWN Down Button has been pressed.
171  * \retval KEY_NO_KEY No Button has been pressed.
172 */
173 key_state_t
175 {
176  int16_t reading;
177 
178  /* Start the A/D conversion */
180 
181  /* Wait for conversion to finish */
182  while ((reading = adc_result_get(ADC_ADJ_RIGHT)) == EOF )
183  ;
184 
185  /* Determine which button (if any) is being pressed */
186  if (!(ENTER_PORT & (1<<ENTER_PIN))){
187  return KEY_ENTER;
188  }
189  if (reading < 0x00A0){
190  return KEY_UP;
191  }
192  if (reading < 0x0180){
193  return KEY_RIGHT;
194  }
195  if (reading < 0x0280){
196  return KEY_LEFT;
197  }
198  if (reading < 0x0380){
199  return KEY_DOWN;
200  }
201  return KEY_NO_KEY;
202 }
203 
204 /** \} */