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,8 +19,15 @@
* THE SOFTWARE.
*/
// ----- Includes -----
// Local Includes
#include "matrix.h"
// ----- Macros -----
#define REG_SET(reg) reg |= (1 << ( matrix[row][col] % 10 ) )
#define PIN_SET_COL(pin) \
@ -59,6 +66,15 @@
break
// ----- Variables -----
uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
// ----- Functions -----
void matrix_pinSetup( uint8_t *matrix )
{
// Setup the variables

View file

@ -22,6 +22,12 @@
#ifndef __MATRIX_H
#define __MATRIX_H
// ----- Includes -----
// Compiler Includes
#include <stdint.h>
// ----- Quick Map (don't change) -----
#define pinA0 0
#define pinA1 1
@ -97,12 +103,13 @@
// ----- Key Settings -----
#define keyboardSize 16 // # of keys
#define KEYBOARD_SIZE 16 // # of keys
#define MAX_ROW_SIZE 16 // # of keys in the largest row
// ----- Matrix Configuration -----
static uint8_t matrix_pinout[][] = {
static const uint8_t matrix_pinout[][MAX_ROW_SIZE + 1] = {
@ -111,6 +118,7 @@ static uint8_t matrix_pinout[][] = {
// The mode allows for optimization in the kind of scanning algorithms that are done
//
// The key numbers are used to translate into the keymap table (array) (and always start from 1, not 0).
// Thus if a row doesn't use all the key positions, you can denote it as 0, which will be ignored/skipped on each scan
// See the keymap.h file for the various preconfigured arrays.
// Scan Mode | Col 1 | Col 2 | Col 3 | Col 4 | Col 4 | ...
@ -135,11 +143,13 @@ static uint8_t matrix_pinout[][] = {
};
// ----- Variables -----
// NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
// Other Bits: Pressed state sample counter
uint8_t keyboardDetectArray[keyboardSize + 1];
extern uint8_t KeyIndex_Array [KEYBOARD_SIZE + 1];
static const uint8_t KeyIndex_Size = KEYBOARD_SIZE;

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)
}

View file

@ -22,8 +22,14 @@
#ifndef __SCAN_LOOP_H
#define __SCAN_LOOP_H
//extern uint8_t keyboardDetectArray[KEYBOARDZ
extern volatile uint8_t sendKeypresses;
// ----- Includes -----
// Local Includes
#include "matrix.h"
// ----- Functions -----
void scan_loop( void );