1 | /************************************************************************/
|
2 | /* */
|
3 | /* Debouncing 8 Keys */
|
4 | /* Sampling 4 Times */
|
5 | /* With Repeat Function */
|
6 | /* */
|
7 | /* Author: Peter Dannegger */
|
8 | /* danni@specs.de */
|
9 | /* */
|
10 | /************************************************************************/
|
11 |
|
12 | #ifndef KEY_ROUTINES_H_
|
13 | #define KEY_ROUTINES_H_
|
14 |
|
15 | #include <stdint.h>
|
16 | #include <avr/io.h>
|
17 | #include <avr/interrupt.h>
|
18 |
|
19 | #ifndef F_CPU
|
20 | #define F_CPU 1000000 // processor clock frequency
|
21 | #warning no F_CPU defined
|
22 | #endif
|
23 |
|
24 | #define KEY_DDR DDRB
|
25 | #define KEY_PORT PORTB
|
26 | #define KEY_PIN PINB
|
27 | #define KEY0 0
|
28 | #define KEY1 1
|
29 | #define KEY2 2
|
30 | #define ALL_KEYS (1<<KEY0 | 1<<KEY1 | 1<<KEY2)
|
31 |
|
32 | #define REPEAT_MASK (1<<KEY1 | 1<<KEY2) // repeat: key1, key2
|
33 | #define REPEAT_START 50 // after 500ms
|
34 | #define REPEAT_NEXT 20 // every 200ms
|
35 |
|
36 | volatile uint8_t key_state; // debounced and inverted key state:
|
37 | // bit = 1: key pressed
|
38 | volatile uint8_t key_press; // key press detect
|
39 |
|
40 | volatile uint8_t key_rpt; // key long press and repeat
|
41 |
|
42 |
|
43 | ISR( TIMER0_OVF_vect ) // every 10ms
|
44 | {
|
45 | static uint8_t ct0 = 0xFF, ct1 = 0xFF, rpt;
|
46 | uint8_t i;
|
47 |
|
48 | TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms
|
49 |
|
50 | i = key_state ^ ~KEY_PIN; // key changed ?
|
51 | ct0 = ~( ct0 & i ); // reset or count ct0
|
52 | ct1 = ct0 ^ (ct1 & i); // reset or count ct1
|
53 | i &= ct0 & ct1; // count until roll over ?
|
54 | key_state ^= i; // then toggle debounced state
|
55 | key_press |= key_state & i; // 0->1: key press detect
|
56 |
|
57 | if( (key_state & REPEAT_MASK) == 0 ) // check repeat function
|
58 | rpt = REPEAT_START; // start delay
|
59 | if( --rpt == 0 ){
|
60 | rpt = REPEAT_NEXT; // repeat delay
|
61 | key_rpt |= key_state & REPEAT_MASK;
|
62 | }
|
63 | }
|
64 |
|
65 | ///////////////////////////////////////////////////////////////////
|
66 | //
|
67 | // check if a key has been pressed. Each pressed key is reported
|
68 | // only once
|
69 | //
|
70 | uint8_t get_key_press( uint8_t key_mask )
|
71 | {
|
72 | cli(); // read and clear atomic !
|
73 | key_mask &= key_press; // read key(s)
|
74 | key_press ^= key_mask; // clear key(s)
|
75 | sei();
|
76 | return key_mask;
|
77 | }
|
78 |
|
79 | ///////////////////////////////////////////////////////////////////
|
80 | //
|
81 | // check if a key has been pressed long enough such that the
|
82 | // key repeat functionality kicks in. After a small setup delay
|
83 | // the key is reported being pressed in subsequent calls
|
84 | // to this function. This simulates the user repeatedly
|
85 | // pressing and releasing the key.
|
86 | //
|
87 | uint8_t get_key_rpt( uint8_t key_mask )
|
88 | {
|
89 | cli(); // read and clear atomic !
|
90 | key_mask &= key_rpt; // read key(s)
|
91 | key_rpt ^= key_mask; // clear key(s)
|
92 | sei();
|
93 | return key_mask;
|
94 | }
|
95 |
|
96 | ///////////////////////////////////////////////////////////////////
|
97 | //
|
98 | // check if a key is pressed right now
|
99 | //
|
100 | uint8_t get_key_state( uint8_t key_mask )
|
101 |
|
102 | {
|
103 | key_mask &= key_state;
|
104 | return key_mask;
|
105 | }
|
106 |
|
107 | ///////////////////////////////////////////////////////////////////
|
108 | //
|
109 | uint8_t get_key_short( uint8_t key_mask )
|
110 | {
|
111 | cli(); // read key state and key press atomic !
|
112 | return get_key_press( ~key_state & key_mask );
|
113 | }
|
114 |
|
115 | ///////////////////////////////////////////////////////////////////
|
116 | //
|
117 | uint8_t get_key_long( uint8_t key_mask )
|
118 | {
|
119 | return get_key_press( get_key_rpt( key_mask ));
|
120 | }
|
121 |
|
122 | #endif
|