-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.c
104 lines (91 loc) · 4.58 KB
/
button.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (C) 2006, Joe Lake, monome.org
*
* This file is part of 40h.
*
* 40h is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 40h is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with 40h; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* $Id: button.c,v. 1.1.1.1 2006/05/02 1:01:22
*/
#include "types.h"
#include "button.h"
uint8 button_current[8],
button_last[8],
button_state[8],
button_debounce_count[8][8],
button_event[8];
/***************************************************************************************************
*
* DESCRIPTION: initializes all button debouncing variables.
*
* ARGUMENTS:
*
* RETURNS:
*
* NOTES:
*
****************************************************************************************************/
void buttonInit(void)
{
uint8 i;
for (i = 0; i < 8; i++) {
button_current[i] = 0x00;
button_last[i] = 0x00;
button_state[i] = 0x00;
button_event[i] = 0x00;
}
}
/***************************************************************************************************
*
* DESCRIPTION: debounces physical button states and queues up debounced button state change events
* in button_event array for outputting.
*
* ARGUMENTS: row - the row of buttons to be debounced.
* index - the index (column) of the button in the specified row to be debounced.
*
* RETURNS:
*
* NOTES: we debounce buttons so that momentary, accidental changes in button state do not
* cause button press events to be reported.
*
****************************************************************************************************/
void buttonCheck(uint8 row, uint8 index)
{
if (((button_current[row] ^ button_last[row]) & (1 << index)) && // if the current physical button state is different from the
((button_current[row] ^ button_state[row]) & (1 << index))) { // last physical button state AND the current debounced state
if (button_current[row] & (1 << index)) { // if the current physical button state is depressed
button_event[row] = kButtonNewEvent << index; // queue up a new button event immediately
button_state[row] |= (1 << index); // and set the debounced state to down.
}
else
button_debounce_count[row][index] = kButtonUpDefaultDebounceCount; // otherwise the button was previously depressed and now
// has been released so we set our debounce counter.
}
else if (((button_current[row] ^ button_last[row]) & (1 << index)) == 0 && // if the current physical button state is the same as
(button_current[row] ^ button_state[row]) & (1 << index)) { // the last physical button state but the current physical
// button state is different from the current debounce
// state...
if (button_debounce_count[row][index] > 0 && --button_debounce_count[row][index] == 0) { // if the the debounce counter has
// been decremented to 0 (meaning the
// the button has been up for
// kButtonUpDefaultDebounceCount
// iterations///
button_event[row] = kButtonNewEvent << index; // queue up a button state change event
if (button_current[row] & (1 << index)) // and toggle the buttons debounce state.
button_state[row] |= (1 << index);
else
button_state[row] &= ~(1 << index);
}
}
}