]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Parts of the upcoming rewrite
[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 "scan_loop.h"
28 //#include "layouts.h"
29 //#include "usb_keyboard.h"
30
31 // TEMP INCLUDES
32 #include "usb_keyboard_debug.h"
33 #include <print.h>
34
35 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
36
37
38 // Debouncing Defines
39 #define SAMPLE_THRESHOLD 110
40 #define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
41
42
43 // Verified Keypress Defines
44 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
45
46 /*
47 // Number of keys
48 #define KEYBOARD_SIZE 63
49 #define KEYPAD_SIZE 16
50
51 // Drive Pin Defines
52 #define DRIVE_reg_1 PORTD
53 #define DRIVE_reg_2 PORTD
54 #define DRIVE_reg_3 PORTD
55 #define DRIVE_reg_4 PORTD
56 #define DRIVE_reg_5 PORTD
57 #define DRIVE_reg_6 PORTD
58 #define DRIVE_reg_7 PORTE
59 #define DRIVE_reg_8 PORTE
60 #define DRIVE_reg_9 PORTE
61 #define DRIVE_reg_10 <blank>
62 #define DRIVE_reg_11 <blank>
63 #define DRIVE_reg_12 <blank>
64
65 #define DRIVE_pin_1 2
66 #define DRIVE_pin_2 3
67 #define DRIVE_pin_3 4
68 #define DRIVE_pin_4 5
69 #define DRIVE_pin_5 6
70 #define DRIVE_pin_6 7
71 #define DRIVE_pin_7 0
72 #define DRIVE_pin_8 1
73 #define DRIVE_pin_9 6
74 #define DRIVE_pin_10 <blank>
75 #define DRIVE_pin_11 <blank>
76 #define DRIVE_pin_12 <blank>
77
78 // Detect Pin/Group Defines
79 #define DETECT_group_1 1
80 #define DETECT_group_2 2
81 #define DETECT_group_3 3
82 #define DETECT_group_4 4
83 #define DETECT_group_5 5
84 #define DETECT_group_6 6
85 #define DETECT_group_7 7
86 #define DETECT_group_8 8
87 #define DETECT_group_9 9
88 #define DETECT_group_10 <blank>
89 #define DETECT_group_11 <blank>
90 #define DETECT_group_12 <blank>
91
92 #define DETECT_group_size_1 7
93 #define DETECT_group_size_2 7
94 #define DETECT_group_size_3 6
95 #define DETECT_group_size_4 8
96 #define DETECT_group_size_5 7
97 #define DETECT_group_size_6 7
98 #define DETECT_group_size_7 8
99 #define DETECT_group_size_8 8
100 #define DETECT_group_size_9 4
101 #define DETECT_group_size_10 <blank>
102 #define DETECT_group_size_11 <blank>
103 #define DETECT_group_size_12 <blank>
104
105 // Switch Codes
106 #define DETECT_group_array_1 {55,22,6 ,40,43,27,11}
107 #define DETECT_group_array_2 {56,23,7 ,41,58,26,10}
108 #define DETECT_group_array_3 {57,24,8 ,42,25,9}
109 #define DETECT_group_array_4 {54,21,5 ,39,44,28,12,59}
110 #define DETECT_group_array_5 {53,20,4 ,38,45,29,13}
111 #define DETECT_group_array_6 {52,19,3 ,37,46,30,14}
112 #define DETECT_group_array_7 {51,18,2 ,36,61,31,15,63}
113 #define DETECT_group_array_8 {50,17,1 ,35,47,32,16,62}
114 #define DETECT_group_array_9 {48,49,33,34} // 49/60 are the same line
115 #define DETECT_group_array_10 <blank>
116 #define DETECT_group_array_11 <blank>
117 #define DETECT_group_array_12 <blank>
118
119
120
121 // Drive Macros (Generally don't need to be changed), except for maybe DRIVE_DETECT
122 // Sleep is for signal debouncing
123 #define DRIVE_DETECT(reg,pin,group) \
124                         reg &= ~(1 << pin); \
125                         detection(group); \
126                         reg |= (1 << pin); \
127                         _delay_us(1);
128
129 #define DD_CASE(number) \
130                         case number:\
131                                 DRIVE_DETECT(DRIVE_reg_##number, DRIVE_pin_##number, DETECT_group_##number)
132
133 #define DD_CASE_ORD(number) \
134                         DD_CASE(number) \
135                         break;
136
137 #define DD_CASE_END(number,var) \
138                         DD_CASE(number) \
139                         var = -1; \
140                         break;
141
142
143 // Updates the current detection sample and last sample bit
144 // Detection Macros (Probably don't need to be changed, but depending the matrix, may have to be)
145 // Determine if key is either normal or a modifier
146 #define DET_GROUP_CHECK(index,test) \
147                         if ( test ) { \
148                                 keyDetectArray[groupArray[index]]++; \
149                         }
150
151
152 // XXX - Detection Groups
153 // Checks each of the specified pins, and then if press detected, determine if the key is normal or a modifier
154 // Inverse logic applies for the PINs
155
156 // Used for 1 detection group (Special group)
157 #define DET_GROUP_1 \
158                         DET_GROUP_CHECK(0,!( PINB & (1 << 7) )) \
159                         DET_GROUP_CHECK(1,!( PINC & (1 << 0) )) \
160                         DET_GROUP_CHECK(2,!( PIND & (1 << 0) )) \
161                         DET_GROUP_CHECK(3,!( PIND & (1 << 1) )) \
162
163 // Used for 4 detection groups (Skips J1 P9)
164 #define DET_GROUP_2 \
165                         DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
166                         DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
167                         DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
168                         DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
169                         DET_GROUP_CHECK(4,!( PINB & (1 << 3) )) \
170                         DET_GROUP_CHECK(5,!( PINB & (1 << 4) )) \
171                         DET_GROUP_CHECK(6,!( PINB & (1 << 5) )) \
172
173 // Used for 1 detection group (Skips J1 P6 and J1 P9)
174 #define DET_GROUP_3 \
175                         DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
176                         DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
177                         DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
178                         DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
179                         DET_GROUP_CHECK(4,!( PINB & (1 << 4) )) \
180                         DET_GROUP_CHECK(5,!( PINB & (1 << 5) )) \
181
182 // Used for 3 detection groups (No skips, except special group 1)
183 #define DET_GROUP_4 \
184                         DET_GROUP_CHECK(0,!( PINE & (1 << 7) )) \
185                         DET_GROUP_CHECK(1,!( PINB & (1 << 0) )) \
186                         DET_GROUP_CHECK(2,!( PINB & (1 << 1) )) \
187                         DET_GROUP_CHECK(3,!( PINB & (1 << 2) )) \
188                         DET_GROUP_CHECK(4,!( PINB & (1 << 3) )) \
189                         DET_GROUP_CHECK(5,!( PINB & (1 << 4) )) \
190                         DET_GROUP_CHECK(6,!( PINB & (1 << 5) )) \
191                         DET_GROUP_CHECK(7,!( PINB & (1 << 6) )) \
192
193 // Combines the DET_GROUP_Xs above for the given groupArray
194 #define DET_GROUP(group,det_group) \
195                         case group: \
196                                 { \
197                                         uint8_t groupArray[DETECT_group_size_##group] = DETECT_group_array_##group; \
198                                         _delay_us(1); \
199                                         DET_GROUP_##det_group \
200                                 } \
201                                 break;
202
203
204 // Loop over all of the sampled keys of the given array
205 // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
206 // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
207 #define DEBOUNCE_ASSESS(table,size) \
208                         for ( uint8_t key = 1; key < size + 1; key++ ) {\
209                                 table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
210                         } \
211
212
213 // Keypad detection
214 // Each switch has it's own detection line, inverse logic
215 #define KEYPAD_DETECT(test,switch_code) \
216                         if ( !(test) ) { \
217                                 keypadDetectArray[switch_code]++; \
218                         } \
219
220
221 // NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
222 //        Other Bits: Pressed state sample counter
223 uint8_t keyboardDetectArray[KEYBOARD_SIZE + 1];
224
225 // Interrupt Variables
226 uint16_t sendKeypressCounter = 0;
227 volatile uint8_t sendKeypresses = 0;
228
229
230 void detection( int group )
231 {
232         // XXX Modify for different detection groups <-> groupArray mappings
233         switch ( group ) {
234                 DET_GROUP(1,2)
235                 DET_GROUP(2,2)
236                 DET_GROUP(3,3)
237                 DET_GROUP(4,4)
238                 DET_GROUP(5,2)
239                 DET_GROUP(6,2)
240                 DET_GROUP(7,4)
241                 DET_GROUP(8,4)
242                 DET_GROUP(9,1)
243         }
244 }
245 */
246
247 // Error LED Control
248 void errorLED( uint8_t on )
249 {
250         // Error LED On
251         if ( on ) {
252                 DDRD  |= (1<<6);
253                 PORTD |= (1<<6);
254         }
255         // Error LED Off
256         else {
257                 DDRD  &= ~(1<<6);
258                 PORTD &= ~(1<<6);
259         }
260 }
261
262
263
264 // Initial Pin Setup
265 // If the matrix is properly set, this function does not need to be changed
266 inline void pinSetup(void)
267 {
268         // For each pin, 0=input, 1=output
269         DDRA = 0x00;
270         DDRB = 0x00;
271         DDRC = 0x00;
272         DDRD = 0x40; // LED Setup
273         DDRE = 0x00;
274         DDRF = 0x00;
275
276
277         // Setting pins to either high or pull-up resistor
278         PORTA = 0x00;
279         PORTB = 0x00;
280         PORTC = 0x00;
281         PORTD = 0x40; // LED Enable
282         PORTE = 0x00;
283         PORTF = 0x00;
284 }
285 /*
286 // Given a sampling array, and the current number of detected keypress
287 // Add as many keypresses from the sampling array to the USB key send array as possible.
288 void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map ) {
289         for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
290                 if ( keys[key] & (1 << 7) ) {
291                         pint8( key );
292                         //print(" ");
293                         uint8_t modFound = 0;
294
295                         // Determine if the key is a modifier
296                         for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
297                                 // Modifier found
298                                 if ( modifiers[mod] == key ) {
299                                         keyboard_modifier_keys |= map[key];
300                                         modFound = 1;
301                                         break;
302                                 }
303                         }
304                         if ( modFound )
305                                 continue;
306
307                         // Too many keys
308                         if ( *validKeys == 6 )
309                                 break;
310
311                         // Allow ignoring keys with 0's
312                         if ( map[key] != 0 )
313                                 keyboard_keys[(*validKeys)++] = map[key];
314                 }
315         }
316 }
317 */
318
319 int main( void )
320 {
321         // Setup with 16 MHz clock
322         CPU_PRESCALE( 0 );
323
324         // Configuring Pins
325         pinSetup();
326
327         // Initialize the USB, and then wait for the host to set configuration.
328         // If the Teensy is powered without a PC connected to the USB port,
329         // this will wait forever.
330         usb_init();
331         while ( !usb_configured() ) /* wait */ ;
332
333         // Wait an extra second for the PC's operating system to load drivers
334         // and do whatever it does to actually be ready for input
335         _delay_ms(1000);
336
337         // Setup ISR Timer for flagging a kepress send to USB
338         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
339         TCCR0A = 0x00;
340         TCCR0B = 0x03;
341         TIMSK0 = (1 << TOIE0);
342
343         // Main Detection Loop
344         while ( 1 ) {
345                 scan_loop();
346
347                 // Loop should never get here (indicate error)
348                 errorLED( 1 );
349
350                 // TODO HID Debug message
351         }
352 }
353 /*
354         int8_t group = 1;
355         uint8_t count = 0;
356         for ( ;;group++ ) {
357                 // XXX Change number of ORDs if number of lines (RowsxColumns) differ
358                 // Determine which keys are being pressed
359                 switch ( group ) {
360                         DD_CASE_ORD(1)
361                         DD_CASE_ORD(2)
362                         DD_CASE_ORD(3)
363                         DD_CASE_ORD(4)
364                         DD_CASE_ORD(5)
365                         DD_CASE_ORD(6)
366                         DD_CASE_ORD(7)
367                         DD_CASE_ORD(8)
368                         DD_CASE_END(9,group)
369                 }
370
371                 // Check all Keyboard keys first
372                 if ( group != -1 )
373                         continue;
374
375                 // Check Keypad keys
376                 KEYPAD_DETECT(PINA & (1 << 0),11)
377                 KEYPAD_DETECT(PINA & (1 << 1),3)
378                 KEYPAD_DETECT(PINA & (1 << 2),7)
379                 KEYPAD_DETECT(PINA & (1 << 3),4)
380                 KEYPAD_DETECT(PINA & (1 << 4),15)
381                 KEYPAD_DETECT(PINA & (1 << 5),6)
382                 KEYPAD_DETECT(PINA & (1 << 6),2)
383                 KEYPAD_DETECT(PINA & (1 << 7),10)
384                 KEYPAD_DETECT(PINF & (1 << 0),8)
385                 KEYPAD_DETECT(PINF & (1 << 1),12)
386                 KEYPAD_DETECT(PINF & (1 << 2),16)
387                 KEYPAD_DETECT(PINF & (1 << 3),13)
388                 KEYPAD_DETECT(PINF & (1 << 4),1)
389                 KEYPAD_DETECT(PINF & (1 << 5),5)
390                 KEYPAD_DETECT(PINF & (1 << 6),9)
391                 KEYPAD_DETECT(PINF & (1 << 7),14)
392
393                 // Check count to see if the sample threshold may have been reached, otherwise collect more data
394                 count++;
395                 if ( count < MAX_SAMPLES )
396                         continue;
397
398                 // Reset Sample Counter
399                 count = 0;
400
401                 // Assess debouncing sample table
402                 DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
403                 DEBOUNCE_ASSESS(keypadDetectArray,KEYPAD_SIZE)
404
405                 // Send keypresses over USB if the ISR has signalled that it's time
406                 if ( !sendKeypresses )
407                         continue;
408
409                 // Detect Valid Keypresses - TODO
410                 uint8_t validKeys = 0;
411
412                 uint8_t *keyboard_MODMASK = keyboard_modifierMask;
413                 uint8_t  keyboard_NUMMODS = MODIFIERS_KEYBOARD;
414                 uint8_t *keyboard_MAP     = defaultMap;
415                 uint8_t *keypad_MODMASK   = keypad_modifierMask;
416                 uint8_t  keypad_NUMMODS   = MODIFIERS_KEYPAD;
417                 uint8_t *keypad_MAP       = keypadDefaultMap;
418
419                 // Map selection - CapsLock FN
420                 if ( keyDetectArray[34] & (1 << 7) ) { // CapsLock FN Modifier
421                                 keyboard_MAP     = colemakMap;
422                                 keyboard_MODMASK = alternate_modifierMask;
423                                 keyboard_NUMMODS = 5;
424
425                         // Function Key
426                         if ( keyDetectArray[61] & (1 << 7) ) {
427                                 keyboard_MAP     = navigationMap;
428                         }
429                 }
430
431                 keyPressDetection( keyDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_MODMASK, keyboard_NUMMODS, keyboard_MAP );
432                 keyPressDetection( keypadDetectArray, &validKeys, KEYPAD_SIZE, keypad_MODMASK, keypad_NUMMODS, keypad_MAP );
433                 //print(":\n");
434
435                 // TODO undo potentially old keys
436                 for ( uint8_t c = validKeys; c < 6; c++ )
437                         keyboard_keys[c] = 0;
438
439                 // Send keypresses
440                 usb_keyboard_send();
441
442                 // Clear sendKeypresses Flag
443                 sendKeypresses = 0;
444
445                 // Clear modifiers
446                 keyboard_modifier_keys = 0;
447         }
448
449         return 0;
450 }
451 */
452
453 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
454 ISR( TIMER0_OVF_vect )
455 {
456         sendKeypressCounter++;
457         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
458                 sendKeypressCounter = 0;
459                 sendKeypresses = 1;
460         }
461 }
462