Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
962 | - | 1 | |
2 | /************************************************************************/ |
||
3 | /* */ |
||
4 | /* Debouncing 8 Keys */ |
||
5 | /* Sampling 4 Times */ |
||
6 | /* With Repeat Function */ |
||
7 | /* */ |
||
8 | /* Author: Peter Dannegger */ |
||
9 | /* danni@specs.de */ |
||
10 | /* */ |
||
11 | /* ergänzt: gebad, beschschleunigende Tastenwiederholung */ |
||
12 | /* */ |
||
13 | /************************************************************************/ |
||
14 | |||
15 | #include <stdint.h> |
||
16 | #include <avr/io.h> |
||
17 | #include <avr/interrupt.h> |
||
18 | |||
19 | #include "keys.h" |
||
20 | #include "config.h" |
||
21 | |||
22 | ISR( TIMER0_OVF_vect ) // every 10ms |
||
23 | { |
||
24 | static uint8_t ct0, ct1, rpt, timer0_1, timer0_2, timer0_3, timer0_4; |
||
25 | static uint16_t timer0_5 = TIMER0_5; |
||
26 | uint8_t i, key_pin; |
||
27 | |||
28 | TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms |
||
29 | |||
30 | key_pin = (KEY_MP_PIN & ( (1<<KEY_MINUS) | (1<<KEY_PLUS))); |
||
31 | key_pin |= (KEY_ENTER_PIN & (1<<KEY_ENTER)) >> 2;// move enter bit to sw_enter bit |
||
32 | i = key_state ^ ~key_pin; // key changed ? |
||
33 | ct0 = ~( ct0 & i ); // reset or count ct0 |
||
34 | ct1 = ct0 ^ (ct1 & i); // reset or count ct1 |
||
35 | i &= ct0 & ct1; // count until roll over ? |
||
36 | key_state ^= i; // then toggle debounced state |
||
37 | key_press |= key_state & i; // 0->1: key press detect |
||
38 | if (key_press & REPEAT_MASK) { // da bei mir alle Tasten in REPEAT_MASK |
||
39 | light_count = 0; // wenn keine Taste gedrückt begint Zähler für Hintergrundbeleuchtung aus |
||
40 | timer0_5 = TIMER0_5; |
||
41 | } |
||
42 | if( (key_state & REPEAT_MASK) == 0 ) { // check repeat function |
||
43 | rpt = REPEAT_START; // start delay |
||
44 | key_repeat_next = REPEAT_NEXT; // Wert bestimmt Tasten-Beschleunigung/-Wiederholrate beginnt, mit 200ms |
||
45 | key_counter = REPEAT_ACC_N; // wenn V_REPEAT, nach dem angegebenen Zeichen beschleunigen |
||
46 | } |
||
47 | if( --rpt == 0 ){ |
||
48 | rpt = key_repeat_next; // repeat delay |
||
49 | key_rpt |= key_state & REPEAT_MASK; |
||
50 | } |
||
51 | if ( --timer0_1 == 0) { |
||
52 | timer0_1 = TIMER0_1; |
||
53 | task_timer0_1 = 1; |
||
54 | } |
||
55 | if ( --timer0_2 == 0) { |
||
56 | timer0_2 = TIMER0_2; |
||
57 | task_timer0_2 = 1; |
||
58 | } |
||
59 | if ( --timer0_3 == 0) { |
||
60 | timer0_3 = TIMER0_3; |
||
61 | task_timer0_3 = 1; |
||
62 | } |
||
63 | if ( --timer0_4 == 0) { |
||
64 | timer0_4 = TIMER0_4; |
||
65 | task_timer0_4 = 1; |
||
66 | } |
||
67 | if ( --timer0_5 == 0) { |
||
68 | timer0_5 = TIMER0_5; |
||
69 | task_timer0_5 = 1; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /////////////////////////////////////////////////////////////////// |
||
74 | // |
||
75 | // check if a key has been pressed. Each pressed key is reported |
||
76 | // only once |
||
77 | // |
||
78 | uint8_t Get_Key_Press( uint8_t key_mask ) |
||
79 | { |
||
80 | cli(); // read and clear atomic ! |
||
81 | key_mask &= key_press; // read key(s) |
||
82 | key_press ^= key_mask; // clear key(s) |
||
83 | sei(); |
||
84 | return key_mask; |
||
85 | } |
||
86 | |||
87 | /////////////////////////////////////////////////////////////////// |
||
88 | // |
||
89 | // check if a key has been pressed long enough such that the |
||
90 | // key repeat functionality kicks in. After a small setup delay |
||
91 | // the key is reported beeing pressed in subsequent calls |
||
92 | // to this function. This simulates the user repeatedly |
||
93 | // pressing and releasing the key. |
||
94 | // |
||
95 | uint8_t Get_Key_Rpt( uint8_t key_mask ) |
||
96 | { |
||
97 | cli(); // read and clear atomic ! |
||
98 | key_mask &= key_rpt; // read key(s) |
||
99 | key_rpt ^= key_mask; // clear key(s) |
||
100 | sei(); |
||
101 | return key_mask; |
||
102 | } |
||
103 | |||
104 | /////////////////////////////////////////////////////////////////// |
||
105 | // |
||
106 | uint8_t Get_Key_Short( uint8_t key_mask ) |
||
107 | { |
||
108 | cli(); // read key state and key press atomic ! |
||
109 | return Get_Key_Press( ~key_state & key_mask ); |
||
110 | } |
||
111 | |||
112 | /////////////////////////////////////////////////////////////////// |
||
113 | // |
||
114 | uint8_t Get_Key_Long( uint8_t key_mask ) |
||
115 | { |
||
116 | return Get_Key_Press( Get_Key_Rpt( key_mask )); |
||
117 | } |
||
118 | |||
119 | void Key_Speedup_rpt( uint8_t speedup ) |
||
120 | { |
||
121 | if (speedup) { |
||
122 | if (key_counter > 0) { // nach x Zeichen wird Wiederholrate kontinuierlich erhöht |
||
123 | cli(); |
||
124 | key_counter--; |
||
125 | sei(); |
||
126 | } |
||
127 | else { |
||
128 | if (key_repeat_next > 3) { // Wert bestimmt die max. Beschleunigung/Wiederholrate |
||
129 | cli(); // kann jedoch nicht kleiner 10ms sein |
||
130 | key_repeat_next -= 2; |
||
131 | sei(); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | } |