Kiibohd Controller
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 470
  28. #define THRESHOLD_MV (MAX_PRESS_DELTA_MV >> 1)
  29. //(2560 / (0x3ff/2)) ~= 5
  30. #define MV_PER_ADC 5
  31. // 5
  32. #define THRESHOLD (THRESHOLD_MV / MV_PER_ADC)
  33. #define BUMP_DETECTION 0
  34. #define BUMP_THRESHOLD 0x50
  35. #define BUMP_REST_US 1200
  36. #define STROBE_SETTLE 1
  37. #define MUX_SETTLE 1
  38. #define TEST_KEY_STROBE (0x05)
  39. #define TEST_KEY_MASK (1 << 0)
  40. #define ADHSM 7
  41. #define RIGHT_JUSTIFY 0
  42. #define LEFT_JUSTIFY (0xff)
  43. // set left or right justification here:
  44. #define JUSTIFY_ADC RIGHT_JUSTIFY
  45. #define ADLAR_MASK (1 << ADLAR)
  46. #ifdef JUSTIFY_ADC
  47. #define ADLAR_BITS ((ADLAR_MASK) & (JUSTIFY_ADC))
  48. #else // defaults to right justification.
  49. #define ADLAR_BITS 0
  50. #endif
  51. // full muxmask
  52. #define FULL_MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2) | (1 << MUX3) | (1 << MUX4))
  53. // F0-f7 pins only muxmask.
  54. #define MUX_MASK ((1 << MUX0) | (1 << MUX1) | (1 << MUX2))
  55. // Strobe Masks
  56. #define D_MASK (0xff)
  57. #define E_MASK (0x03)
  58. #define C_MASK (0xff)
  59. // set ADC clock prescale
  60. #define PRESCALE_MASK ((1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2))
  61. #define PRESCALE_SHIFT (ADPS0)
  62. #define PRESCALE 3
  63. // TODO Remove this define when unnecessary -HaaTa
  64. #define STROBE_LINES 16
  65. #define MUXES_COUNT 8
  66. #define MUXES_COUNT_XSHIFT 3
  67. #define WARMUP_LOOPS ( 1024 )
  68. #define WARMUP_STOP (WARMUP_LOOPS - 1)
  69. #define SAMPLES 10
  70. #define SAMPLE_OFFSET ((SAMPLES) - MUXES_COUNT)
  71. #define SAMPLE_CONTROL 3
  72. // TODO Figure out calculation or best way to determine at startup -HaaTa
  73. //#define DEFAULT_KEY_BASE 0xc8
  74. #define DEFAULT_KEY_BASE 0x95
  75. #define KEY_COUNT ((STROBE_LINES) * (MUXES_COUNT))
  76. #define RECOVERY_CONTROL 1
  77. #define RECOVERY_SOURCE 0
  78. #define RECOVERY_SINK 2
  79. #define ON 1
  80. #define OFF 0
  81. // mix in 1/4 of the current average to the running average. -> (@mux_mix = 2)
  82. #define MUX_MIX 2
  83. #define IDLE_COUNT_MASK 0xff
  84. #define IDLE_COUNT_SHIFT 8
  85. // av = (av << shift) - av + sample; av >>= shift
  86. // e.g. 1 -> (av + sample) / 2 simple average of new and old
  87. // 2 -> (3 * av + sample) / 4 i.e. 3:1 mix of old to new.
  88. // 3 -> (7 * av + sample) / 8 i.e. 7:1 mix of old to new.
  89. #define KEYS_AVERAGES_MIX_SHIFT 3
  90. // ----- Macros -----
  91. // Make sure we haven't overflowed the buffer
  92. #define bufferAdd(byte) \
  93. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  94. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  95. // Select mux
  96. #define SET_FULL_MUX(X) ((ADMUX) = (((ADMUX) & ~(FULL_MUX_MASK)) | ((X) & (FULL_MUX_MASK))))
  97. // ----- Variables -----
  98. // Buffer used to inform the macro processing module which keys have been detected as pressed
  99. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  100. volatile uint8_t KeyIndex_BufferUsed;
  101. // TODO dfj variables...needs cleaning up and commenting
  102. volatile uint16_t full_av = 0;
  103. uint8_t ze_strober = 0;
  104. uint16_t samples [SAMPLES];
  105. uint16_t adc_mux_averages [MUXES_COUNT];
  106. uint16_t adc_strobe_averages[STROBE_LINES];
  107. uint8_t cur_keymap[STROBE_LINES];
  108. uint8_t keymap_change;
  109. uint16_t threshold = 0x25; // HaaTa Hack -TODO
  110. //uint16_t threshold = 0x16; // HaaTa Hack -TODO
  111. //uint16_t threshold = THRESHOLD;
  112. uint8_t column = 0;
  113. uint16_t keys_averages_acc[KEY_COUNT];
  114. uint16_t keys_averages[KEY_COUNT];
  115. uint8_t full_samples[KEY_COUNT];
  116. // TODO: change this to 'booting', then count down.
  117. uint16_t boot_count = 0;
  118. uint16_t idle_count = 0;
  119. uint8_t idle = 1;
  120. uint8_t error = 0;
  121. uint16_t error_data = 0;
  122. uint16_t mux_averages[MUXES_COUNT];
  123. uint16_t strobe_averages[STROBE_LINES];
  124. uint8_t dump_count = 0;
  125. uint16_t db_delta = 0;
  126. uint8_t db_sample = 0;
  127. uint16_t db_threshold = 0;
  128. // ----- Function Declarations -----
  129. void dump( void );
  130. void recovery( uint8_t on );
  131. int sampleColumn( uint8_t column );
  132. void setup_ADC( void );
  133. void strobe_w( uint8_t strobe_num );
  134. uint8_t testColumn( uint8_t strobe );
  135. // ----- Functions -----
  136. // Initial setup for cap sense controller
  137. inline void scan_setup()
  138. {
  139. // TODO dfj code...needs cleanup + commenting...
  140. setup_ADC();
  141. DDRC = C_MASK;
  142. PORTC = 0;
  143. DDRD = D_MASK;
  144. PORTD = 0;
  145. DDRE = E_MASK;
  146. PORTE = 0 ;
  147. // TODO all this code should probably be in scan_resetKeyboard
  148. for (int i=0; i < STROBE_LINES; ++i) {
  149. cur_keymap[i] = 0;
  150. }
  151. for(int i=0; i < MUXES_COUNT; ++i) {
  152. adc_mux_averages[i] = 0x20; // experimentally determined.
  153. }
  154. for(int i=0; i < STROBE_LINES; ++i) {
  155. adc_strobe_averages[i] = 0x20; // yup.
  156. }
  157. for(int i=0; i < KEY_COUNT; ++i) {
  158. keys_averages[i] = DEFAULT_KEY_BASE;
  159. keys_averages_acc[i] = (DEFAULT_KEY_BASE);
  160. }
  161. /** warm things up a bit before we start collecting data, taking real samples. */
  162. for(uint8_t i = 0; i < STROBE_LINES; ++i) {
  163. sampleColumn(i);
  164. }
  165. // Reset the keyboard before scanning, we might be in a wierd state
  166. // Also sets the KeyIndex_BufferUsed to 0
  167. scan_resetKeyboard();
  168. }
  169. // Main Detection Loop
  170. // This is where the important stuff happens
  171. inline uint8_t scan_loop()
  172. {
  173. // TODO dfj code...needs commenting + cleanup...
  174. uint8_t strober = 0;
  175. uint32_t full_av_acc = 0;
  176. for (strober = 0; strober < STROBE_LINES; ++strober)
  177. {
  178. uint8_t tries = 1;
  179. while ( tries++ && sampleColumn( strober ) ) { tries &= 0x7; } // don't waste this one just because the last one was poop.
  180. column = testColumn(strober);
  181. idle |= column; // if column has any pressed keys, then we are not idle.
  182. if( column != cur_keymap[strober] && ( boot_count >= WARMUP_LOOPS ) )
  183. {
  184. cur_keymap[strober] = column;
  185. keymap_change = 1;
  186. // The keypresses on this strobe are now know, send them right away
  187. for ( uint8_t mux = 0; mux < MUXES_COUNT; ++mux )
  188. {
  189. if ( column & (1 << mux) )
  190. {
  191. uint8_t key = (strober << MUXES_COUNT_XSHIFT) + mux;
  192. // Add to the Macro processing buffer
  193. // Automatically handles converting to a USB code and sending off to the PC
  194. //bufferAdd( key );
  195. printHex( key );
  196. print("\n");
  197. }
  198. }
  199. }
  200. idle |= keymap_change; // if any keys have changed inc. released, then we are not idle.
  201. if ( error == 0x50 )
  202. {
  203. error_data |= (((uint16_t)strober) << 12);
  204. }
  205. uint8_t strobe_line = strober << MUXES_COUNT_XSHIFT;
  206. for ( int i = 0; i < MUXES_COUNT; ++i )
  207. {
  208. // discard sketchy low bit, and meaningless high bits.
  209. uint8_t sample = samples[SAMPLE_OFFSET + i] >> 1;
  210. full_samples[strobe_line + i] = sample;
  211. keys_averages_acc[strobe_line + i] += sample;
  212. }
  213. strobe_averages[strober] = 0;
  214. for ( uint8_t i = SAMPLE_OFFSET; i < ( SAMPLE_OFFSET + MUXES_COUNT ); ++i )
  215. {
  216. full_av_acc += (samples[i]);
  217. #ifdef COLLECT_STROBE_AVERAGES
  218. mux_averages[i - SAMPLE_OFFSET] += samples[i];
  219. strobe_averages[strober] += samples[i];
  220. #endif
  221. }
  222. #ifdef COLLECT_STROBE_AVERAGES
  223. adc_strobe_averages[strober] += strobe_averages[strober] >> 3;
  224. adc_strobe_averages[strober] >>= 1;
  225. /** test if we went negative. */
  226. if ( ( adc_strobe_averages[strober] & 0xFF00 ) && ( boot_count >= WARMUP_LOOPS ) )
  227. {
  228. error = 0xf; error_data = adc_strobe_averages[strober];
  229. }
  230. #endif
  231. } // for strober
  232. #ifdef VERIFY_TEST_PAD
  233. // verify test key is not down.
  234. if ( ( cur_keymap[TEST_KEY_STROBE] & TEST_KEY_MASK ) )
  235. {
  236. error = 0x05;
  237. error_data = cur_keymap[TEST_KEY_STROBE] << 8;
  238. error_data += full_samples[TEST_KEY_STROBE * 8];
  239. //threshold++;
  240. }
  241. #endif
  242. #ifdef COLLECT_STROBE_AVERAGES
  243. // calc mux averages.
  244. if ( boot_count < WARMUP_LOOPS )
  245. {
  246. full_av += (full_av_acc >> (7));
  247. full_av >>= 1;
  248. full_av_acc = 0;
  249. for ( int i = 0; i < MUXES_COUNT; ++i )
  250. {
  251. adc_mux_averages[i] = (adc_mux_averages[i] << MUX_MIX) - adc_mux_averages[i];
  252. adc_mux_averages[i] += (mux_averages[i] >> 4);
  253. adc_mux_averages[i] >>= MUX_MIX;
  254. mux_averages[i] = 0;
  255. }
  256. }
  257. #endif
  258. /** aggregate if booting, or if idle;
  259. * else, if not booting, check for dirty USB.
  260. * */
  261. idle_count++;
  262. idle_count &= IDLE_COUNT_MASK;
  263. // Warm up voltage references
  264. if ( boot_count < WARMUP_LOOPS )
  265. {
  266. boot_count++;
  267. switch ( boot_count )
  268. {
  269. // First loop
  270. case 1:
  271. // Show msg at first iteration only
  272. info_msg("Warming up the voltage references");
  273. break;
  274. // Middle iterations
  275. case 300:
  276. case 600:
  277. case 900:
  278. case 1200:
  279. print(".");
  280. break;
  281. // Last loop
  282. case WARMUP_STOP:
  283. print("\n");
  284. info_msg("Warmup finished using ");
  285. printInt16( WARMUP_LOOPS );
  286. print(" iterations\n");
  287. break;
  288. }
  289. }
  290. else
  291. {
  292. // Reset accumulators and idle flag/counter
  293. if ( keymap_change )
  294. {
  295. for ( uint8_t c = 0; c < KEY_COUNT; ++c ) { keys_averages_acc[c] = 0; }
  296. idle_count = 0;
  297. idle = 0;
  298. keymap_change = 0;
  299. }
  300. if ( !idle_count )
  301. {
  302. if( idle )
  303. {
  304. // aggregate
  305. for ( uint8_t i = 0; i < KEY_COUNT; ++i )
  306. {
  307. uint16_t acc = keys_averages_acc[i] >> IDLE_COUNT_SHIFT;
  308. uint32_t av = keys_averages[i];
  309. av = (av << KEYS_AVERAGES_MIX_SHIFT) - av + acc;
  310. av >>= KEYS_AVERAGES_MIX_SHIFT;
  311. keys_averages[i] = av;
  312. keys_averages_acc[i] = 0;
  313. }
  314. }
  315. if ( boot_count >= WARMUP_LOOPS )
  316. {
  317. dump();
  318. }
  319. sampleColumn(0x0); // to resync us if we dumped a mess 'o text.
  320. }
  321. }
  322. // Error case, should not occur in normal operation
  323. if ( error )
  324. {
  325. erro_msg("Problem detected... ");
  326. // Keymap scan debug
  327. for ( uint8_t i = 0; i < STROBE_LINES; ++i )
  328. {
  329. printHex(cur_keymap[i]);
  330. print(" ");
  331. }
  332. print(" : ");
  333. printHex(error);
  334. error = 0;
  335. print(" : ");
  336. printHex(error_data);
  337. error_data = 0;
  338. // Display keymaps and other debug information if warmup completede
  339. if ( boot_count >= WARMUP_LOOPS )
  340. {
  341. dump();
  342. }
  343. }
  344. // Return non-zero if macro and USB processing should be delayed
  345. // Macro processing will always run if returning 0
  346. // USB processing only happens once the USB send timer expires, if it has not, scan_loop will be called
  347. // after the macro processing has been completed
  348. return 0;
  349. }
  350. // Reset Keyboard
  351. void scan_resetKeyboard( void )
  352. {
  353. // Empty buffer, now that keyboard has been reset
  354. KeyIndex_BufferUsed = 0;
  355. }
  356. // Send data to keyboard
  357. // NOTE: Only used for converters, since the scan module shouldn't handle sending data in a controller
  358. uint8_t scan_sendData( uint8_t dataPayload )
  359. {
  360. return 0;
  361. }
  362. // Reset/Hold keyboard
  363. // NOTE: Only used for converters, not needed for full controllers
  364. void scan_lockKeyboard( void )
  365. {
  366. }
  367. // NOTE: Only used for converters, not needed for full controllers
  368. void scan_unlockKeyboard( void )
  369. {
  370. }
  371. // Signal KeyIndex_Buffer that it has been properly read
  372. // NOTE: Only really required for implementing "tricks" in converters for odd protocols
  373. void scan_finishedWithBuffer( uint8_t sentKeys )
  374. {
  375. // Convenient place to clear the KeyIndex_Buffer
  376. KeyIndex_BufferUsed = 0;
  377. return;
  378. }
  379. // Signal KeyIndex_Buffer that it has been properly read and sent out by the USB module
  380. // NOTE: Only really required for implementing "tricks" in converters for odd protocols
  381. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  382. {
  383. return;
  384. }
  385. void _delay_loop( uint8_t __count )
  386. {
  387. __asm__ volatile (
  388. "1: dec %0" "\n\t"
  389. "brne 1b"
  390. : "=r" (__count)
  391. : "0" (__count)
  392. );
  393. }
  394. void setup_ADC()
  395. {
  396. // disable adc digital pins.
  397. DIDR1 |= (1 << AIN0D) | (1<<AIN1D); // set disable on pins 1,0.
  398. DDRF = 0x0;
  399. PORTF = 0x0;
  400. uint8_t mux = 0 & 0x1f; // 0 == first. // 0x1e = 1.1V ref.
  401. // 0 = external aref 1,1 = 2.56V internal ref
  402. uint8_t aref = ((1 << REFS1) | (1 << REFS0)) & ((1 << REFS1) | (1 << REFS0));
  403. uint8_t adate = (1 << ADATE) & (1 << ADATE); // trigger enable
  404. uint8_t trig = 0 & ((1 << ADTS0) | (1 << ADTS1) | (1 << ADTS2)); // 0 = free running
  405. // ps2, ps1 := /64 ( 2^6 ) ps2 := /16 (2^4), ps1 := 4, ps0 :=2, PS1,PS0 := 8 (2^8)
  406. uint8_t prescale = ( ((PRESCALE) << PRESCALE_SHIFT) & PRESCALE_MASK ); // 001 == 2^1 == 2
  407. uint8_t hispeed = (1 << ADHSM);
  408. uint8_t en_mux = (1 << ACME);
  409. ADCSRA = (1 << ADEN) | prescale; // ADC enable
  410. // select ref.
  411. //ADMUX |= ((1 << REFS1) | (1 << REFS0)); // 2.56 V internal.
  412. //ADMUX |= ((1 << REFS0) ); // Vcc with external cap.
  413. //ADMUX &= ~((1 << REFS1) | (1 << REFS0)); // 0,0 : aref.
  414. ADMUX = aref | mux | ADLAR_BITS;
  415. // set free-running
  416. ADCSRA |= adate; // trigger enable
  417. ADCSRB = en_mux | hispeed | trig | (ADCSRB & ~((1 << ADTS0) | (1 << ADTS1) | (1 << ADTS2))); // trigger select free running
  418. ADCSRA |= (1 << ADEN); // ADC enable
  419. ADCSRA |= (1 << ADSC); // start conversions q
  420. }
  421. void recovery( uint8_t on )
  422. {
  423. DDRB |= (1 << RECOVERY_CONTROL);
  424. PORTB &= ~(1 << RECOVERY_SINK); // SINK always zero
  425. DDRB &= ~(1 << RECOVERY_SOURCE); // SOURCE high imp
  426. if ( on )
  427. {
  428. // set strobes to sink to gnd.
  429. DDRC |= C_MASK;
  430. DDRD |= D_MASK;
  431. DDRE |= E_MASK;
  432. PORTC &= ~C_MASK;
  433. PORTD &= ~D_MASK;
  434. PORTE &= ~E_MASK;
  435. DDRB |= (1 << RECOVERY_SINK); // SINK pull
  436. PORTB |= (1 << RECOVERY_CONTROL);
  437. PORTB |= (1 << RECOVERY_SOURCE); // SOURCE high
  438. DDRB |= (1 << RECOVERY_SOURCE);
  439. }
  440. else
  441. {
  442. PORTB &= ~(1 << RECOVERY_CONTROL);
  443. DDRB &= ~(1 << RECOVERY_SOURCE);
  444. PORTB &= ~(1 << RECOVERY_SOURCE); // SOURCE low
  445. DDRB &= ~(1 << RECOVERY_SINK); // SINK high-imp
  446. }
  447. }
  448. void hold_sample( uint8_t on )
  449. {
  450. if ( !on )
  451. {
  452. PORTB |= (1 << SAMPLE_CONTROL);
  453. DDRB |= (1 << SAMPLE_CONTROL);
  454. }
  455. else
  456. {
  457. DDRB |= (1 << SAMPLE_CONTROL);
  458. PORTB &= ~(1 << SAMPLE_CONTROL);
  459. }
  460. }
  461. void strobe_w( uint8_t strobe_num )
  462. {
  463. PORTC &= ~(C_MASK);
  464. PORTD &= ~(D_MASK);
  465. PORTE &= ~(E_MASK);
  466. #ifdef SHORT_C
  467. //strobe_num = 15 - strobe_num;
  468. #endif
  469. /*
  470. printHex( strobe_num );
  471. print(" ");
  472. strobe_num = 9 - strobe_num;
  473. printHex( strobe_num );
  474. print("\n");
  475. */
  476. switch(strobe_num) {
  477. // XXX Kishsaver strobe (note that D0, D1 are not used)
  478. case 0: PORTD |= (1 << 0); break;
  479. case 1: PORTD |= (1 << 1); break;
  480. case 2: PORTD |= (1 << 2); break;
  481. case 3: PORTD |= (1 << 3); break;
  482. case 4: PORTD |= (1 << 4); break;
  483. case 5: PORTD |= (1 << 5); break;
  484. // TODO REMOVEME
  485. case 6: PORTD |= (1 << 6); break;
  486. case 7: PORTD |= (1 << 7); break;
  487. case 8: PORTE |= (1 << 0); break;
  488. case 9: PORTE |= (1 << 1); break;
  489. //case 15: PORTC |= (1 << 5); break; // Test strobe on kishsaver
  490. #if 0
  491. // XXX Kishsaver strobe (note that D0, D1 are not used)
  492. case 0: PORTD |= (1 << 2); break;
  493. case 1: PORTD |= (1 << 3); break;
  494. case 2: PORTD |= (1 << 4); break;
  495. case 3: PORTD |= (1 << 5); break;
  496. // TODO REMOVEME
  497. case 4: PORTD |= (1 << 6); break;
  498. case 5: PORTD |= (1 << 7); break;
  499. case 6: PORTE |= (1 << 0); break;
  500. case 7: PORTE |= (1 << 1); break;
  501. case 15: PORTC |= (1 << 5); break; // Test strobe on kishsaver
  502. #endif
  503. /*
  504. #ifdef ALL_D
  505. case 6: PORTD |= (1 << 6); break;
  506. case 7: PORTD |= (1 << 7); break;
  507. case 8: PORTC |= (1 << 0); break;
  508. case 9: PORTC |= (1 << 1); break;
  509. case 10: PORTC |= (1 << 2); break;
  510. case 11: PORTC |= (1 << 3); break;
  511. case 12: PORTC |= (1 << 4); break;
  512. case 13: PORTC |= (1 << 5); break;
  513. case 14: PORTC |= (1 << 6); break;
  514. case 15: PORTC |= (1 << 7); break;
  515. case 16: PORTE |= (1 << 0); break;
  516. case 17: PORTE |= (1 << 1); break;
  517. #else
  518. #ifdef SHORT_D
  519. case 6: PORTE |= (1 << 0); break;
  520. case 7: PORTE |= (1 << 1); break;
  521. case 8: PORTC |= (1 << 0); break;
  522. case 9: PORTC |= (1 << 1); break;
  523. case 10: PORTC |= (1 << 2); break;
  524. case 11: PORTC |= (1 << 3); break;
  525. case 12: PORTC |= (1 << 4); break;
  526. case 13: PORTC |= (1 << 5); break;
  527. case 14: PORTC |= (1 << 6); break;
  528. case 15: PORTC |= (1 << 7); break;
  529. #else
  530. #ifdef SHORT_C
  531. case 6: PORTD |= (1 << 6); break;
  532. case 7: PORTD |= (1 << 7); break;
  533. case 8: PORTE |= (1 << 0); break;
  534. case 9: PORTE |= (1 << 1); break;
  535. case 10: PORTC |= (1 << 0); break;
  536. case 11: PORTC |= (1 << 1); break;
  537. case 12: PORTC |= (1 << 2); break;
  538. case 13: PORTC |= (1 << 3); break;
  539. case 14: PORTC |= (1 << 4); break;
  540. case 15: PORTC |= (1 << 5); break;
  541. case 16: PORTC |= (1 << 6); break;
  542. case 17: PORTC |= (1 << 7); break;
  543. #endif
  544. #endif
  545. #endif
  546. */
  547. default:
  548. break;
  549. }
  550. #if 0 // New code from dfj -> still needs redoing for kishsaver and autodetection of strobes
  551. #ifdef SHORT_C
  552. strobe_num = 15 - strobe_num;
  553. #endif
  554. #ifdef SINGLE_COLUMN_TEST
  555. strobe_num = 5;
  556. #endif
  557. switch(strobe_num) {
  558. case 0: PORTD |= (1 << 0); DDRD &= ~(1 << 0); break;
  559. case 1: PORTD |= (1 << 1); DDRD &= ~(1 << 1); break;
  560. case 2: PORTD |= (1 << 2); DDRD &= ~(1 << 2); break;
  561. case 3: PORTD |= (1 << 3); DDRD &= ~(1 << 3); break;
  562. case 4: PORTD |= (1 << 4); DDRD &= ~(1 << 4); break;
  563. case 5: PORTD |= (1 << 5); DDRD &= ~(1 << 5); break;
  564. #ifdef ALL_D
  565. case 6: PORTD |= (1 << 6); break;
  566. case 7: PORTD |= (1 << 7); break;
  567. case 8: PORTC |= (1 << 0); break;
  568. case 9: PORTC |= (1 << 1); break;
  569. case 10: PORTC |= (1 << 2); break;
  570. case 11: PORTC |= (1 << 3); break;
  571. case 12: PORTC |= (1 << 4); break;
  572. case 13: PORTC |= (1 << 5); break;
  573. case 14: PORTC |= (1 << 6); break;
  574. case 15: PORTC |= (1 << 7); break;
  575. case 16: PORTE |= (1 << 0); break;
  576. case 17: PORTE |= (1 << 1); break;
  577. #else
  578. #ifdef SHORT_D
  579. case 6: PORTE |= (1 << 0); break;
  580. case 7: PORTE |= (1 << 1); break;
  581. case 8: PORTC |= (1 << 0); break;
  582. case 9: PORTC |= (1 << 1); break;
  583. case 10: PORTC |= (1 << 2); break;
  584. case 11: PORTC |= (1 << 3); break;
  585. case 12: PORTC |= (1 << 4); break;
  586. case 13: PORTC |= (1 << 5); break;
  587. case 14: PORTC |= (1 << 6); break;
  588. case 15: PORTC |= (1 << 7); break;
  589. #else
  590. #ifdef SHORT_C
  591. case 6: PORTD |= (1 << 6); DDRD &= ~(1 << 6); break;
  592. case 7: PORTD |= (1 << 7); DDRD &= ~(1 << 7); break;
  593. case 8: PORTE |= (1 << 0); DDRE &= ~(1 << 0); break;
  594. case 9: PORTE |= (1 << 1); DDRE &= ~(1 << 1); break;
  595. case 10: PORTC |= (1 << 0); DDRC &= ~(1 << 0); break;
  596. case 11: PORTC |= (1 << 1); DDRC &= ~(1 << 1); break;
  597. case 12: PORTC |= (1 << 2); DDRC &= ~(1 << 2); break;
  598. case 13: PORTC |= (1 << 3); DDRC &= ~(1 << 3); break;
  599. case 14: PORTC |= (1 << 4); DDRC &= ~(1 << 4); break;
  600. case 15: PORTC |= (1 << 5); DDRC &= ~(1 << 5); break;
  601. case 16: PORTC |= (1 << 6); DDRC &= ~(1 << 6); break;
  602. case 17: PORTC |= (1 << 7); DDRC &= ~(1 << 7); break;
  603. #endif
  604. #endif
  605. #endif
  606. default:
  607. break;
  608. }
  609. #endif
  610. }
  611. inline uint16_t getADC(void)
  612. {
  613. ADCSRA |= (1 << ADIF); // clear int flag by writing 1.
  614. //wait for last read to complete.
  615. while ( !( ADCSRA & (1 << ADIF) ) );
  616. return ADC; // return sample
  617. }
  618. int sampleColumn_8x( uint8_t column, uint16_t * buffer )
  619. {
  620. // ensure all probe lines are driven low, and chill for recovery delay.
  621. ADCSRA |= (1 << ADEN) | (1 << ADSC); // enable and start conversions
  622. PORTC &= ~C_MASK;
  623. PORTD &= ~D_MASK;
  624. PORTE &= ~E_MASK;
  625. PORTF = 0;
  626. DDRF = 0;
  627. recovery(OFF);
  628. strobe_w(column);
  629. hold_sample(OFF);
  630. SET_FULL_MUX(0);
  631. for ( uint8_t i = 0; i < STROBE_SETTLE; ++i ) { getADC(); }
  632. hold_sample(ON);
  633. #undef MUX_SETTLE
  634. #if (MUX_SETTLE)
  635. for ( uint8_t mux = 0; mux < 8; ++mux )
  636. {
  637. SET_FULL_MUX(mux); // our sample will use this
  638. // wait for mux to settle.
  639. for ( uint8_t i = 0; i < MUX_SETTLE; ++i ) { getADC(); }
  640. // retrieve current read.
  641. buffer[mux] = getADC();
  642. }
  643. #else
  644. uint8_t mux = 0;
  645. SET_FULL_MUX(mux);
  646. getADC(); // throw away; unknown mux.
  647. do {
  648. SET_FULL_MUX(mux + 1); // our *next* sample will use this
  649. // retrieve current read.
  650. buffer[mux] = getADC();
  651. mux++;
  652. } while (mux < 8);
  653. #endif
  654. hold_sample(OFF);
  655. recovery(ON);
  656. // turn off adc.
  657. ADCSRA &= ~(1 << ADEN);
  658. // pull all columns' strobe-lines low.
  659. DDRC |= C_MASK;
  660. DDRD |= D_MASK;
  661. DDRE |= E_MASK;
  662. PORTC &= ~C_MASK;
  663. PORTD &= ~D_MASK;
  664. PORTE &= ~E_MASK;
  665. return 0;
  666. }
  667. int sampleColumn( uint8_t column )
  668. {
  669. int rval = 0;
  670. rval = sampleColumn_8x( column, samples + SAMPLE_OFFSET );
  671. #if (BUMP_DETECTION)
  672. for ( uint8_t i = 0; i < 8; ++i )
  673. {
  674. if ( samples[SAMPLE_OFFSET + i] - adc_mux_averages[i] > BUMP_THRESHOLD )
  675. {
  676. // was a hump
  677. _delay_us(BUMP_REST_US);
  678. rval++;
  679. error = 0x50;
  680. error_data = samples[SAMPLE_OFFSET +i]; // | ((uint16_t)i << 8);
  681. return rval;
  682. }
  683. }
  684. #endif
  685. return rval;
  686. }
  687. uint8_t testColumn( uint8_t strobe )
  688. {
  689. uint8_t column = 0;
  690. uint8_t bit = 1;
  691. for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
  692. {
  693. uint16_t delta = keys_averages[(strobe << MUXES_COUNT_XSHIFT) + i];
  694. if ( (db_sample = samples[SAMPLE_OFFSET + i] >> 1) > (db_threshold = threshold) + (db_delta = delta) )
  695. {
  696. column |= bit;
  697. }
  698. #ifdef THRESHOLD_VERIFICATION
  699. if ( db_sample > 0xA0 )
  700. {
  701. printHex( db_sample );
  702. print(" : ");
  703. printHex( db_threshold );
  704. print(" : ");
  705. printHex( db_delta );
  706. print(" :: ");
  707. printHex( column );
  708. print(" : ");
  709. printHex( strobe );
  710. print(NL);
  711. }
  712. #endif
  713. bit <<= 1;
  714. }
  715. return column;
  716. }
  717. void dump(void) {
  718. #ifdef DEBUG_FULL_SAMPLES_AVERAGES
  719. // we don't want to debug-out during the measurements.
  720. if ( !dump_count )
  721. {
  722. // Averages currently set per key
  723. for ( int i = 0; i < KEY_COUNT; ++i )
  724. {
  725. if ( !(i & 0x0f) )
  726. {
  727. print("\n");
  728. }
  729. else if ( !(i & 0x07) )
  730. {
  731. print(" ");
  732. }
  733. print(" ");
  734. printHex( keys_averages[i] );
  735. }
  736. print("\n");
  737. // Previously read full ADC scans?
  738. for ( int i = 0; i< KEY_COUNT; ++i)
  739. {
  740. if ( !(i & 0x0f) )
  741. {
  742. print("\n");
  743. }
  744. else if ( !(i & 0x07) )
  745. {
  746. print(" ");
  747. }
  748. print(" ");
  749. printHex(full_samples[i]);
  750. }
  751. }
  752. #endif
  753. #ifdef DEBUG_STROBE_SAMPLES_AVERAGES
  754. // Per strobe information
  755. uint8_t cur_strober = ze_strober;
  756. print("\n");
  757. printHex(cur_strober);
  758. // Previously read ADC scans on current strobe
  759. print(" :");
  760. for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
  761. {
  762. print(" ");
  763. printHex(full_samples[(cur_strober << MUXES_COUNT_XSHIFT) + i]);
  764. }
  765. // Averages current set on current strobe
  766. print(" :");
  767. for ( uint8_t i = 0; i < MUXES_COUNT; ++i )
  768. {
  769. print(" ");
  770. printHex(keys_averages[(cur_strober << MUXES_COUNT_XSHIFT) + i]);
  771. }
  772. #endif
  773. #ifdef DEBUG_DELTA_SAMPLE_THRESHOLD
  774. print("\n");
  775. printHex( db_delta );
  776. print(" ");
  777. printHex( db_sample );
  778. print(" ");
  779. printHex( db_threshold );
  780. print(" ");
  781. printHex( column );
  782. #endif
  783. #ifdef DEBUG_USB_KEYMAP
  784. print("\n ");
  785. // Current keymap values
  786. for ( uint8_t i = 0; i < STROBE_LINES; ++i )
  787. {
  788. printHex(cur_keymap[i]);
  789. print(" ");
  790. }
  791. #endif
  792. ze_strober++;
  793. ze_strober &= 0xf;
  794. dump_count++;
  795. dump_count &= 0x0f;
  796. }