1 /* Copyright (C) 2012 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 -----
133 // Make sure we haven't overflowed the buffer
134 #define bufferAdd(byte) \
135 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
136 KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
140 // ----- Variables -----
142 // Buffer used to inform the macro processing module which keys have been detected as pressed
143 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
144 volatile uint8_t KeyIndex_BufferUsed;
145 volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
147 volatile uint8_t KeyScan_Table[MANUAL_SCAN_KEYS]; // Used for tracking key status of manually scanned keys
148 volatile uint8_t KeyScan_Prev [MANUAL_SCAN_KEYS]; // Keeps track of key state changes
149 volatile uint8_t KeyScan_Count;
153 // ----- Functions -----
156 void processKeyValue( uint8_t keyValue );
161 inline void scan_setup()
163 // Setup the external interrupts for
164 // - General keypresses (INT6/E6) -> rising edge (to detect key release)
165 // - "Code" key (INT7/E7) -> falling/rising edge (to detect key press/release)
166 // - General keypress pulse (INT3/D3) -> falling edge (to detect key press )
172 // Setup Interrupt Pins
173 CODEINT_PORT |= (1 << CODEINT_POS );
174 CODEINT_DDR &= ~(1 << CODEINT_POS );
176 PRESSINT_PORT |= (1 << PRESSINT_POS);
177 PRESSINT_DDR &= ~(1 << PRESSINT_POS);
179 PULSEINT_PORT |= (1 << PULSEINT_POS);
180 PULSEINT_DDR &= ~(1 << PULSEINT_POS);
183 // Setup LED Pins (default off)
184 LED1_PORT |= (1 << LED1_POS);
185 LED1_DDR |= (1 << LED1_POS);
187 LED2_PORT |= (1 << LED2_POS);
188 LED2_DDR |= (1 << LED2_POS);
191 // Setup READSCAN pins to read out scancode
192 READSCAN_PORT = 0xFF;
196 // Setup Mode Control Pins
197 // Note: These can be changed at any time, but there is no real reason too for a USB converter
198 REPEAT_PORT |= (1 << REPEAT_POS); // Setting high for single press mode
199 REPEAT_DDR |= (1 << REPEAT_POS);
201 MULTI_PORT &= ~(1 << MULTI_POS ); // Setting low for multi press mode (NKRO)
202 MULTI_DDR |= (1 << MULTI_POS );
204 SIGNAL_PORT &= ~(1 << SIGNAL_POS); // Setting low to enable PULSEINT
205 SIGNAL_DDR |= (1 << SIGNAL_POS);
208 // Setup Troublesome Key Pins
209 SHIFT_PORT &= ~(1 << SHIFT_POS );
210 SHIFT_DDR &= ~(1 << SHIFT_POS );
212 SHIFTLOCK_PORT &= ~(1 << SHIFTLOCK_POS);
213 SHIFTLOCK_DDR &= ~(1 << SHIFTLOCK_POS);
216 // Reset the keyboard before scanning, we might be in a wierd state
217 scan_resetKeyboard();
220 // Main Detection Loop
221 // Not needed for the Sony OA-S3400 as signals are interrupt based, thus this is a busy loop
222 // XXX Function is used for scanning troublesome keys, technically this is not needed for a pure converter
223 // I just want proper use of the shift and shift lock keys, without having to do major rework to attach to the entire matrix
224 inline uint8_t scan_loop()
226 // Loop through known keys
227 for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ ) switch ( key )
230 if ( SHIFT_PIN & (1 << SHIFT_POS) )
232 KeyScan_Table[SHIFT_KEY]++;
236 if ( SHIFTLOCK_PIN & (1 << SHIFTLOCK_POS) )
238 KeyScan_Table[SHIFTLOCK_KEY]++;
242 erro_print("Invalid key scan index");
246 // Increment vote instance
249 // Loop function again if not enough votes have been tallied
250 if ( KeyScan_Count < 255 )
256 // Loop through known keys
257 for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ )
259 // Key scanned as pressed (might have been held from a previous vote)
260 if ( KeyScan_Table[key] > 127 )
263 if ( !KeyScan_Prev[key] )
265 processKeyValue( 0x90 + key ); // Arbitrary key mapping starts at 0x90
266 KeyScan_Prev[key] = 1;
269 // Key scanned as released
273 if ( KeyScan_Prev[key] )
275 processKeyValue( 0xA0 + key ); // Arbitrary key mapping release starts at 0xA0
276 KeyScan_Prev[key] = 0;
281 KeyScan_Table[key] = 0;
284 // End loop, process macros and USB data
288 void processKeyValue( uint8_t keyValue )
290 // - Convert Shifted Value to non-shifted ASCII code -
293 if ( keyValue >= 0x61 && keyValue <= 0x7A )
297 // Other keys with ASCII shift codes
343 // TODO Move to Macro Section
347 scan_sendData( 0x01 );
350 scan_sendData( 0x02 );
354 // Scan code is now finalized, and ready to add to buffer
355 // Note: Scan codes come from 3 different interrupts and a manual key scan into this function
359 hexToStr( keyValue, tmpStr );
360 dPrintStrs( tmpStr, " " );
362 // Detect release condition
378 // Check for the released key, and shift the other keys lower on the buffer
380 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
382 // General key buffer clear
383 if ( keyValue == 0xB0 )
385 switch ( KeyIndex_Buffer[c] )
387 // Ignore these keys on general key release (have their own release codes)
393 // Remove key from buffer
395 // Shift keys from c position
396 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
397 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
400 KeyIndex_BufferUsed--;
402 // Start at this position again for the next loop
408 // Key to release found
409 else if ( KeyIndex_Buffer[c] == keyValue )
411 // Shift keys from c position
412 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
413 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
416 KeyIndex_BufferUsed--;
422 // Error case (no key to release)
423 if ( c == KeyIndex_BufferUsed + 1 )
427 hexToStr( keyValue, tmpStr );
428 erro_dPrint( "Could not find key to release: ", tmpStr );
431 // Press or Repeated Key
434 // Make sure the key isn't already in the buffer
435 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
437 // Key isn't in the buffer yet
438 if ( c == KeyIndex_BufferUsed )
440 bufferAdd( keyValue );
444 // Key already in the buffer
445 if ( KeyIndex_Buffer[c] == keyValue )
451 // Key Press Detected Interrupt
454 cli(); // Disable Interrupts
456 uint8_t keyValue = 0x00;
458 // Bits are flipped coming in from the keyboard
459 keyValue = ~READSCAN_PIN;
461 // Process the scancode
462 processKeyValue( keyValue );
464 sei(); // Re-enable Interrupts
467 // Key Release Detected Interrupt
470 cli(); // Disable Interrupts
472 // Send release code for general keys, 0xB0
473 processKeyValue( 0xB0 );
475 sei(); // Re-enable Interrupts
478 // Code Key Interrupt
481 cli(); // Disable Interrupts
483 // Code Key Released (send scancode)
484 if ( CODEINT_PIN & (1 << CODEINT_POS) )
486 processKeyValue( 0xA2 );
488 // Code Key Pressed (send scancode)
491 processKeyValue( 0x92 );
494 sei(); // Re-enable Interrupts
498 // Send data to keyboard
499 // Sony OA-S3400 has no serial/parallel dataport to send data too...
500 // Using this function for LED enable/disable
501 uint8_t scan_sendData( uint8_t dataPayload )
503 switch ( dataPayload )
506 LED1_PORT ^= (1 << LED1_POS);
509 LED2_PORT ^= (1 << LED2_POS);
512 erro_print("Invalid data send attempt");
518 // Signal KeyIndex_Buffer that it has been properly read
519 // Not needed as a signal is sent to remove key-presses
520 void scan_finishedWithBuffer( uint8_t sentKeys )
525 // Reset/Hold keyboard
526 // Sony OA-S3400 has no locking signals
527 void scan_lockKeyboard( void )
531 void scan_unlockKeyboard( void )
536 void scan_resetKeyboard( void )
538 // Empty buffer, now that keyboard has been reset
539 KeyIndex_BufferUsed = 0;
541 // Clear the KeyScan table and count
542 for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ )
544 KeyScan_Table[key] = 0;
545 KeyScan_Prev [key] = 0;
550 // USB module is finished with buffer
551 // Not needed as a signal is sent to remove key-presses
552 void scan_finishedWithUSBBuffer( uint8_t sentKeys )