]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/Kaypro1/scan_loop.c
Adding Kaypro1 functional support
[kiibohd-controller.git] / Scan / Kaypro1 / 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
28 // Project Includes
29 #include <led.h>
30 #include <print.h>
31
32 // Local Includes
33 #include "scan_loop.h"
34
35
36
37 // ----- Defines -----
38
39
40
41 // ----- Macros -----
42
43 // Make sure we haven't overflowed the buffer
44 #define bufferAdd(byte) \
45                 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
46                         KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
47
48
49
50 // ----- Variables -----
51
52 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
53 volatile uint8_t KeyIndex_BufferUsed;
54
55
56 // Known signals
57 static uint8_t cmd_clickOFF  = 0x0A; // Short beep, turns off clicker
58 static uint8_t cmd_clickON   = 0x04; // Long beep, turns on clicker
59 static uint8_t cmd_ACK_AA    = 0x10; // Keyboard will send ack (0xAA) back to PC
60
61 // Other known signals
62 // 0x02 turns on clicker but with short beep
63
64
65
66 // ----- Functions -----
67
68 // Setup
69 inline void scan_setup()
70 {
71         // Setup the the USART interface for keyboard data input
72         
73         // Setup baud rate
74         // 16 MHz / ( 16 * Baud ) = UBRR
75         // Baud <- 3.358 ms per bit, thus 1000 / 3.358 = 297.80
76         // Thus baud = 3357
77         uint16_t baud = 3357; // Max setting of 4095
78         UBRR1H = (uint8_t)(baud >> 8);
79         UBRR1L = (uint8_t)baud;
80
81         // Enable the receiver, transitter, and RX Complete Interrupt
82         UCSR1B = 0x98;
83
84         // Set frame format: 8 data, no stop bits or parity
85         // Asynchrounous USART mode
86         // Kaypro sends ASCII codes (mostly standard) with 1 start bit and 8 data bits, with no trailing stop or parity bits
87         UCSR1C = 0x06;
88 }
89
90
91 // Main Detection Loop
92 // Nothing is needed here for the Kaypro, but the function is available as part of the api to be called in a polling fashion
93 // TODO
94 //  - Add songs :D
95 inline uint8_t scan_loop()
96 {
97         // We *could* do extra offline processing here, but, it's not really needed for the Kaypro 1 keyboard
98         return 0;
99 }
100
101 // USART Receive Buffer Full Interrupt
102 ISR(USART1_RX_vect)
103 {
104         cli(); // Disable Interrupts
105
106         // Get key from USART
107         uint8_t keyValue = UDR1;
108
109 //#ifdef MAX_DEBUG
110         // Debug print key
111         char tmpStr1[6];
112         hexToStr( keyValue, tmpStr1 );
113         dPrintStrs( tmpStr1, " " );
114 //#endif
115
116         // Add key(s) to processing buffer
117         // First split out Shift and Ctrl
118         //  Reserved Codes:
119         //   Shift - 0xF5
120         //   Ctrl  - 0xF6
121         switch ( keyValue )
122         {
123         // - Ctrl Keys -
124         // Exception keys
125         case 0x08: // ^H
126         case 0x09: // ^I
127         case 0x0D: // ^M
128         case 0x1B: // ^[
129                 bufferAdd( keyValue );
130                 break;
131         // 0x40 Offset Keys
132         // Add Ctrl key and offset to the lower alphabet
133         case 0x00: // ^@
134         case 0x1C: // "^\"
135         case 0x1D: // ^]
136         case 0x1E: // ^^
137         case 0x1F: // ^_
138                 bufferAdd( 0xF6 );
139                 bufferAdd( keyValue + 0x40 );
140                 break;
141
142         // - Add Shift key and offset to non-shifted key -
143         // 0x10 Offset Keys
144         case 0x21: // !
145         case 0x23: // #
146         case 0x24: // $
147         case 0x25: // %
148                 bufferAdd( 0xF5 );
149                 bufferAdd( keyValue + 0x10 );
150                 break;
151         // 0x11 Offset Keys
152         case 0x26: // &
153         case 0x28: // (
154                 bufferAdd( 0xF5 );
155                 bufferAdd( keyValue + 0x11 );
156                 break;
157         // 0x07 Offset Keys
158         case 0x29: // )
159                 bufferAdd( 0xF5 );
160                 bufferAdd( keyValue + 0x07 );
161                 break;
162         // -0x0E Offset Keys
163         case 0x40: // @
164                 bufferAdd( 0xF5 );
165                 bufferAdd( keyValue - 0x0E );
166                 break;
167         // 0x0E Offset Keys
168         case 0x2A: // *
169                 bufferAdd( 0xF5 );
170                 bufferAdd( keyValue + 0x0E );
171                 break;
172         // 0x12 Offset Keys
173         case 0x2B: // +
174                 bufferAdd( 0xF5 );
175                 bufferAdd( keyValue + 0x12 );
176                 break;
177         // 0x05 Offset Keys
178         case 0x22: // "
179                 bufferAdd( 0xF5 );
180                 bufferAdd( keyValue + 0x05 );
181                 break;
182         // 0x01 Offset Keys
183         case 0x3A: // :
184                 bufferAdd( 0xF5 );
185                 bufferAdd( keyValue + 0x01 );
186                 break;
187         // -0x10 Offset Keys
188         case 0x3C: // <
189         case 0x3E: // >
190         case 0x3F: // ?
191                 bufferAdd( 0xF5 );
192                 bufferAdd( keyValue - 0x10 );
193                 break;
194         // -0x28 Offset Keys
195         case 0x5E: // ^
196                 bufferAdd( 0xF5 );
197                 bufferAdd( keyValue - 0x28 );
198                 break;
199         // -0x32 Offset Keys
200         case 0x5F: // _
201                 bufferAdd( 0xF5 );
202                 bufferAdd( keyValue - 0x32 );
203                 break;
204         // -0x20 Offset Keys
205         case 0x7B: // {
206         case 0x7C: // |
207         case 0x7D: // }
208                 bufferAdd( 0xF5 );
209                 bufferAdd( keyValue - 0x20 );
210                 break;
211         // -0x1E Offset Keys
212         case 0x7E: // ~
213                 bufferAdd( 0xF5 );
214                 bufferAdd( keyValue - 0x1E );
215                 break;
216         // All other keys
217         default:
218                 // Ctrl Characters are from 0x00 to 0x1F, excluding:
219                 //  0x08 - Backspace
220                 //  0x09 - [Horizontal] Tab
221                 //  0x0D - [Carriage] Return
222                 //  0x1B - Escape
223                 //  0x7F - Delete (^?) (Doesn't need to be split out)
224
225                 // 0x60 Offset Keys
226                 // Add Ctrl key and offset to the lower alphabet
227                 if ( keyValue >= 0x00 && keyValue <= 0x1F )
228                 {
229                         bufferAdd( 0xF6 );
230                         bufferAdd( keyValue + 0x60 );
231                 }
232
233                 // Shift Characters are from 0x41 to 0x59
234                 //  No exceptions here :D
235                 // Add Shift key and offset to the lower alphabet
236                 else if ( keyValue >= 0x41 && keyValue <= 0x5A )
237                 {
238                         bufferAdd( 0xF5 );
239                         bufferAdd( keyValue + 0x20 );
240                 }
241
242                 // Everything else
243                 else
244                 {
245                         bufferAdd( keyValue );
246                 }
247                 break;
248         }
249
250         // Special keys - For communication to the keyboard
251         // TODO Try to push this functionality into the macros...somehow
252         switch ( keyValue )
253         {
254         case 0xC3: // Keypad Enter
255                 print("\n");
256                 info_print("BEEEEP! - Clicker on");
257                 UDR1 = cmd_clickON;
258                 break;
259
260         case 0xB2: // Keypad Decimal
261                 print("\n");
262                 info_print("BEEP! - Clicker off");
263                 UDR1 = cmd_clickOFF;
264                 break;
265
266         case 0x0A: // Line Feed
267                 print("\n");
268                 info_print("ACK!!");
269                 UDR1 = cmd_ACK_AA;
270                 break;
271         }
272
273         sei(); // Re-enable Interrupts
274 }
275