/* Copyright (C) 2011-2013 by Joseph Makuch * Additions by Jacob Alexander (2013-2014) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ // ----- Includes ----- // Compiler Includes #include // Project Includes #include #include #include #include // Local Includes #include "scan_loop.h" // ----- Defines ----- // TODO dfj defines...needs commenting and maybe some cleaning... #define MAX_PRESS_DELTA_MV 450 // As measured from the Teensy ADC pin #define THRESHOLD_MV (MAX_PRESS_DELTA_MV >> 1) //(2560 / (0x3ff/2)) ~= 5 #define MV_PER_ADC 5 #define THRESHOLD (THRESHOLD_MV / MV_PER_ADC) #define STROBE_SETTLE 1 #define ADHSM 7 // Right justification of ADLAR #define ADLAR_BITS 0 // full muxmask #define FULL_MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2) | (1 << MUX3) | (1 << MUX4)) // F0-f7 pins only muxmask. #define MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2)) // Strobe Masks #define D_MASK (0xff) #define E_MASK (0x03) #define C_MASK (0xff) // set ADC clock prescale #define PRESCALE_MASK ((1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2)) #define PRESCALE_SHIFT (ADPS0) #define PRESCALE 3 // Max number of strobes supported by the hardware // Strobe lines are detected at startup, extra strobes cause anomalies like phantom keypresses #define MAX_STROBES 18 // Number of consecutive samples required to pass debounce #define DEBOUNCE_THRESHOLD 5 // Scans to remain idle after all keys were release before starting averaging // XXX So this makes the initial keypresses fast, // but it's still possible to lose a keypress if you press at the wrong time -HaaTa #define KEY_IDLE_SCANS 30000 // Total number of muxes/sense lines available #define MUXES_COUNT 8 #define MUXES_COUNT_XSHIFT 3 // Number of warm-up loops before starting to scan keys #define WARMUP_LOOPS ( 1024 ) #define WARMUP_STOP (WARMUP_LOOPS - 1) #define SAMPLE_CONTROL 3 #define KEY_COUNT ((MAX_STROBES) * (MUXES_COUNT)) #define RECOVERY_CONTROL 1 #define RECOVERY_SOURCE 0 #define RECOVERY_SINK 2 #define ON 1 #define OFF 0 // mix in 1/4 of the current average to the running average. -> (@mux_mix = 2) #define MUX_MIX 2 #define IDLE_COUNT_SHIFT 8 // av = (av << shift) - av + sample; av >>= shift // e.g. 1 -> (av + sample) / 2 simple average of new and old // 2 -> (3 * av + sample) / 4 i.e. 3:1 mix of old to new. // 3 -> (7 * av + sample) / 8 i.e. 7:1 mix of old to new. #define KEYS_AVERAGES_MIX_SHIFT 3 // ----- Macros ----- // Select mux #define SET_FULL_MUX(X) ((ADMUX) = (((ADMUX) & ~(FULL_MUX_MASK)) | ((X) & (FULL_MUX_MASK)))) // ----- 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_problemKeys( 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_Buffer[KEYBOARD_BUFFER]; volatile uint8_t KeyIndex_BufferUsed; // Scan Module command dictionary const char scanCLIDictName[] = "DPH Module Commands"; const 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 }, { "problemKeys", "Display current list of problem keys,", cliFunc_problemKeys }, { "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; uint32_t high_avg = 0; uint32_t low_avg = 0; uint8_t high_count = 0; uint8_t low_count = 0; 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 key_activity = 0; // Increments for each detected key per each full scan of the keyboard, it is reset before each full scan uint16_t key_idle = 0; // Defines how scans after all keys were released before starting averaging again 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; 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) // TODO: change this to 'booting', then count down. uint16_t boot_count = 0; uint8_t total_strobes = MAX_STROBES; uint8_t strobe_map[MAX_STROBES]; // ----- Functions ----- // Initial setup for cap sense controller inline void Scan_setup() { // Register Scan CLI dictionary CLI_registerDictionary( scanCLIDict, scanCLIDictName ); // Scan for active strobes // NOTE1: On IBM PCBs, each strobe line that is *NOT* used is connected to GND. // This means, the strobe GPIO can be set to Tri-State pull-up to detect which strobe lines are not used. // NOTE2: This will *NOT* detect floating strobes. // NOTE3: Rev 0.4, the strobe numbers are reversed, so D0 is actually strobe 0 and C7 is strobe 17 info_msg("Detecting Strobes..."); DDRC = 0; PORTC = C_MASK; DDRD = 0; PORTD = D_MASK; DDRE = 0; PORTE = E_MASK; // Initially there are 0 strobes total_strobes = 0; // Iterate over each the strobes for ( uint8_t strobe = 0; strobe < MAX_STROBES; strobe++ ) { uint8_t detected = 0; // If PIN is high, then strobe is *NOT* connected to GND and may be a strobe switch ( strobe ) { // Strobe Mappings // Rev Rev // 0.2 0.4 #ifndef REV0_4_DEBUG // XXX These pins should be reworked, and connect to GND on Rev 0.4 case 0: // D0 0 n/c case 1: // D1 1 n/c #endif case 2: // D2 2 15 case 3: // D3 3 14 case 4: // D4 4 13 case 5: // D5 5 12 case 6: // D6 6 11 case 7: // D7 7 10 detected = PIND & (1 << strobe); break; case 8: // E0 8 9 case 9: // E1 9 8 detected = PINE & (1 << (strobe - 8)); break; case 10: // C0 10 7 case 11: // C1 11 6 case 12: // C2 12 5 case 13: // C3 13 4 case 14: // C4 14 3 case 15: // C5 15 2 #ifndef REV0_2_DEBUG // XXX If not using the 18 pin connector on Rev 0.2, rework these pins to GND case 16: // C6 16 1 case 17: // C7 17 0 #endif detected = PINC & (1 << (strobe - 10)); break; default: break; } // Potential strobe line detected if ( detected ) { strobe_map[total_strobes] = strobe; total_strobes++; } } printInt8( total_strobes ); print( " strobes found." NL ); // Setup Pins for Strobing DDRC = C_MASK; PORTC = 0; DDRD = D_MASK; PORTD = 0; DDRE = E_MASK; PORTE = 0 ; // Initialize ADC setup_ADC(); // Reset debounce table for ( int i = 0; i < KEY_COUNT; ++i ) { keys_debounce[i] = 0; } // Warm things up a bit before we start collecting data, taking real samples. for ( uint8_t i = 0; i < total_strobes; ++i ) { sampleColumn( strobe_map[i] ); } } // Main Detection Loop // This is where the important stuff happens inline uint8_t Scan_loop() { capsense_scan(); // 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 // after the macro processing has been completed return 0; } // Signal KeyIndex_Buffer that it has been properly read // NOTE: Only really required for implementing "tricks" in converters for odd protocols void Scan_finishedWithBuffer( uint8_t sentKeys ) { return; } // Signal KeyIndex_Buffer that it has been properly read and sent out by the USB module // NOTE: Only really required for implementing "tricks" in converters for odd protocols void Scan_finishedWithUSBBuffer( uint8_t sentKeys ) { return; } inline void capsense_scan() { // Accumulated average used for the next scan uint32_t cur_full_avg = 0; uint32_t cur_high_avg = 0; // Reset average counters low_avg = 0; low_count = 0; 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]; // Sample the ADCs for the given column/strobe sampleColumn( map_strobe ); // Only process sense data if warmup is finished if ( boot_count >= WARMUP_LOOPS ) { testColumn( map_strobe ); } uint8_t strobe_line = map_strobe << MUXES_COUNT_XSHIFT; for ( int mux = 0; mux < MUXES_COUNT; ++mux ) { // discard sketchy low bit, and meaningless high bits. 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) // full_avg - Average of all sampled lines on the previous scan set // cur_full_avg - Average of all sampled lines for this scan set // high_avg - Average of all sampled lines above full_avg on the previous scan set // cur_high_avg - Average of all sampled lines above full_avg // low_avg - Average of all sampled lines below or equal to full_avg if ( boot_count < WARMUP_LOOPS ) { for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux ) { uint8_t sample = samples[map_strobe][mux] >> 1; // Sample is high, add it to high avg if ( sample > full_avg ) { high_count++; cur_high_avg += sample; } // Sample is low, add it to low avg else { low_count++; low_avg += sample; } // If sample is higher than previous high_avg, then mark as "problem key" // XXX Giving a bit more margin to pass (high_avg vs. high_avg + high_avg - full_avg) -HaaTa keys_problem[strobe_line + mux] = sample > high_avg + (high_avg - full_avg) ? sample : 0; // Prepare for next average cur_full_avg += sample; } } } // for strober // Update total sense average (only during warm-up) if ( boot_count < WARMUP_LOOPS ) { full_avg = cur_full_avg / (total_strobes * MUXES_COUNT); high_avg = cur_high_avg / high_count; low_avg /= low_count; // Update the base average value using the low_avg (best chance of not ignoring a keypress) for ( int i = 0; i < KEY_COUNT; ++i ) { keys_averages[i] = low_avg; keys_averages_acc[i] = low_avg; } } // Warm up voltage references if ( boot_count < WARMUP_LOOPS ) { boot_count++; switch ( boot_count ) { // First loop case 1: // Show msg at first iteration only info_msg("Warming up the voltage references"); break; // Middle iterations case 300: case 600: case 900: case 1200: print("."); break; // Last loop case WARMUP_STOP: print( NL ); info_msg("Warmup finished using "); printInt16( WARMUP_LOOPS ); print(" iterations" NL ); // Display the final calculated averages of all the sensed strobes info_msg("Full average ("); printInt8( total_strobes * MUXES_COUNT ); print("): "); printHex( full_avg ); print(" High average ("); printInt8( high_count ); print("): "); printHex( high_avg ); print(" Low average ("); printInt8( low_count ); print("): "); printHex( low_avg ); print(" Rejection threshold: "); printHex( high_avg + (high_avg - full_avg) ); print( NL ); // Display problem keys, and the sense value at the time for ( uint8_t key = 0; key < KEY_COUNT; key++ ) { if ( keys_problem[key] ) { warn_msg("Problem key detected: "); printHex( key ); print(" ("); printHex( keys_problem[key] ); print(")" NL ); } } info_print("If problem keys were detected, and were being held down, they will be reset as soon as let go."); info_print("Some keys have unusually high sense values, on the first press they should be re-enabled."); break; } } else { // No keypress, accumulate averages if( !key_activity ) { // Only start averaging once the idle counter has counted down to 0 if ( key_idle == 0 ) { // Average Debugging if ( enableAvgDebug ) { print("\033[1mAvg\033[0m: "); } // aggregate for ( uint8_t i = 0; i < KEY_COUNT; ++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; keys_averages[i] = av; keys_averages_acc[i] = 0; // Average Debugging if ( enableAvgDebug && av > 0 ) { printHex( av ); print(" "); } } // Average Debugging if ( enableAvgDebug ) { print( NL ); } // No key presses detected, set key_release indicator key_release = 1; } // Otherwise decrement the idle counter else { key_idle--; } } // 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(); } } } void setup_ADC() { // disable adc digital pins. DIDR1 |= (1 << AIN0D) | (1< sampleMax[column][mux] ) { sampleMax[column][mux] = readVal; } mux++; } while ( mux < 8 ); hold_sample( OFF ); recovery( ON ); // turn off adc. ADCSRA &= ~(1 << ADEN); // pull all columns' strobe-lines low. DDRC |= C_MASK; DDRD |= D_MASK; DDRE |= E_MASK; PORTC &= ~C_MASK; PORTD &= ~D_MASK; PORTE &= ~E_MASK; } void testColumn( uint8_t strobe ) { uint16_t db_delta = 0; uint8_t db_sample = 0; uint16_t db_threshold = 0; uint8_t column = 0; uint8_t bit = 1; for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux ) { uint16_t delta = keys_averages[(strobe << MUXES_COUNT_XSHIFT) + mux]; uint8_t key = (strobe << MUXES_COUNT_XSHIFT) + mux; // Check if this is a bad key (e.g. test point, or non-existent key) if ( keys_problem[key] ) { // If the sample value of the problem key goes above initally recorded result + threshold // re-enable the key if ( (db_sample = samples[strobe][mux] >> 1) > keys_problem[key] + threshold ) //if ( (db_sample = samples[strobe][mux] >> 1) < high_avg ) { info_msg("Re-enabling problem key: "); printHex( key ); print( NL ); keys_problem[key] = 0; } // 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[strobe][mux] >> 1) > (db_threshold = threshold) + (db_delta = delta) ) { column |= bit; key_activity++; // No longer idle, stop averaging ADC data key_idle = KEY_IDLE_SCANS; // Reset idle count-down // Only register keypresses once the warmup is complete, or not enough debounce info if ( keys_debounce[key] <= DEBOUNCE_THRESHOLD ) { // Add to the Macro processing buffer if debounce criteria met // Automatically handles converting to a USB code and sending off to the PC if ( keys_debounce[key] == DEBOUNCE_THRESHOLD ) { // Debug message, pressDebug CLI if ( enablePressDebug ) { print("0x"); printHex_op( key, 2 ); print(" "); } // Initial Keypress Macro_keyState( key, 0x01 ); } else if ( keys_debounce[key] >= DEBOUNCE_THRESHOLD ) { // Held Key Macro_keyState( key, 0x02 ); } keys_debounce[key]++; } // Long form key debugging if ( enableKeyDebug ) { // Debug message // [:] : : : dbug_msg("0x"); printHex_op( key, 1 ); print(" ["); printInt8( strobe ); print(":"); printInt8( mux ); print("] : "); printHex( db_sample ); // Sense print(" : "); printHex( db_threshold ); print("+"); printHex( db_delta ); print("="); printHex( db_threshold + db_delta ); // Sense compare print(" : "); printHex( db_sample - ( db_threshold + db_delta ) ); // Margin print( NL ); } } // Clear debounce entry if no keypress detected else { // Release Key if ( KeyIndex_BufferUsed > 0 && keys_debounce[key] >= DEBOUNCE_THRESHOLD ) { Macro_keyState( key, 0x03 ); } // Clear debounce entry keys_debounce[key] = 0; } bit <<= 1; } } void dumpSenseTable() { // Initial table alignment, with base threshold used for every key print("\033[1m"); printHex( threshold ); print("\033[0m "); // Print out headers first for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux ) { print(" Mux \033[1m"); printInt8( mux ); print("\033[0m "); } print( NL ); // Display the full strobe/sense table for ( uint8_t strober = 0; strober < total_strobes; ++strober ) { uint8_t strobe = strobe_map[strober]; // Display the strobe print("Strobe \033[1m"); printHex( strobe ); print("\033[0m "); // For each mux, display sense:threshold:delta for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux ) { 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("\033[1;32m"); } printHex_op( sample, 2 ); print(":"); printHex_op( max, 2 ); print(":"); printHex_op( delta, 2 ); print("\033[0m "); } // New line for each strobe print( NL ); } } // ----- CLI Command Functions ----- // 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( 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 ); } } void cliFunc_avgDebug( char* args ) { print( NL ); // Args ignored, just toggling if ( enableAvgDebug ) { info_print("Cap Sense averaging debug disabled."); enableAvgDebug = 0; } else { info_print("Cap Sense averaging debug enabled."); enableAvgDebug = 1; } } 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; } } void cliFunc_pressDebug( char* args ) { print( NL ); // Args ignored, just toggling if ( enablePressDebug ) { info_print("Cap Sense key debug disabled - post debounce."); enablePressDebug = 0; } else { info_print("Cap Sense key debug enabled - post debounce."); enablePressDebug = 1; } } void cliFunc_problemKeys( char* args ) { print( NL ); uint8_t count = 0; // Args ignored, just displaying // Display problem keys, and the sense value at the time for ( uint8_t key = 0; key < KEY_COUNT; key++ ) { if ( keys_problem[key] ) { if ( count++ == 0 ) { warn_msg("Problem keys: "); } printHex( key ); print(" ("); printHex( keys_problem[key] ); print(") " ); } } } 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; // If there was an argument, use that instead if ( *arg1Ptr != '\0' ) { senseDebugCount = decToInt( arg1Ptr ); } }