Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
730 | woggle | 1 | /***************************************************************************** |
2 | * Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net * |
||
3 | * based on the key handling by Peter Dannegger * |
||
4 | * see www.mikrocontroller.net * |
||
5 | * * |
||
6 | * This program is free software; you can redistribute it and/or modify * |
||
7 | * it under the terms of the GNU General Public License as published by * |
||
8 | * the Free Software Foundation; either version 2 of the License. * |
||
9 | * * |
||
10 | * This program is distributed in the hope that it will be useful, * |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
13 | * GNU General Public License for more details. * |
||
14 | * * |
||
15 | * You should have received a copy of the GNU General Public License * |
||
16 | * along with this program; if not, write to the * |
||
17 | * Free Software Foundation, Inc., * |
||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
19 | * * |
||
20 | *****************************************************************************/ |
||
21 | |||
22 | #include <avr/io.h> |
||
23 | #include <avr/interrupt.h> |
||
24 | #include "main.h" |
||
25 | #include "timer.h" |
||
26 | |||
27 | volatile uint16_t timer; |
||
28 | volatile uint16_t abo_timer; |
||
29 | |||
30 | uint8_t key_state = 0; // debounced and inverted key state: |
||
31 | // bit = 1: key pressed |
||
32 | uint8_t key_press = 0; // key press detect |
||
33 | uint8_t key_rpt; // key long press and repeat |
||
34 | |||
35 | |||
36 | //***************************************************************************** |
||
37 | // |
||
38 | #if defined (__AVR_ATmega32__) |
||
39 | ISR(TIMER0_COMP_vect) // Timer-Interrupt (100 Hz) |
||
40 | #else |
||
41 | ISR(TIMER0_COMPA_vect) // Timer-Interrupt (100 Hz) |
||
42 | #endif |
||
43 | { |
||
44 | static uint8_t ct0 = 0; |
||
45 | static uint8_t ct1 = 0; |
||
46 | static uint8_t rpt = 0; |
||
47 | uint8_t i; |
||
48 | |||
49 | // Key handling by Peter Dannegger |
||
50 | // see www.mikrocontroller.net |
||
51 | i = key_state ^ ~KEY_PIN; // key changed ? |
||
52 | ct0 = ~(ct0 & i); // reset or count ct0 |
||
53 | ct1 = ct0 ^ (ct1 & i); // reset or count ct1 |
||
54 | i &= (ct0 & ct1); // count until roll over ? |
||
55 | key_state ^= i; // then toggle debounced state |
||
56 | key_press |= (key_state & i); // 0->1: key press detect |
||
57 | |||
58 | if ((key_state & REPEAT_MASK) == 0) // check repeat function |
||
59 | { |
||
60 | rpt = REPEAT_START; // start delay |
||
61 | } |
||
62 | if (--rpt == 0) |
||
63 | { |
||
64 | rpt = REPEAT_NEXT; // repeat delay |
||
65 | key_rpt |= (key_state & REPEAT_MASK); |
||
66 | } |
||
67 | |||
68 | |||
69 | if (timer > 0) |
||
70 | { |
||
71 | timer --; |
||
72 | } |
||
73 | |||
74 | if (abo_timer > 0) |
||
75 | { |
||
76 | abo_timer --; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | |||
81 | //***************************************************************************** |
||
82 | // |
||
83 | void TIMER0_Init (void) |
||
84 | { |
||
85 | timer = 0; |
||
86 | |||
87 | #if defined (__AVR_ATmega32__) |
||
88 | TCCR0 = (1 << CS02) | (1 << CS00) | (1 << WGM01); // Prescaler 1024 |
||
89 | OCR0 = (F_CPU / (100L * 1024L)) ; |
||
90 | |||
91 | TIMSK |= (1 << OCIE0); // enable interrupt for OCR |
||
92 | #else |
||
93 | TCCR0A = (1 << WGM01); |
||
94 | TCCR0B = (1 << CS02) | (1 << CS00); |
||
95 | OCR0A = (F_CPU / (100L * 1024L)) ; |
||
96 | |||
97 | TIMSK0 |= (1 << OCIE0A); // enable interrupt for OCR |
||
98 | #endif |
||
99 | } |
||
100 | |||
101 | |||
102 | //***************************************************************************** |
||
103 | // |
||
104 | uint8_t get_key_press (uint8_t key_mask) |
||
105 | { |
||
106 | uint8_t sreg = SREG; |
||
107 | |||
108 | // disable all interrupts |
||
109 | cli(); |
||
110 | |||
111 | key_mask &= key_press; // read key(s) |
||
112 | key_press ^= key_mask; // clear key(s) |
||
113 | |||
114 | SREG = sreg; // restore status register |
||
115 | |||
116 | return key_mask; |
||
117 | } |
||
118 | |||
119 | |||
120 | //***************************************************************************** |
||
121 | // |
||
122 | uint8_t get_key_rpt (uint8_t key_mask) |
||
123 | { |
||
124 | uint8_t sreg = SREG; |
||
125 | |||
126 | // disable all interrupts |
||
127 | cli(); |
||
128 | |||
129 | key_mask &= key_rpt; // read key(s) |
||
130 | key_rpt ^= key_mask; // clear key(s) |
||
131 | |||
132 | SREG = sreg; // restore status register |
||
133 | |||
134 | return key_mask; |
||
135 | } |
||
136 | |||
137 | |||
138 | //***************************************************************************** |
||
139 | // |
||
140 | uint8_t get_key_short (uint8_t key_mask) |
||
141 | { |
||
142 | uint8_t ret; |
||
143 | uint8_t sreg = SREG; |
||
144 | |||
145 | // disable all interrupts |
||
146 | cli(); |
||
147 | |||
148 | ret = get_key_press (~key_state & key_mask); |
||
149 | |||
150 | SREG = sreg; // restore status register |
||
151 | |||
152 | return ret; |
||
153 | } |
||
154 | |||
155 | |||
156 | //***************************************************************************** |
||
157 | // |
||
158 | uint8_t get_key_long (uint8_t key_mask) |
||
159 | { |
||
160 | return get_key_press (get_key_rpt (key_mask)); |
||
161 | } |
||
162 | |||
163 | |||
164 | //***************************************************************************** |
||
165 | // |
||
166 | uint8_t get_key_long2 (uint8_t key_mask) |
||
167 | { |
||
168 | return get_key_press (get_key_rpt (key_press^key_mask)); |
||
169 | } |
||
170 | |||
171 | |||
172 | //***************************************************************************** |
||
173 | // |
||
174 | uint8_t get_key_long_rpt (uint8_t key_mask) |
||
175 | { |
||
176 | return get_key_rpt (~key_press^key_mask); |
||
177 | } |