1 /* Copyright (C) 2012,2014 by Jacob Alexander
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 // ----- Includes -----
25 #include <Lib/ScanLib.h>
32 #include "scan_loop.h"
36 // ----- Defines -----
39 #define HOLD_PORT PORTD
48 // ----- Variables -----
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
57 volatile uint8_t BufferReadyToClear;
61 // ----- Function Declarations -----
63 void processKeyValue( uint8_t keyValue );
64 void removeKeyValue( uint8_t keyValue );
68 // ----- Interrupt Functions -----
70 // USART Receive Buffer Full Interrupt
73 cli(); // Disable Interrupts
75 uint8_t keyValue = 0x00;
76 uint8_t keyState = 0x00;
78 // Read the scancode packet from the USART (1st to 8th bits)
81 // Read the release/press bit (9th bit) XXX Unnecessary, and wrong it seems, parity bit? or something else?
82 keyState = UCSR1B & 0x02;
84 // High bit of keyValue, also represents press/release
85 keyState = keyValue & 0x80 ? 0x00 : 0x02;
89 hexToStr( keyValue & 0x7F, tmpStr );
91 // Process the scancode
94 case 0x00: // Released
95 dPrintStrs( tmpStr, "R " ); // Debug
97 // Remove key from press buffer
98 removeKeyValue( keyValue & 0x7F );
101 case 0x02: // Pressed
102 dPrintStrs( tmpStr, "P " ); // Debug
104 // New key to process
105 processKeyValue( keyValue & 0x7F );
109 sei(); // Re-enable Interrupts
114 // ----- Functions -----
117 inline void scan_setup()
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
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;
130 // Enable the receiver, and RX Complete Interrupt as well as 9 bit data
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);
138 // Set frame format: 9 data, 1 stop bit, no parity
139 // Asynchrounous USART mode
142 // Initially buffer doesn't need to be cleared (it's empty...)
143 BufferReadyToClear = 0;
145 // InputSignal is off by default
146 KeyIndex_Add_InputSignal = 0x00;
148 // Reset the keyboard before scanning, we might be in a wierd state
149 scan_resetKeyboard();
153 // Main Detection Loop
154 // Not needed for the BETKB, this is just a busy loop
155 inline uint8_t scan_loop()
160 void processKeyValue( uint8_t keyValue )
162 // Interpret scan code
165 case 0x00: // Break code from input?
168 // Make sure the key isn't already in the buffer
169 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
171 // Key isn't in the buffer yet
172 if ( c == KeyIndex_BufferUsed )
174 Macro_bufferAdd( keyValue );
176 // Only send data if enabled
177 if ( KeyIndex_Add_InputSignal )
178 scan_sendData( KeyIndex_Add_InputSignal );
182 // Key already in the buffer
183 if ( KeyIndex_Buffer[c] == keyValue )
190 void removeKeyValue( uint8_t keyValue )
192 // Check for the released key, and shift the other keys lower on the buffer
194 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
196 // Key to release found
197 if ( KeyIndex_Buffer[c] == keyValue )
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];
204 KeyIndex_BufferUsed--;
210 // Error case (no key to release)
211 if ( c == KeyIndex_BufferUsed + 1 )
215 hexToStr( keyValue, tmpStr );
216 erro_dPrint( "Could not find key to release: ", tmpStr );
221 uint8_t scan_sendData( uint8_t dataPayload )
223 // Enable the USART Transmitter
228 hexToStr( dataPayload, tmpStr );
229 info_dPrint( "Sending - ", tmpStr );
233 // Wait for the payload
236 // Disable the USART Transmitter
242 // Signal KeyIndex_Buffer that it has been properly read
243 void Scan_finishedWithBuffer( uint8_t sentKeys )
247 // Signal that the keys have been properly sent over USB
248 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
252 // Reset/Hold keyboard
253 // NOTE: Does nothing with the BETKB
254 void scan_lockKeyboard( void )
258 // NOTE: Does nothing with the BETKB
259 void scan_unlockKeyboard( void )
264 void scan_resetKeyboard( void )
266 // Not a calculated valued...