Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
724 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
 
29
uint8_t key_state = 0;  // debounced and inverted key state:
30
                                                                                                // bit = 1: key pressed
31
uint8_t key_press = 0;  // key press detect
32
uint8_t key_rpt;                                // key long press and repeat
33
 
34
 
35
//*****************************************************************************
36
// 
37
#if defined (__AVR_ATmega32__)
38
ISR(TIMER0_COMP_vect)                                           // Timer-Interrupt (100 Hz)
39
#else
40
ISR(TIMER0_COMPA_vect)                                  // Timer-Interrupt (100 Hz)
41
#endif
42
{
43
  static uint8_t ct0 = 0;
44
        static uint8_t ct1 = 0;
45
        static uint8_t rpt = 0;
46
        uint8_t i;
47
 
48
        // Key handling by Peter Dannegger
49
        // see www.mikrocontroller.net
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
        {
59
                rpt = REPEAT_START;     // start delay
60
        }
61
        if (--rpt == 0)
62
        {
63
                rpt = REPEAT_NEXT;      // repeat delay
64
                key_rpt |= (key_state & REPEAT_MASK);
65
        }
66
 
67
 
68
        if (timer > 0)
69
                timer --;
70
}
71
 
72
 
73
//*****************************************************************************
74
// 
75
void TIMER0_Init (void)
76
{
77
        timer = 0;
78
 
79
#if defined (__AVR_ATmega32__)
80
        TCCR0 = (1 << CS02) | (1 << CS00) | (1 << WGM01);               // Prescaler 1024
81
        OCR0 = (F_CPU / (100L * 1024L)) ;
82
 
83
        TIMSK |= (1 << OCIE0);  // enable interrupt for OCR
84
#else
85
        TCCR0A = (1 << WGM01);
86
        TCCR0B = (1 << CS02) | (1 << CS00);
87
        OCR0A = (F_CPU / (100L * 1024L)) ;
88
 
89
        TIMSK0 |= (1 << OCIE0A);        // enable interrupt for OCR
90
#endif
91
}
92
 
93
 
94
//*****************************************************************************
95
// 
96
uint8_t get_key_press (uint8_t key_mask)
97
{
98
        uint8_t sreg = SREG;
99
 
100
        // disable all interrupts
101
        cli();
102
 
103
  key_mask &= key_press;        // read key(s)
104
  key_press ^= key_mask;        // clear key(s)
105
 
106
        SREG = sreg;    // restore status register
107
 
108
  return key_mask;
109
}
110
 
111
 
112
//*****************************************************************************
113
// 
114
uint8_t get_key_rpt (uint8_t key_mask)
115
{
116
        uint8_t sreg = SREG;
117
 
118
        // disable all interrupts
119
        cli();
120
 
121
  key_mask &= key_rpt;  // read key(s)
122
  key_rpt ^= key_mask;  // clear key(s)
123
 
124
        SREG = sreg;    // restore status register
125
 
126
  return key_mask;
127
}
128
 
129
 
130
//*****************************************************************************
131
// 
132
uint8_t get_key_short (uint8_t key_mask)
133
{
134
        uint8_t ret;
135
        uint8_t sreg = SREG;
136
 
137
        // disable all interrupts
138
        cli();
139
 
140
  ret = get_key_press (~key_state & key_mask);
141
 
142
        SREG = sreg;    // restore status register
143
 
144
  return ret;
145
}
146
 
147
 
148
//*****************************************************************************
149
// 
150
uint8_t get_key_long (uint8_t key_mask)
151
{
152
  return get_key_press (get_key_rpt (key_mask));
153
}
154
 
155
 
156
//*****************************************************************************
157
// 
158
uint8_t get_key_long2 (uint8_t key_mask)
159
{
160
  return get_key_press (get_key_rpt (key_press^key_mask));
161
}
162
 
163
 
164
//*****************************************************************************
165
// 
166
uint8_t get_key_long_rpt (uint8_t key_mask)
167
{
168
  return get_key_rpt (~key_press^key_mask);
169
}