]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Minor modifications.
[kiibohd-controller.git] / main.c
1 /* Copyright (C) 2011 by Jacob Alexander
2  * 
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 #include <avr/io.h>
23 #include <avr/pgmspace.h>
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 #include "usb_keys.h"
27 //#include "usb_keyboard.h"
28
29 // TEMP INCLUDES
30 #include "usb_keyboard_debug.h"
31 #include <print.h>
32
33 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
34
35
36 // Number of keys
37 #define KEYBOARD_SIZE 63
38 #define KEYPAD_SIZE 16
39
40
41 // Debouncing Defines
42 #define SAMPLE_THRESHOLD 100
43 #define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
44
45
46 // Verified Keypress Defines
47 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
48
49
50 // Drive Pin Defines
51 #define DRIVE_reg_1 PORTD
52 #define DRIVE_reg_2 PORTD
53 #define DRIVE_reg_3 PORTD
54 #define DRIVE_reg_4 PORTD
55 #define DRIVE_reg_5 PORTD
56 #define DRIVE_reg_6 PORTD
57 #define DRIVE_reg_7 PORTE
58 #define DRIVE_reg_8 PORTE
59 #define DRIVE_reg_9 PORTE
60 #define DRIVE_reg_10 <blank>
61 #define DRIVE_reg_11 <blank>
62 #define DRIVE_reg_12 <blank>
63
64 #define DRIVE_pin_1 2
65 #define DRIVE_pin_2 3
66 #define DRIVE_pin_3 4
67 #define DRIVE_pin_4 5
68 #define DRIVE_pin_5 6
69 #define DRIVE_pin_6 7
70 #define DRIVE_pin_7 0
71 #define DRIVE_pin_8 1
72 #define DRIVE_pin_9 6
73 #define DRIVE_pin_10 <blank>
74 #define DRIVE_pin_11 <blank>
75 #define DRIVE_pin_12 <blank>
76
77 // Detect Pin/Group Defines
78 #define DETECT_group_1 1
79 #define DETECT_group_2 2
80 #define DETECT_group_3 3
81 #define DETECT_group_4 4
82 #define DETECT_group_5 5
83 #define DETECT_group_6 6
84 #define DETECT_group_7 7
85 #define DETECT_group_8 8
86 #define DETECT_group_9 9
87 #define DETECT_group_10 <blank>
88 #define DETECT_group_11 <blank>
89 #define DETECT_group_12 <blank>
90
91 #define DETECT_group_size_1 7
92 #define DETECT_group_size_2 7
93 #define DETECT_group_size_3 6
94 #define DETECT_group_size_4 8
95 #define DETECT_group_size_5 7
96 #define DETECT_group_size_6 7
97 #define DETECT_group_size_7 8
98 #define DETECT_group_size_8 8
99 #define DETECT_group_size_9 4
100 #define DETECT_group_size_10 <blank>
101 #define DETECT_group_size_11 <blank>
102 #define DETECT_group_size_12 <blank>
103
104 // Switch Codes
105 #define DETECT_group_array_1 {55,22,6 ,40,43,27,11}
106 #define DETECT_group_array_2 {56,23,7 ,41,58,26,10}
107 #define DETECT_group_array_3 {57,24,8 ,42,25,9}
108 #define DETECT_group_array_4 {54,21,5 ,39,44,28,12,59}
109 #define DETECT_group_array_5 {53,20,4 ,38,45,29,13}
110 #define DETECT_group_array_6 {52,19,3 ,37,46,30,14}
111 #define DETECT_group_array_7 {51,18,2 ,36,61,31,15,63}
112 #define DETECT_group_array_8 {50,17,1 ,35,47,32,16,62}
113 #define DETECT_group_array_9 {48,49,33,34} // 49/60 are the same line
114 #define DETECT_group_array_10 <blank>
115 #define DETECT_group_array_11 <blank>
116 #define DETECT_group_array_12 <blank>
117
118
119
120 // Drive Macros (Generally don't need to be changed), except for maybe DRIVE_DETECT
121 #define DRIVE_DETECT(reg,pin,group) \
122                         reg &= ~(1 << pin); \
123                         detection(group); \
124                         reg |= (1 << pin);
125
126 #define DD_CASE(number) \
127                         case number:\
128                                 DRIVE_DETECT(DRIVE_reg_##number, DRIVE_pin_##number, DETECT_group_##number)
129
130 #define DD_CASE_ORD(number) \
131                         DD_CASE(number) \
132                         break;
133
134 #define DD_CASE_END(number,var) \
135                         DD_CASE(number) \
136                         var = -1; \
137                         break;
138
139
140 // Updates the current detection sample and last sample bit
141 // Detection Macros (Probably don't need to be changed, but depending the matrix, may have to be)
142 // Determine if key is either normal or a modifier
143 #define DET_GROUP_CHECK(index,test) \
144                         if ( test ) { \
145                                 keyDetectArray[groupArray[index]]++; \
146                         }
147
148
149 // XXX - Detection Groups
150 // Checks each of the specified pins, and then if press detected, determine if the key is normal or a modifier
151 // Inverse logic applies for the PINs
152
153 // Used for 1 detection group (Special group)
154 #define DET_GROUP_1 \
155                         DET_GROUP_CHECK(0,!( PINB & (1 << 7) )) \
156                         DET_GROUP_CHECK(1,!( PINC & (1 << 0) )) \
157                         DET_GROUP_CHECK(2,!( PIND & (1 << 0) )) \
158                         DET_GROUP_CHECK(3,!( PIND & (1 << 1) )) \
159
160 // Used for 4 detection groups (Skips J1 P9)
161 #define DET_GROUP_2 \
162                         DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
163                         DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
164                         DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
165                         DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
166                         DET_GROUP_CHECK(4,!( PINB & (1 << 3) )) \
167                         DET_GROUP_CHECK(5,!( PINB & (1 << 4) )) \
168                         DET_GROUP_CHECK(6,!( PINB & (1 << 5) )) \
169
170 // Used for 1 detection group (Skips J1 P6 and J1 P9)
171 #define DET_GROUP_3 \
172                         DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
173                         DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
174                         DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
175                         DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
176                         DET_GROUP_CHECK(4,!( PINB & (1 << 4) )) \
177                         DET_GROUP_CHECK(5,!( PINB & (1 << 5) )) \
178
179 // Used for 3 detection groups (No skips, except special group 1)
180 #define DET_GROUP_4 \
181                         DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
182                         DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
183                         DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
184                         DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
185                         DET_GROUP_CHECK(4,!( PINB & (1 << 3) )) \
186                         DET_GROUP_CHECK(5,!( PINB & (1 << 4) )) \
187                         DET_GROUP_CHECK(6,!( PINB & (1 << 5) )) \
188                         DET_GROUP_CHECK(7,!( PINB & (1 << 6) )) \
189
190 // Combines the DET_GROUP_Xs above for the given groupArray
191 #define DET_GROUP(group,det_group) \
192                         case group: \
193                                 { \
194                                         uint8_t groupArray[DETECT_group_size_##group] = DETECT_group_array_##group; \
195                                         _delay_us(1); \
196                                         DET_GROUP_##det_group \
197                                 } \
198                                 break;
199
200
201 // Loop over all of the sampled keys of the given array
202 // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
203 // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
204 #define DEBOUNCE_ASSESS(table,size) \
205                         for ( uint8_t key = 1; key < size + 1; key++ ) {\
206                                 table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
207                         } \
208
209
210 // Keypad detection
211 // Each switch has it's own detection line, inverse logic
212 #define KEYPAD_DETECT(test,switch_code) \
213                         if ( !(test) ) { \
214                                 keypadDetectArray[switch_code]++; \
215                         } \
216
217
218 // NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
219 //        Other Bits: Pressed state sample counter
220 uint8_t keyDetectArray[KEYBOARD_SIZE + 1];
221 uint8_t keypadDetectArray[KEYPAD_SIZE + 1];
222
223 uint16_t sendKeypressCounter = 0;
224 volatile uint8_t sendKeypresses = 0;
225
226
227 // Modifier Mask
228 #define MODIFIERS_KEYPAD   0
229 #define MODIFIERS_KEYBOARD 4
230 static uint8_t   keypad_modifierMask[] = {};
231 static uint8_t keyboard_modifierMask[] = { 1, 17, 33, 49 };
232
233 // Default 1-indexed key mappings
234 static uint8_t keypadDefaultMap[] = { 0,
235                                 KEYPAD_ASTERIX,
236                                 KEYPAD_MINUS,
237                                 KEYPAD_PLUS,
238                                 KEYPAD_ENTER,
239                                 KEYPAD_9,
240                                 KEYPAD_6,
241                                 KEYPAD_3,
242                                 KEYPAD_0,
243                                 KEYPAD_8,
244                                 KEYPAD_5,
245                                 KEYPAD_2,
246                                 KEYPAD_PERIOD,
247                                 KEYPAD_7,
248                                 KEYPAD_4,
249                                 KEYPAD_1,
250                                 KEYPAD_SLASH };
251
252 static uint8_t defaultMap[] = { 0,
253                                 KEY_GUI,
254                                 KEY_1,
255                                 KEY_2,
256                                 KEY_3,
257                                 KEY_4,
258                                 KEY_5,
259                                 KEY_6,
260                                 KEY_7,
261                                 KEY_8,
262                                 KEY_9,
263                                 KEY_0,
264                                 KEY_MINUS,
265                                 KEY_EQUAL,
266                                 KEY_BACKSLASH,
267                                 KEY_TILDE,
268                                 KEY_BACKSPACE,
269                                 KEY_ALT,
270                                 KEY_TAB,
271                                 KEY_Q,
272                                 KEY_W,
273                                 KEY_E,
274                                 KEY_R,
275                                 KEY_T,
276                                 KEY_Y,
277                                 KEY_U,
278                                 KEY_I,
279                                 KEY_O,
280                                 KEY_P,
281                                 KEY_LEFT_BRACE,
282                                 KEY_RIGHT_BRACE,
283                                 KEY_DELETE,
284                                 KEY_UP,
285                                 KEY_CTRL,
286                                 KEY_CAPS_LLOCK,
287                                 KEY_A,
288                                 KEY_S,
289                                 KEY_D,
290                                 KEY_F,
291                                 KEY_G,
292                                 KEY_H,
293                                 KEY_J,
294                                 KEY_K,
295                                 KEY_L,
296                                 KEY_SEMICOLON,
297                                 KEY_QUOTE,
298                                 KEY_ENTER,
299                                 KEY_DOWN,
300                                 KEY_ESC,
301                                 KEY_LEFT_SHIFT,
302                                 KEY_Z,
303                                 KEY_X,
304                                 KEY_C,
305                                 KEY_V,
306                                 KEY_B,
307                                 KEY_N,
308                                 KEY_M,
309                                 KEY_COMMA,
310                                 KEY_PERIOD,
311                                 KEY_SLASH,
312                                 KEY_RIGHT_SHIFT,
313                                 KEY_LEFT,
314                                 KEY_RIGHT,
315                                 KEY_SPACE };
316
317
318 void detection( int group )
319 {
320         // XXX Modify for different detection groups <-> groupArray mappings
321         switch ( group ) {
322                 DET_GROUP(1,2)
323                 DET_GROUP(2,2)
324                 DET_GROUP(3,3)
325                 DET_GROUP(4,4)
326                 DET_GROUP(5,2)
327                 DET_GROUP(6,2)
328                 DET_GROUP(7,4)
329                 DET_GROUP(8,4)
330                 DET_GROUP(9,1)
331         }
332 }
333
334
335
336 // XXX This part is configurable
337 inline void pinSetup(void)
338 {
339         // For each pin, 0=input, 1=output
340         DDRA = 0x00;
341         DDRB = 0x00;
342         DDRC = 0x00;
343         DDRD = 0xFC;
344         DDRE = 0x43;
345         DDRF = 0x00;
346
347
348         // Setting pins to either high or pull-up resistor
349         PORTA = 0xFF;
350         PORTB = 0xFF;
351         PORTC = 0x01;
352         PORTD = 0xFF;
353         PORTE = 0xC3;
354         PORTF = 0xFF;
355 }
356
357 // Given a sampling array, and the current number of detected keypress
358 // Add as many keypresses from the sampling array to the USB key send array as possible.
359 void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map ) {
360         for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
361                 if ( keys[key] & (1 << 7) ) {
362                         pint8( key );
363                         print(" ");
364                         uint8_t modFound = 0;
365
366                         // Determine if the key is a modifier
367                         for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
368                                 // Modifier found
369                                 if ( modifiers[mod] == key ) {
370                                         keyboard_modifier_keys |= map[key];
371                                         modFound = 1;
372                                         break;
373                                 }
374                         }
375                         if ( modFound )
376                                 continue;
377
378                         // Too many keys
379                         if ( *validKeys == 6 )
380                                 break;
381                         keyboard_keys[(*validKeys)++] = map[key];
382                 }
383         }
384 }
385
386 int main( void )
387 {
388         // Setup with 16 MHz clock
389         CPU_PRESCALE( 0 );
390
391         // Configuring Pins
392         pinSetup();
393
394         // Initialize the USB, and then wait for the host to set configuration.
395         // If the Teensy is powered without a PC connected to the USB port,
396         // this will wait forever.
397         usb_init();
398         while ( !usb_configured() ) /* wait */ ;
399
400         // Wait an extra second for the PC's operating system to load drivers
401         // and do whatever it does to actually be ready for input
402         _delay_ms(1000);
403
404         // Setup ISR Timer for flagging a kepress send to USB
405         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
406         TCCR0A = 0x00;
407         TCCR0B = 0x03;
408         TIMSK0 = (1 << TOIE0);
409
410         // Main Detection Loop
411         int8_t group = 1;
412         uint8_t count = 0;
413         for ( ;;group++ ) {
414                 // XXX Change number of ORDs if number of lines (RowsxColumns) differ
415                 // Determine which keys are being pressed
416                 switch ( group ) {
417                         DD_CASE_ORD(1)
418                         DD_CASE_ORD(2)
419                         DD_CASE_ORD(3)
420                         DD_CASE_ORD(4)
421                         DD_CASE_ORD(5)
422                         DD_CASE_ORD(6)
423                         DD_CASE_ORD(7)
424                         DD_CASE_ORD(8)
425                         DD_CASE_END(9,group)
426                 }
427
428                 // Check all Keyboard keys first
429                 if ( group != -1 )
430                         continue;
431
432                 // Check Keypad keys
433                 KEYPAD_DETECT(PINA & (1 << 0),11)
434                 KEYPAD_DETECT(PINA & (1 << 1),3)
435                 KEYPAD_DETECT(PINA & (1 << 2),7)
436                 KEYPAD_DETECT(PINA & (1 << 3),4)
437                 KEYPAD_DETECT(PINA & (1 << 4),15)
438                 KEYPAD_DETECT(PINA & (1 << 5),6)
439                 KEYPAD_DETECT(PINA & (1 << 6),2)
440                 KEYPAD_DETECT(PINA & (1 << 7),10)
441                 KEYPAD_DETECT(PINF & (1 << 0),8)
442                 KEYPAD_DETECT(PINF & (1 << 1),12)
443                 KEYPAD_DETECT(PINF & (1 << 2),16)
444                 KEYPAD_DETECT(PINF & (1 << 3),13)
445                 KEYPAD_DETECT(PINF & (1 << 4),1)
446                 KEYPAD_DETECT(PINF & (1 << 5),5)
447                 KEYPAD_DETECT(PINF & (1 << 6),9)
448                 KEYPAD_DETECT(PINF & (1 << 7),14)
449
450                 // Check count to see if the sample threshold may have been reached, otherwise collect more data
451                 count++;
452                 if ( count < MAX_SAMPLES )
453                         continue;
454
455                 // Reset Sample Counter
456                 count = 0;
457
458                 // Assess debouncing sample table
459                 DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
460                 DEBOUNCE_ASSESS(keypadDetectArray,KEYPAD_SIZE)
461
462                 // Send keypresses over USB if the ISR has signalled that it's time
463                 if ( !sendKeypresses )
464                         continue;
465
466
467                 // Detect Valid Keypresses - TODO
468                 uint8_t validKeys = 0;
469                 keyPressDetection( keyDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_modifierMask, MODIFIERS_KEYBOARD, defaultMap );
470                 keyPressDetection( keypadDetectArray, &validKeys, KEYPAD_SIZE, keypad_modifierMask, MODIFIERS_KEYPAD, keypadDefaultMap );
471                 print(":\n");
472
473                 // TODO undo potentially old keys
474                 for ( uint8_t c = validKeys; c < 6; c++ )
475                         keyboard_keys[c] = 0;
476
477                 // Send keypresses
478                 usb_keyboard_send();
479
480                 // Clear sendKeypresses Flag
481                 sendKeypresses = 0;
482
483                 // Clear modifiers
484                 keyboard_modifier_keys = 0;
485         }
486
487         return 0;
488 }
489
490 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
491 ISR( TIMER0_OVF_vect )
492 {
493         sendKeypressCounter++;
494         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
495                 sendKeypressCounter = 0;
496                 sendKeypresses = 1;
497         }
498 }
499