1 /* Copyright (C) 2013 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 -----
41 // Make sure we haven't overflowed the buffer
42 #define bufferAdd(byte) \
43 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
44 KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
48 // ----- Variables -----
50 // Buffer used to inform the macro processing module which keys have been detected as pressed
51 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
52 volatile uint8_t KeyIndex_BufferUsed;
54 volatile uint8_t KeyBufferRemove[6];
55 volatile uint8_t KeyBufferRemoveCount = 0;
57 static uint8_t KeyBuffer[3];
58 volatile static uint8_t KeyBufferCount = 0;
62 // ----- Function Declarations -----
64 void processKeyValue( uint8_t valueType, uint8_t keyValue );
65 void removeKeyValue( uint8_t keyValue );
69 // ----- Interrupt Functions -----
71 // USART Receive Buffer Full Interrupt
74 cli(); // Disable Interrupts
76 // Read part of the scan code (3 8bit chunks) from USART
77 KeyBuffer[KeyBufferCount] = UDR1;
79 if ( KeyBufferCount >= 2 )
82 for ( uint8_t c = 0; c <= 2; c++ )
86 hexToStr( KeyBuffer[c], tmpStr );
87 dPrintStrs( tmpStr, " " ); // Debug
91 processKeyValue( KeyBuffer[1], KeyBuffer[2] );
100 sei(); // Re-enable Interrupts
105 // ----- Functions -----
108 inline void scan_setup()
110 // Setup the the USART interface for keyboard data input
113 // 16 MHz / ( 16 * Baud ) = UBRR
114 // Baud: 4817 -> 16 MHz / ( 16 * 4817 ) = 207.5981
115 // Thus baud setting = 208
116 uint16_t baud = 208; // Max setting of 4095
117 UBRR1H = (uint8_t)(baud >> 8);
118 UBRR1L = (uint8_t)baud;
120 // Enable the receiver, transmitter, and RX Complete Interrupt
123 // Set frame format: 8 data, 1 stop bit, odd parity
124 // Asynchrounous USART mode
127 // Reset the keyboard before scanning, we might be in a wierd state
128 scan_resetKeyboard();
132 // Main Detection Loop
133 inline uint8_t scan_loop()
135 // Remove any "released keys", this is delayed due to buffer release synchronization issues
136 for ( uint8_t c = 0; c < KeyBufferRemoveCount; c++ )
138 removeKeyValue( KeyBufferRemove[c] );
141 KeyBufferRemoveCount = 0;
146 void processKeyValue( uint8_t valueType, uint8_t keyValue )
153 // Repeated Key Press
156 // Modifier Key Release
158 KeyBufferRemove[KeyBufferRemoveCount++] = keyValue;
162 // Make sure the key isn't already in the buffer
163 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
165 // Key isn't in the buffer yet
166 if ( c == KeyIndex_BufferUsed )
168 bufferAdd( keyValue );
172 // Key already in the buffer
173 if ( KeyIndex_Buffer[c] == keyValue )
178 void removeKeyValue( uint8_t keyValue )
180 // Check for the released key, and shift the other keys lower on the buffer
182 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
184 // Key to release found
185 if ( KeyIndex_Buffer[c] == keyValue )
187 // Shift keys from c position
188 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
189 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
192 KeyIndex_BufferUsed--;
198 // Error case (no key to release)
199 if ( c == KeyIndex_BufferUsed + 1 )
203 hexToStr( keyValue, tmpStr );
204 erro_dPrint( "Could not find key to release: ", tmpStr );
209 uint8_t scan_sendData( uint8_t dataPayload )
213 hexToStr( dataPayload, tmpStr );
214 info_dPrint( "Sending - ", tmpStr );
221 // Signal KeyIndex_Buffer that it has been properly read
222 void scan_finishedWithBuffer( uint8_t sentKeys )
224 // Make sure we aren't in the middle of a receiving a new scancode
225 while ( KeyBufferCount != 0 );
227 cli(); // Disable Interrupts
229 // Count for number of modifiers to maintain in the buffer
232 uint8_t latchBuffer[13]; // There are only 13 keys that can possibly be latched at the same time...
234 uint8_t prevBuffer = KeyIndex_BufferUsed;
236 // Clean out all keys except "special" keys (designated modifiers)
238 for ( key = 0; key < sentKeys; key++ )
240 switch ( KeyIndex_Buffer[key] )
242 // Dedicated Modifier Keys
243 // NOTE: Both shifts are represented as the same scan code
247 KeyIndex_Buffer[filled++] = KeyIndex_Buffer[key];
249 // Latched Keys, only released if a non-modifier is pressed along with it
250 // NOTE: This keys do not have a built in repeating
254 //case 0x0B: // XXX Being used as an alternate Enter, since it is labelled as such
263 //case 0x41: // XXX Being used as ESC
264 latchBuffer[latched++] = KeyIndex_Buffer[key];
266 // Allow the scancode to be removed, normal keys
273 // Reset the buffer counter
274 KeyIndex_BufferUsed = filled;
276 // Add back lost keys, so they are processed on the next USB send
277 for ( ; key < prevBuffer; key++ )
279 bufferAdd( KeyIndex_Buffer[key] );
280 info_print("Re-appending lost key after USB send...");
283 // Only "re-add" the latched keys if they weren't used
284 if ( latched > 0 && normal == 0 )
286 for ( uint8_t c = 0; c < latched; c++ )
288 bufferAdd( latchBuffer[c] );
292 sei(); // Re-enable Interrupts
295 // Signal that the keys have been properly sent over USB
296 void scan_finishedWithUSBBuffer( uint8_t sentKeys )
300 // Reset/Hold keyboard
301 // NOTE: Does nothing with the FACOM6684
302 void scan_lockKeyboard( void )
306 // NOTE: Does nothing with the FACOM6684
307 void scan_unlockKeyboard( void )
312 void scan_resetKeyboard( void )
314 // Not a calculated valued...
318 KeyBufferRemoveCount = 0;
319 KeyIndex_BufferUsed = 0;