1 /* Copyright (C) 2011 by Jacob Alexander
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
23 #include <avr/pgmspace.h>
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 //#include "usb_keyboard.h"
29 #include "usb_keyboard_debug.h"
32 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
34 // Sleep defined in milliseconds
35 #define PRE_DRIVE_SLEEP 50
36 #define POST_DRIVE_SLEEP 50
39 #define DRIVE_reg_1 PORTB
40 #define DRIVE_reg_2 PORTB
41 #define DRIVE_reg_3 PORTB
42 #define DRIVE_reg_4 PORTC
43 #define DRIVE_reg_5 PORTE
44 #define DRIVE_reg_6 PORTE
45 #define DRIVE_reg_7 PORTF
46 #define DRIVE_reg_8 PORTF
47 #define DRIVE_reg_9 PORTF
48 #define DRIVE_reg_10 <blank>
49 #define DRIVE_reg_11 <blank>
50 #define DRIVE_reg_12 <blank>
61 #define DRIVE_pin_10 <blank>
62 #define DRIVE_pin_11 <blank>
63 #define DRIVE_pin_12 <blank>
65 #define DETECT_group_1 1
66 #define DETECT_group_2 2
67 #define DETECT_group_3 3
68 #define DETECT_group_4 4
69 #define DETECT_group_5 5
70 #define DETECT_group_6 6
71 #define DETECT_group_7 7
72 #define DETECT_group_8 8
73 #define DETECT_group_9 9
74 #define DETECT_group_10 <blank>
75 #define DETECT_group_11 <blank>
76 #define DETECT_group_12 <blank>
78 #define DETECT_group_size_1 7
79 #define DETECT_group_size_2 8
80 #define DETECT_group_size_3 8
81 #define DETECT_group_size_4 4
82 #define DETECT_group_size_5 8
83 #define DETECT_group_size_6 7
84 #define DETECT_group_size_7 7
85 #define DETECT_group_size_8 6
86 #define DETECT_group_size_9 7
87 #define DETECT_group_size_10 <blank>
88 #define DETECT_group_size_11 <blank>
89 #define DETECT_group_size_12 <blank>
91 #define DETECT_group_array_1 {{KEY_SLASH,KEY_RIGHT_BRACE,KEY_ENTER,KEY_D,KEY_2,KEY_Q,KEY_C},{0,0,0,0,0,0,0}}
92 #define DETECT_group_array_2 {{KEY_TILDE,KEY_DELETE,KEY_LEFT,KEY_SPACE,KEY_X,KEY_S,KEY_TAB,KEY_1},{0,0,0,0,0,0,0,0}}
93 #define DETECT_group_array_3 {{KEY_BACKSPACE,KEY_UP,KEY_DOWN,KEY_A,KEY_INSERT,KEY_ALT,KEY_Z,KEY_RIGHT},{0,0,0,0,0,1,0,0}}
94 #define DETECT_group_array_4 {{KEY_ESC ,KEY_CTRL,KEY_CAPS_LOCK,KEY_SHIFT} ,{0,1,0,1}}
95 #define DETECT_group_array_5 0
96 #define DETECT_group_array_6 0
97 #define DETECT_group_array_7 {{KEY_L ,KEY_O ,KEY_0 ,KEY_N ,KEY_H,KEY_R,KEY_5},{0,0,0,0,0,0,0}}
98 #define DETECT_group_array_8 0
99 #define DETECT_group_array_9 0
100 #define DETECT_group_array_10 <blank>
101 #define DETECT_group_array_11 <blank>
102 #define DETECT_group_array_12 <blank>
110 // XXX Change number of ORDs if number of lines differ
112 for ( int c = 1;; c++ ) { \
121 #define DRIVE_DETECT(reg,pin,group) \
122 reg &= ~(1 << pin); \
125 _delay_ms(POST_DRIVE_SLEEP);
127 #define DD_CASE(number) \
129 DRIVE_DETECT(DRIVE_reg##_##number, DRIVE_pin##_##number, DETECT_group##_##number)
131 #define DD_CASE_ORD(number) \
135 #define DD_CASE_END(number,var) \
140 // Determine if key is either normal or a modifier
141 #define DET_GROUP_CHECK(index) \
143 if ( groupArray[1][index] ) \
144 curDetect.modifiers |= groupArray[0][index]; \
146 curDetect.keyDetectArray[curDetect.keyDetectCount++] = groupArray[0][index]; \
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
153 // Used for 1 detection group
154 #define DET_GROUP_1 \
155 if ( !( PINB & (1 << 3) ) ) \
157 if ( !( PINF & (1 << 1) ) ) \
159 if ( !( PINF & (1 << 2) ) ) \
161 if ( !( PINF & (1 << 3) ) ) \
164 // Used for 4 detection groups
165 #define DET_GROUP_2 \
166 if ( !( PINC & (1 << 0) ) ) \
168 if ( !( PINC & (1 << 1) ) ) \
170 if ( !( PINC & (1 << 2) ) ) \
172 if ( !( PINC & (1 << 3) ) ) \
174 if ( !( PINC & (1 << 4) ) ) \
176 if ( !( PINC & (1 << 5) ) ) \
178 if ( !( PINC & (1 << 6) ) ) \
181 // Used for 1 detection group
182 #define DET_GROUP_3 \
183 if ( !( PINC & (1 << 0) ) ) \
185 if ( !( PINC & (1 << 1) ) ) \
187 if ( !( PINC & (1 << 2) ) ) \
189 if ( !( PINC & (1 << 4) ) ) \
191 if ( !( PINC & (1 << 5) ) ) \
193 if ( !( PINC & (1 << 6) ) ) \
196 // Used for 3 detection groups
197 #define DET_GROUP_4 \
198 if ( !( PINC & (1 << 0) ) ) \
200 if ( !( PINC & (1 << 1) ) ) \
202 if ( !( PINC & (1 << 2) ) ) \
204 if ( !( PINC & (1 << 3) ) ) \
206 if ( !( PINC & (1 << 4) ) ) \
208 if ( !( PINC & (1 << 5) ) ) \
210 if ( !( PINC & (1 << 6) ) ) \
212 if ( !( PINE & (1 << 1) ) ) \
215 // Combines the DET_GROUP_Xs above for the given groupArray
216 #define DET_GROUP(group,det_group) \
219 uint8_t groupArray[2][DETECT_group_size##_##group] = DETECT_group_array##_##group; \
220 DET_GROUP##_##det_group \
225 uint8_t keyDetectCount;
226 uint8_t keyDetectArray[40];
228 } curDetect, prevDetect;
230 void detection( int group )
232 _delay_ms(PRE_DRIVE_SLEEP);
233 curDetect.keyDetectCount = 0;
234 curDetect.modifiers = 0;
236 // XXX Modify for different detection groups <-> groupArray mappings
250 // Print out the current keys pressed
251 if ( curDetect.keyDetectCount > 0 ) {
253 for ( int c = 0; c < curDetect.keyDetectCount; c++ ) {
254 phex( curDetect.keyDetectArray[c] );
259 if ( curDetect.modifiers ) {
260 print("Modifiers: ");
261 phex( curDetect.modifiers );
268 // XXX This part is configurable
271 // For each pin, 0=input, 1=output
279 // Setting pins to either high or pull-up resistor
290 // set for 16 MHz clock
296 // Initialize the USB, and then wait for the host to set configuration.
297 // If the Teensy is powered without a PC connected to the USB port,
298 // this will wait forever.
300 while ( !usb_configured() ) /* wait */ ;
302 // Wait an extra second for the PC's operating system to load drivers
303 // and do whatever it does to actually be ready for input
306 // Main Detection Loop
309 // usb_keyboard_press(KEY_B, KEY_SHIFT);