/
keyboard.c
311 lines (298 loc) · 8.1 KB
/
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* Keyboard.c
* Detects changes in matrix, translates those changes
* into keycodes, and generates keyboard reports.
*
*/
#include "keyboard.h"
#include "matrix.h"
#include "layout.h"
#include "usb_keyboard_debug.h"
#include "print.h"
#include "bluefruit.h"
#include "led.h"
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
extern uint16_t matrix_rows[NUMROWS]; //defined in matrix.c
extern uint16_t matrix_last[NUMROWS]; //defined in matrix.c
extern volatile uint8_t keyboard_leds; //defined in usb_keyboard_debug.c
static keyboard_report key_report={0,{0,0,0,0,0,0}};
//Keyboard initialization function
void keyboard_init( )
{
//Initialize Matrix,USB,Bluefruit,LEDs
matrix_init();
usb_init();
bluefruit_init();
led_init();
//Set keyboard report for both protocols
usb_keyboard_set_report(&key_report); //For usb
bluefruit_keyboard_set_report(&key_report);//For Bluetooth
//Decide which Protocol to use based on current USB state
_delay_ms(50); // a 50ms wait to ensure systems are ready
// Initial check to determine USB connection state
// Also determine which interface to use
if(USBSTA & (1<<VBUS))
{
//Disable bluetooth enable USB
usb_init(); // Enable USB
bluefruit_disable( ); //Disable bluefruit
}
else
{
//Disable USB enable bluetooth
usb_disable();
bluefruit_init(); //enable bluefruit
}
}
//Keyboard main function
//Handles all keyboard related tasks
void keyboard_main( )
{
//Update LEDs if USB in use (bluetooth uses different method)
if(!bluefruit_configured())
update_leds(keyboard_leds);
uint8_t needs_debounce = 0; // For determining if matrix needs debouncing
uint16_t matrix_changed = 0;
matrix_scan();
// Now that we have scanned the matrix, determine if any actions need to be done
for (uint8_t row = 0; row < NUMROWS; row++)
{
//Check to see if switches in this row has changed
if ((matrix_changed = matrix_rows[row] ^ matrix_last[row]))
{
//At least one switch has changed in this row
needs_debounce = 1; //We need to debounce
for (int col = 0; col < NUMCOLS; col++)
{
//Find out which columns have changed and act
if(matrix_changed & ((uint16_t)1<< col))
{
//This switch has changed, do whatever the switch was programmed to do
if(matrix_rows[row] & ((uint16_t)1<< col))
{
//Keydown event
key_execute(get_keycode(row,col),KEY_IS_DOWN);
//phex(get_keycode(row,col).type);
//print(":");
//phex(get_keycode(row,col).code);
//print(" key is down\n");
}
else
{
//Keyup event
key_execute(get_keycode(row,col),KEY_IS_UP);
//phex(get_keycode(row,col).type);
//print(":");
//phex(get_keycode(row,col).code);
//print(" key is up\n");
}
}
}
matrix_last[row] = matrix_rows[row]; //Row has been dealt with
}
else
{
//Nothing has changed, do nothing?
}
}
//Allow switch state to stabilize if an edge is detected.
if (needs_debounce)
_delay_us(DEBOUNCE_TIME);
}
//Key Execute function
//Decodes a keycode, and executes depending on codetype
void key_execute(keycode aKey, uint8_t down)
{
print("inside key_execute\n");
phex(aKey.type);
print("<-Type:Code->");
phex(aKey.code);
print("\n");
switch(aKey.type)
{
case TYPE_KEY:
//Check for type, and call functions
if(down)
add_key(aKey.code);
else
del_key(aKey.code);
break;
case TYPE_MOD:
//Check for type, and call functions
if(down)
add_mod(aKey.code);
else
del_mod(aKey.code);
break;
case TYPE_MEDIA:
case TYPE_MOUSE:
//Placeholder if mouse and media keys implemented
break;
case TYPE_MOMENTARY:
//For mementary layer changes
clear_keyboard(); //Clear all current plessed keys, because they may not exist in new layer
if(down)
set_current_layer(aKey.code);
else
set_current_layer(0); //Return to default layer
break;
case TYPE_TOGGLE:
//For toggleing layer changes
if(down && (aKey.code ^ get_current_layer()) )
{
clear_keyboard();
set_current_layer(aKey.code);
}
break;
case TYPE_MISC:
//Miscelaneous functions need special treatment
switch(aKey.code)
{
case CODE_JMP_BOOT:
//Jump to boot loader
if(down) //Only want to activate on key-down
{
jump_bootloader();
}
break;
case CODE_BR_UP:
//Raise brightness of LEDS
//In steps of 24
if(down) //Only want to activate on key-down
{
if(OCR4D < 0xF0) OCR4D += 0x20;
}
break;
case CODE_BR_DOWN:
//Lower brightness of LEDS
//In steps of 24
if(down) //Only want to activate on key-down
{
if(OCR4D > 0x10) OCR4D -= 0x20;
}
break;
}
break;
default:
//Default case: Unknown Keytype
//Print error and do nothing
print("Unknown Key Type: ");
phex(aKey.type);
pchar('\n');
}
}
//Adds a key to keyboard's report and then sends
void add_key(uint8_t aKey)
{
//Add a key to report and send
for(uint8_t i = 0; i < KEYS_PER_REPORT; i++)
{
//Check for empty spot in report if available, otherwise do nothing
if(key_report.keys[i] == 0)
{
key_report.keys[i] = aKey;
//send report
keyboard_send_report();
break;
}
}
}
//Deletes a key from keyboard's report and then sends
void del_key(uint8_t aKey)
{
//delete a key from report and send
for(uint8_t i = 0; i < KEYS_PER_REPORT; i++)
{
//Check for matching key in report, and delete if present
if(key_report.keys[i] == aKey)
{
key_report.keys[i] = 0;
//send report
keyboard_send_report();
break;
}
}
}
//Adds a mod to keyboard report and then sends
void add_mod(uint8_t aMod)
{
//Or the mod into mods
key_report.mods |= aMod;
//Send report
keyboard_send_report();
}
//Deletes a mod from keyboard report and then sends
void del_mod(uint8_t aMod)
{
//And the mod out of mods
key_report.mods &= ~aMod;
//Send Report
keyboard_send_report();
}
//Clears and then sends the current keyboard report
void clear_keyboard( )
{
//Clear out keyboard report
key_report.mods = 0;
for (uint8_t i = 0; i < KEYS_PER_REPORT; i++)
key_report.keys[i] = 0;
//Send report
keyboard_send_report();
}
//Sends a keyboard report depending on active protocol
void keyboard_send_report( )
{
//Debug toggle bluetooth status
//Check to see if USB active
if(bluefruit_configured())
{
//Send keyboard report using Bluetooth
bluefruit_keyboard_send();
print("Sent Bluetooth Report!\n");
}
else
{
//Send keyboard report using USB
usb_keyboard_send();
//bluefruit_keyboard_send(); //debug also send bluetooth report
print("Sent USB Report!\n");
}
}
//Jump to bootloader
//courtest of PJRC <http://www.pjrc.com/teensy/jump_to_bootloader.html>
void jump_bootloader( )
{
cli();
// disable watchdog, if enabled
// disable all peripherals
UDCON = 1;
USBCON = (1<<FRZCLK); // disable USB
UCSR1B = 0;
_delay_ms(5);
#if defined(__AVR_AT90USB162__) // Teensy 1.0
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
DDRB = 0; DDRC = 0; DDRD = 0;
PORTB = 0; PORTC = 0; PORTD = 0;
asm volatile("jmp 0x3E00");
#elif defined(__AVR_ATmega32U4__) // Teensy 2.0
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
asm volatile("jmp 0x7E00");
#elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
asm volatile("jmp 0xFC00");
#elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
asm volatile("jmp 0x1FC00");
#endif
}