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 <avr/interrupt.h>
27 #include <util/delay.h>
34 #include "scan_loop.h"
38 // ----- Defines -----
43 // Make sure we haven't overflowed the buffer
44 #define bufferAdd(byte) \
45 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
46 KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
50 // ----- Variables -----
52 // Buffer used to inform the macro processing module which keys have been detected as pressed
53 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
54 volatile uint8_t KeyIndex_BufferUsed;
56 volatile uint8_t KeyBufferRemove[6];
57 volatile uint8_t KeyBufferRemoveCount = 0;
59 static uint8_t KeyBuffer[3];
60 volatile static uint8_t KeyBufferCount = 0;
64 // ----- Function Declarations -----
66 void processKeyValue( uint8_t valueType, uint8_t keyValue );
67 void removeKeyValue( uint8_t keyValue );
71 // ----- Interrupt Functions -----
73 // USART Receive Buffer Full Interrupt
76 cli(); // Disable Interrupts
78 // Read part of the scan code (3 8bit chunks) from USART
79 KeyBuffer[KeyBufferCount] = UDR1;
81 if ( KeyBufferCount >= 2 )
84 for ( uint8_t c = 0; c <= 2; c++ )
88 hexToStr( KeyBuffer[c], tmpStr );
89 dPrintStrs( tmpStr, " " ); // Debug
93 processKeyValue( KeyBuffer[1], KeyBuffer[2] );
102 sei(); // Re-enable Interrupts
107 // ----- Functions -----
110 inline void scan_setup()
112 // Setup the the USART interface for keyboard data input
113 // NOTE: The input data signal needs to be inverted for the Teensy USART to properly work
116 // 16 MHz / ( 16 * Baud ) = UBRR
117 // Baud: 4817 -> 16 MHz / ( 16 * 4817 ) = 207.5981
118 // Thus baud setting = 208
119 uint16_t baud = 208; // Max setting of 4095
120 UBRR1H = (uint8_t)(baud >> 8);
121 UBRR1L = (uint8_t)baud;
123 // Enable the receiver, transmitter, and RX Complete Interrupt
126 // Set frame format: 8 data, 1 stop bit, odd parity
127 // Asynchrounous USART mode
130 // Reset the keyboard before scanning, we might be in a wierd state
131 scan_resetKeyboard();
135 // Main Detection Loop
136 inline uint8_t scan_loop()
138 // Remove any "released keys", this is delayed due to buffer release synchronization issues
139 for ( uint8_t c = 0; c < KeyBufferRemoveCount; c++ )
141 removeKeyValue( KeyBufferRemove[c] );
144 KeyBufferRemoveCount = 0;
149 void processKeyValue( uint8_t valueType, uint8_t keyValue )
156 // Repeated Key Press
159 // Modifier Key Release
161 KeyBufferRemove[KeyBufferRemoveCount++] = keyValue;
165 // Make sure the key isn't already in the buffer
166 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
168 // Key isn't in the buffer yet
169 if ( c == KeyIndex_BufferUsed )
171 bufferAdd( keyValue );
175 // Key already in the buffer
176 if ( KeyIndex_Buffer[c] == keyValue )
181 void removeKeyValue( uint8_t keyValue )
183 // Check for the released key, and shift the other keys lower on the buffer
185 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
187 // Key to release found
188 if ( KeyIndex_Buffer[c] == keyValue )
190 // Shift keys from c position
191 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
192 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
195 KeyIndex_BufferUsed--;
201 // Error case (no key to release)
202 if ( c == KeyIndex_BufferUsed + 1 )
206 hexToStr( keyValue, tmpStr );
207 erro_dPrint( "Could not find key to release: ", tmpStr );
212 uint8_t scan_sendData( uint8_t dataPayload )
216 hexToStr( dataPayload, tmpStr );
217 info_dPrint( "Sending - ", tmpStr );
224 // Signal KeyIndex_Buffer that it has been properly read
225 void scan_finishedWithBuffer( uint8_t sentKeys )
227 // Make sure we aren't in the middle of a receiving a new scancode
228 while ( KeyBufferCount != 0 );
230 cli(); // Disable Interrupts
232 // Count for number of modifiers to maintain in the buffer
235 uint8_t latchBuffer[13]; // There are only 13 keys that can possibly be latched at the same time...
237 uint8_t prevBuffer = KeyIndex_BufferUsed;
239 // Clean out all keys except "special" keys (designated modifiers)
241 for ( key = 0; key < sentKeys; key++ )
243 switch ( KeyIndex_Buffer[key] )
245 // Dedicated Modifier Keys
246 // NOTE: Both shifts are represented as the same scan code
250 KeyIndex_Buffer[filled++] = KeyIndex_Buffer[key];
252 // Latched Keys, only released if a non-modifier is pressed along with it
253 // NOTE: This keys do not have a built in repeating
266 //case 0x41: // XXX Being used as ESC
267 latchBuffer[latched++] = KeyIndex_Buffer[key];
269 // Allow the scancode to be removed, normal keys
276 // Reset the buffer counter
277 KeyIndex_BufferUsed = filled;
279 // Add back lost keys, so they are processed on the next USB send
280 for ( ; key < prevBuffer; key++ )
282 bufferAdd( KeyIndex_Buffer[key] );
283 info_print("Re-appending lost key after USB send...");
286 // Only "re-add" the latched keys if they weren't used
287 if ( latched > 0 && normal == 0 )
289 for ( uint8_t c = 0; c < latched; c++ )
291 bufferAdd( latchBuffer[c] );
295 sei(); // Re-enable Interrupts
298 // Signal that the keys have been properly sent over USB
299 void scan_finishedWithUSBBuffer( uint8_t sentKeys )
303 // Reset/Hold keyboard
304 // NOTE: Does nothing with the FACOM6684
305 void scan_lockKeyboard( void )
309 // NOTE: Does nothing with the FACOM6684
310 void scan_unlockKeyboard( void )
315 void scan_resetKeyboard( void )
317 // Not a calculated valued...
321 KeyBufferRemoveCount = 0;
322 KeyIndex_BufferUsed = 0;