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
24 #include "usb_keyboard_debug.h"
27 #define SAMPLE_THRESHOLD 110
28 #define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
29 // Loop over all of the sampled keys of the given array
30 // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
31 // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
32 #define DEBOUNCE_ASSESS(table,size) \
33 for ( uint8_t key = 1; key < size + 1; key++ ) {\
34 table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
37 // NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
38 // Other Bits: Pressed state sample counter
39 #define KEYBOARD_SIZE 23
40 uint8_t keyboardDetectArray[KEYBOARD_SIZE + 1];
43 volatile uint8_t sendKeypresses = 0;
46 void usb_send( uint8_t validKeys )
48 // TODO undo potentially old keys
49 for ( uint8_t c = validKeys; c < 6; c++ )
55 // Clear sendKeypresses Flag
59 keyboard_modifier_keys = 0;
63 // Given a sampling array, and the current number of detected keypress
64 // Add as many keypresses from the sampling array to the USB key send array as possible.
65 void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map ) {
66 for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
67 if ( keys[key] & (1 << 7) ) {
72 // Determine if the key is a modifier
73 for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
75 if ( modifiers[mod] == key ) {
76 keyboard_modifier_keys |= map[key];
85 if ( *validKeys == 6 )
88 // Allow ignoring keys with 0's
90 keyboard_keys[(*validKeys)++] = map[key];
96 // Main Detection Loop
97 void scan_loop( void )
99 //matrix_pinSetup( matrix_pinout );
103 //matrix_scan( matrix_pinout, keyboardDetectArray );
105 // Check count to see if the sample threshold may have been reached, otherwise collect more data
106 if ( count++ < MAX_SAMPLES )
109 // Reset Sample Counter
112 // Assess debouncing sample table
113 //DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
115 // Send keypresses over USB if the ISR has signalled that it's time
116 if ( !sendKeypresses )
120 uint8_t validKeys = 0;
122 uint8_t *keyboard_MODMASK = keyboard_modifierMask;
123 uint8_t keyboard_NUMMODS = MODIFIERS_KEYBOARD;
124 uint8_t *keyboard_MAP = defaultMap;
126 // TODO Layout Switching
128 // TODO Macro Processing
130 // Debounce Sampling Array to USB Data Array
131 keyPressDetection( keyboardDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_MODMASK, keyboard_NUMMODS, keyboard_MAP );
134 usb_send( validKeys );