Archived
1
0

Cleaning up cap sense Alpha code

- Added LGPL headers to appropriate files
- Removed *LOTS* of dead code
- Started standardizing formatting (important stuff done)
This commit is contained in:
Jacob Alexander 2013-11-16 19:21:21 -05:00
parent afc227d515
commit a8e35f5cf5
2 changed files with 192 additions and 302 deletions

View File

@ -1,7 +1,18 @@
/* Copyright (C) 2011-2013 by Joseph Makuch /* Copyright (C) 2011-2013 by Joseph Makuch
* Additions by Jacob Alexander (2013) * Additions by Jacob Alexander (2013)
* *
* dfj, put whatever license here you want -HaaTa * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// ----- Includes ----- // ----- Includes -----
@ -20,12 +31,7 @@
// ----- Defines ----- // ----- Defines -----
// TODO dfj defines...needs cleaning up and commenting... // TODO dfj defines...needs commenting and maybe some cleaning...
#define LED_CONFIG (DDRD |= (1<<6))
#define LED_ON (PORTD &= ~(1<<6))
#define LED_OFF (PORTD |= (1<<6))
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
#define MAX_PRESS_DELTA_MV 470 #define MAX_PRESS_DELTA_MV 470
#define THRESHOLD_MV (MAX_PRESS_DELTA_MV >> 1) #define THRESHOLD_MV (MAX_PRESS_DELTA_MV >> 1)
//(2560 / (0x3ff/2)) ~= 5 //(2560 / (0x3ff/2)) ~= 5
@ -36,161 +42,81 @@
#define BUMP_DETECTION 0 #define BUMP_DETECTION 0
#define BUMP_THRESHOLD 0x50 #define BUMP_THRESHOLD 0x50
//((THRESHOLD) * 3)
#define BUMP_REST_US 1200 #define BUMP_REST_US 1200
#define STROBE_SETTLE 1 #define STROBE_SETTLE 1
#define MUX_SETTLE 1 #define MUX_SETTLE 1
#define HYST 1
#define HYST_T 0x10
#define TEST_KEY_STROBE (0x05) #define TEST_KEY_STROBE (0x05)
#define TEST_KEY_MASK (1 << 0) #define TEST_KEY_MASK (1 << 0)
#define ADHSM 7 #define ADHSM 7
/** Whether to use all of D and C, vs using E0, E1 instead of D6, D7,
* or alternately all of D, and E0,E1 and C0,..5 */
//#define ALL_D_C
//#define SHORT_D
#define SHORT_C
// rough offset voltage: one diode drop, about 50mV = 0x3ff * 50/3560 = 20
//#define OFFSET_VOLTAGE 0x14
//#define OFFSET_VOLTAGE 0x28
#define RIGHT_JUSTIFY 0 #define RIGHT_JUSTIFY 0
#define LEFT_JUSTIFY (0xff) #define LEFT_JUSTIFY (0xff)
// set left or right justification here: // set left or right justification here:
#define JUSTIFY_ADC RIGHT_JUSTIFY #define JUSTIFY_ADC RIGHT_JUSTIFY
#define ADLAR_MASK (1 << ADLAR) #define ADLAR_MASK (1 << ADLAR)
#ifdef JUSTIFY_ADC #ifdef JUSTIFY_ADC
#define ADLAR_BITS ((ADLAR_MASK) & (JUSTIFY_ADC)) #define ADLAR_BITS ((ADLAR_MASK) & (JUSTIFY_ADC))
#else // defaults to right justification. #else // defaults to right justification.
#define ADLAR_BITS 0 #define ADLAR_BITS 0
#endif #endif
// full muxmask // full muxmask
#define FULL_MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2) | (1 << MUX3) | (1 << MUX4)) #define FULL_MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2) | (1 << MUX3) | (1 << MUX4))
// F0-f7 pins only muxmask. // F0-f7 pins only muxmask.
#define MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2)) #define MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2))
#define SET_MUX(X) ((ADMUX) = (((ADMUX) & ~(MUX_MASK)) | ((X) & (MUX_MASK)))) // Strobe Masks
#define SET_FULL_MUX(X) ((ADMUX) = (((ADMUX) & ~(FULL_MUX_MASK)) | ((X) & (FULL_MUX_MASK)))) #define D_MASK (0xff)
#define E_MASK (0x03)
#define C_MASK (0xff)
#define MUX_1_1 0x1e // set ADC clock prescale
#define MUX_GND 0x1f
// set ADC clock prescale
#define PRESCALE_MASK ((1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2)) #define PRESCALE_MASK ((1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2))
#define PRESCALE_SHIFT (ADPS0) #define PRESCALE_SHIFT (ADPS0)
#define PRESCALE 3 #define PRESCALE 3
// TODO Remove this define when unnecessary -HaaTa
#ifdef EXTENDED_STROBE
#define STROBE_LINES 18
#else
#define STROBE_LINES 16 #define STROBE_LINES 16
#endif
#define STROBE_LINES_XSHIFT 4
#define STROBE_LINES_MASK 0x0f
#define MUXES_COUNT 8 #define MUXES_COUNT 8
#define MUXES_COUNT_XSHIFT 3 #define MUXES_COUNT_XSHIFT 3
#define MUXES_MASK 0x7
#define WARMUP_LOOPS ( 1024 ) #define WARMUP_LOOPS ( 1024 )
#define RECOVERY_US 2
#define SAMPLES 10 #define SAMPLES 10
#define SAMPLE_OFFSET ((SAMPLES) - MUXES_COUNT) #define SAMPLE_OFFSET ((SAMPLES) - MUXES_COUNT)
//#define SAMPLE_OFFSET 9
#define STROBE_OFFSET 0
#define SAMPLE_CONTROL 3 #define SAMPLE_CONTROL 3
// TODO Figure out calculation or best way to determine at startup -HaaTa
//#define DEFAULT_KEY_BASE 0xc8 //#define DEFAULT_KEY_BASE 0xc8
#define DEFAULT_KEY_BASE 0x95 #define DEFAULT_KEY_BASE 0x95
#define KEY_COUNT ((STROBE_LINES) * (MUXES_COUNT)) #define KEY_COUNT ((STROBE_LINES) * (MUXES_COUNT))
#define LX2FX
#define RECOVERY_CONTROL 1 #define RECOVERY_CONTROL 1
#define RECOVERY_SOURCE 0
#define RECOVERY_SINK 2
#define RECOVERY_SOURCE 0 #define ON 1
#define RECOVERY_SINK 2
#define RECOVERY_MASK 0x03
#define ON 1
#define OFF 0 #define OFF 0
// mix in 1/4 of the current average to the running average. -> (@mux_mix = 2) // mix in 1/4 of the current average to the running average. -> (@mux_mix = 2)
#define MUX_MIX 2 #define MUX_MIX 2
#define IDLE_COUNT_MASK 0xff #define IDLE_COUNT_MASK 0xff
#define IDLE_COUNT_MAX (IDLE_COUNT_MASK + 1)
#define IDLE_COUNT_SHIFT 8 #define IDLE_COUNT_SHIFT 8
#define KEYS_AVERAGES_MIX 2 // 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.
#ifdef ALL_D_C // 3 -> (7 * av + sample) / 8 i.e. 7:1 mix of old to new.
#define KEYS_AVERAGES_MIX_SHIFT 3
#define D_MASK (0xff)
#define D_SHIFT 0
#define E_MASK (0x00)
#define E_SHIFT 0
#define C_MASK (0xff)
#define C_SHIFT 8
#else
#if defined(SHORT_D)
#define D_MASK (0x3f)
#define D_SHIFT 0
#define E_MASK (0x03)
#define E_SHIFT 6
#define C_MASK (0xff)
#define C_SHIFT 8
#else
#if defined(SHORT_C)
#define D_MASK (0xff)
#define D_SHIFT 0
#define E_MASK (0x03)
#define E_SHIFT 6
#define C_MASK (0xff)
#define C_SHIFT 8
#endif
#endif
#endif
@ -201,18 +127,11 @@
if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \ if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
// Select mux
// TODO dfj macros...needs cleaning up and commenting...
#define STROBE_CASE(SC_CASE, SC_REG_A) case (SC_CASE): PORT##SC_REG_A = \
(( (PORT##SC_REG_A) & ~(1 << (SC_CASE - SC_REG_A##_SHIFT)) ) | (1 << (SC_CASE - SC_REG_A##_SHIFT)))
#define SET_MUX(X) ((ADMUX) = (((ADMUX) & ~(MUX_MASK)) | ((X) & (MUX_MASK))))
#define SET_FULL_MUX(X) ((ADMUX) = (((ADMUX) & ~(FULL_MUX_MASK)) | ((X) & (FULL_MUX_MASK)))) #define SET_FULL_MUX(X) ((ADMUX) = (((ADMUX) & ~(FULL_MUX_MASK)) | ((X) & (FULL_MUX_MASK))))
// ----- Variables ----- // ----- Variables -----
// Buffer used to inform the macro processing module which keys have been detected as pressed // Buffer used to inform the macro processing module which keys have been detected as pressed
@ -221,39 +140,25 @@ volatile uint8_t KeyIndex_BufferUsed;
// TODO dfj variables...needs cleaning up and commenting // TODO dfj variables...needs cleaning up and commenting
uint8_t blink = 0;
volatile uint16_t full_av = 0; volatile uint16_t full_av = 0;
/**/ uint8_t ze_strober = 0; uint8_t ze_strober = 0;
uint16_t samples [SAMPLES]; uint16_t samples [SAMPLES];
//int16_t gsamples [SAMPLES]; uint16_t adc_mux_averages [MUXES_COUNT];
uint16_t adc_strobe_averages[STROBE_LINES];
int16_t adc_mux_averages[MUXES_COUNT];
int16_t adc_strobe_averages[STROBE_LINES];
uint8_t cur_keymap[STROBE_LINES]; uint8_t cur_keymap[STROBE_LINES];
// /**/ int8_t last_keymap[STROBE_LINES];
uint8_t usb_keymap[STROBE_LINES]; uint8_t usb_keymap[STROBE_LINES];
uint16_t keys_down=0;
uint8_t dirty;
uint8_t unstable;
uint8_t usb_dirty; uint8_t usb_dirty;
uint16_t threshold = 0x25; // HaaTa Hack -TODO uint16_t threshold = 0x25; // HaaTa Hack -TODO
//uint16_t threshold = 0x16; // HaaTa Hack -TODO //uint16_t threshold = 0x16; // HaaTa Hack -TODO
//uint16_t threshold = THRESHOLD; //uint16_t threshold = THRESHOLD;
uint16_t tests = 0;
uint8_t col_a=0;
uint8_t col_b=0;
uint8_t col_c=0;
uint8_t column=0;
uint8_t column = 0;
uint16_t keys_averages_acc[KEY_COUNT]; uint16_t keys_averages_acc[KEY_COUNT];
uint16_t keys_averages[KEY_COUNT]; uint16_t keys_averages[KEY_COUNT];
@ -261,30 +166,22 @@ uint16_t keys_averages_acc_count=0;
uint8_t full_samples[KEY_COUNT]; uint8_t full_samples[KEY_COUNT];
// 0x9f...f
// #define COUNT_MASK 0x9fff
// #define COUNT_HIGH_BIT (INT16_MIN)
// TODO: change this to 'booting', then count down. // TODO: change this to 'booting', then count down.
uint16_t boot_count = 0; uint16_t boot_count = 0;
uint16_t idle_count=0; uint16_t idle_count = 0;
uint8_t idle = 1; uint8_t idle = 1;
uint16_t count = 0;
uint8_t error = 0; uint8_t error = 0;
uint16_t error_data = 0; uint16_t error_data = 0;
uint16_t mux_averages[MUXES_COUNT];
int16_t mux_averages[MUXES_COUNT]; uint16_t strobe_averages[STROBE_LINES];
int16_t strobe_averages[STROBE_LINES];
uint8_t dump_count = 0; uint8_t dump_count = 0;
//uint8_t column =0;
uint16_t db_delta = 0; uint16_t db_delta = 0;
uint8_t db_sample = 0; uint8_t db_sample = 0;
uint16_t db_threshold = 0; uint16_t db_threshold = 0;
@ -314,23 +211,17 @@ inline void scan_setup()
// TODO dfj code...needs cleanup + commenting... // TODO dfj code...needs cleanup + commenting...
setup_ADC(); setup_ADC();
DDRC = C_MASK; DDRC = C_MASK;
PORTC = 0; PORTC = 0;
DDRD = D_MASK; DDRD = D_MASK;
PORTD = 0; PORTD = 0;
DDRE = E_MASK; DDRE = E_MASK;
PORTE = 0 ; PORTE = 0 ;
//DDRC |= (1 << 6);
//PORTC &= ~(1<< 6);
//uint16_t strobe = 1;
// TODO all this code should probably be in scan_resetKeyboard // TODO all this code should probably be in scan_resetKeyboard
for (int i=0; i < STROBE_LINES; ++i) { for (int i=0; i < STROBE_LINES; ++i) {
cur_keymap[i] = 0; cur_keymap[i] = 0;
//last_keymap[i] = 0;
usb_keymap[i] = 0; usb_keymap[i] = 0;
} }
@ -366,30 +257,31 @@ inline uint8_t scan_loop()
uint8_t strober = 0; uint8_t strober = 0;
uint32_t full_av_acc = 0; uint32_t full_av_acc = 0;
for (strober = 0; strober < STROBE_LINES; ++strober) { for (strober = 0; strober < STROBE_LINES; ++strober)
{
uint8_t tries; uint8_t tries = 1;
tries = 1; while ( tries++ && sampleColumn( strober ) ) { tries &= 0x7; } // don't waste this one just because the last one was poop.
while (tries++ && sampleColumn(strober)) { tries &= 0x7; } // don't waste this one just because the last one was poop.
column = testColumn(strober); column = testColumn(strober);
idle |= column; // if column has any pressed keys, then we are not idle. idle |= column; // if column has any pressed keys, then we are not idle.
if( column != cur_keymap[strober] && (boot_count >= WARMUP_LOOPS) ) { if( column != cur_keymap[strober] && ( boot_count >= WARMUP_LOOPS ) )
tests++; {
cur_keymap[strober] = column; cur_keymap[strober] = column;
usb_dirty = 1; usb_dirty = 1;
} }
idle |= usb_dirty; // if any keys have changed inc. released, then we are not idle. idle |= usb_dirty; // if any keys have changed inc. released, then we are not idle.
if(error == 0x50) { if ( error == 0x50 )
{
error_data |= (((uint16_t)strober) << 12); error_data |= (((uint16_t)strober) << 12);
} }
uint8_t strobe_line = strober << MUXES_COUNT_XSHIFT; uint8_t strobe_line = strober << MUXES_COUNT_XSHIFT;
for(int i=0; i<MUXES_COUNT; ++i) { for ( int i = 0; i < MUXES_COUNT; ++i )
{
// discard sketchy low bit, and meaningless high bits. // discard sketchy low bit, and meaningless high bits.
uint8_t sample = samples[SAMPLE_OFFSET + i] >> 1; uint8_t sample = samples[SAMPLE_OFFSET + i] >> 1;
full_samples[strobe_line + i] = sample; full_samples[strobe_line + i] = sample;
@ -398,18 +290,13 @@ inline uint8_t scan_loop()
keys_averages_acc_count++; keys_averages_acc_count++;
strobe_averages[strober] = 0; strobe_averages[strober] = 0;
for (uint8_t i = SAMPLE_OFFSET; i < (SAMPLE_OFFSET + MUXES_COUNT); ++i) { for ( uint8_t i = SAMPLE_OFFSET; i < ( SAMPLE_OFFSET + MUXES_COUNT ); ++i )
//samples[i] -= samples[i-SAMPLE_OFFSET]; // av; // + full_av); // -something. {
//samples[i] -= OFFSET_VOLTAGE; // moved to sampleColumn.
full_av_acc += (samples[i]); full_av_acc += (samples[i]);
#ifdef COLLECT_STROBE_AVERAGES #ifdef COLLECT_STROBE_AVERAGES
mux_averages[i - SAMPLE_OFFSET] += samples[i]; mux_averages[i - SAMPLE_OFFSET] += samples[i];
strobe_averages[strober] += samples[i]; strobe_averages[strober] += samples[i];
#endif #endif
//samples[i] -= (full_av - HYST_T);
//++count;
} }
#ifdef COLLECT_STROBE_AVERAGES #ifdef COLLECT_STROBE_AVERAGES
@ -417,8 +304,8 @@ inline uint8_t scan_loop()
adc_strobe_averages[strober] >>= 1; adc_strobe_averages[strober] >>= 1;
/** test if we went negative. */ /** test if we went negative. */
if ((adc_strobe_averages[strober] & 0xFF00) && (boot_count if ( ( adc_strobe_averages[strober] & 0xFF00 ) && ( boot_count >= WARMUP_LOOPS ) )
>= WARMUP_LOOPS)) { {
error = 0xf; error_data = adc_strobe_averages[strober]; error = 0xf; error_data = adc_strobe_averages[strober];
} }
#endif #endif
@ -426,8 +313,8 @@ inline uint8_t scan_loop()
#ifdef VERIFY_TEST_PAD #ifdef VERIFY_TEST_PAD
// verify test key is not down. // verify test key is not down.
if((cur_keymap[TEST_KEY_STROBE] & TEST_KEY_MASK) ) { if ( ( cur_keymap[TEST_KEY_STROBE] & TEST_KEY_MASK ) )
//count=0; {
error = 0x05; error = 0x05;
error_data = cur_keymap[TEST_KEY_STROBE] << 8; error_data = cur_keymap[TEST_KEY_STROBE] << 8;
error_data += full_samples[TEST_KEY_STROBE * 8]; error_data += full_samples[TEST_KEY_STROBE * 8];
@ -437,14 +324,14 @@ inline uint8_t scan_loop()
#ifdef COLLECT_STROBE_AVERAGES #ifdef COLLECT_STROBE_AVERAGES
// calc mux averages. // calc mux averages.
if (boot_count < WARMUP_LOOPS) { if ( boot_count < WARMUP_LOOPS )
{
full_av += (full_av_acc >> (7)); full_av += (full_av_acc >> (7));
full_av >>= 1; full_av >>= 1;
//full_av = full_av_acc / count;
full_av_acc = 0; full_av_acc = 0;
for (int i=0; i < MUXES_COUNT; ++i) { for ( int i = 0; i < MUXES_COUNT; ++i )
#define MUX_MIX 2 // mix in 1/4 of the current average to the running average. -> (@mux_mix = 2) {
adc_mux_averages[i] = (adc_mux_averages[i] << MUX_MIX) - adc_mux_averages[i]; adc_mux_averages[i] = (adc_mux_averages[i] << MUX_MIX) - adc_mux_averages[i];
adc_mux_averages[i] += (mux_averages[i] >> 4); adc_mux_averages[i] += (mux_averages[i] >> 4);
adc_mux_averages[i] >>= MUX_MIX; adc_mux_averages[i] >>= MUX_MIX;
@ -454,12 +341,6 @@ inline uint8_t scan_loop()
} }
#endif #endif
// 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
/** aggregate if booting, or if idle; /** aggregate if booting, or if idle;
* else, if not booting, check for dirty USB. * else, if not booting, check for dirty USB.
* */ * */
@ -467,20 +348,23 @@ inline uint8_t scan_loop()
idle_count++; idle_count++;
idle_count &= IDLE_COUNT_MASK; idle_count &= IDLE_COUNT_MASK;
idle = idle && !keys_down; if ( boot_count < WARMUP_LOOPS )
{
if (boot_count < WARMUP_LOOPS) {
error = 0x0C; error = 0x0C;
error_data = boot_count; error_data = boot_count;
boot_count++; boot_count++;
} else { // count >= WARMUP_LOOPS }
if (usb_dirty) { else
for (int i=0; i < STROBE_LINES; ++i) { {
if ( usb_dirty )
{
for ( int i = 0; i < STROBE_LINES; ++i )
{
usb_keymap[i] = cur_keymap[i]; usb_keymap[i] = cur_keymap[i];
} }
dumpkeys(); dumpkeys();
usb_dirty=0; usb_dirty = 0;
memset(((void *)keys_averages_acc), 0, (size_t)(KEY_COUNT * sizeof (uint16_t))); memset(((void *)keys_averages_acc), 0, (size_t)(KEY_COUNT * sizeof (uint16_t)));
keys_averages_acc_count = 0; keys_averages_acc_count = 0;
idle_count = 0; idle_count = 0;
@ -488,10 +372,13 @@ inline uint8_t scan_loop()
_delay_us(100); _delay_us(100);
} }
if (!idle_count) { if ( !idle_count )
if(idle) { {
if( idle )
{
// aggregate // aggregate
for (uint8_t i = 0; i < KEY_COUNT; ++i) { for ( uint8_t i = 0; i < KEY_COUNT; ++i )
{
uint16_t acc = keys_averages_acc[i] >> IDLE_COUNT_SHIFT; uint16_t acc = keys_averages_acc[i] >> IDLE_COUNT_SHIFT;
uint32_t av = keys_averages[i]; uint32_t av = keys_averages[i];
@ -504,7 +391,8 @@ inline uint8_t scan_loop()
} }
keys_averages_acc_count = 0; keys_averages_acc_count = 0;
if(boot_count >= WARMUP_LOOPS) { if ( boot_count >= WARMUP_LOOPS )
{
dump(); dump();
} }
@ -568,7 +456,7 @@ void scan_finishedWithUSBBuffer( uint8_t sentKeys )
} }
void _delay_loop(uint8_t __count) void _delay_loop( uint8_t __count )
{ {
__asm__ volatile ( __asm__ volatile (
"1: dec %0" "\n\t" "1: dec %0" "\n\t"
@ -579,17 +467,16 @@ void _delay_loop(uint8_t __count)
} }
void setup_ADC (void) { void setup_ADC()
{
// disable adc digital pins. // disable adc digital pins.
DIDR1 |= (1 << AIN0D) | (1<<AIN1D); // set disable on pins 1,0. DIDR1 |= (1 << AIN0D) | (1<<AIN1D); // set disable on pins 1,0.
//DIDR0 = 0xff; // disable all. (port F, usually). - testing w/o disable.
DDRF = 0x0; DDRF = 0x0;
PORTF = 0x0; PORTF = 0x0;
uint8_t mux = 0 & 0x1f; // 0 == first. // 0x1e = 1.1V ref. uint8_t mux = 0 & 0x1f; // 0 == first. // 0x1e = 1.1V ref.
// 0 = external aref 1,1 = 2.56V internal ref // 0 = external aref 1,1 = 2.56V internal ref
uint8_t aref = ((1 << REFS1) | (1 << REFS0)) & ((1 << REFS1) | (1 << REFS0)); uint8_t aref = ((1 << REFS1) | (1 << REFS0)) & ((1 << REFS1) | (1 << REFS0));
// uint8_t adlar = 0xff & (1 << ADLAR); // 1 := left justify bits, 0 := right
uint8_t adate = (1 << ADATE) & (1 << ADATE); // trigger enable uint8_t adate = (1 << ADATE) & (1 << ADATE); // trigger enable
uint8_t trig = 0 & ((1 << ADTS0) | (1 << ADTS1) | (1 << ADTS2)); // 0 = free running uint8_t trig = 0 & ((1 << ADTS0) | (1 << ADTS1) | (1 << ADTS2)); // 0 = free running
// ps2, ps1 := /64 ( 2^6 ) ps2 := /16 (2^4), ps1 := 4, ps0 :=2, PS1,PS0 := 8 (2^8) // ps2, ps1 := /64 ( 2^6 ) ps2 := /16 (2^4), ps1 := 4, ps0 :=2, PS1,PS0 := 8 (2^8)
@ -597,10 +484,6 @@ void setup_ADC (void) {
uint8_t hispeed = (1 << ADHSM); uint8_t hispeed = (1 << ADHSM);
uint8_t en_mux = (1 << ACME); uint8_t en_mux = (1 << ACME);
//ADCSRA = (ADCSRA & ~PRESCALES) | ((1 << ADPS1) | (1 << ADPS2)); // 2, 1 := /64 ( 2^6 )
//ADCSRA = (ADCSRA & ~PRESCALES) | ((1 << ADPS0) | (1 << ADPS2)); // 2, 0 := /32 ( 2^5 )
//ADCSRA = (ADCSRA & ~PRESCALES) | ((1 << ADPS2)); // 2 := /16 ( 2^4 )
ADCSRA = (1 << ADEN) | prescale; // ADC enable ADCSRA = (1 << ADEN) | prescale; // ADC enable
// select ref. // select ref.
@ -609,38 +492,23 @@ void setup_ADC (void) {
//ADMUX &= ~((1 << REFS1) | (1 << REFS0)); // 0,0 : aref. //ADMUX &= ~((1 << REFS1) | (1 << REFS0)); // 0,0 : aref.
ADMUX = aref | mux | ADLAR_BITS; ADMUX = aref | mux | ADLAR_BITS;
// enable MUX
// ADCSRB |= (1 << ACME); // enable
// ADCSRB &= ~(1 << ADEN); // ?
// select first mux.
//ADMUX = (ADMUX & ~MUXES); // start at 000 = ADC0
// clear adlar to left justify data
//ADMUX = ~();
// set adlar to right justify data
//ADMUX |= (1 << ADLAR);
// set free-running // set free-running
ADCSRA |= adate; // trigger enable ADCSRA |= adate; // trigger enable
ADCSRB = en_mux | hispeed | trig | (ADCSRB & ~((1 << ADTS0) | (1 << ADTS1) | (1 << ADTS2))); // trigger select free running ADCSRB = en_mux | hispeed | trig | (ADCSRB & ~((1 << ADTS0) | (1 << ADTS1) | (1 << ADTS2))); // trigger select free running
// ADCSRA |= (1 << ADATE); // tiggger enable
ADCSRA |= (1 << ADEN); // ADC enable ADCSRA |= (1 << ADEN); // ADC enable
ADCSRA |= (1 << ADSC); // start conversions q ADCSRA |= (1 << ADSC); // start conversions q
} }
void recovery(uint8_t on) { void recovery( uint8_t on )
DDRB |= (1 << RECOVERY_CONTROL); {
DDRB |= (1 << RECOVERY_CONTROL);
PORTB &= ~(1 << RECOVERY_SINK); // SINK always zero
DDRB &= ~(1 << RECOVERY_SOURCE); // SOURCE high imp
PORTB &= ~(1 << RECOVERY_SINK); // SINK always zero if ( on )
DDRB &= ~(1 << RECOVERY_SOURCE); // SOURCE high imp {
if(on) {
// set strobes to sink to gnd. // set strobes to sink to gnd.
DDRC |= C_MASK; DDRC |= C_MASK;
DDRD |= D_MASK; DDRD |= D_MASK;
@ -650,38 +518,38 @@ void recovery(uint8_t on) {
PORTD &= ~D_MASK; PORTD &= ~D_MASK;
PORTE &= ~E_MASK; PORTE &= ~E_MASK;
DDRB |= (1 << RECOVERY_SINK); // SINK pull DDRB |= (1 << RECOVERY_SINK); // SINK pull
PORTB |= (1 << RECOVERY_CONTROL); PORTB |= (1 << RECOVERY_CONTROL);
PORTB |= (1 << RECOVERY_SOURCE); // SOURCE high PORTB |= (1 << RECOVERY_SOURCE); // SOURCE high
DDRB |= (1 << RECOVERY_SOURCE); DDRB |= (1 << RECOVERY_SOURCE);
} else { }
// _delay_loop(10); else
{
PORTB &= ~(1 << RECOVERY_CONTROL); PORTB &= ~(1 << RECOVERY_CONTROL);
DDRB &= ~(1 << RECOVERY_SOURCE);
DDRB &= ~(1 << RECOVERY_SOURCE);
PORTB &= ~(1 << RECOVERY_SOURCE); // SOURCE low PORTB &= ~(1 << RECOVERY_SOURCE); // SOURCE low
DDRB &= ~(1 << RECOVERY_SINK); // SINK high-imp DDRB &= ~(1 << RECOVERY_SINK); // SINK high-imp
//DDRB &= ~(1 << RECOVERY_SINK);
} }
} }
void hold_sample(uint8_t on) { void hold_sample( uint8_t on )
if (!on) { {
if ( !on )
{
PORTB |= (1 << SAMPLE_CONTROL); PORTB |= (1 << SAMPLE_CONTROL);
DDRB |= (1 << SAMPLE_CONTROL); DDRB |= (1 << SAMPLE_CONTROL);
} else { }
DDRB |= (1 << SAMPLE_CONTROL); else
{
DDRB |= (1 << SAMPLE_CONTROL);
PORTB &= ~(1 << SAMPLE_CONTROL); PORTB &= ~(1 << SAMPLE_CONTROL);
} }
} }
void strobe_w(uint8_t strobe_num) { void strobe_w( uint8_t strobe_num )
{
PORTC &= ~(C_MASK); PORTC &= ~(C_MASK);
PORTD &= ~(D_MASK); PORTD &= ~(D_MASK);
PORTE &= ~(E_MASK); PORTE &= ~(E_MASK);
@ -873,65 +741,61 @@ void strobe_w(uint8_t strobe_num) {
} }
inline uint16_t getADC() { inline uint16_t getADC(void)
{
ADCSRA |= (1 << ADIF); // clear int flag by writing 1. ADCSRA |= (1 << ADIF); // clear int flag by writing 1.
//wait for last read to complete. //wait for last read to complete.
while (! (ADCSRA & (1 << ADIF))); while ( !( ADCSRA & (1 << ADIF) ) );
return ADC; // return sample return ADC; // return sample
} }
int sampleColumn_8x(uint8_t column, uint16_t * buffer) { int sampleColumn_8x( uint8_t column, uint16_t * buffer )
{
// ensure all probe lines are driven low, and chill for recovery delay. // ensure all probe lines are driven low, and chill for recovery delay.
uint16_t sample;
ADCSRA |= (1 << ADEN) | (1 << ADSC); // enable and start conversions ADCSRA |= (1 << ADEN) | (1 << ADSC); // enable and start conversions
// sync up with adc clock:
//sample = getADC();
PORTC &= ~C_MASK; PORTC &= ~C_MASK;
PORTD &= ~D_MASK; PORTD &= ~D_MASK;
PORTE &= ~E_MASK; PORTE &= ~E_MASK;
PORTF = 0; PORTF = 0;
DDRF = 0; DDRF = 0;
recovery(OFF); recovery(OFF);
strobe_w(column); strobe_w(column);
hold_sample(OFF); hold_sample(OFF);
SET_FULL_MUX(0); SET_FULL_MUX(0);
for(uint8_t i=0; i < STROBE_SETTLE; ++i) {
sample = getADC(); for ( uint8_t i = 0; i < STROBE_SETTLE; ++i ) { getADC(); }
}
hold_sample(ON); hold_sample(ON);
#undef MUX_SETTLE #undef MUX_SETTLE
#if (MUX_SETTLE) #if (MUX_SETTLE)
for(uint8_t mux=0; mux < 8; ++mux) { for ( uint8_t mux = 0; mux < 8; ++mux )
{
SET_FULL_MUX(mux); // our sample will use this SET_FULL_MUX(mux); // our sample will use this
// wait for mux to settle.
for(uint8_t i=0; i < MUX_SETTLE; ++i) {
sample = getADC();
}
// wait for mux to settle.
for ( uint8_t i = 0; i < MUX_SETTLE; ++i ) { getADC(); }
// retrieve current read. // retrieve current read.
buffer[mux] = getADC();// - OFFSET_VOLTAGE; buffer[mux] = getADC();
} }
#else #else
uint8_t mux=0; uint8_t mux = 0;
SET_FULL_MUX(mux); SET_FULL_MUX(mux);
sample = getADC(); // throw away; unknown mux. getADC(); // throw away; unknown mux.
do { do {
SET_FULL_MUX(mux + 1); // our *next* sample will use this SET_FULL_MUX(mux + 1); // our *next* sample will use this
// retrieve current read. // retrieve current read.
buffer[mux] = getADC();// - OFFSET_VOLTAGE; buffer[mux] = getADC();
mux++; mux++;
} while (mux < 8); } while (mux < 8);
@ -956,16 +820,18 @@ int sampleColumn_8x(uint8_t column, uint16_t * buffer) {
} }
int sampleColumn(uint8_t column) { int sampleColumn( uint8_t column )
{
int rval = 0; int rval = 0;
rval = sampleColumn_8x(column, samples+SAMPLE_OFFSET); rval = sampleColumn_8x( column, samples + SAMPLE_OFFSET );
#if (BUMP_DETECTION) #if (BUMP_DETECTION)
for(uint8_t i=0; i<8; ++i) { for ( uint8_t i = 0; i < 8; ++i )
if(samples[SAMPLE_OFFSET + i] - adc_mux_averages[i] > BUMP_THRESHOLD) { {
if ( samples[SAMPLE_OFFSET + i] - adc_mux_averages[i] > BUMP_THRESHOLD )
{
// was a hump // was a hump
_delay_us(BUMP_REST_US); _delay_us(BUMP_REST_US);
rval++; rval++;
error = 0x50; error = 0x50;
@ -979,15 +845,15 @@ int sampleColumn(uint8_t column) {
} }
uint8_t testColumn(uint8_t strobe) uint8_t testColumn( uint8_t strobe )
{ {
uint8_t column = 0; uint8_t column = 0;
uint8_t bit = 1; uint8_t bit = 1;
for (uint8_t i = 0; i < MUXES_COUNT; ++i) for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
{ {
uint16_t delta = keys_averages[(strobe << MUXES_COUNT_XSHIFT) + i]; uint16_t delta = keys_averages[(strobe << MUXES_COUNT_XSHIFT) + i];
if ((db_sample = samples[SAMPLE_OFFSET + i] >> 1) > (db_threshold = threshold) + (db_delta = delta)) if ( (db_sample = samples[SAMPLE_OFFSET + i] >> 1) > (db_threshold = threshold) + (db_delta = delta) )
{ {
column |= bit; column |= bit;
} }
@ -1014,17 +880,20 @@ uint8_t testColumn(uint8_t strobe)
} }
void dumpkeys(void) { void dumpkeys()
//print(" \n"); {
if(error) { if ( error )
/* {
if (count >= WARMUP_LOOPS && error) { erro_print("Problem detected...");
if ( boot_count >= WARMUP_LOOPS )
{
dump(); dump();
} }
*/
// Key scan debug // Key scan debug
for (uint8_t i=0; i < STROBE_LINES; ++i) { for ( uint8_t i = 0; i < STROBE_LINES; ++i )
{
printHex(usb_keymap[i]); printHex(usb_keymap[i]);
print(" "); print(" ");
} }
@ -1039,16 +908,19 @@ void dumpkeys(void) {
} }
// XXX Will be cleaned up eventually, but this will do for now :P -HaaTa // XXX Will be cleaned up eventually, but this will do for now :P -HaaTa
for (uint8_t i=0; i < STROBE_LINES; ++i) { for ( uint8_t i = 0; i < STROBE_LINES; ++i )
for(uint8_t j=0; j<MUXES_COUNT; ++j) { {
if ( usb_keymap[i] & (1 << j) ) { for ( uint8_t j = 0; j < MUXES_COUNT; ++j )
{
if ( usb_keymap[i] & (1 << j) )
{
uint8_t key = (i << MUXES_COUNT_XSHIFT) + j; uint8_t key = (i << MUXES_COUNT_XSHIFT) + j;
// Add to the Macro processing buffer // Add to the Macro processing buffer
// Automatically handles converting to a USB code and sending off to the PC // Automatically handles converting to a USB code and sending off to the PC
//bufferAdd( key ); //bufferAdd( key );
if(usb_dirty) if ( usb_dirty )
{ {
printHex( key ); printHex( key );
print("\n"); print("\n");
@ -1056,37 +928,47 @@ void dumpkeys(void) {
} }
} }
} }
//if(usb_dirty) print("\n");
usb_keyboard_send(); usb_keyboard_send();
} }
void dump(void) { void dump(void) {
//#define DEBUG_FULL_SAMPLES_AVERAGES
#ifdef DEBUG_FULL_SAMPLES_AVERAGES #ifdef DEBUG_FULL_SAMPLES_AVERAGES
if(!dump_count) { // we don't want to debug-out during the measurements. // we don't want to debug-out during the measurements.
if ( !dump_count )
{
// Averages currently set per key // Averages currently set per key
for(int i =0; i< KEY_COUNT; ++i) { for ( int i = 0; i < KEY_COUNT; ++i )
if(!(i & 0x0f)) { {
if ( !(i & 0x0f) )
{
print("\n"); print("\n");
} else if (!(i & 0x07)) { }
else if ( !(i & 0x07) )
{
print(" "); print(" ");
} }
print(" "); print(" ");
printHex (keys_averages[i]); printHex( keys_averages[i] );
} }
print("\n"); print("\n");
// Previously read full ADC scans? // Previously read full ADC scans?
for(int i =0; i< KEY_COUNT; ++i) { for ( int i = 0; i< KEY_COUNT; ++i)
if(!(i & 0x0f)) { {
if ( !(i & 0x0f) )
{
print("\n"); print("\n");
} else if (!(i & 0x07)) { }
else if ( !(i & 0x07) )
{
print(" "); print(" ");
} }
print(" "); print(" ");
printHex(full_samples[i]); printHex(full_samples[i]);
} }
@ -1102,7 +984,8 @@ void dump(void) {
// Previously read ADC scans on current strobe // Previously read ADC scans on current strobe
print(" :"); print(" :");
for (uint8_t i=0; i < MUXES_COUNT; ++i) { for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
{
print(" "); print(" ");
printHex(full_samples[(cur_strober << MUXES_COUNT_XSHIFT) + i]); printHex(full_samples[(cur_strober << MUXES_COUNT_XSHIFT) + i]);
} }
@ -1110,19 +993,16 @@ void dump(void) {
// Averages current set on current strobe // Averages current set on current strobe
print(" :"); print(" :");
for (uint8_t i=0; i < MUXES_COUNT; ++i) { for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
{
print(" "); print(" ");
printHex(keys_averages[(cur_strober << MUXES_COUNT_XSHIFT) + i]); printHex(keys_averages[(cur_strober << MUXES_COUNT_XSHIFT) + i]);
} }
#endif #endif
//#define DEBUG_DELTA_SAMPLE_THRESHOLD
#ifdef DEBUG_DELTA_SAMPLE_THRESHOLD #ifdef DEBUG_DELTA_SAMPLE_THRESHOLD
print("\n"); print("\n");
//uint16_t db_delta = 0;
//uint16_t db_sample = 0;
//uint16_t db_threshold = 0;
printHex( db_delta ); printHex( db_delta );
print(" "); print(" ");
printHex( db_sample ); printHex( db_sample );
@ -1132,12 +1012,12 @@ void dump(void) {
printHex( column ); printHex( column );
#endif #endif
//#define DEBUG_USB_KEYMAP
#ifdef DEBUG_USB_KEYMAP #ifdef DEBUG_USB_KEYMAP
print("\n "); print("\n ");
// Current keymap values // Current keymap values
for (uint8_t i=0; i < STROBE_LINES; ++i) { for ( uint8_t i = 0; i < STROBE_LINES; ++i )
{
printHex(cur_keymap[i]); printHex(cur_keymap[i]);
print(" "); print(" ");
} }

View File

@ -1,7 +1,17 @@
/* Copyright (C) 2013 by Jacob Alexander /* Copyright (C) 2013 by Jacob Alexander
* *
* dfj, put whatever license here you want * This program is free software: you can redistribute it and/or modify
* This file will probably be removed though. * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef __SCAN_LOOP_H #ifndef __SCAN_LOOP_H