#include <Lib/ScanLib.h>
// Project Includes
+#include <cli.h>
#include <led.h>
#include <print.h>
#define STROBE_SETTLE 1
-#define TEST_KEY_STROBE (0x05)
-#define TEST_KEY_MASK (1 << 0)
-
#define ADHSM 7
#define RIGHT_JUSTIFY 0
#define MUXES_COUNT 8
#define MUXES_COUNT_XSHIFT 3
-#define WARMUP_LOOPS ( 1024 )
+#define WARMUP_LOOPS ( 2048 )
#define WARMUP_STOP (WARMUP_LOOPS - 1)
#define SAMPLE_CONTROL 3
+// ----- Function Declarations -----
+
+// CLI Functions
+void cliFunc_avgDebug ( char* args );
+void cliFunc_echo ( char* args );
+void cliFunc_keyDebug ( char* args );
+void cliFunc_pressDebug( char* args );
+void cliFunc_senseDebug( char* args );
+
+// Debug Functions
+void dumpSenseTable();
+
+// High-level Capsense Functions
+void setup_ADC();
+void capsense_scan();
+
+// Capsense Sense Functions
+void testColumn ( uint8_t strobe );
+void sampleColumn( uint8_t column );
+
+// Low-level Capsense Functions
+void strobe_w( uint8_t strobe_num );
+void recovery( uint8_t on );
+
+
+
// ----- Variables -----
// Buffer used to inform the macro processing module which keys have been detected as pressed
volatile uint8_t KeyIndex_BufferUsed;
-// TODO dfj variables...needs cleaning up and commenting
+// Scan Module command dictionary
+char* scanCLIDictName = "DPH Module Commands";
+CLIDictItem scanCLIDict[] = {
+ { "echo", "Example command, echos the arguments.", cliFunc_echo },
+ { "avgDebug", "Enables/Disables averaging results." NL "\t\tDisplays each average, starting from Key 0x00, ignoring 0 valued averages.", cliFunc_avgDebug },
+ { "keyDebug", "Enables/Disables long debug for each keypress." NL "\t\tkeycode - [strobe:mux] : sense val : threshold+delta=total : margin", cliFunc_keyDebug },
+ { "pressDebug", "Enables/Disables short debug for each keypress.", cliFunc_pressDebug },
+ { "senseDebug", "Prints out the current sense table N times." NL "\t\tsense:max sense:delta", cliFunc_senseDebug },
+ { 0, 0, 0 } // Null entry for dictionary end
+};
+
+// CLI Control Variables
+uint8_t enableAvgDebug = 0;
+uint8_t enableKeyDebug = 0;
+uint8_t enablePressDebug = 1;
+uint8_t senseDebugCount = 3; // In order to get boot-time oddities
+
// Variables used to calculate the starting sense value (averaging)
uint32_t full_avg = 0;
uint8_t low_count = 0;
-uint8_t ze_strober = 0;
-
-uint16_t samples[MUXES_COUNT];
+uint16_t samples[MAX_STROBES][MUXES_COUNT]; // Overall table of cap sense ADC values
+uint16_t sampleMax[MAX_STROBES][MUXES_COUNT]; // Records the max seen ADC value
-uint8_t cur_keymap[MAX_STROBES];
-
-uint8_t keymap_change;
+uint8_t key_activity = 0; // Increments for each detected key per each full scan of the keyboard, it is reset before each full scan
+uint8_t key_release = 0; // Indicates if going from key press state to release state (some keys pressed to no keys pressed)
uint16_t threshold = THRESHOLD;
-uint8_t column = 0;
-
uint16_t keys_averages_acc[KEY_COUNT];
uint16_t keys_averages [KEY_COUNT];
uint8_t keys_debounce [KEY_COUNT]; // Contains debounce statistics
uint8_t keys_problem [KEY_COUNT]; // Marks keys that should be ignored (determined by averaging at startup)
-uint8_t full_samples[KEY_COUNT];
-
// TODO: change this to 'booting', then count down.
uint16_t boot_count = 0;
-uint16_t idle_count = 0;
-uint8_t idle = 1;
-
-uint8_t error = 0;
-uint16_t error_data = 0;
-
uint8_t total_strobes = MAX_STROBES;
uint8_t strobe_map[MAX_STROBES];
-uint8_t dump_count = 0;
-
-
-
-// ----- Function Declarations -----
-
-void dump( void );
-
-void recovery( uint8_t on );
-
-int sampleColumn( uint8_t column );
-
-void capsense_scan( void );
-
-void setup_ADC( void );
-
-void strobe_w( uint8_t strobe_num );
-
-uint8_t testColumn( uint8_t strobe );
-
// ----- Functions -----
// Initial setup for cap sense controller
inline void Scan_setup()
{
+ // Register Scan CLI dictionary
+ CLI_registerDictionary( scanCLIDict, scanCLIDictName );
+
// TODO dfj code...needs cleanup + commenting...
setup_ADC();
// Hardcoded strobes for debugging
// Strobes start at 0 and go to 17 (18), not all Model Fs use all of the available strobes
// The single row ribbon connector Model Fs only have a max of 16 strobes
-#define KISHSAVER_STROBE
+#define KEYPAD_50KEY
+//#define KISHSAVER_STROBE
//#define KISHSAVER_OLD_STROBE
//#define TERMINAL_6110668_OLD_STROBE
//#define UNSAVER_OLD_STROBE
strobe_map[6] = 9;
strobe_map[7] = 8;
strobe_map[8] = 2; // Test point strobe (3 test points, sense 1, 4, 5)
+#elif defined(KEYPAD_50KEY)
+ total_strobes = 8;
+
+ strobe_map[0] = 14;
+ strobe_map[1] = 13;
+ strobe_map[2] = 12;
+ strobe_map[3] = 11;
+ strobe_map[4] = 10;
+ strobe_map[5] = 9;
+ strobe_map[6] = 8;
+ strobe_map[7] = 0;
#elif defined(TERMINAL_6110668_OLD_STROBE)
total_strobes = 16;
// TODO
#endif
- // TODO all this code should probably be in Scan_resetKeyboard
- for ( int i = 0; i < total_strobes; ++i)
- {
- cur_keymap[i] = 0;
- }
-
// Reset debounce table
for ( int i = 0; i < KEY_COUNT; ++i )
{
{
sampleColumn( strobe_map[i] );
}
-
-
- // Reset the keyboard before scanning, we might be in a wierd state
- // Also sets the KeyIndex_BufferUsed to 0
- scan_resetKeyboard();
}
{
capsense_scan();
- // Error case, should not occur in normal operation
- if ( error )
- {
- erro_msg("Problem detected... ");
-
- // Keymap scan debug
- for ( uint8_t i = 0; i < total_strobes; ++i )
- {
- printHex(cur_keymap[strobe_map[i]]);
- print(" ");
- }
-
- print(" : ");
- printHex(error);
- error = 0;
- print(" : ");
- printHex(error_data);
- error_data = 0;
-
- // Display keymaps and other debug information if warmup completede
- if ( boot_count >= WARMUP_LOOPS )
- {
- dump();
- }
- }
-
-
// Return non-zero if macro and USB processing should be delayed
// Macro processing will always run if returning 0
// USB processing only happens once the USB send timer expires, if it has not, Scan_loop will be called
high_count = 0;
+ // Reset key activity, if there is no key activity, averages will accumulate for sense deltas, otherwise they will be reset
+ key_activity = 0;
+
// Scan each of the mapped strobes in the matrix
for ( uint8_t strober = 0; strober < total_strobes; ++strober )
{
uint8_t map_strobe = strobe_map[strober];
- uint8_t tries = 1;
- while ( tries++ && sampleColumn( map_strobe ) ) { tries &= 0x7; } // don't waste this one just because the last one was poop.
+ // Sample the ADCs for the given column/strobe
+ sampleColumn( map_strobe );
// Only process sense data if warmup is finished
if ( boot_count >= WARMUP_LOOPS )
{
- column = testColumn( map_strobe );
-
- idle |= column; // if column has any pressed keys, then we are not idle.
-
- // TODO Is this needed anymore? Really only helps debug -HaaTa
- if( column != cur_keymap[map_strobe] && ( boot_count >= WARMUP_LOOPS ) )
- {
- cur_keymap[map_strobe] = column;
- keymap_change = 1;
- }
-
- idle |= keymap_change; // if any keys have changed inc. released, then we are not idle.
- }
-
- if ( error == 0x50 )
- {
- error_data |= (((uint16_t)map_strobe) << 12);
+ testColumn( map_strobe );
}
uint8_t strobe_line = map_strobe << MUXES_COUNT_XSHIFT;
- for ( int i = 0; i < MUXES_COUNT; ++i )
+ for ( int mux = 0; mux < MUXES_COUNT; ++mux )
{
// discard sketchy low bit, and meaningless high bits.
- uint8_t sample = samples[i] >> 1;
- full_samples[strobe_line + i] = sample;
- keys_averages_acc[strobe_line + i] += sample;
+ uint8_t sample = samples[map_strobe][mux] >> 1;
+ keys_averages_acc[strobe_line + mux] += sample;
}
// Accumulate 3 total averages (used for determining starting average during warmup)
// low_avg - Average of all sampled lines below or equal to full_avg
if ( boot_count < WARMUP_LOOPS )
{
- for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
+ for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux )
{
- uint8_t sample = samples[i] >> 1;
+ uint8_t sample = samples[map_strobe][mux] >> 1;
// Sample is high, add it to high avg
if ( sample > full_avg )
}
// If sample is higher than previous high_avg, then mark as "problem key"
- keys_problem[strobe_line + i] = sample > high_avg ? sample : 0;
+ keys_problem[strobe_line + mux] = sample > high_avg ? sample : 0;
// Prepare for next average
cur_full_avg += sample;
}
}
-#ifdef VERIFY_TEST_PAD
- // verify test key is not down.
- if ( ( cur_keymap[TEST_KEY_STROBE] & TEST_KEY_MASK ) )
- {
- error = 0x05;
- error_data = cur_keymap[TEST_KEY_STROBE] << 8;
- error_data += full_samples[TEST_KEY_STROBE * 8];
- }
-#endif
-
- /** aggregate if booting, or if idle;
- * else, if not booting, check for dirty USB.
- * */
-
- idle_count++;
- idle_count &= IDLE_COUNT_MASK;
-
// Warm up voltage references
if ( boot_count < WARMUP_LOOPS )
{
break;
// Last loop
case WARMUP_STOP:
- print("\n");
+ print( NL );
info_msg("Warmup finished using ");
printInt16( WARMUP_LOOPS );
- print(" iterations\n");
+ print(" iterations" NL );
// Display the final calculated averages of all the sensed strobes
info_msg("Full average (");
printInt8( low_count );
print("): ");
printHex( low_avg );
- print("\n");
+ print( NL );
// Display problem keys, and the sense value at the time
for ( uint8_t key = 0; key < KEY_COUNT; key++ )
printHex( key );
print(" (");
printHex( keys_problem[key] );
- print(")\n");
+ print(")" NL );
}
}
}
else
{
- // Reset accumulators and idle flag/counter
- if ( keymap_change )
+ // No keypress, accumulate averages
+ if( !key_activity )
{
- for ( uint8_t c = 0; c < KEY_COUNT; ++c ) { keys_averages_acc[c] = 0; }
- idle_count = 0;
- idle = 0;
-
- keymap_change = 0;
- }
+ // Average Debugging
+ if ( enableAvgDebug )
+ {
+ print("\033[1mAvg\033[0m: ");
+ }
- if ( !idle_count )
- {
- if( idle )
+ // aggregate
+ for ( uint8_t i = 0; i < KEY_COUNT; ++i )
{
- // aggregate
- for ( uint8_t i = 0; i < KEY_COUNT; ++i )
- {
- uint16_t acc = keys_averages_acc[i] >> IDLE_COUNT_SHIFT;
- uint32_t av = keys_averages[i];
+ uint16_t acc = keys_averages_acc[i];
+ //uint16_t acc = keys_averages_acc[i] >> IDLE_COUNT_SHIFT; // XXX This fixes things... -HaaTa
+ uint32_t av = keys_averages[i];
- av = (av << KEYS_AVERAGES_MIX_SHIFT) - av + acc;
- av >>= KEYS_AVERAGES_MIX_SHIFT;
+ av = (av << KEYS_AVERAGES_MIX_SHIFT) - av + acc;
+ av >>= KEYS_AVERAGES_MIX_SHIFT;
- keys_averages[i] = av;
- keys_averages_acc[i] = 0;
+ keys_averages[i] = av;
+ keys_averages_acc[i] = 0;
+
+ // Average Debugging
+ if ( enableAvgDebug && av > 0 )
+ {
+ printHex( av );
+ print(" ");
}
}
- if ( boot_count >= WARMUP_LOOPS )
+ // Average Debugging
+ if ( enableAvgDebug )
{
- dump();
+ print( NL );
}
+
+ // No key presses detected, set key_release indicator
+ key_release = 1;
}
+ // Keypresses, reset accumulators
+ else if ( key_release )
+ {
+ for ( uint8_t c = 0; c < KEY_COUNT; ++c ) { keys_averages_acc[c] = 0; }
+ key_release = 0;
+ }
+
+ // If the debugging sense table is non-zero, display
+ if ( senseDebugCount > 0 )
+ {
+ senseDebugCount--;
+ print( NL );
+ dumpSenseTable();
+ }
}
}
}
-int sampleColumn_8x( uint8_t column, uint16_t * buffer )
+void sampleColumn( uint8_t column )
{
// ensure all probe lines are driven low, and chill for recovery delay.
ADCSRA |= (1 << ADEN) | (1 << ADSC); // enable and start conversions
SET_FULL_MUX( mux + 1 ); // our *next* sample will use this
// retrieve current read.
- buffer[mux] = getADC();
+ uint16_t readVal = getADC();
+ samples[column][mux] = readVal;
+
+ // Update max sense sample table
+ if ( readVal > sampleMax[column][mux] )
+ {
+ sampleMax[column][mux] = readVal;
+ }
+
mux++;
} while ( mux < 8 );
PORTC &= ~C_MASK;
PORTD &= ~D_MASK;
PORTE &= ~E_MASK;
-
- return 0;
-}
-
-
-int sampleColumn( uint8_t column )
-{
- int rval = 0;
-
- rval = sampleColumn_8x( column, samples );
-
- return rval;
}
-uint8_t testColumn( uint8_t strobe )
+void testColumn( uint8_t strobe )
{
uint16_t db_delta = 0;
uint8_t db_sample = 0;
{
// If the sample value of the problem key goes below full_avg (overall initial average)
// re-enable the key
- if ( (db_sample = samples[mux] >> 1) < full_avg )
+ if ( (db_sample = samples[strobe][mux] >> 1) < full_avg )
{
info_msg("Re-enabling problem key: ");
printHex( key );
- print("\n");
+ print( NL );
keys_problem[key] = 0;
}
- // Otherwise, don't waste any more cycles processing the problem key
- else
- {
- continue;
- }
+
+ // Do not waste any more cycles processing, regardless, a keypress cannot be detected
+ continue;
}
// Keypress detected
// db_sample (uint8_t), discard meaningless high bit, and garbage low bit
- if ( (db_sample = samples[mux] >> 1) > (db_threshold = threshold) + (db_delta = delta) )
+ if ( (db_sample = samples[strobe][mux] >> 1) > (db_threshold = threshold) + (db_delta = delta) )
{
column |= bit;
+ key_activity++; // No longer idle, stop averaging ADC data
// Only register keypresses once the warmup is complete, or not enough debounce info
if ( keys_debounce[key] <= DEBOUNCE_THRESHOLD )
// Automatically handles converting to a USB code and sending off to the PC
if ( keys_debounce[key] == DEBOUNCE_THRESHOLD )
{
-//#define KEYSCAN_DEBOUNCE_DEBUG
-#ifdef KEYSCAN_DEBOUNCE_DEBUG
- // Debug message
- print("0x");
- printHex_op( key, 2 );
- print(" ");
-#endif
+ // Debug message, pressDebug CLI
+ if ( enablePressDebug )
+ {
+ print("0x");
+ printHex_op( key, 2 );
+ print(" ");
+ }
// Only add the key to the buffer once
// NOTE: Buffer can easily handle multiple adds, just more efficient
keys_debounce[key]++;
-#define KEYSCAN_THRESHOLD_DEBUG
-#ifdef KEYSCAN_THRESHOLD_DEBUG
+ }
+
+ // Long form key debugging
+ if ( enableKeyDebug )
+ {
// Debug message
// <key> [<strobe>:<mux>] : <sense val> : <delta + threshold> : <margin>
dbug_msg("0x");
printHex( db_threshold + db_delta ); // Sense compare
print(" : ");
printHex( db_sample - ( db_threshold + db_delta ) ); // Margin
- print("\n");
-#endif
+ print( NL );
}
}
// Clear debounce entry if no keypress detected
bit <<= 1;
}
- return column;
}
-void dump(void) {
+void dumpSenseTable()
+{
+ // Initial table alignment, with base threshold used for every key
+ print("\033[1m");
+ printHex( threshold );
+ print("\033[0m ");
-#ifdef DEBUG_FULL_SAMPLES_AVERAGES
- // we don't want to debug-out during the measurements.
- if ( !dump_count )
+ // Print out headers first
+ for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux )
{
- // Averages currently set per key
- for ( int i = 0; i < KEY_COUNT; ++i )
- {
- if ( !(i & 0x0f) )
- {
- print("\n");
- }
- else if ( !(i & 0x07) )
- {
- print(" ");
- }
+ print(" Mux \033[1m");
+ printInt8( mux );
+ print("\033[0m ");
+ }
- print(" ");
- printHex( keys_averages[i] );
- }
+ print( NL );
+
+ // Display the full strobe/sense table
+ for ( uint8_t strober = 0; strober < total_strobes; ++strober )
+ {
+ uint8_t strobe = strobe_map[strober];
- print("\n");
+ // Display the strobe
+ print("Strobe \033[1m");
+ printHex( strobe );
+ print("\033[0m ");
- // Previously read full ADC scans?
- for ( int i = 0; i< KEY_COUNT; ++i)
+ // For each mux, display sense:threshold:delta
+ for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux )
{
- if ( !(i & 0x0f) )
- {
- print("\n");
- }
- else if ( !(i & 0x07) )
+ uint8_t delta = keys_averages[(strobe << MUXES_COUNT_XSHIFT) + mux];
+ uint8_t sample = samples[strobe][mux] >> 1;
+ uint8_t max = sampleMax[strobe][mux] >> 1;
+
+ // Indicate if the key is being pressed by displaying green
+ if ( sample > delta + threshold )
{
- print(" ");
+ print("\033[1;32m");
}
- print(" ");
- printHex(full_samples[i]);
+ printHex_op( sample, 2 );
+ print(":");
+ printHex_op( max, 2 );
+ print(":");
+ printHex_op( delta, 2 );
+ print("\033[0m ");
}
+
+ // New line for each strobe
+ print( NL );
}
-#endif
+}
-#ifdef DEBUG_STROBE_SAMPLES_AVERAGES
- // Per strobe information
- uint8_t cur_strober = ze_strober;
- print("\n");
- printHex(cur_strober);
+// ----- CLI Command Functions -----
- // Previously read ADC scans on current strobe
- print(" :");
- for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
+// XXX Just an example command showing how to parse arguments (more complex than generally needed)
+void cliFunc_echo( char* args )
+{
+ char* curArgs;
+ char* arg1Ptr;
+ char* arg2Ptr = args;
+
+ // Parse args until a \0 is found
+ while ( 1 )
{
- print(" ");
- printHex(full_samples[(cur_strober << MUXES_COUNT_XSHIFT) + i]);
+ print( NL ); // No \r\n by default after the command is entered
+
+ curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
+ CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
+
+ // Stop processing args if no more are found
+ if ( *arg1Ptr == '\0' )
+ break;
+
+ // Print out the arg
+ dPrint( arg1Ptr );
}
+}
- // Averages current set on current strobe
- print(" :");
+void cliFunc_avgDebug( char* args )
+{
+ print( NL );
- for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
+ // Args ignored, just toggling
+ if ( enableAvgDebug )
{
- print(" ");
- printHex(keys_averages[(cur_strober << MUXES_COUNT_XSHIFT) + i]);
+ info_print("Cap Sense averaging debug disabled.");
+ enableAvgDebug = 0;
}
+ else
+ {
+ info_print("Cap Sense averaging debug enabled.");
+ enableAvgDebug = 1;
+ }
+}
-#endif
+void cliFunc_keyDebug( char* args )
+{
+ print( NL );
+
+ // Args ignored, just toggling
+ if ( enableKeyDebug )
+ {
+ info_print("Cap Sense key long debug disabled - pre debounce.");
+ enableKeyDebug = 0;
+ }
+ else
+ {
+ info_print("Cap Sense key long debug enabled - pre debounce.");
+ enableKeyDebug = 1;
+ }
+}
-#ifdef DEBUG_USB_KEYMAP
- print("\n ");
+void cliFunc_pressDebug( char* args )
+{
+ print( NL );
- // Current keymap values
- for ( uint8_t i = 0; i < total_strobes; ++i )
+ // Args ignored, just toggling
+ if ( enablePressDebug )
{
- printHex(cur_keymap[i]);
- print(" ");
+ info_print("Cap Sense key debug disabled - post debounce.");
+ enablePressDebug = 0;
}
-#endif
+ else
+ {
+ info_print("Cap Sense key debug enabled - post debounce.");
+ enablePressDebug = 1;
+ }
+}
- ze_strober++;
- ze_strober &= 0xf;
+void cliFunc_senseDebug( char* args )
+{
+ // Parse code from argument
+ // NOTE: Only first argument is used
+ char* arg1Ptr;
+ char* arg2Ptr;
+ CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
+
+ // Default to a single print
+ senseDebugCount = 1;
- dump_count++;
- dump_count &= 0x0f;
+ // If there was an argument, use that instead
+ if ( *arg1Ptr != '\0' )
+ {
+ senseDebugCount = decToInt( arg1Ptr );
+ }
}