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