Formalizing code module structure and inheritance (Large Commit)

- Commenting cleaned up
- usb_keyboard_debug is not finished yet in terms of cleanup (needs DEBUB_PRINT #ifdef's)
- Added LoadFile script generation
- Formalized KeyIndex and USBKeys data containers
- Split parts of scan_loop into Macro, USB, and main.c
- Added interface to USB modules
- Added interface to Macro modules
- Added proper Debug module enable/disable
This commit is contained in:
Jacob Alexander 2011-09-29 22:22:19 -07:00
parent a0439f627b
commit c01efa2d53
29 changed files with 774 additions and 198 deletions

View file

@ -19,13 +19,23 @@
* THE SOFTWARE.
*/
// ----- Includes -----
// Local Includes
#include "scan_loop.h"
// ----- Defines -----
#include <stdint.h>
#include <usb_keyboard_debug.h>
#include <keymap.h>
// Debouncing Defines
#define SAMPLE_THRESHOLD 110
#define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
#define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
// ----- Macros -----
// Loop over all of the sampled keys of the given array
// If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
// This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
@ -34,104 +44,32 @@
table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
} \
// NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
// Other Bits: Pressed state sample counter
#define KEYBOARD_SIZE 23
uint8_t keyboardDetectArray[KEYBOARD_SIZE + 1];
// Interrupt Variable
volatile uint8_t sendKeypresses = 0;
// USB Data Send
void usb_send( uint8_t validKeys )
{
// TODO undo potentially old keys
for ( uint8_t c = validKeys; c < 6; c++ )
keyboard_keys[c] = 0;
// Send keypresses
usb_keyboard_send();
// Clear sendKeypresses Flag
sendKeypresses = 0;
// Clear modifiers
keyboard_modifier_keys = 0;
}
// Given a sampling array, and the current number of detected keypress
// Add as many keypresses from the sampling array to the USB key send array as possible.
void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map ) {
for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
if ( keys[key] & (1 << 7) ) {
pint8( key );
//print(" ");
uint8_t modFound = 0;
// ----- Variables -----
// Determine if the key is a modifier
for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
// Modifier found
if ( modifiers[mod] == key ) {
keyboard_modifier_keys |= map[key];
modFound = 1;
break;
}
}
if ( modFound )
continue;
// Keeps track of the number of scans, so we only do a debounce assess when it would be valid (as it throws away data)
uint8_t scan_count = 0;
// Too many keys
if ( *validKeys == 6 )
break;
// Allow ignoring keys with 0's
if ( map[key] != 0 )
keyboard_keys[(*validKeys)++] = map[key];
}
}
}
// ----- Functions -----
// Main Detection Loop
void scan_loop( void )
void scan_loop()
{
//matrix_pinSetup( matrix_pinout );
uint8_t count = 0;
for ( ;; ) {
//matrix_scan( matrix_pinout, keyboardDetectArray );
//matrix_scan( matrix_pinout, keyboardDetectArray );
// Check count to see if the sample threshold may have been reached, otherwise collect more data
if ( count++ < MAX_SAMPLES )
continue;
// Check count to see if the sample threshold may have been reached, otherwise collect more data
if ( scan_count++ < MAX_SAMPLES )
return;
// Reset Sample Counter
count = 0;
// Reset Sample Counter
scan_count = 0;
// Assess debouncing sample table
//DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
// Send keypresses over USB if the ISR has signalled that it's time
if ( !sendKeypresses )
continue;
// Layout Setup
uint8_t validKeys = 0;
uint8_t *keyboard_MODMASK = keyboard_modifierMask;
uint8_t keyboard_NUMMODS = MODIFIERS_KEYBOARD;
uint8_t *keyboard_MAP = defaultMap;
// TODO Layout Switching
// TODO Macro Processing
// Debounce Sampling Array to USB Data Array
keyPressDetection( keyboardDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_MODMASK, keyboard_NUMMODS, keyboard_MAP );
// Send USB Data
usb_send( validKeys );
}
// Assess debouncing sample table
//DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
}