-
Notifications
You must be signed in to change notification settings - Fork 50
/
comms.c
116 lines (99 loc) · 4.14 KB
/
comms.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
105
106
107
108
109
110
111
112
113
114
115
#include <avr/io.h>
#include <inttypes.h>
#include "tapuino.h"
#include "config.h"
#include "comms.h"
volatile uint8_t g_cur_command = COMMAND_IDLE;
// repeat needed on all keys for short/long on select/abort and repeat on prev/next
#define REPEAT_MASK (_BV(KEY_SELECT_PIN) | _BV(KEY_ABORT_PIN) | _BV(KEY_PREV_PIN) | _BV(KEY_NEXT_PIN))
volatile unsigned char key_press = 0;
volatile unsigned char key_state = 0;
volatile unsigned char key_rpt = 0;
/*--------------------------------------------------------------------------
FUNC: 8/1/11 - Used to read debounced button presses
PARAMS: A keymask corresponding to the pin for the button you with to poll
RETURNS: A keymask where any high bits represent a button press
--------------------------------------------------------------------------*/
unsigned char get_key_press( unsigned char key_mask ) {
// cli(); // read and clear atomic !
key_mask &= key_press; // read key(s)
key_press ^= key_mask; // clear key(s)
// sei();
return key_mask;
}
/*--------------------------------------------------------------------------
FUNC: 8/1/11 - Used to check for debounced buttons that are held down
PARAMS: A keymask corresponding to the pin for the button you with to poll
RETURNS: A keymask where any high bits is a button held long enough for
its input to be repeated
--------------------------------------------------------------------------*/
unsigned char get_key_rpt( unsigned char key_mask ) {
// cli(); // read and clear atomic !
key_mask &= key_rpt; // read key(s)
key_rpt ^= key_mask; // clear key(s)
// sei();
return key_mask;
}
/*--------------------------------------------------------------------------
FUNC: 8/1/11 - Used to read debounced button released after a short press
PARAMS: A keymask corresponding to the pin for the button you with to poll
RETURNS: A keymask where any high bits represent a quick press and release
--------------------------------------------------------------------------*/
unsigned char get_key_short( unsigned char key_mask )
{
//cli(); // read key state and key press atomic !
return get_key_press( ~key_state & key_mask );
}
/*--------------------------------------------------------------------------
FUNC: 8/1/11 - Used to read debounced button held for REPEAT_START amount
of time.
PARAMS: A keymask corresponding to the pin for the button you with to poll
RETURNS: A keymask where any high bits represent a long button press
--------------------------------------------------------------------------*/
unsigned char get_key_long( unsigned char key_mask )
{
return get_key_press( get_key_rpt( key_mask ));
}
void player_handleInputKeys() {
if (get_key_short(_BV(KEY_SELECT_PIN))) {
g_cur_command = COMMAND_SELECT;
}
if (get_key_short(_BV(KEY_ABORT_PIN))) {
g_cur_command = COMMAND_ABORT;
}
if (get_key_long(_BV(KEY_SELECT_PIN))) {
g_cur_command = COMMAND_SELECT_LONG;
}
if (get_key_long(_BV(KEY_ABORT_PIN))) {
g_cur_command = COMMAND_ABORT_LONG;
}
if (get_key_press(_BV(KEY_PREV_PIN)) || get_key_rpt(_BV(KEY_PREV_PIN))) {
g_cur_command = COMMAND_PREVIOUS;
}
if (get_key_press(_BV(KEY_NEXT_PIN)) || get_key_rpt(_BV(KEY_NEXT_PIN))) {
g_cur_command = COMMAND_NEXT;
}
}
void input_callback()
{
static unsigned char ct0, ct1, rpt;
unsigned char i;
// key changed ?
#ifdef KEYS_INPUT_PULLUP
i = key_state ^ ~KEYS_READ_PINS; // HW V2.0 for internal pullup the reading is inverted
#else
i = key_state ^ KEYS_READ_PINS; // HW V1.0 normal read
#endif
ct0 = ~( ct0 & i ); // reset or count ct0
ct1 = ct0 ^ (ct1 & i); // reset or count ct1
i &= ct0 & ct1; // count until roll over ?
key_state ^= i; // then toggle debounced state
key_press |= key_state & i; // 0->1: key press detect
if( (key_state & REPEAT_MASK) == 0 ) // check repeat function
rpt = (KEY_REPEAT_START / 10); // start delay
if( --rpt == 0 ){
rpt = g_key_repeat_next; // repeat delay
key_rpt |= key_state & REPEAT_MASK;
}
player_handleInputKeys();
}