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 22KB

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