1 /* Copyright (C) 2011 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
51 // Make sure we haven't overflowed the buffer
52 #define bufferAdd(byte) \
53 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
54 KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
58 // ----- Variables -----
60 // Buffer used to inform the macro processing module which keys have been detected as pressed
61 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
62 volatile uint8_t KeyIndex_BufferUsed;
66 // ----- Functions -----
69 inline void scan_setup()
71 // Setup the the USART interface for keyboard data input
74 // 16 MHz / ( 16 * Baud ) = UBRR
75 // Baud <- 0.10450 ms per bit, thus 1000 / 0.10450 = 9569.4
77 // To deal with the 0.5, setting to double speed, which means UBRR = 209
78 uint16_t baud = 209; // Max setting of 4095
79 UBRR1H = (uint8_t)(baud >> 8);
80 UBRR1L = (uint8_t)baud;
82 // Enable Double Read Speed
85 // Enable the receiver, transitter, and RX Complete Interrupt
88 // Set frame format: 8 data, no stop bits or parity
89 // Asynchrounous USART mode
92 // Set Speaker Pin to Pull-Up gives a low-volume click (XXX no other setting does, why?)
93 SPKR_DDR &= ~(1 << SPKR_POS);
94 SPKR_PORT |= (1 << SPKR_POS);
96 // Set Power Pin (I've traced this back to the "Power On" Switch, but I don't really know what it's for)
97 // 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
98 // XXX Currently not used by the controller
99 POWR_DDR &= ~(1 << POWR_POS);
100 POWR_PORT |= (1 << POWR_POS);
102 // Reset the keyboard before scanning, we might be in a wierd state
103 scan_resetKeyboard();
106 // Main Detection Loop
107 // Not needed for the Sony NEWS, this is just a busy loop
108 inline uint8_t scan_loop()
113 void processKeyValue( uint8_t keyValue )
115 // Detect release condition
116 uint8_t release = keyValue & 0x80;
118 // Finalize output buffer
125 // Check for the released key, and shift the other keys lower on the buffer
127 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
129 // Key to release found
130 if ( KeyIndex_Buffer[c] == keyValue )
132 // Shift keys from c position
133 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
134 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
137 KeyIndex_BufferUsed--;
143 // Error case (no key to release)
144 if ( c == KeyIndex_BufferUsed + 1 )
148 hexToStr( keyValue, tmpStr );
149 erro_dPrint( "Could not find key to release: ", tmpStr );
152 // Press or Repeated Key
155 // Make sure the key isn't already in the buffer
156 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
158 // Key isn't in the buffer yet
159 if ( c == KeyIndex_BufferUsed )
161 bufferAdd( keyValue );
165 // Key already in the buffer
166 if ( KeyIndex_Buffer[c] == keyValue )
172 // USART Receive Buffer Full Interrupt
175 cli(); // Disable Interrupts
177 uint8_t keyValue = 0x00;
179 // One scancode at a time (fastest interval ~3.95 ms - recorded, should still be ok for interrupt polling)
180 // Read the raw packet from the USART
185 hexToStr( keyValue, tmpStr );
186 dPrintStrs( tmpStr, " " );
188 // Process the scancode
189 if ( keyValue != 0x00 )
190 processKeyValue( keyValue );
192 sei(); // Re-enable Interrupts
195 // Send data to keyboard
196 uint8_t scan_sendData( uint8_t dataPayload )
200 hexToStr( dataPayload, tmpStr );
201 info_dPrint( tmpStr, " " );
207 // Signal KeyIndex_Buffer that it has been properly read
208 // Not needed as a signal is sent to remove key-presses
209 void scan_finishedWithBuffer( uint8_t sentKeys )
214 // Reset/Hold keyboard TODO
215 // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
216 void scan_lockKeyboard( void )
220 void scan_unlockKeyboard( void )
225 void scan_resetKeyboard( void )
227 // Empty buffer, now that keyboard has been reset
228 KeyIndex_BufferUsed = 0;
231 void scan_finishedWithUSBBuffer( uint8_t sentKeys )