1 /* Copyright (C) 2013-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 -----
43 // ----- Variables -----
45 // Buffer used to inform the macro processing module which keys have been detected as pressed
46 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
47 volatile uint8_t KeyIndex_BufferUsed;
49 volatile uint8_t KeyBufferRemove[6];
50 volatile uint8_t KeyBufferRemoveCount = 0;
52 static uint8_t KeyBuffer[3];
53 volatile static uint8_t KeyBufferCount = 0;
57 // ----- Function Declarations -----
59 void processKeyValue( uint8_t valueType, uint8_t keyValue );
60 void removeKeyValue( uint8_t keyValue );
64 // ----- Interrupt Functions -----
66 // USART Receive Buffer Full Interrupt
69 cli(); // Disable Interrupts
71 // Read part of the scan code (3 8bit chunks) from USART
72 KeyBuffer[KeyBufferCount] = UDR1;
74 if ( KeyBufferCount >= 2 )
77 for ( uint8_t c = 0; c <= 2; c++ )
81 hexToStr( KeyBuffer[c], tmpStr );
82 dPrintStrs( tmpStr, " " ); // Debug
86 processKeyValue( KeyBuffer[1], KeyBuffer[2] );
95 sei(); // Re-enable Interrupts
100 // ----- Functions -----
103 inline void Scan_setup()
105 // Setup the the USART interface for keyboard data input
108 // 16 MHz / ( 16 * Baud ) = UBRR
109 // Baud: 4817 -> 16 MHz / ( 16 * 4817 ) = 207.5981
110 // Thus baud setting = 208
111 uint16_t baud = 208; // Max setting of 4095
112 UBRR1H = (uint8_t)(baud >> 8);
113 UBRR1L = (uint8_t)baud;
115 // Enable the receiver, transmitter, and RX Complete Interrupt
118 // Set frame format: 8 data, 1 stop bit, odd parity
119 // Asynchrounous USART mode
122 // Reset the keyboard before scanning, we might be in a wierd state
123 scan_resetKeyboard();
127 // Main Detection Loop
128 inline uint8_t Scan_loop()
130 // Remove any "released keys", this is delayed due to buffer release synchronization issues
131 for ( uint8_t c = 0; c < KeyBufferRemoveCount; c++ )
133 removeKeyValue( KeyBufferRemove[c] );
136 KeyBufferRemoveCount = 0;
141 void processKeyValue( uint8_t valueType, uint8_t keyValue )
148 // Repeated Key Press
151 // Modifier Key Release
153 KeyBufferRemove[KeyBufferRemoveCount++] = keyValue;
157 // Make sure the key isn't already in the buffer
158 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
160 // Key isn't in the buffer yet
161 if ( c == KeyIndex_BufferUsed )
163 Macro_bufferAdd( keyValue );
167 // Key already in the buffer
168 if ( KeyIndex_Buffer[c] == keyValue )
173 void removeKeyValue( uint8_t keyValue )
175 // Check for the released key, and shift the other keys lower on the buffer
177 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
179 // Key to release found
180 if ( KeyIndex_Buffer[c] == keyValue )
182 // Shift keys from c position
183 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
184 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
187 KeyIndex_BufferUsed--;
193 // Error case (no key to release)
194 if ( c == KeyIndex_BufferUsed + 1 )
198 hexToStr( keyValue, tmpStr );
199 erro_dPrint( "Could not find key to release: ", tmpStr );
204 uint8_t Scan_sendData( uint8_t dataPayload )
208 hexToStr( dataPayload, tmpStr );
209 info_dPrint( "Sending - ", tmpStr );
216 // Signal KeyIndex_Buffer that it has been properly read
217 void Scan_finishedWithBuffer( uint8_t sentKeys )
219 // Make sure we aren't in the middle of a receiving a new scancode
220 while ( KeyBufferCount != 0 );
222 cli(); // Disable Interrupts
224 // Count for number of modifiers to maintain in the buffer
227 uint8_t latchBuffer[13]; // There are only 13 keys that can possibly be latched at the same time...
229 uint8_t prevBuffer = KeyIndex_BufferUsed;
231 // Clean out all keys except "special" keys (designated modifiers)
233 for ( key = 0; key < sentKeys; key++ )
235 switch ( KeyIndex_Buffer[key] )
237 // Dedicated Modifier Keys
238 // NOTE: Both shifts are represented as the same scan code
242 KeyIndex_Buffer[filled++] = KeyIndex_Buffer[key];
244 // Latched Keys, only released if a non-modifier is pressed along with it
245 // NOTE: This keys do not have a built in repeating
249 //case 0x0B: // XXX Being used as an alternate Enter, since it is labelled as such
258 //case 0x41: // XXX Being used as ESC
259 latchBuffer[latched++] = KeyIndex_Buffer[key];
261 // Allow the scancode to be removed, normal keys
268 // Reset the buffer counter
269 KeyIndex_BufferUsed = filled;
271 // Add back lost keys, so they are processed on the next USB send
272 for ( ; key < prevBuffer; key++ )
274 Macro_bufferAdd( KeyIndex_Buffer[key] );
275 info_print("Re-appending lost key after USB send...");
278 // Only "re-add" the latched keys if they weren't used
279 if ( latched > 0 && normal == 0 )
281 for ( uint8_t c = 0; c < latched; c++ )
283 Macro_bufferAdd( latchBuffer[c] );
287 sei(); // Re-enable Interrupts
290 // Signal that the keys have been properly sent over USB
291 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
295 // Reset/Hold keyboard
296 // NOTE: Does nothing with the FACOM6684
297 void Scan_lockKeyboard( void )
301 // NOTE: Does nothing with the FACOM6684
302 void Scan_unlockKeyboard( void )
307 void Scan_resetKeyboard( void )
309 // Not a calculated valued...
313 KeyBufferRemoveCount = 0;
314 KeyIndex_BufferUsed = 0;