1 /* Copyright (C) 2011 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>
33 #include "scan_loop.h"
37 // ----- Defines -----
45 // ----- Variables -----
47 uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
51 static uint8_t cmd_clickOFF = 0x0A; // Short beep, turns off clicker
52 static uint8_t cmd_clickON = 0x04; // Long beep, turns on clicker
53 static uint8_t cmd_ACK_AA = 0x10; // Keyboard will send ack (0xAA) back to PC
55 // Other known signals
56 // 0x02 turns on clicker but with short beep
60 // ----- Functions -----
63 inline void scan_setup()
65 // Setup the the USART interface for keyboard data input
68 // 16 MHz / ( 16 * Baud ) = UBRR
69 // Baud <- 3.358 ms per bit, thus 1000 / 3.358 = 297.80
71 uint16_t baud = 3357; // Max setting of 4095
72 UBRR1H = (uint8_t)(baud >> 8);
73 UBRR1L = (uint8_t)baud;
75 // Enable the receiver, transitter, and RX Complete Interrupt
78 // Set frame format: 8 data, no stop bits or parity
79 // Asynchrounous USART mode
80 // Kaypro sends ASCII codes (mostly standard) with 1 start bit and 8 data bits, with no trailing stop or parity bits
85 // Main Detection Loop
86 inline uint8_t scan_loop()
90 if ( packet_index == 8 )
92 // Disable Error LED, proper key found
96 // Crazy Debug (Read the Scan Code)
98 hexToStr_op( inputData, tmpStr, 2 );
99 dPrintStrsNL( "Read Data: 0x", tmpStr );
101 // - Map the scan code to the index array -
102 // If the 8th bit is high, remove the keypress, else, add the keypress
103 // The lower 7 bits are the array index
104 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
111 else if ( packet_index > 8 )
117 int8ToStr( packet_index, tmpStr );
118 erro_dPrint( "Big packet? Mismatched... ", tmpStr );
125 // Disable keyboard interrupt (does nothing if already off)
128 // Read the clock 8 times
131 // Mis-read packet, set back to 0
132 if ( packet_index == -1 )
135 // Append 1 bit of data
136 inputData &= ~(READ_DATA << packet_index);
139 // 8 Bits have been read
140 if ( packet_index == 8 )
142 // Wait till clock edge falls
145 // Sample both lines to make sure this is not a data value
146 // and definitely the end of packet data blip
147 uint16_t badDataCounter = 0;
148 while ( !( READ_DATA ) && !( READ_CLK ) )
151 if ( badDataCounter < 25 )
154 // Crazy Debug (Read the Scan Code)
156 hexToStr_op( inputData, tmpStr, 2 );
157 dbug_dPrint( "Read Data: 0x", tmpStr );
159 // - Map the scan code to the index array -
160 // If the 8th bit is high, remove the keypress, else, add the keypress
161 // The lower 7 bits are the array index
162 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
164 // Even though this is a mis-read packet, we still know what the value is
170 hexToStr_op( inputData, tmpStr, 2 );
171 erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
178 // Interrupt the keyboard, so we don't get packet pieces...
181 // Do not wait for next clock, let USB do it's thing (if desired)
185 // Wait till clock edge falls
189 // Interrupt keyboard if there is no pending packet
195 // USART Receive Buffer Full Interrupt
198 cli(); // Disable Interrupts
200 uint8_t keyValue = UDR1;
202 hexToStr( keyValue, tmpStr1 );
203 dPrintStrs( tmpStr1, " " );
205 // Special keys - For communication to the keyboard
206 // TODO Try to push this functionality into the macros...somehow
209 case 0xC3: // Keypad Enter
211 info_print("BEEEEP! - Clicker on");
215 case 0xB2: // Keypad Decimal
217 info_print("BEEP! - Clicker off");
221 case 0x0A: // Line Feed
228 // Add key to processing buffer
230 sei(); // Re-enable Interrupts