-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathavr_keyboard.c
188 lines (165 loc) · 4.85 KB
/
avr_keyboard.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* USB Keyboard Firmware code for generic Teensy Keyboards
* Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
* http://bathroomepiphanies.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "usb_keyboard_debug.h"
#include "print.h"
#include "avr.h"
#include __INCLUDE_KEYBOARD
#include __INCLUDE_LAYOUT
typedef struct key {
uint8_t value;
uint8_t type;
} KEY;
typedef struct status {
bool pressed;
uint8_t release;
} STATUS;
/* pressed keeps track of which keys that are pressed
bouncer keeps track of which keys that may be released
queue contains the keys that are sent in the HID packet
mod_keys is the bit pattern corresponding to pressed modifier keys */
uint8_t queue[7] = {255,255,255,255,255,255,255};
uint8_t mod_keys = 0;
extern const uint16_t layer0[];
KEY * layout = (KEY *)(layer0);
STATUS key_status[NKEY];
extern uint8_t *const row_ddr[];
extern uint8_t *const row_port[];
extern uint8_t *const row_pin[];
extern const uint8_t row_bit[];
void init(void);
void send(void);
void poll(void);
void key_press(uint8_t key);
void key_release(uint8_t key);
void setup_io_pins(void);
/* Check for keys ready to be released, and
advance the bouncer counter on all keys. */
ISR(INTERRUPT_FUNCTION) {
uint8_t key;
for(key = 0; key < NKEY; key++) {
if(key_status[key].release == 0x01)
key_release(key);
key_status[key].release >>= 1;
}
update_leds();
if(mod_keys == (uint8_t)(KEY_LEFT_SHIFT | KEY_RIGHT_SHIFT))
jump_bootloader();
}
int main(void) {
uint8_t key, row, col;
uint8_t this, previous[NKEY];
init();
for(key = 0; key < NKEY; key++)
previous[key] = 0;
for(;;) {
// Pull one column at a time high/low,
// register which rows are affected.
for(col = 0, key = 0; col < NCOL; col++) {
pull_column(col);
_delay_us(SETTLE_TIME_US);
for(row = 0; row < NROW; row++, key++) {
#if defined ACTIVE_LOW
this = !(*row_pin[row] & row_bit[row]);
#elif defined ACTIVE_HIGH
this = (*row_pin[row] & row_bit[row]);
#endif
// Detect rising and falling edges on key status
if(this && !previous[key]){
// print("Press: "); phex(row); print(" "); phex(col); print("\n");
if(!key_status[key].pressed)
key_press(key);
else
key_status[key].release = 0x00; // Abort release
}
else if(previous[key] && !this) {
// print("Relea: "); phex(row); print(" "); phex(col); print("\n");
key_status[key].release = 0x80; // Initiate release
}
previous[key] = this;
}
release_column(col);
}
}
}
void send(void) {
//return;
uint8_t i;
for(i=0; i<6; i++)
keyboard_keys[i] = queue[i]<255? layout[queue[i]].value: 0;
keyboard_modifier_keys = mod_keys;
usb_keyboard_send();
}
/* */
void key_press(uint8_t key) {
uint8_t i;
key_status[key].pressed = true;
key_status[key].release = 0x00;
if(layout[key].type)
mod_keys |= layout[key].value;
else {
for(i = 5; i > 0; i--) queue[i] = queue[i-1];
queue[0] = key;
}
send();
}
/* */
void key_release(uint8_t key) {
uint8_t i;
key_status[key].pressed = false;
key_status[key].release = 0x00;
if(layout[key].type)
mod_keys &= ~layout[key].value;
else {
for(i = 0; i < 6; i++)
if(queue[i]==key)
break;
for(; i < 6; i++)
queue[i] = queue[i+1];
}
send();
}
/* Call initialization functions */
void init(void) {
uint8_t key;
// 16 MHz clock
CLKPR = 0x80; CLKPR = 0;
// Disable JTAG
MCUCR |= 0x80; MCUCR |= 0x80;
usb_init();
while(!usb_configured())
_delay_ms(100);
setup_io_pins();
setup_leds();
setup_bounce_timer();
mod_keys = 0;
for(key = 0; key < NKEY; key++) {
key_status[key].pressed = false;
key_status[key].release = 0x00;
}
sei(); // Enable interrupts
}