1 /* Copyright (C) 2011,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 SPKR_PORT PORTD
43 #define POWR_PORT PORTC
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;
61 // ----- Functions -----
64 inline void Scan_setup()
66 // Setup the the USART interface for keyboard data input
69 // 16 MHz / ( 16 * Baud ) = UBRR
70 // Baud <- 0.10450 ms per bit, thus 1000 / 0.10450 = 9569.4
72 // To deal with the 0.5, setting to double speed, which means UBRR = 209
73 uint16_t baud = 209; // Max setting of 4095
74 UBRR1H = (uint8_t)(baud >> 8);
75 UBRR1L = (uint8_t)baud;
77 // Enable Double Read Speed
80 // Enable the receiver, transitter, and RX Complete Interrupt
83 // Set frame format: 8 data, no stop bits or parity
84 // Asynchrounous USART mode
87 // Set Speaker Pin to Pull-Up gives a low-volume click (XXX no other setting does, why?)
88 SPKR_DDR &= ~(1 << SPKR_POS);
89 SPKR_PORT |= (1 << SPKR_POS);
91 // Set Power Pin (I've traced this back to the "Power On" Switch, but I don't really know what it's for)
92 // Configured as a Pull-up Input - This pin "can" be read as well, it will go to GND when the "Power On" switch is pressed, and will read ~5V otherwise
93 // XXX Currently not used by the controller
94 POWR_DDR &= ~(1 << POWR_POS);
95 POWR_PORT |= (1 << POWR_POS);
97 // Reset the keyboard before scanning, we might be in a wierd state
101 // Main Detection Loop
102 // Not needed for the Sony NEWS, this is just a busy loop
103 inline uint8_t Scan_loop()
108 void processKeyValue( uint8_t keyValue )
110 // Detect release condition
111 uint8_t release = keyValue & 0x80;
113 // Finalize output buffer
120 // Check for the released key, and shift the other keys lower on the buffer
122 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
124 // Key to release found
125 if ( KeyIndex_Buffer[c] == keyValue )
127 // Shift keys from c position
128 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
129 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
132 KeyIndex_BufferUsed--;
138 // Error case (no key to release)
139 if ( c == KeyIndex_BufferUsed + 1 )
143 hexToStr( keyValue, tmpStr );
144 erro_dPrint( "Could not find key to release: ", tmpStr );
147 // Press or Repeated Key
150 // Make sure the key isn't already in the buffer
151 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
153 // Key isn't in the buffer yet
154 if ( c == KeyIndex_BufferUsed )
156 Macro_bufferAdd( keyValue );
160 // Key already in the buffer
161 if ( KeyIndex_Buffer[c] == keyValue )
167 // USART Receive Buffer Full Interrupt
170 cli(); // Disable Interrupts
172 uint8_t keyValue = 0x00;
174 // One scancode at a time (fastest interval ~3.95 ms - recorded, should still be ok for interrupt polling)
175 // Read the raw packet from the USART
180 hexToStr( keyValue, tmpStr );
181 dPrintStrs( tmpStr, " " );
183 // Process the scancode
184 if ( keyValue != 0x00 )
185 processKeyValue( keyValue );
187 sei(); // Re-enable Interrupts
190 // Send data to keyboard
191 uint8_t Scan_sendData( uint8_t dataPayload )
195 hexToStr( dataPayload, tmpStr );
196 info_dPrint( tmpStr, " " );
202 // Signal KeyIndex_Buffer that it has been properly read
203 // Not needed as a signal is sent to remove key-presses
204 void Scan_finishedWithBuffer( uint8_t sentKeys )
209 // Reset/Hold keyboard TODO
210 // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
211 void Scan_lockKeyboard( void )
215 void Scan_unlockKeyboard( void )
220 void Scan_resetKeyboard( void )
222 // Empty buffer, now that keyboard has been reset
223 KeyIndex_BufferUsed = 0;
226 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )