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