Caddy
A 2005 Roborodentia entry with vision and path planning capability
 All Data Structures Files Functions Variables Typedefs Macros Pages
buttons.c
Go to the documentation of this file.
1 /*
2  * This file is part of Caddy.
3  *
4  * Caddy is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Caddy is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Caddy. If not, see <http://www.gnu.org/licenses/>.
16  */
18 #include "buttons.h"
19 
20 // AVRLIB
21 #include "avrlibdefs.h"
22 
23 // avr-libc
24 #include <stdint.h>
25 #include <stdbool.h>
26 
27 #define DEBOUNCE_COUNT 3 // should be equal to 2 or greater
28 
29 static bool isDown[NUM_BUTTONS] = { false, false, false,
30  false, false, false };
31 static bool wasEvent[NUM_BUTTONS] = { false, false, false,
32  false, false, false };
33 static uint8_t upCount[NUM_BUTTONS] = { DEBOUNCE_COUNT, DEBOUNCE_COUNT,
34  DEBOUNCE_COUNT, DEBOUNCE_COUNT,
35  DEBOUNCE_COUNT, DEBOUNCE_COUNT };
36 static uint8_t downCount[NUM_BUTTONS] = { 0, 0, 0, 0, 0, 0 };
37 
38 void waitFor(uint8_t button)
39 {
41  while (!justReleased(button))
42  {
44  }
45 }
46 
50 inline bool justPressed(uint8_t button)
51 {
52  return wasEvent[button] && isDown[button];
53 }
54 
58 inline bool justReleased(uint8_t button)
59 {
60  return wasEvent[button] && !isDown[button];
61 }
62 
66 void debounceButtons(void)
67 {
68  uint8_t button;
69  for (button = 0; button < NUM_BUTTONS; button++)
70  {
71  // count times buttons have been consecutively up/down (upto DEBOUNCE_COUNT).
72  if (isPressed(button))
73  {
74  downCount[button] = MIN(downCount[button]+1, DEBOUNCE_COUNT);
75  upCount[button] = 0;
76  }
77  else
78  {
79  upCount[button] = MIN(upCount[button]+1, DEBOUNCE_COUNT);
80  downCount[button] = 0;
81  }
82 
83  // check for confirmed up/down event
84  if (isDown[button])
85  {
86  if (upCount[button] >= DEBOUNCE_COUNT)
87  {
88  isDown[button] = false;
89  wasEvent[button] = true;
90  }
91  else
92  {
93  wasEvent[button] = false;
94  }
95  }
96  else
97  {
98  if (downCount[button] >= DEBOUNCE_COUNT)
99  {
100  isDown[button] = true;
101  wasEvent[button] = true;
102  }
103  else
104  {
105  wasEvent[button] = false;
106  }
107  }
108  }
109 }
110 
114 inline bool isPressed(uint8_t button)
115 {
116  switch (button)
117  {
118  case RED_BUTTON: return RED_BUTTON_DOWN;
119  case L_UP_BUTTON: return L_UP_BUTTON_DOWN;
120  case L_DOWN_BUTTON: return L_DOWN_BUTTON_DOWN;
121  case R_UP_BUTTON: return R_UP_BUTTON_DOWN;
122  case R_DOWN_BUTTON: return R_DOWN_BUTTON_DOWN;
123  case NEST_BUTTON: return NEST_BUTTON_DOWN;
124  default: break;
125  }
126 
127  return false;
128 }
129 
130 inline bool bothRightButtonsPressed(void)
131 {
132  return (justPressed(R_UP_BUTTON) && justPressed(R_DOWN_BUTTON)) ||
133  (justPressed(R_UP_BUTTON) && isDown[R_DOWN_BUTTON]) ||
134  (justPressed(R_DOWN_BUTTON) && isDown[R_UP_BUTTON]);
135 
136 }
137 
138 inline bool bothLeftButtonsPressed(void)
139 {
140  return (justPressed(L_UP_BUTTON) && justPressed(L_DOWN_BUTTON)) ||
141  (justPressed(L_UP_BUTTON) && isDown[L_DOWN_BUTTON]) ||
142  (justPressed(L_DOWN_BUTTON) && isDown[L_UP_BUTTON]);
143 
144 }