]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/Tandy1000/scan_loop.c
Commenting out Tandy1000 code to make compile.
[kiibohd-controller.git] / Scan / Tandy1000 / scan_loop.c
1 /* Copyright (C) 2011 by Jacob Alexander
2  * 
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:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
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
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // AVR Includes
25 #include <avr/interrupt.h>
26 #include <avr/io.h>
27 #include <util/delay.h>
28
29 // Project Includes
30 #include <led.h>
31 #include <print.h>
32
33 // Local Includes
34 #include "scan_loop.h"
35
36
37
38 // ----- Defines -----
39
40 // Pinout Defines
41 #define CLK_READ  PIND
42 #define CLK_PORT PORTD
43 #define CLK_DDR   DDRD
44 #define CLK_PIN      1
45
46 #define DATA_READ  PIND
47 #define DATA_PORT PORTD
48 #define DATA_DDR   DDRD
49 #define DATA_PIN      0
50
51 #define INTR_PORT PORTD
52 #define INTR_DDR   DDRD
53 #define INTR_PIN      0
54
55
56
57 // ----- Macros -----
58
59 #define READ_CLK       CLK_READ &   (1 <<  CLK_PIN) ? 1 : 0
60 #define READ_DATA     DATA_READ &   (1 << DATA_PIN) ? 0 : 1
61
62 #define UNSET_INTR()  INTR_DDR &= ~(1 << INTR_PIN)
63 #define   SET_INTR()  INTR_DDR |=  (1 << INTR_PIN)
64
65 #define bufferAdd(byte) \
66                 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
67                         KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
68
69
70 // ----- Variables -----
71
72 // Buffer used to inform the macro processing module which keys have been detected as pressed
73 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
74 volatile uint8_t KeyIndex_BufferUsed;
75
76
77 // Scan Code Retrieval Variables
78 uint8_t inputData    = 0xFF;
79 uint8_t packet_index = 0;
80
81
82
83 // ----- Functions -----
84
85 // Setup
86 inline void scan_setup()
87 {
88         // Initially reset the keyboard (just in case we are in a wierd state)
89         scan_resetKeyboard();
90
91         // Setup SPI for data input using the clock and data inputs
92         // TODO
93         /*
94         // Setup inputs
95         CLK_DDR  &= ~(1 << CLK_PIN);
96         DATA_DDR &= ~(1 << DATA_PIN);
97
98         // Setup Pull-up's
99         CLK_PORT  &= ~(1 << CLK_PIN);  // (CLK)
100         DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
101         */
102
103         // Setup Keyboard Interrupt
104         INTR_DDR  &= ~(1 << INTR_PIN);
105         INTR_PORT &= ~(1 << INTR_PIN);
106
107         // Setup Keyboard Reset Line
108         // TODO
109 }
110
111
112 // Main Detection Loop
113 inline uint8_t scan_loop()
114 {
115         /*
116         // Packet Read
117         if ( packet_index == 8 )
118         {
119                 // Disable Error LED, proper key found
120                 errorLED( 0 );
121
122 //#ifdef MAX_DEBUG
123                 // Crazy Debug (Read the Scan Code)
124                 char tmpStr[3];
125                 hexToStr_op( inputData, tmpStr, 2 );
126                 dPrintStrsNL( "Read Data: 0x", tmpStr );
127 //#endif
128                 // - Map the scan code to the index array -
129                 // If the 8th bit is high, remove the keypress, else, add the keypress
130                 // The lower 7 bits are the array index
131                 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
132
133                 // Reset Containers
134                 packet_index = 0;
135                 inputData = 0xFF;
136         }
137         // Bad Packet
138         else if ( packet_index > 8 )
139         {
140                 // Signal Error
141                 errorLED( 1 );
142
143                 char tmpStr[3];
144                 int8ToStr( packet_index, tmpStr );
145                 erro_dPrint( "Big packet? Mismatched... ", tmpStr );
146
147                 packet_index = 0;
148                 inputData = 0xFF;
149         }
150         */
151
152         // Disable keyboard interrupt (does nothing if already off)
153         UNSET_INTR();
154
155         /* XXX OLD CODE - Somewhat worked, has glitches, and is not compatible with the current API
156
157         // Read the clock 8 times
158         if ( READ_CLK )
159         {
160                 // Mis-read packet, set back to 0
161                 if ( packet_index == -1 )
162                         packet_index = 0;
163
164                 // Append 1 bit of data
165                 inputData &= ~(READ_DATA << packet_index);
166                 packet_index++;
167
168                 // 8 Bits have been read
169                 if ( packet_index == 8 )
170                 {
171                         // Wait till clock edge falls
172                         while ( READ_CLK );
173
174                         // Sample both lines to make sure this is not a data value
175                         //  and definitely the end of packet data blip
176                         uint16_t badDataCounter = 0;
177                         while ( !( READ_DATA ) && !( READ_CLK ) )
178                                         badDataCounter++;
179
180                         if ( badDataCounter < 25 )
181                         {
182 //#ifdef MAX_DEBUG
183                                 // Crazy Debug (Read the Scan Code)
184                                 char tmpStr[3];
185                                 hexToStr_op( inputData, tmpStr, 2 );
186                                 dbug_dPrint( "Read Data: 0x", tmpStr );
187 //#endif
188                                 // - Map the scan code to the index array -
189                                 // If the 8th bit is high, remove the keypress, else, add the keypress
190                                 // The lower 7 bits are the array index
191                                 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
192                         }
193                         // Even though this is a mis-read packet, we still know what the value is
194                         else
195                         {
196                                 // Signal Error
197                                 errorLED( 1 );
198                                 char tmpStr[3];
199                                 hexToStr_op( inputData, tmpStr, 2 );
200                                 erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
201                         }
202
203                         // Reset Containers
204                         inputData = 0xFF;
205                         packet_index = 0;
206
207                         // Interrupt the keyboard, so we don't get packet pieces...
208                         SET_INTR();
209
210                         // Do not wait for next clock, let USB do it's thing (if desired)
211                         return packet_index;
212                 }
213
214                 // Wait till clock edge falls
215                 while ( READ_CLK );
216         }
217
218         */
219
220         // Interrupt keyboard if there is no pending packet
221         SET_INTR();
222
223         return packet_index;
224 }
225
226 // Send data
227 // XXX Not used with the Tandy1000
228 uint8_t scan_sendData( uint8_t dataPayload )
229 {
230         return 0;
231 }
232
233 // Signal KeyIndex_Buffer that it has been properly read
234 // TODO
235 void scan_finishedWithBuffer( void )
236 {
237 }
238
239 // Signal that the keys have been properly sent over USB
240 void scan_finishedWithUSBBuffer( void )
241 {
242 }
243
244 // Reset/Hold keyboard
245 // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
246 // The Tandy 1000 keyboard has a dedicated hold/processor interrupt line
247 void scan_lockKeyboard( void )
248 {
249         UNSET_INTR();
250 }
251
252 void scan_unlockKeyboard( void )
253 {
254         SET_INTR();
255 }
256
257 // Reset Keyboard
258 void scan_resetKeyboard( void )
259 {
260         // TODO Tandy1000 has a dedicated reset line
261 }
262