]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/Tandy1000/scan_loop.c
Tandy 1000 Converter, basicly works, except for packet mismatches
[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
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 #define READ_CLK       CLK_READ &   (1 <<  CLK_PIN) ? 1 : 0
57 #define READ_DATA     DATA_READ &   (1 << DATA_PIN) ? 0 : 1
58
59 #define UNSET_INTR()  INTR_DDR &= ~(1 << INTR_PIN)
60 #define   SET_INTR()  INTR_DDR |=  (1 << INTR_PIN)
61
62
63
64 // ----- Variables -----
65
66 uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
67
68 // Scan Code Retrieval Variables
69 uint8_t inputData    = 0xFF;
70 uint8_t packet_index = 0;
71
72
73
74 // ----- Functions -----
75
76 // Setup
77 inline void scan_setup()
78 {
79         // Setup inputs
80         CLK_DDR  &= ~(1 << CLK_PIN);
81         DATA_DDR &= ~(1 << DATA_PIN);
82
83         // Setup Pull-up's
84         CLK_PORT  &= ~(1 << CLK_PIN);  // (CLK)
85         DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
86
87         // Setup Keyboard Interrupt
88         INTR_DDR  &= ~(1 << INTR_PIN);
89         INTR_PORT &= ~(1 << INTR_PIN);
90
91         /* Interrupt Style (Not working fully)
92         cli();
93         // Setup interrupt on the CLK pin TODO Better defines
94         EICRA |= 0x03; // Rising Edge Interrupt
95         EIMSK |= (1 << INT0);
96
97         // Setup interrupt on the DATA pin TODO Better defines
98         EICRA |= 0x08; // Falling Edge Interrupt
99         EIMSK |= (1 << INT1);
100         sei();
101         */
102 }
103
104
105 // Main Detection Loop
106 inline uint8_t scan_loop()
107 {
108         /*
109         // Packet Read
110         if ( packet_index == 8 )
111         {
112                 // Disable Error LED, proper key found
113                 errorLED( 0 );
114
115 //#ifdef MAX_DEBUG
116                 // Crazy Debug (Read the Scan Code)
117                 char tmpStr[3];
118                 hexToStr_op( inputData, tmpStr, 2 );
119                 dPrintStrsNL( "Read Data: 0x", tmpStr );
120 //#endif
121                 // - Map the scan code to the index array -
122                 // If the 8th bit is high, remove the keypress, else, add the keypress
123                 // The lower 7 bits are the array index
124                 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
125
126                 // Reset Containers
127                 packet_index = 0;
128                 inputData = 0xFF;
129         }
130         // Bad Packet
131         else if ( packet_index > 8 )
132         {
133                 // Signal Error
134                 errorLED( 1 );
135
136                 char tmpStr[3];
137                 int8ToStr( packet_index, tmpStr );
138                 erro_dPrint( "Big packet? Mismatched... ", tmpStr );
139
140                 packet_index = 0;
141                 inputData = 0xFF;
142         }
143         */
144
145         // Disable keyboard interrupt (does nothing if already off)
146         UNSET_INTR();
147
148         // Read the clock 8 times
149         if ( READ_CLK )
150         {
151                 // Mis-read packet, set back to 0
152                 if ( packet_index == -1 )
153                         packet_index = 0;
154
155                 // Append 1 bit of data
156                 inputData &= ~(READ_DATA << packet_index);
157                 packet_index++;
158
159                 // 8 Bits have been read
160                 if ( packet_index == 8 )
161                 {
162                         // Wait till clock edge falls
163                         while ( READ_CLK );
164
165                         // Sample both lines to make sure this is not a data value
166                         //  and definitely the end of packet data blip
167                         uint16_t badDataCounter = 0;
168                         while ( !( READ_DATA ) && !( READ_CLK ) )
169                                         badDataCounter++;
170
171                         if ( badDataCounter < 25 )
172                         {
173 //#ifdef MAX_DEBUG
174                                 // Crazy Debug (Read the Scan Code)
175                                 char tmpStr[3];
176                                 hexToStr_op( inputData, tmpStr, 2 );
177                                 dbug_dPrint( "Read Data: 0x", tmpStr );
178 //#endif
179                                 // - Map the scan code to the index array -
180                                 // If the 8th bit is high, remove the keypress, else, add the keypress
181                                 // The lower 7 bits are the array index
182                                 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
183                         }
184                         // Even though this is a mis-read packet, we still know what the value is
185                         else
186                         {
187                                 // Signal Error
188                                 errorLED( 1 );
189                                 char tmpStr[3];
190                                 hexToStr_op( inputData, tmpStr, 2 );
191                                 erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
192                         }
193
194                         // Reset Containers
195                         inputData = 0xFF;
196                         packet_index = 0;
197
198                         // Interrupt the keyboard, so we don't get packet pieces...
199                         SET_INTR();
200
201                         // Do not wait for next clock, let USB do it's thing (if desired)
202                         return packet_index;
203                 }
204
205                 // Wait till clock edge falls
206                 while ( READ_CLK );
207         }
208
209         // Interrupt keyboard if there is no pending packet
210         SET_INTR();
211
212         return packet_index;
213 }
214
215 // Detection interrupt, signalled by a clock pulse from CLK_PIN
216 ISR(INT0_vect)
217 {
218         //cli(); // Disable Interrupts
219
220         // Append 1 bit of data
221         //inputData &= ~(READ_DATA << packet_index);
222         packet_index++;
223
224         //sei(); // Re-enable Interrupts
225 }
226
227 // Data Detected
228 ISR(INT1_vect)
229 {
230         // Append 1 bit of data
231         inputData &= ~(1 << packet_index);
232         packet_index++;
233
234         // Disable Clk Signal (Not needed if there's a data signal)
235         EIFR |= (1 << INTF0);
236 }
237
238
239