]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/BETKB/scan_loop.c
Code cleanup
[kiibohd-controller.git] / Scan / BETKB / scan_loop.c
1 /* Copyright (C) 2012,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 // Pinout Defines
39 #define HOLD_PORT PORTD
40 #define HOLD_DDR   DDRD
41 #define HOLD_PIN      3
42
43
44 // ----- Macros -----
45
46
47
48 // ----- Variables -----
49
50 // Buffer used to inform the macro processing module which keys have been detected as pressed
51 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
52 volatile uint8_t KeyIndex_BufferUsed;
53 volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
54
55
56 // Buffer Signals
57 volatile uint8_t BufferReadyToClear;
58
59
60
61 // ----- Function Declarations -----
62
63 void processKeyValue( uint8_t keyValue );
64 void  removeKeyValue( uint8_t keyValue );
65
66
67
68 // ----- Interrupt Functions -----
69
70 // USART Receive Buffer Full Interrupt
71 ISR(USART1_RX_vect)
72 {
73         cli(); // Disable Interrupts
74
75         uint8_t keyValue = 0x00;
76         uint8_t keyState = 0x00;
77
78         // Read the scancode packet from the USART (1st to 8th bits)
79         keyValue = UDR1;
80
81         // Read the release/press bit (9th bit) XXX Unnecessary, and wrong it seems, parity bit? or something else?
82         keyState = UCSR1B & 0x02;
83
84         // High bit of keyValue, also represents press/release
85         keyState = keyValue & 0x80 ? 0x00 : 0x02;
86
87         // Debug
88         char tmpStr[6];
89         hexToStr( keyValue & 0x7F, tmpStr );
90
91         // Process the scancode
92         switch ( keyState )
93         {
94         case 0x00: // Released
95                 dPrintStrs( tmpStr, "R  " ); // Debug
96
97                 // Remove key from press buffer
98                 removeKeyValue( keyValue & 0x7F );
99                 break;
100
101         case 0x02: // Pressed
102                 dPrintStrs( tmpStr, "P " ); // Debug
103
104                 // New key to process
105                 processKeyValue( keyValue & 0x7F );
106                 break;
107         }
108
109         sei(); // Re-enable Interrupts
110 }
111
112
113
114 // ----- Functions -----
115
116 // Setup
117 inline void scan_setup()
118 {
119         // Setup the the USART interface for keyboard data input
120         // NOTE: The input data signal needs to be inverted for the Teensy USART to properly work
121         
122         // Setup baud rate
123         // 16 MHz / ( 16 * Baud ) = UBRR
124         // Baud <- 0.823284 ms per bit, thus 1000 / 0.823284 = 1214.65004 -> 823.2824
125         // Thus baud setting = 823
126         uint16_t baud = 823; // Max setting of 4095
127         UBRR1H = (uint8_t)(baud >> 8);
128         UBRR1L = (uint8_t)baud;
129
130         // Enable the receiver, and RX Complete Interrupt as well as 9 bit data
131         UCSR1B = 0x94;
132
133         // The transmitter is only to be enabled when needed
134         // Set the pin to be pull-up otherwise (use the lowered voltage inverter in order to sink)
135         HOLD_DDR  &= ~(1 << HOLD_PIN);
136         HOLD_PORT |=  (1 << HOLD_PIN);
137
138         // Set frame format: 9 data, 1 stop bit, no parity
139         // Asynchrounous USART mode
140         UCSR1C = 0x06;
141
142         // Initially buffer doesn't need to be cleared (it's empty...)
143         BufferReadyToClear = 0;
144
145         // InputSignal is off by default
146         KeyIndex_Add_InputSignal = 0x00;
147
148         // Reset the keyboard before scanning, we might be in a wierd state
149         scan_resetKeyboard();
150 }
151
152
153 // Main Detection Loop
154 // Not needed for the BETKB, this is just a busy loop
155 inline uint8_t scan_loop()
156 {
157         return 0;
158 }
159
160 void processKeyValue( uint8_t keyValue )
161 {
162         // Interpret scan code
163         switch ( keyValue )
164         {
165         case 0x00: // Break code from input?
166                 break;
167         default:
168                 // Make sure the key isn't already in the buffer
169                 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
170                 {
171                         // Key isn't in the buffer yet
172                         if ( c == KeyIndex_BufferUsed )
173                         {
174                                 Macro_bufferAdd( keyValue );
175
176                                 // Only send data if enabled
177                                 if ( KeyIndex_Add_InputSignal )
178                                         scan_sendData( KeyIndex_Add_InputSignal );
179                                 break;
180                         }
181
182                         // Key already in the buffer
183                         if ( KeyIndex_Buffer[c] == keyValue )
184                                 break;
185                 }
186                 break;
187         }
188 }
189
190 void removeKeyValue( uint8_t keyValue )
191 {
192         // Check for the released key, and shift the other keys lower on the buffer
193         uint8_t c;
194         for ( c = 0; c < KeyIndex_BufferUsed; c++ )
195         {
196                 // Key to release found
197                 if ( KeyIndex_Buffer[c] == keyValue )
198                 {
199                         // Shift keys from c position
200                         for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
201                                 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
202
203                         // Decrement Buffer
204                         KeyIndex_BufferUsed--;
205
206                         break;
207                 }
208         }
209
210         // Error case (no key to release)
211         if ( c == KeyIndex_BufferUsed + 1 )
212         {
213                 errorLED( 1 );
214                 char tmpStr[6];
215                 hexToStr( keyValue, tmpStr );
216                 erro_dPrint( "Could not find key to release: ", tmpStr );
217         }
218 }
219
220 // Send data
221 uint8_t scan_sendData( uint8_t dataPayload )
222 {
223         // Enable the USART Transmitter
224         UCSR1B |=  (1 << 3);
225
226         // Debug
227         char tmpStr[6];
228         hexToStr( dataPayload, tmpStr );
229         info_dPrint( "Sending - ", tmpStr );
230
231         UDR1 = dataPayload;
232
233         // Wait for the payload
234         _delay_us( 800 );
235
236         // Disable the USART Transmitter
237         UCSR1B &= ~(1 << 3);
238
239         return 0;
240 }
241
242 // Signal KeyIndex_Buffer that it has been properly read
243 void Scan_finishedWithBuffer( uint8_t sentKeys )
244 {
245 }
246
247 // Signal that the keys have been properly sent over USB
248 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
249 {
250 }
251
252 // Reset/Hold keyboard
253 // NOTE: Does nothing with the BETKB
254 void scan_lockKeyboard( void )
255 {
256 }
257
258 // NOTE: Does nothing with the BETKB
259 void scan_unlockKeyboard( void )
260 {
261 }
262
263 // Reset Keyboard
264 void scan_resetKeyboard( void )
265 {
266         // Not a calculated valued...
267         _delay_ms( 50 );
268 }
269