]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Redefining pinouts and adding key sending code.
[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  10
36 #define POST_DRIVE_SLEEP 10
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 4
81 #define DETECT_group_size_2 8
82 #define DETECT_group_size_3 8
83 #define DETECT_group_size_4 7
84 #define DETECT_group_size_5 7
85 #define DETECT_group_size_6 8
86 #define DETECT_group_size_7 6
87 #define DETECT_group_size_8 7
88 #define DETECT_group_size_9 7
89 #define DETECT_group_size_10 <blank>
90 #define DETECT_group_size_11 <blank>
91 #define DETECT_group_size_12 <blank>
92
93 #define DETECT_group_array_1 {{KEY_ESC,KEY_CTRL,KEY_CAPS_LOCK,KEY_SHIFT},{0,1,0,1}}
94 #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}}
95 #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}}
96 #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}}
97 #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}}
98 #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}}
99 #define DETECT_group_array_7 {{KEY_8,KEY_U,KEY_K,KEY_7,KEY_Y,KEY_COMMA},{0,0,0,0,0,0}}
100 #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}}
101 #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}}
102 #define DETECT_group_array_10 <blank>
103 #define DETECT_group_array_11 <blank>
104 #define DETECT_group_array_12 <blank>
105
106
107
108 // Drive Macros (Generally don't need to be changed), except for maybe DRIVE_DETECT
109 #define DRIVE_DETECT(reg,pin,group) \
110                         reg &= ~(1 << pin); \
111                         detection(group); \
112                         reg |= (1 << pin); \
113                         _delay_ms(POST_DRIVE_SLEEP);
114
115 #define DD_CASE(number) \
116                         case number:\
117                                 DRIVE_DETECT(DRIVE_reg##_##number, DRIVE_pin##_##number, DETECT_group##_##number)
118
119 #define DD_CASE_ORD(number) \
120                         DD_CASE(number) \
121                         break;
122
123 #define DD_CASE_END(number,var) \
124                         DD_CASE(number) \
125                         var = -1; \
126                         break;
127
128
129 // Detection Macros (Probably don't need to be changed, but depending the matrix, may have to be)
130 // Determine if key is either normal or a modifier
131 #define DET_GROUP_CHECK(index) \
132                         { \
133                         if ( groupArray[1][index] ) \
134                                 curDetect.modifiers |= groupArray[0][index]; \
135                         else \
136                                 curDetect.keyDetectArray[curDetect.keyDetectCount++] = groupArray[0][index]; \
137                         }
138
139
140 // XXX - Detection Groups
141 // Checks each of the specified pins, and then if press detected, determine if the key is normal or a modifier
142 // Inverse logic applies for the PINs
143
144 // Used for 1 detection group
145 #define DET_GROUP_1 \
146                         if ( !( PINC & (1 << 0) ) ) \
147                                 DET_GROUP_CHECK(3) \
148                         if ( !( PINE & (1 << 1) ) ) \
149                                 DET_GROUP_CHECK(2) \
150                         if ( !( PINE & (1 << 0) ) ) \
151                                 DET_GROUP_CHECK(1) \
152                         if ( !( PINB & (1 << 7) ) ) \
153                                 DET_GROUP_CHECK(0)
154
155 // Used for 4 detection groups
156 #define DET_GROUP_2 \
157                         if ( !( PINC & (1 << 0) ) ) \
158                                 DET_GROUP_CHECK(0) \
159                         if ( !( PINC & (1 << 1) ) ) \
160                                 DET_GROUP_CHECK(1) \
161                         if ( !( PINC & (1 << 2) ) ) \
162                                 DET_GROUP_CHECK(2) \
163                         if ( !( PINC & (1 << 3) ) ) \
164                                 DET_GROUP_CHECK(3) \
165                         if ( !( PINC & (1 << 4) ) ) \
166                                 DET_GROUP_CHECK(4) \
167                         if ( !( PINC & (1 << 5) ) ) \
168                                 DET_GROUP_CHECK(5) \
169                         if ( !( PINC & (1 << 6) ) ) \
170                                 DET_GROUP_CHECK(6) \
171
172 // Used for 1 detection group
173 #define DET_GROUP_3 \
174                         if ( !( PINC & (1 << 0) ) ) \
175                                 DET_GROUP_CHECK(0) \
176                         if ( !( PINC & (1 << 1) ) ) \
177                                 DET_GROUP_CHECK(1) \
178                         if ( !( PINC & (1 << 3) ) ) \
179                                 DET_GROUP_CHECK(2) \
180                         if ( !( PINC & (1 << 4) ) ) \
181                                 DET_GROUP_CHECK(3) \
182                         if ( !( PINC & (1 << 5) ) ) \
183                                 DET_GROUP_CHECK(4) \
184                         if ( !( PINC & (1 << 6) ) ) \
185                                 DET_GROUP_CHECK(5) \
186
187 // Used for 3 detection groups
188 #define DET_GROUP_4 \
189                         if ( !( PINC & (1 << 0) ) ) \
190                                 DET_GROUP_CHECK(0) \
191                         if ( !( PINC & (1 << 1) ) ) \
192                                 DET_GROUP_CHECK(1) \
193                         if ( !( PINC & (1 << 2) ) ) \
194                                 DET_GROUP_CHECK(2) \
195                         if ( !( PINC & (1 << 3) ) ) \
196                                 DET_GROUP_CHECK(3) \
197                         if ( !( PINC & (1 << 4) ) ) \
198                                 DET_GROUP_CHECK(4) \
199                         if ( !( PINC & (1 << 5) ) ) \
200                                 DET_GROUP_CHECK(5) \
201                         if ( !( PINC & (1 << 6) ) ) \
202                                 DET_GROUP_CHECK(6) \
203                         if ( !( PINE & (1 << 1) ) ) \
204                                 DET_GROUP_CHECK(7) \
205
206 // Combines the DET_GROUP_Xs above for the given groupArray
207 #define DET_GROUP(group,det_group) \
208                         case group: \
209                                 { \
210                                         uint8_t groupArray[2][DETECT_group_size##_##group] = DETECT_group_array##_##group; \
211                                         DET_GROUP##_##det_group \
212                                 } \
213                                 break;
214
215 struct keys {
216         uint8_t keyDetectCount;
217         uint8_t keyDetectArray[40];
218         uint8_t modifiers;
219 } curDetect, prevDetect;
220
221 void detection( int group )
222 {
223         _delay_ms(PRE_DRIVE_SLEEP);
224
225         // XXX Modify for different detection groups <-> groupArray mappings
226         switch ( group ) {
227                 DET_GROUP(1,1)
228                 /*
229                 DET_GROUP(2,4)
230                 DET_GROUP(3,4)
231                 DET_GROUP(4,1)
232                 DET_GROUP(5,4)
233                 DET_GROUP(6,2)
234                 DET_GROUP(7,2)
235                 DET_GROUP(8,3)
236                 DET_GROUP(9,2)
237                 */
238         }
239
240
241         // Print out the current keys pressed
242         if ( curDetect.keyDetectCount > 0 ) {
243                 print("Keys: ");
244                 for ( int c = 0; c < curDetect.keyDetectCount; c++ ) {
245                         phex( curDetect.keyDetectArray[c] );
246                         print(" ");
247                 }
248                 print("\n");
249         }
250         if ( curDetect.modifiers ) {
251                 print("Modifiers: ");
252                 phex( curDetect.modifiers );
253                 print("\n");
254         }
255 }
256
257
258
259 // XXX This part is configurable
260 void pinSetup(void)
261 {
262         // For each pin, 0=input, 1=output
263         DDRA = 0x00;
264         DDRB = 0x07;
265         DDRC = 0x80;
266         DDRD = 0x00;
267         DDRE = 0xC0;
268         DDRF = 0x31;
269
270         // Setting pins to either high or pull-up resistor
271         PORTA = 0x00;
272         PORTB = 0x0F;
273         PORTC = 0xFF;
274         PORTD = 0x00;
275         PORTE = 0xC2;
276         PORTF = 0x3F;
277 }
278
279 int main( void )
280 {
281         // set for 16 MHz clock
282         CPU_PRESCALE( 0 );
283
284         // Configuring Pins
285         pinSetup();
286
287         // Initialize the USB, and then wait for the host to set configuration.
288         // If the Teensy is powered without a PC connected to the USB port,
289         // this will wait forever.
290         usb_init();
291         while ( !usb_configured() ) /* wait */ ;
292
293         // Wait an extra second for the PC's operating system to load drivers
294         // and do whatever it does to actually be ready for input
295         _delay_ms(1000);
296
297         // Make sure variables are properly initialized
298         curDetect.keyDetectCount = 0;
299         curDetect.modifiers = 0;
300
301         // Main Detection Loop
302         // XXX Change number of ORDs if number of lines differ
303         for ( int group = 1;;group++ ) {
304                 // Determine which keys are being pressed
305                 switch ( group ) {
306                         /*
307                         DD_CASE_ORD(1)
308                         DD_CASE_ORD(2)
309                         DD_CASE_ORD(3)
310                         DD_CASE_ORD(4)
311                         DD_CASE_ORD(5)
312                         DD_CASE_ORD(6)
313                         DD_CASE_ORD(7)
314                         DD_CASE_ORD(8)
315                         */
316                         DD_CASE_END(9,group)
317                 }
318
319                 if ( group != -1 )
320                         continue;
321
322                 // After going through each of the key groups, send the detected keys and modifiers
323                 // Currently limited to the USB spec (6 keys + modifiers)
324                 // Making sure to pass zeros when there are no keys being pressed
325                 for ( int c = 0; c < 6 && c < curDetect.keyDetectCount; c++ )
326                         keyboard_keys[c] = c < curDetect.keyDetectCount ? curDetect.keyDetectArray[c] : 0;
327
328                 // Modifiers
329                 keyboard_modifier_keys = curDetect.modifiers;
330
331                 // Send keypresses
332                 usb_keyboard_send();
333
334                 // Cleanup
335                 curDetect.keyDetectCount = 0;
336                 curDetect.modifiers = 0;
337         }
338
339         // usb_keyboard_press(KEY_B, KEY_SHIFT);
340         return 0;
341 }
342