]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Updating for the latest pinout
[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_keyboard.h"
27
28 // TEMP INCLUDES
29 #include "usb_keyboard_debug.h"
30 #include <print.h>
31
32 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
33
34 // Sleep defined in milliseconds
35 #define PRE_DRIVE_SLEEP  50
36 #define POST_DRIVE_SLEEP 50
37
38
39 // Drive Pin Defines
40 #define DRIVE_reg_1 PORTD
41 #define DRIVE_reg_2 PORTD
42 #define DRIVE_reg_3 PORTD
43 #define DRIVE_reg_4 PORTD
44 #define DRIVE_reg_5 PORTD
45 #define DRIVE_reg_6 PORTD
46 #define DRIVE_reg_7 PORTE
47 #define DRIVE_reg_8 PORTE
48 #define DRIVE_reg_9 PORTE
49 #define DRIVE_reg_10 <blank>
50 #define DRIVE_reg_11 <blank>
51 #define DRIVE_reg_12 <blank>
52
53 #define DRIVE_pin_1 2
54 #define DRIVE_pin_2 3
55 #define DRIVE_pin_3 4
56 #define DRIVE_pin_4 5
57 #define DRIVE_pin_5 6
58 #define DRIVE_pin_6 7
59 #define DRIVE_pin_7 0
60 #define DRIVE_pin_8 1
61 #define DRIVE_pin_9 6
62 #define DRIVE_pin_10 <blank>
63 #define DRIVE_pin_11 <blank>
64 #define DRIVE_pin_12 <blank>
65
66 // Detect Pin/Group Defines
67 #define DETECT_group_1 1
68 #define DETECT_group_2 2
69 #define DETECT_group_3 3
70 #define DETECT_group_4 4
71 #define DETECT_group_5 5
72 #define DETECT_group_6 6
73 #define DETECT_group_7 7
74 #define DETECT_group_8 8
75 #define DETECT_group_9 9
76 #define DETECT_group_10 <blank>
77 #define DETECT_group_11 <blank>
78 #define DETECT_group_12 <blank>
79
80 #define DETECT_group_size_1 7
81 #define DETECT_group_size_2 7
82 #define DETECT_group_size_3 6
83 #define DETECT_group_size_4 8
84 #define DETECT_group_size_5 7
85 #define DETECT_group_size_6 7
86 #define DETECT_group_size_7 8
87 #define DETECT_group_size_8 8
88 #define DETECT_group_size_9 4
89 #define DETECT_group_size_10 <blank>
90 #define DETECT_group_size_11 <blank>
91 #define DETECT_group_size_12 <blank>
92
93 /*
94 #define DETECT_group_array_1 {{KEY_ESC,KEY_CTRL,KEY_CAPS_LOCK,KEY_SHIFT},{0,1,0,1}}
95 #define DETECT_group_array_2 {{KEY_BACKSPACE,KEY_UP,KEY_DOWN,KEY_A,KEY_INSERT,KEY_ALT,KEY_Z,KEY_RIGHT},{0,0,0,0,0,1,0,0}}
96 #define DETECT_group_array_3 {{KEY_TILDE,KEY_DELETE,KEY_LEFT,KEY_SPACE,KEY_X,KEY_S,KEY_TAB,KEY_1},{0,0,0,0,0,0,0,0}}
97 #define DETECT_group_array_4 {{KEY_SLASH,KEY_RIGHT_BRACE,KEY_ENTER,KEY_D,KEY_2,KEY_Q,KEY_C},{0,0,0,0,0,0,0}}
98 #define DETECT_group_array_5 {{KEY_EQUAL,KEY_LEFT_BRACE,KEY_QUOTE,KEY_F,KEY_3,KEY_W,KEY_V},{0,0,0,0,0,0,0}}
99 #define DETECT_group_array_6 {{KEY_MINUS,KEY_P,KEY_SEMICOLON,KEY_G,KEY_4,KEY_E,KEY_B,KEY_BACKSLASH},{0,0,0,0,0,0,0,0}}
100 #define DETECT_group_array_7 {{KEY_8,KEY_U,KEY_K,KEY_7,KEY_Y,KEY_COMMA},{0,0,0,0,0,0}}
101 #define DETECT_group_array_8 {{KEY_9,KEY_I,KEY_PERIOD,KEY_J,KEY_6,KEY_T,KEY_M},{0,0,0,0,0,0,0}}
102 #define DETECT_group_array_9 {{KEY_0,KEY_O,KEY_L,KEY_H,KEY_5,KEY_R,KEY_N},{0,0,0,0,0,0,0}}
103 */
104 // Switch Codes
105 #define DETECT_group_array_1 {11,27,43,40,6, 22,55}
106 #define DETECT_group_array_2 {10,26,58,41,7, 23,56}
107 #define DETECT_group_array_3 {9, 25,42,8, 24,57}
108 #define DETECT_group_array_4 {12,28,44,39,5, 21,54,59}
109 #define DETECT_group_array_5 {13,29,45,38,4, 20,53}
110 #define DETECT_group_array_6 {14,30,46,37,3, 19,52}
111 #define DETECT_group_array_7 {15,31,61,63,51,36,18,2}
112 #define DETECT_group_array_8 {16,32,47,35,1, 17,50,62}
113 #define DETECT_group_array_9 {48,34,33,49} // 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                         _delay_ms(POST_DRIVE_SLEEP);
126
127 #define DD_CASE(number) \
128                         case number:\
129                                 DRIVE_DETECT(DRIVE_reg_##number, DRIVE_pin_##number, DETECT_group_##number)
130
131 #define DD_CASE_ORD(number) \
132                         DD_CASE(number) \
133                         break;
134
135 #define DD_CASE_END(number,var) \
136                         DD_CASE(number) \
137                         var = -1; \
138                         break;
139
140
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) \
144                         curDetect.keyDetectArray[curDetect.keyDetectCount++] = groupArray[index];
145
146
147 // XXX - Detection Groups
148 // Checks each of the specified pins, and then if press detected, determine if the key is normal or a modifier
149 // Inverse logic applies for the PINs
150
151 // Used for 1 detection group (Special group)
152 #define DET_GROUP_1 \
153                         if ( !( PINB & (1 << 7) ) ) \
154                                 DET_GROUP_CHECK(0) \
155                         if ( !( PINC & (1 << 0) ) ) \
156                                 DET_GROUP_CHECK(1) \
157                         if ( !( PIND & (1 << 0) ) ) \
158                                 DET_GROUP_CHECK(2) \
159                         if ( !( PIND & (1 << 1) ) ) \
160                                 DET_GROUP_CHECK(3)
161
162 // Used for 4 detection groups (Skips J1 P9)
163 #define DET_GROUP_2 \
164                         if ( !( PINE & (1 << 7) ) ) \
165                                 DET_GROUP_CHECK(0) \
166                         if ( !( PINB & (1 << 0) ) ) \
167                                 DET_GROUP_CHECK(1) \
168                         if ( !( PINB & (1 << 1) ) ) \
169                                 DET_GROUP_CHECK(2) \
170                         if ( !( PINB & (1 << 2) ) ) \
171                                 DET_GROUP_CHECK(3) \
172                         if ( !( PINB & (1 << 3) ) ) \
173                                 DET_GROUP_CHECK(4) \
174                         if ( !( PINB & (1 << 4) ) ) \
175                                 DET_GROUP_CHECK(5) \
176                         if ( !( PINB & (1 << 5) ) ) \
177                                 DET_GROUP_CHECK(6) \
178
179 // Used for 1 detection group (Skips J1 P6 and J1 P9)
180 #define DET_GROUP_3 \
181                         if ( !( PINE & (1 << 7) ) ) \
182                                 DET_GROUP_CHECK(0) \
183                         if ( !( PINB & (1 << 0) ) ) \
184                                 DET_GROUP_CHECK(1) \
185                         if ( !( PINB & (1 << 1) ) ) \
186                                 DET_GROUP_CHECK(2) \
187                         if ( !( PINB & (1 << 2) ) ) \
188                                 DET_GROUP_CHECK(3) \
189                         if ( !( PINB & (1 << 4) ) ) \
190                                 DET_GROUP_CHECK(4) \
191                         if ( !( PINB & (1 << 5) ) ) \
192                                 DET_GROUP_CHECK(5) \
193
194 // Used for 3 detection groups (No skips, except special group 1)
195 #define DET_GROUP_4 \
196                         if ( !( PINE & (1 << 7) ) ) \
197                                 DET_GROUP_CHECK(0) \
198                         if ( !( PINB & (1 << 0) ) ) \
199                                 DET_GROUP_CHECK(1) \
200                         if ( !( PINB & (1 << 1) ) ) \
201                                 DET_GROUP_CHECK(2) \
202                         if ( !( PINB & (1 << 2) ) ) \
203                                 DET_GROUP_CHECK(3) \
204                         if ( !( PINB & (1 << 3) ) ) \
205                                 DET_GROUP_CHECK(4) \
206                         if ( !( PINB & (1 << 4) ) ) \
207                                 DET_GROUP_CHECK(5) \
208                         if ( !( PINB & (1 << 5) ) ) \
209                                 DET_GROUP_CHECK(6) \
210                         if ( !( PINB & (1 << 6) ) ) \
211                                 DET_GROUP_CHECK(7) \
212
213 // Combines the DET_GROUP_Xs above for the given groupArray
214 #define DET_GROUP(group,det_group) \
215                         case group: \
216                                 { \
217                                         uint8_t groupArray[DETECT_group_size_##group] = DETECT_group_array_##group; \
218                                         DET_GROUP_##det_group \
219                                 } \
220                                 break;
221
222 struct keys {
223         uint8_t keyDetectCount;
224         uint8_t keyDetectArray[40];
225         uint8_t modifiers;
226 } curDetect, prevDetect;
227
228
229 // Scan Code Decoder (for debug)
230 void printDecodeScancode( int code )
231 {
232         static const uint8_t defaultMap[] = { 0,
233                                         KEY_INSERT,
234                                         KEY_1,
235                                         KEY_2,
236                                         KEY_3,
237                                         KEY_4,
238                                         KEY_5,
239                                         KEY_6,
240                                         KEY_7,
241                                         KEY_8,
242                                         KEY_9,
243                                         KEY_0,
244                                         KEY_MINUS,
245                                         KEY_EQUAL,
246                                         KEY_BACKSLASH,
247                                         KEY_ALT,
248                                         KEY_TAB,
249                                         KEY_Q,
250                                         KEY_W,
251                                         KEY_E,
252                                         KEY_R,
253                                         KEY_T,
254                                         KEY_Y,
255                                         KEY_U,
256                                         KEY_I,
257                                         KEY_O,
258                                         KEY_P,
259                                         KEY_LEFT_BRACE,
260                                         KEY_RIGHT_BRACE,
261                                         KEY_DELETE,
262                                         KEY_UP,
263                                         KEY_CTRL,
264                                         KEY_CAPS_LOCK,
265                                         KEY_A,
266                                         KEY_S,
267                                         KEY_D,
268                                         KEY_F,
269                                         KEY_G,
270                                         KEY_H,
271                                         KEY_J,
272                                         KEY_K,
273                                         KEY_L,
274                                         KEY_SEMICOLON,
275                                         KEY_QUOTE,
276                                         KEY_ENTER,
277                                         KEY_DOWN,
278                                         KEY_ESC,
279                                         KEY_LEFT_SHIFT,
280                                         KEY_Z,
281                                         KEY_X,
282                                         KEY_C,
283                                         KEY_V,
284                                         KEY_B,
285                                         KEY_N,
286                                         KEY_M,
287                                         KEY_COMMA,
288                                         KEY_PERIOD,
289                                         KEY_SLASH,
290                                         KEY_RIGHT_SHIFT,
291                                         KEY_LEFT,
292                                         KEY_RIGHT,
293                                         KEY_SPACE };
294
295         static const char* decodeArray[] = { "", "", "", "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Enter", "Esc", "Backspace", "Tab", "Space", "-_", "=+", "[{", "]}", "\\", "#", ";:", "'\"", "`~", ",<", ".>", "/?", "Caps Lock", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Print Screen", "Scroll Lock", "Pause", "Insert", "Home", "Page Up", "Delete", "End", "Page Down", "Right", "Left", "Down", "Up", "Num Lock", "K1", "K2", "K3", "K4", "K5", "K6", "K7", "K8", "K9", "K0", "K." };
296         print_P( decodeArray[ defaultMap[code] ] );
297 }
298
299 void detection( int group )
300 {
301         _delay_ms(PRE_DRIVE_SLEEP);
302
303         // XXX Modify for different detection groups <-> groupArray mappings
304         switch ( group ) {
305                 DET_GROUP(1,2)
306                 DET_GROUP(2,2)
307                 DET_GROUP(3,3)
308                 DET_GROUP(4,4)
309                 DET_GROUP(5,2)
310                 DET_GROUP(6,2)
311                 DET_GROUP(7,4)
312                 DET_GROUP(8,4)
313                 DET_GROUP(9,1)
314         }
315 }
316
317
318
319 // XXX This part is configurable
320 void pinSetup(void)
321 {
322         // For each pin, 0=input, 1=output
323         DDRA = 0x00;
324         DDRB = 0x00;
325         DDRC = 0x00;
326         DDRD = 0xFC;
327         DDRE = 0x43;
328         DDRF = 0x00;
329
330         // Setting pins to either high or pull-up resistor
331         PORTA = 0x00;
332         PORTB = 0xFF;
333         PORTC = 0x01;
334         PORTD = 0xFF;
335         PORTE = 0xC3;
336         PORTF = 0x00;
337 }
338
339 int main( void )
340 {
341         // set for 16 MHz clock
342         CPU_PRESCALE( 0 );
343
344         // Configuring Pins
345         pinSetup();
346
347         // Initialize the USB, and then wait for the host to set configuration.
348         // If the Teensy is powered without a PC connected to the USB port,
349         // this will wait forever.
350         usb_init();
351         while ( !usb_configured() ) /* wait */ ;
352
353         // Wait an extra second for the PC's operating system to load drivers
354         // and do whatever it does to actually be ready for input
355         _delay_ms(1000);
356
357         // Make sure variables are properly initialized
358         curDetect.keyDetectCount = 0;
359         curDetect.modifiers = 0;
360
361         // Main Detection Loop
362         // XXX Change number of ORDs if number of lines differ
363         for ( int group = 1;;group++ ) {
364                 // Determine which keys are being pressed
365                 switch ( group ) {
366                         DD_CASE_ORD(1)
367                         DD_CASE_ORD(2)
368                         DD_CASE_ORD(3)
369                         DD_CASE_ORD(4)
370                         DD_CASE_ORD(5)
371                         DD_CASE_ORD(6)
372                         DD_CASE_ORD(7)
373                         DD_CASE_ORD(8)
374                         DD_CASE_END(9,group)
375                 }
376
377                 if ( group != -1 )
378                         continue;
379
380                 // Print out the current keys pressed
381                 if ( curDetect.keyDetectCount > 0 ) {
382                         print("Switch: ");
383                         for ( int c = 0; c < curDetect.keyDetectCount; c++ ) {
384                                 print("0x");
385                                 phex( curDetect.keyDetectArray[c] );
386                                 print("|");
387                                 //printDecodeScancode( curDetect.keyDetectArray[c] );
388                                 print(" ");
389                         }
390                         print("\n");
391                 }
392                 if ( curDetect.modifiers ) {
393                         print("Modifiers: ");
394                         phex( curDetect.modifiers );
395                         print("\n");
396                 }
397
398                 // After going through each of the key groups, send the detected keys and modifiers
399                 // Currently limited to the USB spec (6 keys + modifiers)
400                 // Making sure to pass zeros when there are no keys being pressed
401                 for ( int c = 0; c < 6 && c < curDetect.keyDetectCount; c++ )
402                         keyboard_keys[c] = c < curDetect.keyDetectCount ? curDetect.keyDetectArray[c] : 0;
403
404                 // Modifiers
405                 keyboard_modifier_keys = curDetect.modifiers;
406
407                 // Send keypresses
408                 //usb_keyboard_send();
409
410                 // Cleanup
411                 curDetect.keyDetectCount = 0;
412                 curDetect.modifiers = 0;
413         }
414
415         // usb_keyboard_press(KEY_B, KEY_SHIFT);
416         return 0;
417 }
418