1 /* Copyright (C) 2011-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 -----
44 // ----- Variables -----
46 // Buffer used to inform the macro processing module which keys have been detected as pressed
47 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
48 volatile uint8_t KeyIndex_BufferUsed;
52 static uint8_t cmd_clickOFF = 0x0A; // Short beep, turns off clicker
53 static uint8_t cmd_clickON = 0x04; // Long beep, turns on clicker
54 static uint8_t cmd_ACK_AA = 0x10; // Keyboard will send ack (0xAA) back to PC
56 // Other known signals
57 // 0x02 turns on clicker but with short beep
61 // ----- Functions -----
64 inline void Scan_setup()
66 // Setup the the USART interface for keyboard data input
69 // 16 MHz / ( 16 * Baud ) = UBRR
70 // Baud <- 3.358 ms per bit, thus 1000 / 3.358 = 297.80
72 uint16_t baud = 3357; // Max setting of 4095
73 UBRR1H = (uint8_t)(baud >> 8);
74 UBRR1L = (uint8_t)baud;
76 // Enable the receiver, transitter, and RX Complete Interrupt
79 // Set frame format: 8 data, no stop bits or parity
80 // Asynchrounous USART mode
81 // Kaypro sends ASCII codes (mostly standard) with 1 start bit and 8 data bits, with no trailing stop or parity bits
86 // Main Detection Loop
87 // Nothing is needed here for the Kaypro, but the function is available as part of the api to be called in a polling fashion
90 inline uint8_t Scan_loop()
92 // We *could* do extra offline processing here, but, it's not really needed for the Kaypro 1 keyboard
96 // USART Receive Buffer Full Interrupt
99 cli(); // Disable Interrupts
101 // Get key from USART
102 uint8_t keyValue = UDR1;
107 hexToStr( keyValue, tmpStr1 );
108 dPrintStrs( tmpStr1, " " );
111 // Add key(s) to processing buffer
112 // First split out Shift and Ctrl
124 Macro_bufferAdd( keyValue );
127 // Add Ctrl key and offset to the lower alphabet
133 Macro_bufferAdd( 0xF6 );
134 Macro_bufferAdd( keyValue + 0x40 );
137 // - Add Shift key and offset to non-shifted key -
143 Macro_bufferAdd( 0xF5 );
144 Macro_bufferAdd( keyValue + 0x10 );
149 Macro_bufferAdd( 0xF5 );
150 Macro_bufferAdd( keyValue + 0x11 );
154 Macro_bufferAdd( 0xF5 );
155 Macro_bufferAdd( keyValue + 0x07 );
159 Macro_bufferAdd( 0xF5 );
160 Macro_bufferAdd( keyValue - 0x0E );
164 Macro_bufferAdd( 0xF5 );
165 Macro_bufferAdd( keyValue + 0x0E );
169 Macro_bufferAdd( 0xF5 );
170 Macro_bufferAdd( keyValue + 0x12 );
174 Macro_bufferAdd( 0xF5 );
175 Macro_bufferAdd( keyValue + 0x05 );
179 Macro_bufferAdd( 0xF5 );
180 Macro_bufferAdd( keyValue + 0x01 );
186 Macro_bufferAdd( 0xF5 );
187 Macro_bufferAdd( keyValue - 0x10 );
191 Macro_bufferAdd( 0xF5 );
192 Macro_bufferAdd( keyValue - 0x28 );
196 Macro_bufferAdd( 0xF5 );
197 Macro_bufferAdd( keyValue - 0x32 );
203 Macro_bufferAdd( 0xF5 );
204 Macro_bufferAdd( keyValue - 0x20 );
208 Macro_bufferAdd( 0xF5 );
209 Macro_bufferAdd( keyValue - 0x1E );
213 // Ctrl Characters are from 0x00 to 0x1F, excluding:
215 // 0x09 - [Horizontal] Tab
216 // 0x0D - [Carriage] Return
218 // 0x7F - Delete (^?) (Doesn't need to be split out)
221 // Add Ctrl key and offset to the lower alphabet
222 if ( keyValue >= 0x00 && keyValue <= 0x1F )
224 Macro_bufferAdd( 0xF6 );
225 Macro_bufferAdd( keyValue + 0x60 );
228 // Shift Characters are from 0x41 to 0x59
229 // No exceptions here :D
230 // Add Shift key and offset to the lower alphabet
231 else if ( keyValue >= 0x41 && keyValue <= 0x5A )
233 Macro_bufferAdd( 0xF5 );
234 Macro_bufferAdd( keyValue + 0x20 );
240 Macro_bufferAdd( keyValue );
245 // Special keys - For communication to the keyboard
246 // TODO Try to push this functionality into the macros...somehow
249 case 0xC3: // Keypad Enter
251 info_print("BEEEEP! - Clicker on");
252 scan_sendData( cmd_clickON );
255 case 0xB2: // Keypad Decimal
257 info_print("BEEP! - Clicker off");
258 scan_sendData( cmd_clickOFF );
261 case 0x0A: // Line Feed
264 scan_sendData( cmd_ACK_AA );
268 sei(); // Re-enable Interrupts
272 uint8_t Scan_sendData( uint8_t dataPayload )
278 // Signal KeyIndex_Buffer that it has been properly read
279 void Scan_finishedWithBuffer( uint8_t sentKeys )
283 // Signal that the keys have been properly sent over USB
284 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
288 // Reset/Hold keyboard
289 // NOTE: Does nothing with the BETKB
290 void Scan_lockKeyboard( void )
294 // NOTE: Does nothing with the BETKB
295 void Scan_unlockKeyboard( void )
300 void Scan_resetKeyboard( void )