Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

scan_loop.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /* Copyright (C) 2011-2013 by Joseph Makuch
  2. * Additions by Jacob Alexander (2013)
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 3.0 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // ----- Includes -----
  18. // Compiler Includes
  19. #include <Lib/ScanLib.h>
  20. // Project Includes
  21. #include <led.h>
  22. #include <print.h>
  23. // Local Includes
  24. #include "scan_loop.h"
  25. // ----- Defines -----
  26. // TODO dfj defines...needs commenting and maybe some cleaning...
  27. #define MAX_PRESS_DELTA_MV 380
  28. #define THRESHOLD_MV (MAX_PRESS_DELTA_MV >> 1)
  29. //(2560 / (0x3ff/2)) ~= 5
  30. #define MV_PER_ADC 5
  31. #define THRESHOLD (THRESHOLD_MV / MV_PER_ADC)
  32. #define STROBE_SETTLE 1
  33. #define MUX_SETTLE 1
  34. #define TEST_KEY_STROBE (0x05)
  35. #define TEST_KEY_MASK (1 << 0)
  36. #define ADHSM 7
  37. #define RIGHT_JUSTIFY 0
  38. #define LEFT_JUSTIFY (0xff)
  39. // set left or right justification here:
  40. #define JUSTIFY_ADC RIGHT_JUSTIFY
  41. #define ADLAR_MASK (1 << ADLAR)
  42. #ifdef JUSTIFY_ADC
  43. #define ADLAR_BITS ((ADLAR_MASK) & (JUSTIFY_ADC))
  44. #else // defaults to right justification.
  45. #define ADLAR_BITS 0
  46. #endif
  47. // full muxmask
  48. #define FULL_MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2) | (1 << MUX3) | (1 << MUX4))
  49. // F0-f7 pins only muxmask.
  50. #define MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2))
  51. // Strobe Masks
  52. #define D_MASK (0xff)
  53. #define E_MASK (0x03)
  54. #define C_MASK (0xff)
  55. // set ADC clock prescale
  56. #define PRESCALE_MASK ((1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2))
  57. #define PRESCALE_SHIFT (ADPS0)
  58. #define PRESCALE 3
  59. // Max number of strobes supported by the hardware
  60. // Strobe lines are detected at startup, extra strobes cause anomalies like phantom keypresses
  61. #define MAX_STROBES 18
  62. #define MUXES_COUNT 8
  63. #define MUXES_COUNT_XSHIFT 3
  64. #define WARMUP_LOOPS ( 1024 )
  65. #define WARMUP_STOP (WARMUP_LOOPS - 1)
  66. #define SAMPLES 10
  67. #define SAMPLE_OFFSET ((SAMPLES) - MUXES_COUNT)
  68. #define SAMPLE_CONTROL 3
  69. // Starting average for keys, per key will adjust during runtime
  70. // XXX - A better method is needed to choose this value (i.e. not experimental)
  71. // The ideal average is not always found for weak keys if this is set too high...
  72. #define DEFAULT_KEY_BASE 0xB0
  73. #define KEY_COUNT ((MAX_STROBES) * (MUXES_COUNT))
  74. #define RECOVERY_CONTROL 1
  75. #define RECOVERY_SOURCE 0
  76. #define RECOVERY_SINK 2
  77. #define ON 1
  78. #define OFF 0
  79. // mix in 1/4 of the current average to the running average. -> (@mux_mix = 2)
  80. #define MUX_MIX 2
  81. #define IDLE_COUNT_MASK 0xff
  82. #define IDLE_COUNT_SHIFT 8
  83. // av = (av << shift) - av + sample; av >>= shift
  84. // e.g. 1 -> (av + sample) / 2 simple average of new and old
  85. // 2 -> (3 * av + sample) / 4 i.e. 3:1 mix of old to new.
  86. // 3 -> (7 * av + sample) / 8 i.e. 7:1 mix of old to new.
  87. #define KEYS_AVERAGES_MIX_SHIFT 3
  88. // ----- Macros -----
  89. // Make sure we haven't overflowed the buffer
  90. #define bufferAdd(byte) \
  91. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  92. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  93. // Select mux
  94. #define SET_FULL_MUX(X) ((ADMUX) = (((ADMUX) & ~(FULL_MUX_MASK)) | ((X) & (FULL_MUX_MASK))))
  95. // ----- Variables -----
  96. // Buffer used to inform the macro processing module which keys have been detected as pressed
  97. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  98. volatile uint8_t KeyIndex_BufferUsed;
  99. // TODO dfj variables...needs cleaning up and commenting
  100. volatile uint16_t full_av = 0;
  101. uint8_t ze_strober = 0;
  102. uint16_t samples [SAMPLES];
  103. uint8_t cur_keymap[MAX_STROBES];
  104. uint8_t keymap_change;
  105. uint16_t threshold = THRESHOLD;
  106. uint8_t column = 0;
  107. uint16_t keys_averages_acc[KEY_COUNT];
  108. uint16_t keys_averages [KEY_COUNT];
  109. uint8_t full_samples[KEY_COUNT];
  110. // TODO: change this to 'booting', then count down.
  111. uint16_t boot_count = 0;
  112. uint16_t idle_count = 0;
  113. uint8_t idle = 1;
  114. uint8_t error = 0;
  115. uint16_t error_data = 0;
  116. uint8_t total_strobes = MAX_STROBES;
  117. uint8_t strobe_map[MAX_STROBES];
  118. uint8_t dump_count = 0;
  119. uint16_t db_delta = 0;
  120. uint8_t db_sample = 0;
  121. uint16_t db_threshold = 0;
  122. // ----- Function Declarations -----
  123. void dump( void );
  124. void recovery( uint8_t on );
  125. int sampleColumn( uint8_t column );
  126. void setup_ADC( void );
  127. void strobe_w( uint8_t strobe_num );
  128. uint8_t testColumn( uint8_t strobe );
  129. // ----- Functions -----
  130. // Initial setup for cap sense controller
  131. inline void scan_setup()
  132. {
  133. // TODO dfj code...needs cleanup + commenting...
  134. setup_ADC();
  135. DDRC = C_MASK;
  136. PORTC = 0;
  137. DDRD = D_MASK;
  138. PORTD = 0;
  139. DDRE = E_MASK;
  140. PORTE = 0 ;
  141. // Hardcoded strobes for debugging
  142. // Strobes start at 0 and go to 17 (18), not all Model Fs use all of the available strobes
  143. // The single row ribbon connector Model Fs only have a max of 16 strobes
  144. #define KISHSAVER_STROBE
  145. #ifdef KISHSAVER_STROBE
  146. total_strobes = 10;
  147. strobe_map[0] = 1; // Kishsaver doesn't use strobe 0
  148. strobe_map[1] = 2;
  149. strobe_map[2] = 3;
  150. strobe_map[3] = 4;
  151. strobe_map[4] = 5;
  152. strobe_map[5] = 6;
  153. strobe_map[6] = 7;
  154. strobe_map[7] = 8;
  155. strobe_map[8] = 9;
  156. strobe_map[9] = 15; // Test point strobe (3 test points, sense 1, 4, 5)
  157. #else
  158. // Strobe detection
  159. // TODO
  160. #endif
  161. // TODO all this code should probably be in scan_resetKeyboard
  162. for ( int i = 0; i < total_strobes; ++i)
  163. {
  164. cur_keymap[i] = 0;
  165. }
  166. for ( int i = 0; i < KEY_COUNT; ++i )
  167. {
  168. keys_averages[i] = DEFAULT_KEY_BASE;
  169. keys_averages_acc[i] = (DEFAULT_KEY_BASE);
  170. }
  171. /** warm things up a bit before we start collecting data, taking real samples. */
  172. for ( uint8_t i = 0; i < total_strobes; ++i )
  173. {
  174. sampleColumn( strobe_map[i] );
  175. }
  176. // Reset the keyboard before scanning, we might be in a wierd state
  177. // Also sets the KeyIndex_BufferUsed to 0
  178. scan_resetKeyboard();
  179. }
  180. // Main Detection Loop
  181. // This is where the important stuff happens
  182. inline uint8_t scan_loop()
  183. {
  184. // TODO dfj code...needs commenting + cleanup...
  185. uint8_t strober = 0;
  186. uint32_t full_av_acc = 0;
  187. for (strober = 0; strober < total_strobes; ++strober)
  188. {
  189. uint8_t tries = 1;
  190. while ( tries++ && sampleColumn( strobe_map[strober] ) ) { tries &= 0x7; } // don't waste this one just because the last one was poop.
  191. column = testColumn(strober);
  192. idle |= column; // if column has any pressed keys, then we are not idle.
  193. // TODO Is this needed anymore? Really only helps debug -HaaTa
  194. if( column != cur_keymap[strober] && ( boot_count >= WARMUP_LOOPS ) )
  195. {
  196. cur_keymap[strober] = column;
  197. keymap_change = 1;
  198. }
  199. idle |= keymap_change; // if any keys have changed inc. released, then we are not idle.
  200. if ( error == 0x50 )
  201. {
  202. error_data |= (((uint16_t)strober) << 12);
  203. }
  204. uint8_t strobe_line = strober << MUXES_COUNT_XSHIFT;
  205. for ( int i = 0; i < MUXES_COUNT; ++i )
  206. {
  207. // discard sketchy low bit, and meaningless high bits.
  208. uint8_t sample = samples[SAMPLE_OFFSET + i] >> 1;
  209. full_samples[strobe_line + i] = sample;
  210. keys_averages_acc[strobe_line + i] += sample;
  211. }
  212. for ( uint8_t i = SAMPLE_OFFSET; i < ( SAMPLE_OFFSET + MUXES_COUNT ); ++i )
  213. {
  214. full_av_acc += (samples[i]);
  215. }
  216. } // for strober
  217. #ifdef VERIFY_TEST_PAD
  218. // verify test key is not down.
  219. if ( ( cur_keymap[TEST_KEY_STROBE] & TEST_KEY_MASK ) )
  220. {
  221. error = 0x05;
  222. error_data = cur_keymap[TEST_KEY_STROBE] << 8;
  223. error_data += full_samples[TEST_KEY_STROBE * 8];
  224. }
  225. #endif
  226. /** aggregate if booting, or if idle;
  227. * else, if not booting, check for dirty USB.
  228. * */
  229. idle_count++;
  230. idle_count &= IDLE_COUNT_MASK;
  231. // Warm up voltage references
  232. if ( boot_count < WARMUP_LOOPS )
  233. {
  234. boot_count++;
  235. switch ( boot_count )
  236. {
  237. // First loop
  238. case 1:
  239. // Show msg at first iteration only
  240. info_msg("Warming up the voltage references");
  241. break;
  242. // Middle iterations
  243. case 300:
  244. case 600:
  245. case 900:
  246. case 1200:
  247. print(".");
  248. break;
  249. // Last loop
  250. case WARMUP_STOP:
  251. print("\n");
  252. info_msg("Warmup finished using ");
  253. printInt16( WARMUP_LOOPS );
  254. print(" iterations\n");
  255. break;
  256. }
  257. }
  258. else
  259. {
  260. // Reset accumulators and idle flag/counter
  261. if ( keymap_change )
  262. {
  263. for ( uint8_t c = 0; c < KEY_COUNT; ++c ) { keys_averages_acc[c] = 0; }
  264. idle_count = 0;
  265. idle = 0;
  266. keymap_change = 0;
  267. }
  268. if ( !idle_count )
  269. {
  270. if( idle )
  271. {
  272. // aggregate
  273. for ( uint8_t i = 0; i < KEY_COUNT; ++i )
  274. {
  275. uint16_t acc = keys_averages_acc[i] >> IDLE_COUNT_SHIFT;
  276. uint32_t av = keys_averages[i];
  277. av = (av << KEYS_AVERAGES_MIX_SHIFT) - av + acc;
  278. av >>= KEYS_AVERAGES_MIX_SHIFT;
  279. keys_averages[i] = av;
  280. keys_averages_acc[i] = 0;
  281. }
  282. }
  283. if ( boot_count >= WARMUP_LOOPS )
  284. {
  285. dump();
  286. }
  287. }
  288. }
  289. // Error case, should not occur in normal operation
  290. if ( error )
  291. {
  292. erro_msg("Problem detected... ");
  293. // Keymap scan debug
  294. for ( uint8_t i = 0; i < total_strobes; ++i )
  295. {
  296. printHex(cur_keymap[i]);
  297. print(" ");
  298. }
  299. print(" : ");
  300. printHex(error);
  301. error = 0;
  302. print(" : ");
  303. printHex(error_data);
  304. error_data = 0;
  305. // Display keymaps and other debug information if warmup completede
  306. if ( boot_count >= WARMUP_LOOPS )
  307. {
  308. dump();
  309. }
  310. }
  311. // Return non-zero if macro and USB processing should be delayed
  312. // Macro processing will always run if returning 0
  313. // USB processing only happens once the USB send timer expires, if it has not, scan_loop will be called
  314. // after the macro processing has been completed
  315. return 0;
  316. }
  317. // Reset Keyboard
  318. void scan_resetKeyboard( void )
  319. {
  320. // Empty buffer, now that keyboard has been reset
  321. KeyIndex_BufferUsed = 0;
  322. }
  323. // Send data to keyboard
  324. // NOTE: Only used for converters, since the scan module shouldn't handle sending data in a controller
  325. uint8_t scan_sendData( uint8_t dataPayload )
  326. {
  327. return 0;
  328. }
  329. // Reset/Hold keyboard
  330. // NOTE: Only used for converters, not needed for full controllers
  331. void scan_lockKeyboard( void )
  332. {
  333. }
  334. // NOTE: Only used for converters, not needed for full controllers
  335. void scan_unlockKeyboard( void )
  336. {
  337. }
  338. // Signal KeyIndex_Buffer that it has been properly read
  339. // NOTE: Only really required for implementing "tricks" in converters for odd protocols
  340. void scan_finishedWithBuffer( uint8_t sentKeys )
  341. {
  342. // Convenient place to clear the KeyIndex_Buffer
  343. KeyIndex_BufferUsed = 0;
  344. return;
  345. }
  346. // Signal KeyIndex_Buffer that it has been properly read and sent out by the USB module
  347. // NOTE: Only really required for implementing "tricks" in converters for odd protocols
  348. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  349. {
  350. return;
  351. }
  352. void setup_ADC()
  353. {
  354. // disable adc digital pins.
  355. DIDR1 |= (1 << AIN0D) | (1<<AIN1D); // set disable on pins 1,0.
  356. DDRF = 0x0;
  357. PORTF = 0x0;
  358. uint8_t mux = 0 & 0x1f; // 0 == first. // 0x1e = 1.1V ref.
  359. // 0 = external aref 1,1 = 2.56V internal ref
  360. uint8_t aref = ((1 << REFS1) | (1 << REFS0)) & ((1 << REFS1) | (1 << REFS0));
  361. uint8_t adate = (1 << ADATE) & (1 << ADATE); // trigger enable
  362. uint8_t trig = 0 & ((1 << ADTS0) | (1 << ADTS1) | (1 << ADTS2)); // 0 = free running
  363. // ps2, ps1 := /64 ( 2^6 ) ps2 := /16 (2^4), ps1 := 4, ps0 :=2, PS1,PS0 := 8 (2^8)
  364. uint8_t prescale = ( ((PRESCALE) << PRESCALE_SHIFT) & PRESCALE_MASK ); // 001 == 2^1 == 2
  365. uint8_t hispeed = (1 << ADHSM);
  366. uint8_t en_mux = (1 << ACME);
  367. ADCSRA = (1 << ADEN) | prescale; // ADC enable
  368. // select ref.
  369. //ADMUX |= ((1 << REFS1) | (1 << REFS0)); // 2.56 V internal.
  370. //ADMUX |= ((1 << REFS0) ); // Vcc with external cap.
  371. //ADMUX &= ~((1 << REFS1) | (1 << REFS0)); // 0,0 : aref.
  372. ADMUX = aref | mux | ADLAR_BITS;
  373. // set free-running
  374. ADCSRA |= adate; // trigger enable
  375. ADCSRB = en_mux | hispeed | trig | (ADCSRB & ~((1 << ADTS0) | (1 << ADTS1) | (1 << ADTS2))); // trigger select free running
  376. ADCSRA |= (1 << ADEN); // ADC enable
  377. ADCSRA |= (1 << ADSC); // start conversions q
  378. }
  379. void recovery( uint8_t on )
  380. {
  381. DDRB |= (1 << RECOVERY_CONTROL);
  382. PORTB &= ~(1 << RECOVERY_SINK); // SINK always zero
  383. DDRB &= ~(1 << RECOVERY_SOURCE); // SOURCE high imp
  384. if ( on )
  385. {
  386. // set strobes to sink to gnd.
  387. DDRC |= C_MASK;
  388. DDRD |= D_MASK;
  389. DDRE |= E_MASK;
  390. PORTC &= ~C_MASK;
  391. PORTD &= ~D_MASK;
  392. PORTE &= ~E_MASK;
  393. DDRB |= (1 << RECOVERY_SINK); // SINK pull
  394. PORTB |= (1 << RECOVERY_CONTROL);
  395. PORTB |= (1 << RECOVERY_SOURCE); // SOURCE high
  396. DDRB |= (1 << RECOVERY_SOURCE);
  397. }
  398. else
  399. {
  400. PORTB &= ~(1 << RECOVERY_CONTROL);
  401. DDRB &= ~(1 << RECOVERY_SOURCE);
  402. PORTB &= ~(1 << RECOVERY_SOURCE); // SOURCE low
  403. DDRB &= ~(1 << RECOVERY_SINK); // SINK high-imp
  404. }
  405. }
  406. void hold_sample( uint8_t on )
  407. {
  408. if ( !on )
  409. {
  410. PORTB |= (1 << SAMPLE_CONTROL);
  411. DDRB |= (1 << SAMPLE_CONTROL);
  412. }
  413. else
  414. {
  415. DDRB |= (1 << SAMPLE_CONTROL);
  416. PORTB &= ~(1 << SAMPLE_CONTROL);
  417. }
  418. }
  419. void strobe_w( uint8_t strobe_num )
  420. {
  421. PORTC &= ~(C_MASK);
  422. PORTD &= ~(D_MASK);
  423. PORTE &= ~(E_MASK);
  424. // Strobe table
  425. // Not all strobes are used depending on which are detected
  426. switch ( strobe_num )
  427. {
  428. case 0: PORTD |= (1 << 0); break;
  429. case 1: PORTD |= (1 << 1); break;
  430. case 2: PORTD |= (1 << 2); break;
  431. case 3: PORTD |= (1 << 3); break;
  432. case 4: PORTD |= (1 << 4); break;
  433. case 5: PORTD |= (1 << 5); break;
  434. case 6: PORTD |= (1 << 6); break;
  435. case 7: PORTD |= (1 << 7); break;
  436. case 8: PORTE |= (1 << 0); break;
  437. case 9: PORTE |= (1 << 1); break;
  438. case 10: PORTC |= (1 << 0); break;
  439. case 11: PORTC |= (1 << 1); break;
  440. case 12: PORTC |= (1 << 2); break;
  441. case 13: PORTC |= (1 << 3); break;
  442. case 14: PORTC |= (1 << 4); break;
  443. case 15: PORTC |= (1 << 5); break;
  444. case 16: PORTC |= (1 << 6); break;
  445. case 17: PORTC |= (1 << 7); break;
  446. default:
  447. break;
  448. }
  449. }
  450. inline uint16_t getADC(void)
  451. {
  452. ADCSRA |= (1 << ADIF); // clear int flag by writing 1.
  453. //wait for last read to complete.
  454. while ( !( ADCSRA & (1 << ADIF) ) );
  455. return ADC; // return sample
  456. }
  457. int sampleColumn_8x( uint8_t column, uint16_t * buffer )
  458. {
  459. // ensure all probe lines are driven low, and chill for recovery delay.
  460. ADCSRA |= (1 << ADEN) | (1 << ADSC); // enable and start conversions
  461. PORTC &= ~C_MASK;
  462. PORTD &= ~D_MASK;
  463. PORTE &= ~E_MASK;
  464. PORTF = 0;
  465. DDRF = 0;
  466. recovery(OFF);
  467. strobe_w(column);
  468. hold_sample(OFF);
  469. SET_FULL_MUX(0);
  470. for ( uint8_t i = 0; i < STROBE_SETTLE; ++i ) { getADC(); }
  471. hold_sample(ON);
  472. #undef MUX_SETTLE
  473. #if (MUX_SETTLE)
  474. for ( uint8_t mux = 0; mux < 8; ++mux )
  475. {
  476. SET_FULL_MUX(mux); // our sample will use this
  477. // wait for mux to settle.
  478. for ( uint8_t i = 0; i < MUX_SETTLE; ++i ) { getADC(); }
  479. // retrieve current read.
  480. buffer[mux] = getADC();
  481. }
  482. #else
  483. uint8_t mux = 0;
  484. SET_FULL_MUX(mux);
  485. getADC(); // throw away; unknown mux.
  486. do {
  487. SET_FULL_MUX(mux + 1); // our *next* sample will use this
  488. // retrieve current read.
  489. buffer[mux] = getADC();
  490. mux++;
  491. } while (mux < 8);
  492. #endif
  493. hold_sample(OFF);
  494. recovery(ON);
  495. // turn off adc.
  496. ADCSRA &= ~(1 << ADEN);
  497. // pull all columns' strobe-lines low.
  498. DDRC |= C_MASK;
  499. DDRD |= D_MASK;
  500. DDRE |= E_MASK;
  501. PORTC &= ~C_MASK;
  502. PORTD &= ~D_MASK;
  503. PORTE &= ~E_MASK;
  504. return 0;
  505. }
  506. int sampleColumn( uint8_t column )
  507. {
  508. int rval = 0;
  509. rval = sampleColumn_8x( column, samples + SAMPLE_OFFSET );
  510. return rval;
  511. }
  512. uint8_t testColumn( uint8_t strobe )
  513. {
  514. uint8_t column = 0;
  515. uint8_t bit = 1;
  516. for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux )
  517. {
  518. uint16_t delta = keys_averages[(strobe << MUXES_COUNT_XSHIFT) + mux];
  519. // Keypress detected
  520. if ( (db_sample = samples[SAMPLE_OFFSET + mux] >> 1) > (db_threshold = threshold) + (db_delta = delta) )
  521. {
  522. column |= bit;
  523. // Only register keypresses once the warmup is complete
  524. if ( boot_count >= WARMUP_LOOPS )
  525. {
  526. uint8_t key = (strobe << MUXES_COUNT_XSHIFT) + mux;
  527. // TODO Add debounce first
  528. // Add to the Macro processing buffer
  529. // Automatically handles converting to a USB code and sending off to the PC
  530. //bufferAdd( key );
  531. #define KEYSCAN_THRESHOLD_DEBUG
  532. #ifdef KEYSCAN_THRESHOLD_DEBUG
  533. // Debug message
  534. // <key> [<strobe>:<mux>] : <sense val> : <delta + threshold> : <margin>
  535. dbug_msg("0x");
  536. printHex_op( key, 2 );
  537. print(" [");
  538. printInt8( strobe );
  539. print(":");
  540. printInt8( mux );
  541. print("] : ");
  542. printHex( db_sample ); // Sense
  543. print(" : ");
  544. printHex( db_threshold );
  545. print("+");
  546. printHex( db_delta );
  547. print("=");
  548. printHex( db_threshold + db_delta ); // Sense compare
  549. print(" : ");
  550. printHex( db_sample - ( db_threshold + db_delta ) ); // Margin
  551. print("\n");
  552. #endif
  553. }
  554. }
  555. bit <<= 1;
  556. }
  557. return column;
  558. }
  559. void dump(void) {
  560. #ifdef DEBUG_FULL_SAMPLES_AVERAGES
  561. // we don't want to debug-out during the measurements.
  562. if ( !dump_count )
  563. {
  564. // Averages currently set per key
  565. for ( int i = 0; i < KEY_COUNT; ++i )
  566. {
  567. if ( !(i & 0x0f) )
  568. {
  569. print("\n");
  570. }
  571. else if ( !(i & 0x07) )
  572. {
  573. print(" ");
  574. }
  575. print(" ");
  576. printHex( keys_averages[i] );
  577. }
  578. print("\n");
  579. // Previously read full ADC scans?
  580. for ( int i = 0; i< KEY_COUNT; ++i)
  581. {
  582. if ( !(i & 0x0f) )
  583. {
  584. print("\n");
  585. }
  586. else if ( !(i & 0x07) )
  587. {
  588. print(" ");
  589. }
  590. print(" ");
  591. printHex(full_samples[i]);
  592. }
  593. }
  594. #endif
  595. #ifdef DEBUG_STROBE_SAMPLES_AVERAGES
  596. // Per strobe information
  597. uint8_t cur_strober = ze_strober;
  598. print("\n");
  599. printHex(cur_strober);
  600. // Previously read ADC scans on current strobe
  601. print(" :");
  602. for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
  603. {
  604. print(" ");
  605. printHex(full_samples[(cur_strober << MUXES_COUNT_XSHIFT) + i]);
  606. }
  607. // Averages current set on current strobe
  608. print(" :");
  609. for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
  610. {
  611. print(" ");
  612. printHex(keys_averages[(cur_strober << MUXES_COUNT_XSHIFT) + i]);
  613. }
  614. #endif
  615. #ifdef DEBUG_DELTA_SAMPLE_THRESHOLD
  616. print("\n");
  617. printHex( db_delta );
  618. print(" ");
  619. printHex( db_sample );
  620. print(" ");
  621. printHex( db_threshold );
  622. print(" ");
  623. printHex( column );
  624. #endif
  625. #ifdef DEBUG_USB_KEYMAP
  626. print("\n ");
  627. // Current keymap values
  628. for ( uint8_t i = 0; i < total_strobes; ++i )
  629. {
  630. printHex(cur_keymap[i]);
  631. print(" ");
  632. }
  633. #endif
  634. ze_strober++;
  635. ze_strober &= 0xf;
  636. dump_count++;
  637. dump_count &= 0x0f;
  638. }