]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/MBC-55X/scan_loop.c
Major code cleanup and preparation for PartialMap Macro Module
[kiibohd-controller.git] / Scan / MBC-55X / scan_loop.c
1 /* Copyright (C) 2013,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 // ----- Macros -----
40
41
42
43 // ----- Variables -----
44
45 // Buffer used to inform the macro processing module which keys have been detected as pressed
46 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
47 volatile uint8_t KeyIndex_BufferUsed;
48
49
50
51 // ----- Function Declarations -----
52
53 void processKeyValue( uint8_t valueType );
54 void  removeKeyValue( uint8_t keyValue );
55
56
57
58 // ----- Interrupt Functions -----
59
60 // UART Receive Buffer Full Interrupt
61 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
62 ISR(USART1_RX_vect)
63 #elif defined(_mk20dx128_) // ARM
64 void uart0_status_isr(void)
65 #endif
66 {
67         cli(); // Disable Interrupts
68
69         // Variable for UART data read
70         uint8_t keyValue = 0x00;
71
72 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
73         keyValue = UDR1;
74 #elif defined(_mk20dx128_) // ARM
75         // UART0_S1 must be read for the interrupt to be cleared
76         if ( UART0_S1 & UART_S1_RDRF )
77         {
78                 // Only doing single byte FIFO here
79                 keyValue = UART0_D;
80         }
81 #endif
82
83         // Debug
84         char tmpStr[6];
85         hexToStr( keyValue, tmpStr );
86         dPrintStrs( tmpStr, " " ); // Debug
87
88         // Decipher scan value
89         processKeyValue( keyValue );
90
91         sei(); // Re-enable Interrupts
92 }
93
94
95
96 // ----- Functions -----
97
98 // Setup
99 inline void Scan_setup()
100 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
101 {
102         // Setup the the USART interface for keyboard data input
103
104         // Setup baud rate - 1205 Baud
105         // 16 MHz / ( 16 * Baud ) = UBRR
106         // Baud: 1205 -> 16 MHz / ( 16 * 1205 ) = 829.8755
107         // Thus baud setting = 830
108         uint16_t baud = 830; // Max setting of 4095
109         UBRR1H = (uint8_t)(baud >> 8);
110         UBRR1L = (uint8_t)baud;
111
112         // Enable the receiver, and RX Complete Interrupt
113         UCSR1B = 0x90;
114
115         // Set frame format: 8 data, 1 stop bit, even parity
116         // Asynchrounous USART mode
117         UCSR1C = 0x26;
118
119         // Reset the keyboard before scanning, we might be in a wierd state
120         scan_resetKeyboard();
121 }
122 #elif defined(_mk20dx128_) // ARM
123 {
124         // Setup the the UART interface for keyboard data input
125         SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
126
127         // Pin Setup for UART0
128         PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
129         PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
130
131         // Setup baud rate - 1205 Baud
132         // 48 MHz / ( 16 * Baud ) = BDH/L
133         // Baud: 1205 -> 48 MHz / ( 16 * 1205 ) = 2489.6266
134         // Thus baud setting = 2490
135         // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
136         uint16_t baud = 2490; // Max setting of 8191
137         UART0_BDH = (uint8_t)(baud >> 8);
138         UART0_BDL = (uint8_t)baud;
139
140         // 8 bit, Even Parity, Idle Character bit after stop
141         // NOTE: For 8 bit with Parity you must enable 9 bit transmission (pg. 1065)
142         //       You only need to use UART0_D for 8 bit reading/writing though
143         // UART_C1_M UART_C1_PE UART_C1_PT UART_C1_ILT
144         UART0_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
145
146         // Number of bytes in FIFO before TX Interrupt
147         UART0_TWFIFO = 1;
148
149         // Number of bytes in FIFO before RX Interrupt
150         UART0_RWFIFO = 1;
151
152         // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
153         // TX/RX FIFO Size:
154         //  0x0 - 1 dataword
155         //  0x1 - 4 dataword
156         //  0x2 - 8 dataword
157         //UART0_PFIFO = UART_PFIFO_TXFE | /*TXFIFOSIZE*/ (0x0 << 4) | UART_PFIFO_RXFE | /*RXFIFOSIZE*/ (0x0);
158
159         // Reciever Inversion Disabled, LSBF
160         // UART_S2_RXINV UART_S2_MSBF
161         UART0_S2 |= 0x00;
162
163         // Transmit Inversion Disabled
164         // UART_C3_TXINV
165         UART0_C3 |= 0x00;
166
167         // TX Disabled, RX Enabled, RX Interrupt Enabled
168         // UART_C2_TE UART_C2_RE UART_C2_RIE
169         UART0_C2 = UART_C2_RE | UART_C2_RIE;
170
171         // Add interrupt to the vector table
172         NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
173
174         // Reset the keyboard before scanning, we might be in a wierd state
175         scan_resetKeyboard();
176 }
177 #endif
178
179
180 // Main Detection Loop
181 inline uint8_t Scan_loop()
182 {
183         return 0;
184 }
185
186 void processKeyValue( uint8_t keyValue )
187 {
188         // XXX NOTE: The key processing is not complete for this keyboard
189         //           Mostly due to laziness, and that the keyboard can't really be useful on a modern computer
190         //           Basic typing will work, but some of the keys and the Graph mode changes things around
191
192         // Add key(s) to processing buffer
193         // First split out Shift and Ctrl
194         //  Reserved Codes:
195         //   Shift - 0xF5
196         //   Ctrl  - 0xF6
197         switch ( keyValue )
198         {
199         // - Ctrl Keys -
200         // Exception keys
201         case 0x08: // ^H
202         case 0x09: // ^I
203         case 0x0D: // ^M
204         case 0x1B: // ^[
205                 Macro_bufferAdd( keyValue );
206                 break;
207         // 0x40 Offset Keys
208         // Add Ctrl key and offset to the lower alphabet
209         case 0x00: // ^@
210         case 0x1C: // "^\"
211         case 0x1D: // ^]
212         case 0x1E: // ^^
213         case 0x1F: // ^_
214                 Macro_bufferAdd( 0xF6 );
215                 Macro_bufferAdd( keyValue + 0x40 );
216                 break;
217
218         // - Add Shift key and offset to non-shifted key -
219         // 0x10 Offset Keys
220         case 0x21: // !
221         case 0x23: // #
222         case 0x24: // $
223         case 0x25: // %
224                 Macro_bufferAdd( 0xF5 );
225                 Macro_bufferAdd( keyValue + 0x10 );
226                 break;
227         // 0x11 Offset Keys
228         case 0x26: // &
229         case 0x28: // (
230                 Macro_bufferAdd( 0xF5 );
231                 Macro_bufferAdd( keyValue + 0x11 );
232                 break;
233         // 0x07 Offset Keys
234         case 0x29: // )
235                 Macro_bufferAdd( 0xF5 );
236                 Macro_bufferAdd( keyValue + 0x07 );
237                 break;
238         // -0x0E Offset Keys
239         case 0x40: // @
240                 Macro_bufferAdd( 0xF5 );
241                 Macro_bufferAdd( keyValue - 0x0E );
242                 break;
243         // 0x0E Offset Keys
244         case 0x2A: // *
245                 Macro_bufferAdd( 0xF5 );
246                 Macro_bufferAdd( keyValue + 0x0E );
247                 break;
248         // 0x12 Offset Keys
249         case 0x2B: // +
250                 Macro_bufferAdd( 0xF5 );
251                 Macro_bufferAdd( keyValue + 0x12 );
252                 break;
253         // 0x05 Offset Keys
254         case 0x22: // "
255                 Macro_bufferAdd( 0xF5 );
256                 Macro_bufferAdd( keyValue + 0x05 );
257                 break;
258         // 0x01 Offset Keys
259         case 0x3A: // :
260                 Macro_bufferAdd( 0xF5 );
261                 Macro_bufferAdd( keyValue + 0x01 );
262                 break;
263         // -0x10 Offset Keys
264         case 0x3C: // <
265         case 0x3E: // >
266         case 0x3F: // ?
267                 Macro_bufferAdd( 0xF5 );
268                 Macro_bufferAdd( keyValue - 0x10 );
269                 break;
270         // -0x28 Offset Keys
271         case 0x5E: // ^
272                 Macro_bufferAdd( 0xF5 );
273                 Macro_bufferAdd( keyValue - 0x28 );
274                 break;
275         // -0x32 Offset Keys
276         case 0x5F: // _
277                 Macro_bufferAdd( 0xF5 );
278                 Macro_bufferAdd( keyValue - 0x32 );
279                 break;
280         // -0x20 Offset Keys
281         case 0x7B: // {
282         case 0x7C: // |
283         case 0x7D: // }
284                 Macro_bufferAdd( 0xF5 );
285                 Macro_bufferAdd( keyValue - 0x20 );
286                 break;
287         // -0x1E Offset Keys
288         case 0x7E: // ~
289                 Macro_bufferAdd( 0xF5 );
290                 Macro_bufferAdd( keyValue - 0x1E );
291                 break;
292         // All other keys
293         default:
294                 // Ctrl Characters are from 0x00 to 0x1F, excluding:
295                 //  0x08 - Backspace
296                 //  0x09 - [Horizontal] Tab
297                 //  0x0D - [Carriage] Return
298                 //  0x1B - Escape
299                 //  0x7F - Delete (^?) (Doesn't need to be split out)
300
301                 // 0x60 Offset Keys
302                 // Add Ctrl key and offset to the lower alphabet
303                 if ( keyValue >= 0x00 && keyValue <= 0x1F )
304                 {
305                         Macro_bufferAdd( 0xF6 );
306                         Macro_bufferAdd( keyValue + 0x60 );
307                 }
308
309                 // Shift Characters are from 0x41 to 0x59
310                 //  No exceptions here :D
311                 // Add Shift key and offset to the lower alphabet
312                 else if ( keyValue >= 0x41 && keyValue <= 0x5A )
313                 {
314                         Macro_bufferAdd( 0xF5 );
315                         Macro_bufferAdd( keyValue + 0x20 );
316                 }
317
318                 // Everything else
319                 else
320                 {
321                         Macro_bufferAdd( keyValue );
322                 }
323                 break;
324         }
325 }
326
327 // Send data
328 // NOTE: Example only, MBC-55X cannot receive user data
329 uint8_t Scan_sendData( uint8_t dataPayload )
330 {
331         // Debug
332         char tmpStr[6];
333         hexToStr( dataPayload, tmpStr );
334         info_dPrint( "Sending - ", tmpStr );
335
336 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
337         UDR1 = dataPayload;
338 #elif defined(_mk20dx128_) // ARM
339         UART0_D = dataPayload;
340 #endif
341
342         return 0;
343 }
344
345 // Signal KeyIndex_Buffer that it has been properly read
346 void Scan_finishedWithBuffer( uint8_t sentKeys )
347 {
348 }
349
350 // Signal that the keys have been properly sent over USB
351 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
352 {
353         cli(); // Disable Interrupts
354
355         // Reset the buffer counter
356         KeyIndex_BufferUsed = 0;
357
358         sei(); // Re-enable Interrupts
359 }
360
361 // Reset/Hold keyboard
362 // NOTE: Does nothing with the MBC-55x
363 void Scan_lockKeyboard( void )
364 {
365 }
366
367 // NOTE: Does nothing with the MBC-55x
368 void Scan_unlockKeyboard( void )
369 {
370 }
371
372 // Reset Keyboard
373 void Scan_resetKeyboard( void )
374 {
375         // Not a calculated valued...
376         _delay_ms( 50 );
377
378         KeyIndex_BufferUsed = 0;
379 }
380