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 <avr/interrupt.h>
27 #include <util/delay.h>
34 #include "scan_loop.h"
38 // ----- Defines -----
41 #define DATA_PORT PORTC
45 #define CLOCK_PORT PORTC
46 #define CLOCK_DDR DDRC
49 #define RESET_PORT PORTF
50 #define RESET_DDR DDRF
56 // Make sure we haven't overflowed the buffer
57 #define bufferAdd(byte) \
58 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
59 KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
63 // ----- Variables -----
65 // Buffer used to inform the macro processing module which keys have been detected as pressed
66 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
67 volatile uint8_t KeyIndex_BufferUsed;
68 volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
70 volatile uint8_t currentWaveState = 0;
74 volatile uint8_t BufferReadyToClear;
78 // ----- Function Declarations -----
80 void processKeyValue( uint8_t keyValue );
81 void removeKeyValue( uint8_t keyValue );
85 // ----- Interrupt Functions -----
87 // Generates a constant external clock
88 ISR( TIMER1_COMPA_vect )
90 if ( currentWaveState )
92 CLOCK_PORT &= ~(1 << CLOCK_PIN);
97 CLOCK_PORT |= (1 << CLOCK_PIN);
104 // ----- Functions -----
107 inline void scan_setup()
109 // Setup Timer Pulse (16 bit)
111 // TODO Clock can be adjusted to whatever (read chip datasheets for limits)
112 // 16 MHz / (2 * Prescaler * (1 + OCR1A)) = 1200.1 baud
114 // Twice every 1200 baud (actually 1200.1, timer isn't accurate enough)
115 // This is close to 820 us, but a bit slower
120 TIMSK1 = (1 << OCIE1A);
121 CLOCK_DDR = (1 << CLOCK_PIN);
125 // Initially buffer doesn't need to be cleared (it's empty...)
126 BufferReadyToClear = 0;
128 // InputSignal is off by default
129 KeyIndex_Add_InputSignal = 0x00;
131 // Reset the keyboard before scanning, we might be in a wierd state
132 scan_resetKeyboard();
136 // Main Detection Loop
137 // Since this function is non-interruptable, we can do checks here on what stage of the
138 // output clock we are at (0 or 1)
139 // We are looking for a start of packet
140 // If detected, all subsequent bits are then logged into a variable
141 // Once the end of the packet has been detected (always the same length), decode the pressed keys
142 inline uint8_t scan_loop()
147 void processKeyValue( uint8_t keyValue )
149 // Interpret scan code
152 case 0x00: // Break code from input?
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 );
163 // Only send data if enabled
164 if ( KeyIndex_Add_InputSignal )
165 scan_sendData( KeyIndex_Add_InputSignal );
169 // Key already in the buffer
170 if ( KeyIndex_Buffer[c] == keyValue )
177 void removeKeyValue( uint8_t keyValue )
179 // Check for the released key, and shift the other keys lower on the buffer
181 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
183 // Key to release found
184 if ( KeyIndex_Buffer[c] == keyValue )
186 // Shift keys from c position
187 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
188 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
191 KeyIndex_BufferUsed--;
197 // Error case (no key to release)
198 if ( c == KeyIndex_BufferUsed + 1 )
202 hexToStr( keyValue, tmpStr );
203 erro_dPrint( "Could not find key to release: ", tmpStr );
208 uint8_t scan_sendData( uint8_t dataPayload )
213 // Signal KeyIndex_Buffer that it has been properly read
214 void scan_finishedWithBuffer( void )
218 // Signal that the keys have been properly sent over USB
219 void scan_finishedWithUSBBuffer( void )
223 // Reset/Hold keyboard
224 // NOTE: Does nothing with the BETKB
225 void scan_lockKeyboard( void )
229 // NOTE: Does nothing with the BETKB
230 void scan_unlockKeyboard( void )
235 void scan_resetKeyboard( void )
237 // TODO Determine the scan period, and the interval to scan each bit