CircleOS  1
button.c
Go to the documentation of this file.
1 /****************** COPYRIGHT (C) 2007-2013 KEOLABS S.A.S. ********************/
14 /******************************************************************************/
16 
17 /* Includes ------------------------------------------------------------------*/
18 #include "circle.h"
19 
20 /* Private define ------------------------------------------------------------*/
21 //~ #define BUTTON_DIVIDER 10 /*!< Passes in BUTTON_Handler needed to enter it */
22 #define SHUTDOWNCOUNT 200
23 
24 /* Private variables ---------------------------------------------------------*/
25 bool fWaitForRelease = 0;
26 enum BUTTON_state Button_State = BUTTON_UNDEF;
27 enum BUTTON_mode Button_Mode = BUTTON_DISABLED;
28 enum JOYSTICK_state Joystick_State = JOYSTICK_UNDEF;
29 counter_t Joystick_Counter = 0;
30 bool fJoyWaitForRelease = 0;
31 counter_t Button_LongCounter = 0;
32 counter_t Button_Counter = 0;
33 
34 
35 /* Extern variables ----------------------------------------------------------*/
36 
37 /* Private function prototypes -----------------------------------------------*/
38 void JOYSTICK_Handler( void );
39 
40 /* Private functions ---------------------------------------------------------*/
41 
42 /*******************************************************************************
43 *
44 * BUTTON_Handler
45 *
46 *******************************************************************************/
54 /******************************************************************************/
55 void BUTTON_Handler( void )
56 {
57  u8 state;
58  enum BUTTON_state old_state = Button_State;
59  enum BUTTON_state new_state;
60 
61  //~ static divider_t divider = 0;
62 
63  //~ if ( ++divider % BUTTON_DIVIDER ) // YRT130718 divider is now managed by scheduler
64  //~ {
65  //~ return;
66  //~ }
67 
68  state = BUTTON_GetNewState();
69 
70  /* Manage shutdown by a long pressing.*/
71  if ( state )
72  {
73  Button_LongCounter++;
74 
75  if ( Button_LongCounter > WEIGHTED_TIME( SHUTDOWNCOUNT ) )
76  {
78  LCD_ClearAllScreen( RGB_BLACK );
80  DRAW_RestoreCharMagniCoeff();
81  DRAW_DisplayStringWithMode( 0, 100, "Shutting down !", ALL_SCREEN, INVERTED_TEXT, CENTER );
83  DRAW_DisplayStringWithMode( 0, 20, "the button !", ALL_SCREEN, INVERTED_TEXT, CENTER );
85 
86  #if DMA2D_AVAIL
87  DMA_LCD_UpdateScreen();
88  #endif
89 
90  while ( 1 )
91  {
93  }
94  }
95  }
96  else
97  {
98  Button_LongCounter = 0;
99  }
100 
101  if ( ( Button_Mode == BUTTON_ONOFF ) || ( Button_Mode == BUTTON_ONOFF_FORMAIN ) )
102  {
103  new_state = state ? BUTTON_PUSHED : BUTTON_RELEASED;
104 
105  if ( ( Button_Counter == 0 ) && ( new_state != old_state ) )
106  {
107  Button_State = new_state;
108  Button_Counter++;
109  }
110  else if ( ( Button_Counter < WEIGHTED_TIME( ANTI_BOUNCING_COUNT ) ) && fWaitForRelease )
111  {
112  Button_Counter++;
113  }
114  else
115  {
116  if ( fWaitForRelease && ( new_state == BUTTON_PUSHED ) )
117  {
118  /* Restart again until release.*/
119  Button_Counter = 1;
120 
121  return;
122  }
123  else
124  {
125  Button_Counter = 0;
126  fWaitForRelease = 0;
127  Button_State = new_state;
128  }
129  }
130  }
131 
132  /* Call the Joystick management if available */
133 #if JOYSTICK_AVAIL
134  JOYSTICK_Handler();
135 #endif
136 }
137 
138 #if JOYSTICK_AVAIL
139 /*******************************************************************************
140 *
141 * JOYSTICK_Handler
142 *
143 *******************************************************************************/
151 /******************************************************************************/
152 void JOYSTICK_Handler( void )
153 {
154  enum JOYSTICK_state old_state_joy = Joystick_State;
155  enum JOYSTICK_state new_state_joy;
156 
157  new_state_joy = JOYSTICK_GetNewState();
158 
159  if ( ( Joystick_Counter == 0 ) && ( new_state_joy != old_state_joy ) )
160  {
161  Joystick_State = new_state_joy;
162  Joystick_Counter ++;
163  }
164  else if ( ( Joystick_Counter < WEIGHTED_TIME( ANTI_BOUNCING_COUNT / 3 ) ) && fJoyWaitForRelease )
165  {
166  Joystick_Counter++;
167  }
168  else
169  {
170  if ( fJoyWaitForRelease && ( new_state_joy != JOYSTICK_RELEASED ) )
171  {
172  /* Restart again until release.*/
173  Joystick_Counter = 1;
174  }
175  else
176  {
177  Joystick_Counter = 0;
178  fJoyWaitForRelease = 0;
179  Joystick_State = new_state_joy;
180  }
181  }
182 }
183 
184 
185 /*******************************************************************************
186 *
187 * JOYSTICK_CircularPermutation
188 *
189 *******************************************************************************/
204 /******************************************************************************/
205 enum JOYSTICK_state JOYSTICK_CircularPermutation( enum JOYSTICK_state abs_direction, s8 iter )
206 {
207  s8 i;
208  enum JOYSTICK_state direction = abs_direction;
209 
210 // for(i = 0 ; i < iter ; i++)
211  for ( i = iter % 4; i > 0; --i )
212  {
213  switch ( direction )
214  {
215  case JOYSTICK_LEFT :
216  direction = JOYSTICK_UP;
217  break;
218  case JOYSTICK_UP :
219  direction = JOYSTICK_RIGHT;
220  break;
221  case JOYSTICK_RIGHT :
222  direction = JOYSTICK_DOWN;
223  break;
224  case JOYSTICK_DOWN :
225  direction = JOYSTICK_LEFT;
226  break;
227  case JOYSTICK_LEFT_UP :
228  direction = JOYSTICK_RIGHT_UP;
229  break;
230  case JOYSTICK_RIGHT_UP :
231  direction = JOYSTICK_RIGHT_DOWN;
232  break;
233  case JOYSTICK_RIGHT_DOWN :
234  direction = JOYSTICK_LEFT_DOWN;
235  break;
236  case JOYSTICK_LEFT_DOWN :
237  direction = JOYSTICK_LEFT_UP;
238  break;
239  default:
240  break;
241  }
242  }
243 
244  return direction;
245 }
246 
247 #endif // JOYSTICK_AVAIL
248 
250 
251 /* Public functions ----------------------------------------------------------*/
252 
253 
254 /*******************************************************************************
255 *
256 * BUTTON_GetMode
257 *
258 *******************************************************************************/
266 /******************************************************************************/
268 {
269  return Button_Mode;
270 }
271 
272 /*******************************************************************************
273 *
274 * BUTTON_SetMode
275 *
276 ********************************************************************************/
284 /********************************************************************************/
285 void BUTTON_SetMode( enum BUTTON_mode mode )
286 {
287  Button_Mode = mode;
288 
289 #if POWER_MNGT
290  if ( mode == BUTTON_ONOFF_FORMAIN )
291  {
292  POWER_Set_Time();
293  }
294  else
295  {
296  POWER_Reset_Time();
297  }
298 #endif /* POWER_MNGT */
299 }
300 
301 
302 /*******************************************************************************
303 *
304 * BUTTON_GetState
305 *
306 *******************************************************************************/
314 /******************************************************************************/
316 {
317  if ( fWaitForRelease )
318  {
319  return BUTTON_UNDEF;
320  }
321 
322  if ( Button_State == BUTTON_PUSHED )
323  {
324  if ( Button_Mode == BUTTON_ONOFF_FORMAIN )
325  {
326  return BUTTON_PUSHED_FORMAIN;
327  }
328  }
329 
330  return Button_State;
331 }
332 
333 /*******************************************************************************
334 *
335 * BUTTON_WaitForRelease
336 *
337 *******************************************************************************/
343 /******************************************************************************/
345 {
346  fWaitForRelease = 1;
347 }
348 
349 #if JOYSTICK_AVAIL
350 /*******************************************************************************
351 *
352 * JOYSTICK_GetState
353 *
354 *******************************************************************************/
362 {
363  if ( fJoyWaitForRelease )
364  return JOYSTICK_UNDEF;
365  else
366  return Joystick_State;
367 }
368 
369 
370 /*******************************************************************************
371 *
372 * JOYSTICK_WaitForRelease
373 *
374 *******************************************************************************/
380 /******************************************************************************/
382 {
383  fJoyWaitForRelease = 1;
384 }
385 
386 #endif // JOYSTICK_AVAIL