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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* Copyright (C) 2012 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. // Make sure we haven't overflowed the buffer
  107. #define bufferAdd(byte) \
  108. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  109. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  110. // ----- Variables -----
  111. // Buffer used to inform the macro processing module which keys have been detected as pressed
  112. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  113. volatile uint8_t KeyIndex_BufferUsed;
  114. volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
  115. volatile uint8_t KeyScan_Table[MANUAL_SCAN_KEYS]; // Used for tracking key status of manually scanned keys
  116. volatile uint8_t KeyScan_Prev [MANUAL_SCAN_KEYS]; // Keeps track of key state changes
  117. volatile uint8_t KeyScan_Count;
  118. // ----- Functions -----
  119. // Pre-declarations
  120. void processKeyValue( uint8_t keyValue );
  121. // Setup
  122. inline void scan_setup()
  123. {
  124. // Setup the external interrupts for
  125. // - General keypresses (INT6/E6) -> rising edge (to detect key release)
  126. // - "Code" key (INT7/E7) -> falling/rising edge (to detect key press/release)
  127. // - General keypress pulse (INT3/D3) -> falling edge (to detect key press )
  128. EICRA = 0x80;
  129. EICRB = 0x70;
  130. EIMSK = 0xC8;
  131. // Setup Interrupt Pins
  132. CODEINT_PORT |= (1 << CODEINT_POS );
  133. CODEINT_DDR &= ~(1 << CODEINT_POS );
  134. PRESSINT_PORT |= (1 << PRESSINT_POS);
  135. PRESSINT_DDR &= ~(1 << PRESSINT_POS);
  136. PULSEINT_PORT |= (1 << PULSEINT_POS);
  137. PULSEINT_DDR &= ~(1 << PULSEINT_POS);
  138. // Setup LED Pins (default off)
  139. LED1_PORT |= (1 << LED1_POS);
  140. LED1_DDR |= (1 << LED1_POS);
  141. LED2_PORT |= (1 << LED2_POS);
  142. LED2_DDR |= (1 << LED2_POS);
  143. // Setup READSCAN pins to read out scancode
  144. READSCAN_PORT = 0xFF;
  145. READSCAN_DDR = 0x00;
  146. // Setup Mode Control Pins
  147. // Note: These can be changed at any time, but there is no real reason too for a USB converter
  148. REPEAT_PORT |= (1 << REPEAT_POS); // Setting high for single press mode
  149. REPEAT_DDR |= (1 << REPEAT_POS);
  150. MULTI_PORT &= ~(1 << MULTI_POS ); // Setting low for multi press mode (NKRO)
  151. MULTI_DDR |= (1 << MULTI_POS );
  152. SIGNAL_PORT &= ~(1 << SIGNAL_POS); // Setting low to enable PULSEINT
  153. SIGNAL_DDR |= (1 << SIGNAL_POS);
  154. // Setup Troublesome Key Pins
  155. SHIFT_PORT &= ~(1 << SHIFT_POS );
  156. SHIFT_DDR &= ~(1 << SHIFT_POS );
  157. SHIFTLOCK_PORT &= ~(1 << SHIFTLOCK_POS);
  158. SHIFTLOCK_DDR &= ~(1 << SHIFTLOCK_POS);
  159. // Reset the keyboard before scanning, we might be in a wierd state
  160. scan_resetKeyboard();
  161. }
  162. // Main Detection Loop
  163. // Not needed for the Sony OA-S3400 as signals are interrupt based, thus this is a busy loop
  164. // XXX Function is used for scanning troublesome keys, technically this is not needed for a pure converter
  165. // I just want proper use of the shift and shift lock keys, without having to do major rework to attach to the entire matrix
  166. inline uint8_t scan_loop()
  167. {
  168. // Loop through known keys
  169. for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ ) switch ( key )
  170. {
  171. case SHIFT_KEY:
  172. if ( SHIFT_PIN & (1 << SHIFT_POS) )
  173. {
  174. KeyScan_Table[SHIFT_KEY]++;
  175. }
  176. break;
  177. case SHIFTLOCK_KEY:
  178. if ( SHIFTLOCK_PIN & (1 << SHIFTLOCK_POS) )
  179. {
  180. KeyScan_Table[SHIFTLOCK_KEY]++;
  181. }
  182. break;
  183. default:
  184. erro_print("Invalid key scan index");
  185. break;
  186. }
  187. // Increment vote instance
  188. KeyScan_Count++;
  189. // Loop function again if not enough votes have been tallied
  190. if ( KeyScan_Count < 255 )
  191. return 1;
  192. // Clear vote data
  193. KeyScan_Count = 0;
  194. // Loop through known keys
  195. for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ )
  196. {
  197. // Key scanned as pressed (might have been held from a previous vote)
  198. if ( KeyScan_Table[key] > 127 )
  199. {
  200. // Keypress detected
  201. if ( !KeyScan_Prev[key] )
  202. {
  203. processKeyValue( 0x90 + key ); // Arbitrary key mapping starts at 0x90
  204. KeyScan_Prev[key] = 1;
  205. }
  206. }
  207. // Key scanned as released
  208. else
  209. {
  210. // Keypress detected
  211. if ( KeyScan_Prev[key] )
  212. {
  213. processKeyValue( 0xA0 + key ); // Arbitrary key mapping release starts at 0xA0
  214. KeyScan_Prev[key] = 0;
  215. }
  216. }
  217. // Clear votes
  218. KeyScan_Table[key] = 0;
  219. }
  220. // End loop, process macros and USB data
  221. return 0;
  222. }
  223. void processKeyValue( uint8_t keyValue )
  224. {
  225. // - Convert Shifted Value to non-shifted ASCII code -
  226. // Alphabetic
  227. if ( keyValue >= 0x61 && keyValue <= 0x7A )
  228. {
  229. keyValue -= 0x20;
  230. }
  231. // Other keys with ASCII shift codes
  232. else
  233. {
  234. switch ( keyValue )
  235. {
  236. case 0x21: // 1
  237. case 0x23: // 3
  238. case 0x24: // 4
  239. case 0x25: // 5
  240. keyValue += 0x10;
  241. break;
  242. case 0x26: // 7
  243. case 0x28: // 9
  244. keyValue += 0x11;
  245. break;
  246. case 0x81: // 1/2
  247. case 0x3A: // ;
  248. keyValue += 0x01;
  249. break;
  250. case 0x29: // 0
  251. keyValue = 0x30;
  252. break;
  253. case 0x40: // 2
  254. keyValue = 0x32;
  255. break;
  256. case 0x80: // 6
  257. keyValue = 0x36;
  258. break;
  259. case 0x2A: // 8
  260. keyValue = 0x38;
  261. break;
  262. case 0x5F: // -
  263. keyValue = 0x2D;
  264. break;
  265. case 0x2B: // +
  266. keyValue = 0x3D;
  267. break;
  268. case 0x22: // "
  269. keyValue = 0x27;
  270. break;
  271. case 0x3F: // ?
  272. keyValue = 0x2F;
  273. break;
  274. }
  275. }
  276. // TODO Move to Macro Section
  277. switch ( keyValue )
  278. {
  279. case 0xD3: // F11
  280. scan_sendData( 0x01 );
  281. break;
  282. case 0xD4: // F12
  283. scan_sendData( 0x02 );
  284. break;
  285. }
  286. // Scan code is now finalized, and ready to add to buffer
  287. // Note: Scan codes come from 3 different interrupts and a manual key scan into this function
  288. // Debug
  289. char tmpStr[6];
  290. hexToStr( keyValue, tmpStr );
  291. dPrintStrs( tmpStr, " " );
  292. // Detect release condition
  293. uint8_t release = 0;
  294. switch ( keyValue )
  295. {
  296. case 0xA0:
  297. case 0xA1:
  298. case 0xA2:
  299. keyValue -= 0x10;
  300. case 0xB0:
  301. release = 1;
  302. break;
  303. }
  304. // Key Release
  305. if ( release )
  306. {
  307. // Check for the released key, and shift the other keys lower on the buffer
  308. uint8_t c;
  309. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  310. {
  311. // General key buffer clear
  312. if ( keyValue == 0xB0 )
  313. {
  314. switch ( KeyIndex_Buffer[c] )
  315. {
  316. // Ignore these keys on general key release (have their own release codes)
  317. case 0x90:
  318. case 0x91:
  319. case 0x92:
  320. break;
  321. // Remove key from buffer
  322. default:
  323. // Shift keys from c position
  324. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  325. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  326. // Decrement Buffer
  327. KeyIndex_BufferUsed--;
  328. // Start at this position again for the next loop
  329. c--;
  330. break;
  331. }
  332. }
  333. // Key to release found
  334. else if ( KeyIndex_Buffer[c] == keyValue )
  335. {
  336. // Shift keys from c position
  337. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  338. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  339. // Decrement Buffer
  340. KeyIndex_BufferUsed--;
  341. break;
  342. }
  343. }
  344. // Error case (no key to release)
  345. if ( c == KeyIndex_BufferUsed + 1 )
  346. {
  347. errorLED( 1 );
  348. char tmpStr[6];
  349. hexToStr( keyValue, tmpStr );
  350. erro_dPrint( "Could not find key to release: ", tmpStr );
  351. }
  352. }
  353. // Press or Repeated Key
  354. else
  355. {
  356. // Make sure the key isn't already in the buffer
  357. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  358. {
  359. // Key isn't in the buffer yet
  360. if ( c == KeyIndex_BufferUsed )
  361. {
  362. bufferAdd( keyValue );
  363. break;
  364. }
  365. // Key already in the buffer
  366. if ( KeyIndex_Buffer[c] == keyValue )
  367. break;
  368. }
  369. }
  370. }
  371. // Key Press Detected Interrupt
  372. ISR(INT3_vect)
  373. {
  374. cli(); // Disable Interrupts
  375. uint8_t keyValue = 0x00;
  376. // Bits are flipped coming in from the keyboard
  377. keyValue = ~READSCAN_PIN;
  378. // Process the scancode
  379. processKeyValue( keyValue );
  380. sei(); // Re-enable Interrupts
  381. }
  382. // Key Release Detected Interrupt
  383. ISR(INT6_vect)
  384. {
  385. cli(); // Disable Interrupts
  386. // Send release code for general keys, 0xB0
  387. processKeyValue( 0xB0 );
  388. sei(); // Re-enable Interrupts
  389. }
  390. // Code Key Interrupt
  391. ISR(INT7_vect)
  392. {
  393. cli(); // Disable Interrupts
  394. // Code Key Released (send scancode)
  395. if ( CODEINT_PIN & (1 << CODEINT_POS) )
  396. {
  397. processKeyValue( 0xA2 );
  398. }
  399. // Code Key Pressed (send scancode)
  400. else
  401. {
  402. processKeyValue( 0x92 );
  403. }
  404. sei(); // Re-enable Interrupts
  405. }
  406. // Send data to keyboard
  407. // Sony OA-S3400 has no serial/parallel dataport to send data too...
  408. // Using this function for LED enable/disable
  409. uint8_t scan_sendData( uint8_t dataPayload )
  410. {
  411. switch ( dataPayload )
  412. {
  413. case 0x01:
  414. LED1_PORT ^= (1 << LED1_POS);
  415. break;
  416. case 0x02:
  417. LED2_PORT ^= (1 << LED2_POS);
  418. break;
  419. default:
  420. erro_print("Invalid data send attempt");
  421. break;
  422. }
  423. return 0;
  424. }
  425. // Signal KeyIndex_Buffer that it has been properly read
  426. // Not needed as a signal is sent to remove key-presses
  427. void scan_finishedWithBuffer( uint8_t sentKeys )
  428. {
  429. return;
  430. }
  431. // Reset/Hold keyboard
  432. // Sony OA-S3400 has no locking signals
  433. void scan_lockKeyboard( void )
  434. {
  435. }
  436. void scan_unlockKeyboard( void )
  437. {
  438. }
  439. // Reset Keyboard
  440. void scan_resetKeyboard( void )
  441. {
  442. // Empty buffer, now that keyboard has been reset
  443. KeyIndex_BufferUsed = 0;
  444. // Clear the KeyScan table and count
  445. for ( uint8_t key = 0; key < MANUAL_SCAN_KEYS; key++ )
  446. {
  447. KeyScan_Table[key] = 0;
  448. KeyScan_Prev [key] = 0;
  449. }
  450. KeyScan_Count = 0;
  451. }
  452. // USB module is finished with buffer
  453. // Not needed as a signal is sent to remove key-presses
  454. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  455. {
  456. return;
  457. }