]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/matrix/scan_loop.c
Reorganization for use with the CMake "Modules"
[kiibohd-controller.git] / Scan / matrix / scan_loop.c
1 /* Copyright (C) 2011 by Jacob Alexander
2  * 
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:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
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
19  * THE SOFTWARE.
20  */
21
22
23 #include <stdint.h>
24 #include <usb_keyboard_debug.h>
25 #include <keymap.h>
26 // Debouncing Defines
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; \
35                         } \
36
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];
41
42 // Interrupt Variable
43 volatile uint8_t sendKeypresses = 0;
44
45 // USB Data Send
46 void usb_send( uint8_t validKeys )
47 {
48                 // TODO undo potentially old keys
49                 for ( uint8_t c = validKeys; c < 6; c++ )
50                         keyboard_keys[c] = 0;
51
52                 // Send keypresses
53                 usb_keyboard_send();
54
55                 // Clear sendKeypresses Flag
56                 sendKeypresses = 0;
57
58                 // Clear modifiers
59                 keyboard_modifier_keys = 0;
60 }
61
62
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) ) {
68                         pint8( key );
69                         //print(" ");
70                         uint8_t modFound = 0;
71
72                         // Determine if the key is a modifier
73                         for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
74                                 // Modifier found
75                                 if ( modifiers[mod] == key ) {
76                                         keyboard_modifier_keys |= map[key];
77                                         modFound = 1;
78                                         break;
79                                 }
80                         }
81                         if ( modFound )
82                                 continue;
83
84                         // Too many keys
85                         if ( *validKeys == 6 )
86                                 break;
87
88                         // Allow ignoring keys with 0's
89                         if ( map[key] != 0 )
90                                 keyboard_keys[(*validKeys)++] = map[key];
91                 }
92         }
93 }
94
95
96 // Main Detection Loop
97 void scan_loop( void )
98 {
99         //matrix_pinSetup( matrix_pinout );
100         uint8_t count = 0;
101
102         for ( ;; ) {
103                 //matrix_scan( matrix_pinout, keyboardDetectArray );
104
105                 // Check count to see if the sample threshold may have been reached, otherwise collect more data
106                 if ( count++ < MAX_SAMPLES )
107                         continue;
108
109                 // Reset Sample Counter
110                 count = 0;
111
112                 // Assess debouncing sample table
113                 //DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
114
115                 // Send keypresses over USB if the ISR has signalled that it's time
116                 if ( !sendKeypresses )
117                         continue;
118
119                 // Layout Setup
120                 uint8_t validKeys = 0;
121
122                 uint8_t *keyboard_MODMASK = keyboard_modifierMask;
123                 uint8_t  keyboard_NUMMODS = MODIFIERS_KEYBOARD;
124                 uint8_t *keyboard_MAP     = defaultMap;
125
126                 // TODO Layout Switching
127
128                 // TODO Macro Processing
129
130                 // Debounce Sampling Array to USB Data Array
131                 keyPressDetection( keyboardDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_MODMASK, keyboard_NUMMODS, keyboard_MAP );
132
133                 // Send USB Data
134                 usb_send( validKeys );
135         }
136 }
137