1 /* Copyright (C) 2012,2014 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
22 // ----- Includes -----
25 #include <Lib/ScanLib.h>
32 #include "scan_loop.h"
36 // ----- Defines -----
40 // Scan Bit Pins (from keyboard)
41 // - Reads in the ASCII scancode
42 // - Shift and ShiftLock are handled internally
43 #define READSCAN_PORT PORTC
44 #define READSCAN_DDR DDRC
45 #define READSCAN_PIN PINC
48 // Interrupt Pins (from keyboard)
49 // - CODEINT (Code key signal interrupt/press)
50 // - Normally high, low when "Code" key is pressed; separate from all other key presses
51 // - PRESSINT (Press signal interrupt/press)
52 // - Normally high, low when any key (or multiple except "Code") is pressed, returns to high once all keys are released
53 // - Signal is changed BEFORE the Scan Bits are updated
54 // - PULSEINT (Key action pulse interrupt/press)
55 // - Normally high, low pulses of 147us on key presses (depending on the combination of mode control pins)
56 // - Pulse is guarranteed to sent after the Scan Bits are updated
57 #define CODEINT_PORT PORTE
58 #define CODEINT_DDR DDRE
59 #define CODEINT_PIN PINE
62 #define PRESSINT_PORT PORTE
63 #define PRESSINT_DDR DDRE
64 #define PRESSINT_POS 6
66 #define PULSEINT_PORT PORTD
67 #define PULSEINT_DDR DDRD
68 #define PULSEINT_POS 3
71 // LED Pins (to keyboard)
74 #define LED1_PORT PORTF // [Pin 19]
78 #define LED2_PORT PORTF // [Pin 20]
83 // Mode Control Pins (to keyboard)
85 // - 1 Single pulse mode (PULSEINT)
86 // - 0 Repeated pulse mode (PULSEINT) (1 pulse, pause, then constant pulses)
88 // - 1 1KRO mode (typewriter compatibility mode)
89 // - 0 NKRO mode (new pulse on each keypress - PULSEINT)
91 // - 1 disables pulse interrupt (PULSEINT)
92 // - 0 enables pulse interrupt (PULSEINT)
93 #define REPEAT_PORT PORTF
94 #define REPEAT_DDR DDRF
97 #define MULTI_PORT PORTF
98 #define MULTI_DDR DDRF
101 #define SIGNAL_PORT PORTF
102 #define SIGNAL_DDR DDRF
106 // Manually Scanned Keys
107 // Keys that the controller screws up, requiring a separate wire to be brought to the controller
108 // Note: Safer to route these through a NOT gate to boost the signal strength
109 // Values below are AFTER NOT gate
110 // - Shift (both shift keys are on the same scan line)
116 #define MANUAL_SCAN_KEYS 2
119 #define SHIFT_PORT PORTF
120 #define SHIFT_DDR DDRF
121 #define SHIFT_PIN PINF
124 #define SHIFTLOCK_KEY 1
125 #define SHIFTLOCK_PORT PORTF
126 #define SHIFTLOCK_DDR DDRF
127 #define SHIFTLOCK_PIN PINF
128 #define SHIFTLOCK_POS 1
131 // ----- Macros -----
135 // ----- Variables -----
137 // Buffer used to inform the macro processing module which keys have been detected as pressed
138 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
139 volatile uint8_t KeyIndex_BufferUsed;
140 volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
142 volatile uint8_t KeyScan_Table[MANUAL_SCAN_KEYS]; // Used for tracking key status of manually scanned keys
143 volatile uint8_t KeyScan_Prev [MANUAL_SCAN_KEYS]; // Keeps track of key state changes
144 volatile uint8_t KeyScan_Count;
148 // ----- Functions -----
151 void processKeyValue( uint8_t keyValue );
156 inline void Scan_setup()
158 // Setup the external interrupts for
159 // - General keypresses (INT6/E6) -> rising edge (to detect key release)
160 // - "Code" key (INT7/E7) -> falling/rising edge (to detect key press/release)
161 // - General keypress pulse (INT3/D3) -> falling edge (to detect key press )
167 // Setup Interrupt Pins
168 CODEINT_PORT |= (1 << CODEINT_POS );
169 CODEINT_DDR &= ~(1 << CODEINT_POS );
171 PRESSINT_PORT |= (1 << PRESSINT_POS);
172 PRESSINT_DDR &= ~(1 << PRESSINT_POS);
174 PULSEINT_PORT |= (1 << PULSEINT_POS);
175 PULSEINT_DDR &= ~(1 << PULSEINT_POS);
178 // Setup LED Pins (default off)
179 LED1_PORT |= (1 << LED1_POS);
180 LED1_DDR |= (1 << LED1_POS);
182 LED2_PORT |= (1 << LED2_POS);
183 LED2_DDR |= (1 << LED2_POS);
186 // Setup READSCAN pins to read out scancode
187 READSCAN_PORT = 0xFF;
191 // Setup Mode Control Pins
192 // Note: These can be changed at any time, but there is no real reason too for a USB converter
193 REPEAT_PORT |= (1 << REPEAT_POS); // Setting high for single press mode
194 REPEAT_DDR |= (1 << REPEAT_POS);
196 MULTI_PORT &= ~(1 << MULTI_POS ); // Setting low for multi press mode (NKRO)
197 MULTI_DDR |= (1 << MULTI_POS );
199 SIGNAL_PORT &= ~(1 << SIGNAL_POS); // Setting low to enable PULSEINT
200 SIGNAL_DDR |= (1 << SIGNAL_POS);
203 // Setup Troublesome Key Pins
204 SHIFT_PORT &= ~(1 << SHIFT_POS );
205 SHIFT_DDR &= ~(1 << SHIFT_POS );
207 SHIFTLOCK_PORT &= ~(1 << SHIFTLOCK_POS);
208 SHIFTLOCK_DDR &= ~(1 << SHIFTLOCK_POS);
211 // Reset the keyboard before scanning, we might be in a wierd state
212 scan_resetKeyboard();
215 // Main Detection Loop
216 // Not needed for the Sony OA-S3400 as signals are interrupt based, thus this is a busy loop
217 // XXX Function is used for scanning troublesome keys, technically this is not needed for a pure converter
218 // I just want proper use of the shift and shift lock keys, without having to do major rework to attach to the entire matrix
219 inline uint8_t Scan_loop()
221 // Loop through known keys
222 for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ ) switch ( key )
225 if ( SHIFT_PIN & (1 << SHIFT_POS) )
227 KeyScan_Table[SHIFT_KEY]++;
231 if ( SHIFTLOCK_PIN & (1 << SHIFTLOCK_POS) )
233 KeyScan_Table[SHIFTLOCK_KEY]++;
237 erro_print("Invalid key scan index");
241 // Increment vote instance
244 // Loop function again if not enough votes have been tallied
245 if ( KeyScan_Count < 255 )
251 // Loop through known keys
252 for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ )
254 // Key scanned as pressed (might have been held from a previous vote)
255 if ( KeyScan_Table[key] > 127 )
258 if ( !KeyScan_Prev[key] )
260 processKeyValue( 0x90 + key ); // Arbitrary key mapping starts at 0x90
261 KeyScan_Prev[key] = 1;
264 // Key scanned as released
268 if ( KeyScan_Prev[key] )
270 processKeyValue( 0xA0 + key ); // Arbitrary key mapping release starts at 0xA0
271 KeyScan_Prev[key] = 0;
276 KeyScan_Table[key] = 0;
279 // End loop, process macros and USB data
283 void processKeyValue( uint8_t keyValue )
285 // - Convert Shifted Value to non-shifted ASCII code -
288 if ( keyValue >= 0x61 && keyValue <= 0x7A )
292 // Other keys with ASCII shift codes
338 // TODO Move to Macro Section
342 scan_sendData( 0x01 );
345 scan_sendData( 0x02 );
349 // Scan code is now finalized, and ready to add to buffer
350 // Note: Scan codes come from 3 different interrupts and a manual key scan into this function
354 hexToStr( keyValue, tmpStr );
355 dPrintStrs( tmpStr, " " );
357 // Detect release condition
373 // Check for the released key, and shift the other keys lower on the buffer
375 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
377 // General key buffer clear
378 if ( keyValue == 0xB0 )
380 switch ( KeyIndex_Buffer[c] )
382 // Ignore these keys on general key release (have their own release codes)
388 // Remove key from buffer
390 // Shift keys from c position
391 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
392 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
395 KeyIndex_BufferUsed--;
397 // Start at this position again for the next loop
403 // Key to release found
404 else if ( KeyIndex_Buffer[c] == keyValue )
406 // Shift keys from c position
407 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
408 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
411 KeyIndex_BufferUsed--;
417 // Error case (no key to release)
418 if ( c == KeyIndex_BufferUsed + 1 )
422 hexToStr( keyValue, tmpStr );
423 erro_dPrint( "Could not find key to release: ", tmpStr );
426 // Press or Repeated Key
429 // Make sure the key isn't already in the buffer
430 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
432 // Key isn't in the buffer yet
433 if ( c == KeyIndex_BufferUsed )
435 Macro_bufferAdd( keyValue );
439 // Key already in the buffer
440 if ( KeyIndex_Buffer[c] == keyValue )
446 // Key Press Detected Interrupt
449 cli(); // Disable Interrupts
451 uint8_t keyValue = 0x00;
453 // Bits are flipped coming in from the keyboard
454 keyValue = ~READSCAN_PIN;
456 // Process the scancode
457 processKeyValue( keyValue );
459 sei(); // Re-enable Interrupts
462 // Key Release Detected Interrupt
465 cli(); // Disable Interrupts
467 // Send release code for general keys, 0xB0
468 processKeyValue( 0xB0 );
470 sei(); // Re-enable Interrupts
473 // Code Key Interrupt
476 cli(); // Disable Interrupts
478 // Code Key Released (send scancode)
479 if ( CODEINT_PIN & (1 << CODEINT_POS) )
481 processKeyValue( 0xA2 );
483 // Code Key Pressed (send scancode)
486 processKeyValue( 0x92 );
489 sei(); // Re-enable Interrupts
493 // Send data to keyboard
494 // Sony OA-S3400 has no serial/parallel dataport to send data too...
495 // Using this function for LED enable/disable
496 uint8_t Scan_sendData( uint8_t dataPayload )
498 switch ( dataPayload )
501 LED1_PORT ^= (1 << LED1_POS);
504 LED2_PORT ^= (1 << LED2_POS);
507 erro_print("Invalid data send attempt");
513 // Signal KeyIndex_Buffer that it has been properly read
514 // Not needed as a signal is sent to remove key-presses
515 void Scan_finishedWithBuffer( uint8_t sentKeys )
520 // Reset/Hold keyboard
521 // Sony OA-S3400 has no locking signals
522 void Scan_lockKeyboard( void )
526 void Scan_unlockKeyboard( void )
531 void Scan_resetKeyboard( void )
533 // Empty buffer, now that keyboard has been reset
534 KeyIndex_BufferUsed = 0;
536 // Clear the KeyScan table and count
537 for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ )
539 KeyScan_Table[key] = 0;
540 KeyScan_Prev [key] = 0;
545 // USB module is finished with buffer
546 // Not needed as a signal is sent to remove key-presses
547 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )