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 -----
40 #define CLK_PORT PORTD
44 #define DATA_READ PIND
45 #define DATA_PORT PORTD
49 #define INTR_PORT PORTD
57 #define READ_CLK CLK_READ & (1 << CLK_PIN) ? 1 : 0
58 #define READ_DATA DATA_READ & (1 << DATA_PIN) ? 0 : 1
60 #define UNSET_INTR() INTR_DDR &= ~(1 << INTR_PIN)
61 #define SET_INTR() INTR_DDR |= (1 << INTR_PIN)
65 // ----- Variables -----
67 // Buffer used to inform the macro processing module which keys have been detected as pressed
68 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
69 volatile uint8_t KeyIndex_BufferUsed;
72 // Scan Code Retrieval Variables
73 uint8_t inputData = 0xFF;
74 uint8_t packet_index = 0;
78 // ----- Functions -----
81 inline void Scan_setup()
83 // Initially reset the keyboard (just in case we are in a wierd state)
86 // Setup SPI for data input using the clock and data inputs
90 CLK_DDR &= ~(1 << CLK_PIN);
91 DATA_DDR &= ~(1 << DATA_PIN);
94 CLK_PORT &= ~(1 << CLK_PIN); // (CLK)
95 DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
98 // Setup Keyboard Interrupt
99 INTR_DDR &= ~(1 << INTR_PIN);
100 INTR_PORT &= ~(1 << INTR_PIN);
102 // Setup Keyboard Reset Line
107 // Main Detection Loop
108 inline uint8_t Scan_loop()
112 if ( packet_index == 8 )
114 // Disable Error LED, proper key found
118 // Crazy Debug (Read the Scan Code)
120 hexToStr_op( inputData, tmpStr, 2 );
121 dPrintStrsNL( "Read Data: 0x", tmpStr );
123 // - Map the scan code to the index array -
124 // If the 8th bit is high, remove the keypress, else, add the keypress
125 // The lower 7 bits are the array index
126 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
133 else if ( packet_index > 8 )
139 int8ToStr( packet_index, tmpStr );
140 erro_dPrint( "Big packet? Mismatched... ", tmpStr );
147 // Disable keyboard interrupt (does nothing if already off)
150 /* XXX OLD CODE - Somewhat worked, has glitches, and is not compatible with the current API
152 // Read the clock 8 times
155 // Mis-read packet, set back to 0
156 if ( packet_index == -1 )
159 // Append 1 bit of data
160 inputData &= ~(READ_DATA << packet_index);
163 // 8 Bits have been read
164 if ( packet_index == 8 )
166 // Wait till clock edge falls
169 // Sample both lines to make sure this is not a data value
170 // and definitely the end of packet data blip
171 uint16_t badDataCounter = 0;
172 while ( !( READ_DATA ) && !( READ_CLK ) )
175 if ( badDataCounter < 25 )
178 // Crazy Debug (Read the Scan Code)
180 hexToStr_op( inputData, tmpStr, 2 );
181 dbug_dPrint( "Read Data: 0x", tmpStr );
183 // - Map the scan code to the index array -
184 // If the 8th bit is high, remove the keypress, else, add the keypress
185 // The lower 7 bits are the array index
186 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
188 // Even though this is a mis-read packet, we still know what the value is
194 hexToStr_op( inputData, tmpStr, 2 );
195 erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
202 // Interrupt the keyboard, so we don't get packet pieces...
205 // Do not wait for next clock, let USB do it's thing (if desired)
209 // Wait till clock edge falls
215 // Interrupt keyboard if there is no pending packet
222 // XXX Not used with the Tandy1000
223 uint8_t Scan_sendData( uint8_t dataPayload )
228 // Signal KeyIndex_Buffer that it has been properly read
230 void Scan_finishedWithBuffer( uint8_t sentKeys )
234 // Signal that the keys have been properly sent over USB
235 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
239 // Reset/Hold keyboard
240 // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
241 // The Tandy 1000 keyboard has a dedicated hold/processor interrupt line
242 void Scan_lockKeyboard( void )
247 void Scan_unlockKeyboard( void )
253 void Scan_resetKeyboard( void )
255 // TODO Tandy1000 has a dedicated reset line