-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard.cpp
More file actions
154 lines (140 loc) · 3.66 KB
/
keyboard.cpp
File metadata and controls
154 lines (140 loc) · 3.66 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "keyboard.h"
#include "config.h"
#include <arduino.h>
#include "main.h"
#include <MsTimer2.h>
const int BUTTON_HOLD_FRAMES = ((HOLD_TIME*1000)/KEYBOARD_READ_PERIOD);
const int KEYBOARD_REPEAT_PERIOD = (1000/KEYBOARD_REPEAT_RATE);
const char BUTTON_PINS[] = {BUTTON_UP_PIN, BUTTON_DOWN_PIN, BUTTON_OK_PIN, BUTTON_CANCEL_PIN};
static int button_press_state[NUM_BUTTONS];
static char button_release_state[NUM_BUTTONS];
/************************************
* Name : init_keyboard
* Purpuse : Initialize keyboard pins and variables
* Inputs : None
* Outputs : None
* Returns : None
************************************/
void init_keyboard()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
button_press_state[i] = 0;
button_release_state[i] = 0;
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
}
MsTimer2::set (KEYBOARD_READ_PERIOD, read_keyboard);
MsTimer2::start ();
}
/************************************
* Name : clear_keyboard
* Purpuse : Clear keyboard states
* Inputs : None
* Outputs : None
* Returns : None
************************************/
void clear_keyboard()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
button_press_state[i] = 0;
button_release_state[i] = 0;
}
}
/************************************
* Name : read_keyboard
* Purpuse : This funnction is called on timer2 interrupt
* Inputs : None
* Outputs : None
* Returns : None
************************************/
void read_keyboard()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
/* low value == button pressed */
if (!(digitalRead(BUTTON_PINS[i])))
{
button_press_state[i]++;
button_release_state[i] = 1;
}
else
{
button_press_state[i] = 0;
}
}
}
/************************************
* Name : button_pressed
* Purpuse : Returns the button press status
* Inputs : id - button ID
* Outputs : None
* Returns : 1 if button is pressed now (according to repeat rate), 0 otherwise
************************************/
int button_pressed(char id)
{
/*
Serial.print("button_pressed(");
Serial.print(id, DEC);
Serial.print(") - ");
Serial.println(button_press_state[id], DEC);
*/
/* TBD * Add repeat rate * TBD */
/* do not clear button_press_state[i] - only return state */
return (button_press_state[id] > 0);
}
/************************************
* Name : button_released
* Purpuse : Returns the button release status
* Inputs : id - button ID
* Outputs : None
* Returns : 1 if button has been released, 0 otherwise
************************************/
int button_released(char id)
{
/* When button is pressed, set flag...
* and then wait for button release.
* If button_hold event got, flag is also cleared */
if (button_press_state[id] == 0)
{
if ( button_release_state[id] == 1)
{
Serial.print("button_released(");
Serial.print(id, DEC);
Serial.print(") - ");
Serial.print(button_press_state[id], DEC);
Serial.print(" - ");
Serial.println(button_release_state[id], DEC);
button_release_state[id] = 0;
return 1;
}
else
{
return 0;
}
}
return 0;
}
/************************************
* Name : button_hold
* Purpuse : Returns the button hold status
* Inputs : id - button ID
* Outputs : None
* Returns : 1 if button has been hold for more than 2 seconds, 0 otherwise
************************************/
int button_hold(char id)
{
if (button_press_state[id] > BUTTON_HOLD_FRAMES)
{
Serial.print("button_hold(");
Serial.print(id, DEC);
Serial.print(") - ");
Serial.println(button_press_state[id], DEC);
button_release_state[id] = 0;
return 1;
}
else
{
return 0;
}
}