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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /* Copyright (C) 2012,2014 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. // ----- Includes -----
  22. // Compiler Includes
  23. #include <Lib/ScanLib.h>
  24. // Project Includes
  25. #include <led.h>
  26. #include <print.h>
  27. // Local Includes
  28. #include "scan_loop.h"
  29. // ----- Defines -----
  30. // - Pinout Defines -
  31. // Scan Bit Pins (from keyboard)
  32. // - Reads in the ASCII scancode
  33. // - Shift and ShiftLock are handled internally
  34. #define READSCAN_PORT PORTC
  35. #define READSCAN_DDR DDRC
  36. #define READSCAN_PIN PINC
  37. // Interrupt Pins (from keyboard)
  38. // - CODEINT (Code key signal interrupt/press)
  39. // - Normally high, low when "Code" key is pressed; separate from all other key presses
  40. // - PRESSINT (Press signal interrupt/press)
  41. // - Normally high, low when any key (or multiple except "Code") is pressed, returns to high once all keys are released
  42. // - Signal is changed BEFORE the Scan Bits are updated
  43. // - PULSEINT (Key action pulse interrupt/press)
  44. // - Normally high, low pulses of 147us on key presses (depending on the combination of mode control pins)
  45. // - Pulse is guarranteed to sent after the Scan Bits are updated
  46. #define CODEINT_PORT PORTE
  47. #define CODEINT_DDR DDRE
  48. #define CODEINT_PIN PINE
  49. #define CODEINT_POS 7
  50. #define PRESSINT_PORT PORTE
  51. #define PRESSINT_DDR DDRE
  52. #define PRESSINT_POS 6
  53. #define PULSEINT_PORT PORTD
  54. #define PULSEINT_DDR DDRD
  55. #define PULSEINT_POS 3
  56. // LED Pins (to keyboard)
  57. // - 1 disable LED
  58. // - 1 enable LED
  59. #define LED1_PORT PORTF // [Pin 19]
  60. #define LED1_DDR DDRF
  61. #define LED1_POS 6
  62. #define LED2_PORT PORTF // [Pin 20]
  63. #define LED2_DDR DDRF
  64. #define LED2_POS 7
  65. // Mode Control Pins (to keyboard)
  66. // - Repeat [Pin 14]
  67. // - 1 Single pulse mode (PULSEINT)
  68. // - 0 Repeated pulse mode (PULSEINT) (1 pulse, pause, then constant pulses)
  69. // - Multi [Pin 15]
  70. // - 1 1KRO mode (typewriter compatibility mode)
  71. // - 0 NKRO mode (new pulse on each keypress - PULSEINT)
  72. // - Signal [Pin 18]
  73. // - 1 disables pulse interrupt (PULSEINT)
  74. // - 0 enables pulse interrupt (PULSEINT)
  75. #define REPEAT_PORT PORTF
  76. #define REPEAT_DDR DDRF
  77. #define REPEAT_POS 3
  78. #define MULTI_PORT PORTF
  79. #define MULTI_DDR DDRF
  80. #define MULTI_POS 4
  81. #define SIGNAL_PORT PORTF
  82. #define SIGNAL_DDR DDRF
  83. #define SIGNAL_POS 5
  84. // Manually Scanned Keys
  85. // Keys that the controller screws up, requiring a separate wire to be brought to the controller
  86. // Note: Safer to route these through a NOT gate to boost the signal strength
  87. // Values below are AFTER NOT gate
  88. // - Shift (both shift keys are on the same scan line)
  89. // - 0 Released
  90. // - 1 Pressed
  91. // - Shift Lock
  92. // - 0 Released
  93. // - 1 Pressed
  94. #define MANUAL_SCAN_KEYS 2
  95. #define SHIFT_KEY 0
  96. #define SHIFT_PORT PORTF
  97. #define SHIFT_DDR DDRF
  98. #define SHIFT_PIN PINF
  99. #define SHIFT_POS 0
  100. #define SHIFTLOCK_KEY 1
  101. #define SHIFTLOCK_PORT PORTF
  102. #define SHIFTLOCK_DDR DDRF
  103. #define SHIFTLOCK_PIN PINF
  104. #define SHIFTLOCK_POS 1
  105. // ----- Macros -----
  106. // ----- Variables -----
  107. // Buffer used to inform the macro processing module which keys have been detected as pressed
  108. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  109. volatile uint8_t KeyIndex_BufferUsed;
  110. volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
  111. volatile uint8_t KeyScan_Table[MANUAL_SCAN_KEYS]; // Used for tracking key status of manually scanned keys
  112. volatile uint8_t KeyScan_Prev [MANUAL_SCAN_KEYS]; // Keeps track of key state changes
  113. volatile uint8_t KeyScan_Count;
  114. // ----- Functions -----
  115. // Pre-declarations
  116. void processKeyValue( uint8_t keyValue );
  117. // Setup
  118. inline void Scan_setup()
  119. {
  120. // Setup the external interrupts for
  121. // - General keypresses (INT6/E6) -> rising edge (to detect key release)
  122. // - "Code" key (INT7/E7) -> falling/rising edge (to detect key press/release)
  123. // - General keypress pulse (INT3/D3) -> falling edge (to detect key press )
  124. EICRA = 0x80;
  125. EICRB = 0x70;
  126. EIMSK = 0xC8;
  127. // Setup Interrupt Pins
  128. CODEINT_PORT |= (1 << CODEINT_POS );
  129. CODEINT_DDR &= ~(1 << CODEINT_POS );
  130. PRESSINT_PORT |= (1 << PRESSINT_POS);
  131. PRESSINT_DDR &= ~(1 << PRESSINT_POS);
  132. PULSEINT_PORT |= (1 << PULSEINT_POS);
  133. PULSEINT_DDR &= ~(1 << PULSEINT_POS);
  134. // Setup LED Pins (default off)
  135. LED1_PORT |= (1 << LED1_POS);
  136. LED1_DDR |= (1 << LED1_POS);
  137. LED2_PORT |= (1 << LED2_POS);
  138. LED2_DDR |= (1 << LED2_POS);
  139. // Setup READSCAN pins to read out scancode
  140. READSCAN_PORT = 0xFF;
  141. READSCAN_DDR = 0x00;
  142. // Setup Mode Control Pins
  143. // Note: These can be changed at any time, but there is no real reason too for a USB converter
  144. REPEAT_PORT |= (1 << REPEAT_POS); // Setting high for single press mode
  145. REPEAT_DDR |= (1 << REPEAT_POS);
  146. MULTI_PORT &= ~(1 << MULTI_POS ); // Setting low for multi press mode (NKRO)
  147. MULTI_DDR |= (1 << MULTI_POS );
  148. SIGNAL_PORT &= ~(1 << SIGNAL_POS); // Setting low to enable PULSEINT
  149. SIGNAL_DDR |= (1 << SIGNAL_POS);
  150. // Setup Troublesome Key Pins
  151. SHIFT_PORT &= ~(1 << SHIFT_POS );
  152. SHIFT_DDR &= ~(1 << SHIFT_POS );
  153. SHIFTLOCK_PORT &= ~(1 << SHIFTLOCK_POS);
  154. SHIFTLOCK_DDR &= ~(1 << SHIFTLOCK_POS);
  155. // Reset the keyboard before scanning, we might be in a wierd state
  156. scan_resetKeyboard();
  157. }
  158. // Main Detection Loop
  159. // Not needed for the Sony OA-S3400 as signals are interrupt based, thus this is a busy loop
  160. // XXX Function is used for scanning troublesome keys, technically this is not needed for a pure converter
  161. // I just want proper use of the shift and shift lock keys, without having to do major rework to attach to the entire matrix
  162. inline uint8_t Scan_loop()
  163. {
  164. // Loop through known keys
  165. for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ ) switch ( key )
  166. {
  167. case SHIFT_KEY:
  168. if ( SHIFT_PIN & (1 << SHIFT_POS) )
  169. {
  170. KeyScan_Table[SHIFT_KEY]++;
  171. }
  172. break;
  173. case SHIFTLOCK_KEY:
  174. if ( SHIFTLOCK_PIN & (1 << SHIFTLOCK_POS) )
  175. {
  176. KeyScan_Table[SHIFTLOCK_KEY]++;
  177. }
  178. break;
  179. default:
  180. erro_print("Invalid key scan index");
  181. break;
  182. }
  183. // Increment vote instance
  184. KeyScan_Count++;
  185. // Loop function again if not enough votes have been tallied
  186. if ( KeyScan_Count < 255 )
  187. return 1;
  188. // Clear vote data
  189. KeyScan_Count = 0;
  190. // Loop through known keys
  191. for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ )
  192. {
  193. // Key scanned as pressed (might have been held from a previous vote)
  194. if ( KeyScan_Table[key] > 127 )
  195. {
  196. // Keypress detected
  197. if ( !KeyScan_Prev[key] )
  198. {
  199. processKeyValue( 0x90 + key ); // Arbitrary key mapping starts at 0x90
  200. KeyScan_Prev[key] = 1;
  201. }
  202. }
  203. // Key scanned as released
  204. else
  205. {
  206. // Keypress detected
  207. if ( KeyScan_Prev[key] )
  208. {
  209. processKeyValue( 0xA0 + key ); // Arbitrary key mapping release starts at 0xA0
  210. KeyScan_Prev[key] = 0;
  211. }
  212. }
  213. // Clear votes
  214. KeyScan_Table[key] = 0;
  215. }
  216. // End loop, process macros and USB data
  217. return 0;
  218. }
  219. void processKeyValue( uint8_t keyValue )
  220. {
  221. // - Convert Shifted Value to non-shifted ASCII code -
  222. // Alphabetic
  223. if ( keyValue >= 0x61 && keyValue <= 0x7A )
  224. {
  225. keyValue -= 0x20;
  226. }
  227. // Other keys with ASCII shift codes
  228. else
  229. {
  230. switch ( keyValue )
  231. {
  232. case 0x21: // 1
  233. case 0x23: // 3
  234. case 0x24: // 4
  235. case 0x25: // 5
  236. keyValue += 0x10;
  237. break;
  238. case 0x26: // 7
  239. case 0x28: // 9
  240. keyValue += 0x11;
  241. break;
  242. case 0x81: // 1/2
  243. case 0x3A: // ;
  244. keyValue += 0x01;
  245. break;
  246. case 0x29: // 0
  247. keyValue = 0x30;
  248. break;
  249. case 0x40: // 2
  250. keyValue = 0x32;
  251. break;
  252. case 0x80: // 6
  253. keyValue = 0x36;
  254. break;
  255. case 0x2A: // 8
  256. keyValue = 0x38;
  257. break;
  258. case 0x5F: // -
  259. keyValue = 0x2D;
  260. break;
  261. case 0x2B: // +
  262. keyValue = 0x3D;
  263. break;
  264. case 0x22: // "
  265. keyValue = 0x27;
  266. break;
  267. case 0x3F: // ?
  268. keyValue = 0x2F;
  269. break;
  270. }
  271. }
  272. // TODO Move to Macro Section
  273. switch ( keyValue )
  274. {
  275. case 0xD3: // F11
  276. scan_sendData( 0x01 );
  277. break;
  278. case 0xD4: // F12
  279. scan_sendData( 0x02 );
  280. break;
  281. }
  282. // Scan code is now finalized, and ready to add to buffer
  283. // Note: Scan codes come from 3 different interrupts and a manual key scan into this function
  284. // Debug
  285. char tmpStr[6];
  286. hexToStr( keyValue, tmpStr );
  287. dPrintStrs( tmpStr, " " );
  288. // Detect release condition
  289. uint8_t release = 0;
  290. switch ( keyValue )
  291. {
  292. case 0xA0:
  293. case 0xA1:
  294. case 0xA2:
  295. keyValue -= 0x10;
  296. case 0xB0:
  297. release = 1;
  298. break;
  299. }
  300. // Key Release
  301. if ( release )
  302. {
  303. // Check for the released key, and shift the other keys lower on the buffer
  304. uint8_t c;
  305. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  306. {
  307. // General key buffer clear
  308. if ( keyValue == 0xB0 )
  309. {
  310. switch ( KeyIndex_Buffer[c] )
  311. {
  312. // Ignore these keys on general key release (have their own release codes)
  313. case 0x90:
  314. case 0x91:
  315. case 0x92:
  316. break;
  317. // Remove key from buffer
  318. default:
  319. // Shift keys from c position
  320. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  321. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  322. // Decrement Buffer
  323. KeyIndex_BufferUsed--;
  324. // Start at this position again for the next loop
  325. c--;
  326. break;
  327. }
  328. }
  329. // Key to release found
  330. else if ( KeyIndex_Buffer[c] == keyValue )
  331. {
  332. // Shift keys from c position
  333. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  334. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  335. // Decrement Buffer
  336. KeyIndex_BufferUsed--;
  337. break;
  338. }
  339. }
  340. // Error case (no key to release)
  341. if ( c == KeyIndex_BufferUsed + 1 )
  342. {
  343. errorLED( 1 );
  344. char tmpStr[6];
  345. hexToStr( keyValue, tmpStr );
  346. erro_dPrint( "Could not find key to release: ", tmpStr );
  347. }
  348. }
  349. // Press or Repeated Key
  350. else
  351. {
  352. // Make sure the key isn't already in the buffer
  353. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  354. {
  355. // Key isn't in the buffer yet
  356. if ( c == KeyIndex_BufferUsed )
  357. {
  358. Macro_bufferAdd( keyValue );
  359. break;
  360. }
  361. // Key already in the buffer
  362. if ( KeyIndex_Buffer[c] == keyValue )
  363. break;
  364. }
  365. }
  366. }
  367. // Key Press Detected Interrupt
  368. ISR(INT3_vect)
  369. {
  370. cli(); // Disable Interrupts
  371. uint8_t keyValue = 0x00;
  372. // Bits are flipped coming in from the keyboard
  373. keyValue = ~READSCAN_PIN;
  374. // Process the scancode
  375. processKeyValue( keyValue );
  376. sei(); // Re-enable Interrupts
  377. }
  378. // Key Release Detected Interrupt
  379. ISR(INT6_vect)
  380. {
  381. cli(); // Disable Interrupts
  382. // Send release code for general keys, 0xB0
  383. processKeyValue( 0xB0 );
  384. sei(); // Re-enable Interrupts
  385. }
  386. // Code Key Interrupt
  387. ISR(INT7_vect)
  388. {
  389. cli(); // Disable Interrupts
  390. // Code Key Released (send scancode)
  391. if ( CODEINT_PIN & (1 << CODEINT_POS) )
  392. {
  393. processKeyValue( 0xA2 );
  394. }
  395. // Code Key Pressed (send scancode)
  396. else
  397. {
  398. processKeyValue( 0x92 );
  399. }
  400. sei(); // Re-enable Interrupts
  401. }
  402. // Send data to keyboard
  403. // Sony OA-S3400 has no serial/parallel dataport to send data too...
  404. // Using this function for LED enable/disable
  405. uint8_t Scan_sendData( uint8_t dataPayload )
  406. {
  407. switch ( dataPayload )
  408. {
  409. case 0x01:
  410. LED1_PORT ^= (1 << LED1_POS);
  411. break;
  412. case 0x02:
  413. LED2_PORT ^= (1 << LED2_POS);
  414. break;
  415. default:
  416. erro_print("Invalid data send attempt");
  417. break;
  418. }
  419. return 0;
  420. }
  421. // Signal KeyIndex_Buffer that it has been properly read
  422. // Not needed as a signal is sent to remove key-presses
  423. void Scan_finishedWithBuffer( uint8_t sentKeys )
  424. {
  425. return;
  426. }
  427. // Reset/Hold keyboard
  428. // Sony OA-S3400 has no locking signals
  429. void Scan_lockKeyboard( void )
  430. {
  431. }
  432. void Scan_unlockKeyboard( void )
  433. {
  434. }
  435. // Reset Keyboard
  436. void Scan_resetKeyboard( void )
  437. {
  438. // Empty buffer, now that keyboard has been reset
  439. KeyIndex_BufferUsed = 0;
  440. // Clear the KeyScan table and count
  441. for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ )
  442. {
  443. KeyScan_Table[key] = 0;
  444. KeyScan_Prev [key] = 0;
  445. }
  446. KeyScan_Count = 0;
  447. }
  448. // USB module is finished with buffer
  449. // Not needed as a signal is sent to remove key-presses
  450. void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
  451. {
  452. return;
  453. }