1 /* Copyright (C) 2012 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
46 // Make sure we haven't overflowed the buffer
47 #define bufferAdd(byte) \
48 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
49 KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
53 // ----- Variables -----
55 // Buffer used to inform the macro processing module which keys have been detected as pressed
56 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
57 volatile uint8_t KeyIndex_BufferUsed;
58 volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
62 volatile uint8_t BufferReadyToClear;
66 // ----- Function Declarations -----
68 void processKeyValue( uint8_t keyValue );
69 void removeKeyValue( uint8_t keyValue );
73 // ----- Interrupt Functions -----
75 // USART Receive Buffer Full Interrupt
78 cli(); // Disable Interrupts
80 uint8_t keyValue = 0x00;
81 uint8_t keyState = 0x00;
83 // Read the scancode packet from the USART (1st to 8th bits)
86 // Read the release/press bit (9th bit) XXX Unnecessary, and wrong it seems, parity bit? or something else?
87 keyState = UCSR1B & 0x02;
89 // High bit of keyValue, also represents press/release
90 keyState = keyValue & 0x80 ? 0x00 : 0x02;
94 hexToStr( keyValue & 0x7F, tmpStr );
96 // Process the scancode
99 case 0x00: // Released
100 dPrintStrs( tmpStr, "R " ); // Debug
102 // Remove key from press buffer
103 removeKeyValue( keyValue & 0x7F );
106 case 0x02: // Pressed
107 dPrintStrs( tmpStr, "P " ); // Debug
109 // New key to process
110 processKeyValue( keyValue & 0x7F );
114 sei(); // Re-enable Interrupts
119 // ----- Functions -----
122 inline void scan_setup()
124 // Setup the the USART interface for keyboard data input
125 // NOTE: The input data signal needs to be inverted for the Teensy USART to properly work
128 // 16 MHz / ( 16 * Baud ) = UBRR
129 // Baud <- 0.823284 ms per bit, thus 1000 / 0.823284 = 1214.65004 -> 823.2824
130 // Thus baud setting = 823
131 uint16_t baud = 823; // Max setting of 4095
132 UBRR1H = (uint8_t)(baud >> 8);
133 UBRR1L = (uint8_t)baud;
135 // Enable the receiver, and RX Complete Interrupt as well as 9 bit data
138 // The transmitter is only to be enabled when needed
139 // Set the pin to be pull-up otherwise (use the lowered voltage inverter in order to sink)
140 HOLD_DDR &= ~(1 << HOLD_PIN);
141 HOLD_PORT |= (1 << HOLD_PIN);
143 // Set frame format: 9 data, 1 stop bit, no parity
144 // Asynchrounous USART mode
147 // Initially buffer doesn't need to be cleared (it's empty...)
148 BufferReadyToClear = 0;
150 // InputSignal is off by default
151 KeyIndex_Add_InputSignal = 0x00;
153 // Reset the keyboard before scanning, we might be in a wierd state
154 scan_resetKeyboard();
158 // Main Detection Loop
159 // Not needed for the BETKB, this is just a busy loop
160 inline uint8_t scan_loop()
165 void processKeyValue( uint8_t keyValue )
167 // Interpret scan code
170 case 0x00: // Break code from input?
173 // Make sure the key isn't already in the buffer
174 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
176 // Key isn't in the buffer yet
177 if ( c == KeyIndex_BufferUsed )
179 bufferAdd( keyValue );
181 // Only send data if enabled
182 if ( KeyIndex_Add_InputSignal )
183 scan_sendData( KeyIndex_Add_InputSignal );
187 // Key already in the buffer
188 if ( KeyIndex_Buffer[c] == keyValue )
195 void removeKeyValue( uint8_t keyValue )
197 // Check for the released key, and shift the other keys lower on the buffer
199 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
201 // Key to release found
202 if ( KeyIndex_Buffer[c] == keyValue )
204 // Shift keys from c position
205 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
206 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
209 KeyIndex_BufferUsed--;
215 // Error case (no key to release)
216 if ( c == KeyIndex_BufferUsed + 1 )
220 hexToStr( keyValue, tmpStr );
221 erro_dPrint( "Could not find key to release: ", tmpStr );
226 uint8_t scan_sendData( uint8_t dataPayload )
228 // Enable the USART Transmitter
233 hexToStr( dataPayload, tmpStr );
234 info_dPrint( "Sending - ", tmpStr );
238 // Wait for the payload
241 // Disable the USART Transmitter
247 // Signal KeyIndex_Buffer that it has been properly read
248 void scan_finishedWithBuffer( uint8_t sentKeys )
252 // Signal that the keys have been properly sent over USB
253 void scan_finishedWithUSBBuffer( uint8_t sentKeys )
257 // Reset/Hold keyboard
258 // NOTE: Does nothing with the BETKB
259 void scan_lockKeyboard( void )
263 // NOTE: Does nothing with the BETKB
264 void scan_unlockKeyboard( void )
269 void scan_resetKeyboard( void )
271 // Not a calculated valued...