]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/Tandy1000/scan_loop.c
Move matrix information to a cli command
[kiibohd-controller.git] / Scan / Tandy1000 / scan_loop.c
1 /* Copyright (C) 2011,2014 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 // Compiler Includes
25 #include <Lib/ScanLib.h>
26
27 // Project Includes
28 #include <led.h>
29 #include <print.h>
30
31 // Local Includes
32 #include "scan_loop.h"
33
34
35
36 // ----- Defines -----
37
38 // Pinout Defines
39 #define CLK_READ  PIND
40 #define CLK_PORT PORTD
41 #define CLK_DDR   DDRD
42 #define CLK_PIN      1
43
44 #define DATA_READ  PIND
45 #define DATA_PORT PORTD
46 #define DATA_DDR   DDRD
47 #define DATA_PIN      0
48
49 #define INTR_PORT PORTD
50 #define INTR_DDR   DDRD
51 #define INTR_PIN      0
52
53
54
55 // ----- Macros -----
56
57 #define READ_CLK       CLK_READ &   (1 <<  CLK_PIN) ? 1 : 0
58 #define READ_DATA     DATA_READ &   (1 << DATA_PIN) ? 0 : 1
59
60 #define UNSET_INTR()  INTR_DDR &= ~(1 << INTR_PIN)
61 #define   SET_INTR()  INTR_DDR |=  (1 << INTR_PIN)
62
63
64
65 // ----- Variables -----
66
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;
70
71
72 // Scan Code Retrieval Variables
73 uint8_t inputData    = 0xFF;
74 uint8_t packet_index = 0;
75
76
77
78 // ----- Functions -----
79
80 // Setup
81 inline void Scan_setup()
82 {
83         // Initially reset the keyboard (just in case we are in a wierd state)
84         Scan_resetKeyboard();
85
86         // Setup SPI for data input using the clock and data inputs
87         // TODO
88         /*
89         // Setup inputs
90         CLK_DDR  &= ~(1 << CLK_PIN);
91         DATA_DDR &= ~(1 << DATA_PIN);
92
93         // Setup Pull-up's
94         CLK_PORT  &= ~(1 << CLK_PIN);  // (CLK)
95         DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
96         */
97
98         // Setup Keyboard Interrupt
99         INTR_DDR  &= ~(1 << INTR_PIN);
100         INTR_PORT &= ~(1 << INTR_PIN);
101
102         // Setup Keyboard Reset Line
103         // TODO
104 }
105
106
107 // Main Detection Loop
108 inline uint8_t Scan_loop()
109 {
110         /*
111         // Packet Read
112         if ( packet_index == 8 )
113         {
114                 // Disable Error LED, proper key found
115                 errorLED( 0 );
116
117 //#ifdef MAX_DEBUG
118                 // Crazy Debug (Read the Scan Code)
119                 char tmpStr[3];
120                 hexToStr_op( inputData, tmpStr, 2 );
121                 dPrintStrsNL( "Read Data: 0x", tmpStr );
122 //#endif
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;
127
128                 // Reset Containers
129                 packet_index = 0;
130                 inputData = 0xFF;
131         }
132         // Bad Packet
133         else if ( packet_index > 8 )
134         {
135                 // Signal Error
136                 errorLED( 1 );
137
138                 char tmpStr[3];
139                 int8ToStr( packet_index, tmpStr );
140                 erro_dPrint( "Big packet? Mismatched... ", tmpStr );
141
142                 packet_index = 0;
143                 inputData = 0xFF;
144         }
145         */
146
147         // Disable keyboard interrupt (does nothing if already off)
148         UNSET_INTR();
149
150         /* XXX OLD CODE - Somewhat worked, has glitches, and is not compatible with the current API
151
152         // Read the clock 8 times
153         if ( READ_CLK )
154         {
155                 // Mis-read packet, set back to 0
156                 if ( packet_index == -1 )
157                         packet_index = 0;
158
159                 // Append 1 bit of data
160                 inputData &= ~(READ_DATA << packet_index);
161                 packet_index++;
162
163                 // 8 Bits have been read
164                 if ( packet_index == 8 )
165                 {
166                         // Wait till clock edge falls
167                         while ( READ_CLK );
168
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 ) )
173                                         badDataCounter++;
174
175                         if ( badDataCounter < 25 )
176                         {
177 //#ifdef MAX_DEBUG
178                                 // Crazy Debug (Read the Scan Code)
179                                 char tmpStr[3];
180                                 hexToStr_op( inputData, tmpStr, 2 );
181                                 dbug_dPrint( "Read Data: 0x", tmpStr );
182 //#endif
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;
187                         }
188                         // Even though this is a mis-read packet, we still know what the value is
189                         else
190                         {
191                                 // Signal Error
192                                 errorLED( 1 );
193                                 char tmpStr[3];
194                                 hexToStr_op( inputData, tmpStr, 2 );
195                                 erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
196                         }
197
198                         // Reset Containers
199                         inputData = 0xFF;
200                         packet_index = 0;
201
202                         // Interrupt the keyboard, so we don't get packet pieces...
203                         SET_INTR();
204
205                         // Do not wait for next clock, let USB do it's thing (if desired)
206                         return packet_index;
207                 }
208
209                 // Wait till clock edge falls
210                 while ( READ_CLK );
211         }
212
213         */
214
215         // Interrupt keyboard if there is no pending packet
216         SET_INTR();
217
218         return packet_index;
219 }
220
221 // Send data
222 // XXX Not used with the Tandy1000
223 uint8_t Scan_sendData( uint8_t dataPayload )
224 {
225         return 0;
226 }
227
228 // Signal KeyIndex_Buffer that it has been properly read
229 // TODO
230 void Scan_finishedWithBuffer( uint8_t sentKeys )
231 {
232 }
233
234 // Signal that the keys have been properly sent over USB
235 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
236 {
237 }
238
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 )
243 {
244         UNSET_INTR();
245 }
246
247 void Scan_unlockKeyboard( void )
248 {
249         SET_INTR();
250 }
251
252 // Reset Keyboard
253 void Scan_resetKeyboard( void )
254 {
255         // TODO Tandy1000 has a dedicated reset line
256 }
257