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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* Copyright (C) 2011 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. #define CLOCK_PORT PORTB
  32. #define CLOCK_DDR DDRB
  33. #define CLOCK_PIN 0
  34. // ----- Macros -----
  35. // Make sure we haven't overflowed the buffer
  36. #define bufferAdd(byte) \
  37. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  38. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  39. #define setLED(id, status) \
  40. status = status ? 0 : 1; \
  41. scan_setLED( id, status )
  42. // ----- Variables -----
  43. // Buffer used to inform the macro processing module which keys have been detected as pressed
  44. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  45. volatile uint8_t KeyIndex_BufferUsed;
  46. volatile uint8_t currentWaveState = 0;
  47. volatile uint8_t calcLED = 0;
  48. volatile uint8_t insertLED = 0;
  49. volatile uint8_t shiftLockLED = 0;
  50. volatile uint8_t schedLED = 0;
  51. volatile uint8_t drawLED = 0;
  52. // ----- Function Declarations -----
  53. void scan_diagnostics( void );
  54. void processKeyValue( uint8_t keyValue );
  55. void scan_diagnostics( void );
  56. void scan_setRepeatStart( uint8_t n );
  57. void scan_readSwitchStatus( void );
  58. void scan_repeatControl( uint8_t on );
  59. void scan_enableKeyboard( uint8_t enable );
  60. void scan_setRepeatRate( uint8_t n );
  61. void scan_setLED( uint8_t ledNumber, uint8_t on );
  62. void scan_readLED( void );
  63. // ----- Interrupt Functions -----
  64. // Generates a constant external clock
  65. ISR( TIMER1_COMPA_vect )
  66. {
  67. if ( currentWaveState )
  68. {
  69. CLOCK_PORT &= ~(1 << CLOCK_PIN);
  70. currentWaveState--;
  71. }
  72. else
  73. {
  74. CLOCK_PORT |= (1 << CLOCK_PIN);
  75. currentWaveState++;
  76. }
  77. }
  78. // USART Receive Buffer Full Interrupt
  79. ISR(USART1_RX_vect)
  80. {
  81. cli(); // Disable Interrupts
  82. uint8_t keyValue = 0x00;
  83. // Read the raw packet from the USART
  84. keyValue = UDR1;
  85. // Debug
  86. char tmpStr[6];
  87. hexToStr( keyValue, tmpStr );
  88. dPrintStrs( tmpStr, " " );
  89. // Process the scancode
  90. if ( keyValue != 0x00 )
  91. processKeyValue( keyValue );
  92. sei(); // Re-enable Interrupts
  93. }
  94. // ----- Functions -----
  95. // Setup
  96. inline void scan_setup()
  97. {
  98. // Setup Timer Pulse (16 bit)
  99. // 16 MHz / (2 * Prescaler * (1 + OCR1A)) = 1204.8 baud (820 us)
  100. // Prescaler is 1
  101. /*
  102. TCCR1B = 0x09;
  103. OCR1AH = 0x19;
  104. OCR1AL = 0xEF;
  105. TIMSK1 = (1 << OCIE1A);
  106. CLOCK_DDR = (1 << CLOCK_PIN);
  107. */
  108. // 16 MHz / (2 * Prescaler * (1 + OCR1A)) = 1200.1 baud
  109. // Prescaler is 1
  110. // Twice every 1200 baud (actually 1200.1, timer isn't accurate enough)
  111. // This is close to 820 us, but a bit slower
  112. cli();
  113. TCCR1B = 0x09;
  114. OCR1AH = 0x1A;
  115. OCR1AL = 0x09;
  116. TIMSK1 = (1 << OCIE1A);
  117. CLOCK_DDR = (1 << CLOCK_PIN);
  118. // Setup the the USART interface for keyboard data input
  119. // Setup baud rate
  120. // 16 MHz / ( 16 * Baud ) = UBRR
  121. // Baud <- 1200 as per the spec (see datasheet archives), rounding to 1200.1 (as that's as accurate as the timer can be)
  122. // Thus UBRR = 833.26 -> round to 833
  123. uint16_t baud = 833; // Max setting of 4095
  124. UBRR1H = (uint8_t)(baud >> 8);
  125. UBRR1L = (uint8_t)baud;
  126. // Enable the receiver, transitter, and RX Complete Interrupt
  127. UCSR1B = 0x98;
  128. // Set frame format: 8 data, no stop bits or parity
  129. // Synchrounous USART mode
  130. // Tx Data on Falling Edge, Rx on Rising
  131. UCSR1C = 0x47;
  132. sei();
  133. // Reset the keyboard before scanning, we might be in a wierd state
  134. _delay_ms( 50 );
  135. scan_resetKeyboard();
  136. _delay_ms( 5000 ); // Wait for the reset command to finish enough for new settings to take hold afterwards
  137. scan_setRepeatRate( 0x00 ); // Set the fastest repeat rate
  138. }
  139. // Main Detection Loop
  140. // Nothing is required here with the Epson QX-10 Keyboards as the interrupts take care of the inputs
  141. inline uint8_t scan_loop()
  142. {
  143. return 0;
  144. }
  145. // TODO
  146. void processKeyValue( uint8_t keyValue )
  147. {
  148. // Detect LED Status
  149. uint8_t inputType = keyValue & 0xC0;
  150. // Determine the input type
  151. switch ( inputType )
  152. {
  153. // LED Status
  154. case 0xC0:
  155. // Binary Representation: 1100 llln
  156. // Hex Range: 0xC0 to 0xCF
  157. // - First 3 bits determine which LED (0 to 7)
  158. // - Last bit is whether the LED is On (1) or Off (0)
  159. // 000 - N/A (A)
  160. // 001 - N/A (B)
  161. // 010 - INSERT
  162. // 011 - SHIFT LOCK
  163. // 100 - N/A (C)
  164. // 101 - DRAW
  165. // 110 - SCHED
  166. // 111 - CALC
  167. break;
  168. // SW (Switch) Status
  169. case 0x80:
  170. {
  171. // Binary Representation: 1000 dddn
  172. // Hex Range: 0x80 to 0x8F
  173. // - First 3 bits determine which DB (KRTN) (See datasheet)
  174. // - Last bit is whether the key is enabled
  175. // 000 - N/A?
  176. // 001 - N/A?
  177. // 010 - Right SHIFT
  178. // 011 - Left SHIFT
  179. // 100 - N/A?
  180. // 101 - Left CTRL
  181. // 110 - GRPH SHIFT
  182. // 111 - Right CTRL
  183. // Detect Modifier Press/Release
  184. uint8_t press = keyValue & 0x01;
  185. // Modifier Press Detected
  186. if ( press )
  187. {
  188. // Make sure the key isn't already in the buffer
  189. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  190. {
  191. // Key isn't in the buffer yet
  192. if ( c == KeyIndex_BufferUsed )
  193. {
  194. bufferAdd( keyValue );
  195. break;
  196. }
  197. // Key already in the buffer
  198. if ( KeyIndex_Buffer[c] == keyValue )
  199. break;
  200. }
  201. }
  202. // Modifier Release Detected
  203. else
  204. {
  205. uint8_t actualKeyValue = keyValue | 0x01;
  206. // Check for the released key, and shift the other keys lower on the buffer
  207. uint8_t c;
  208. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  209. {
  210. // Key to release found
  211. if ( KeyIndex_Buffer[c] == actualKeyValue )
  212. {
  213. // Shift keys from c position
  214. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  215. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  216. // Decrement Buffer
  217. KeyIndex_BufferUsed--;
  218. break;
  219. }
  220. }
  221. // Error case (no key to release)
  222. if ( c == KeyIndex_BufferUsed + 1 )
  223. {
  224. errorLED( 1 );
  225. char tmpStr[6];
  226. hexToStr( keyValue, tmpStr );
  227. erro_dPrint( "Could not find key to release: ", tmpStr );
  228. }
  229. }
  230. break;
  231. }
  232. // Key code
  233. default:
  234. // Binary Representation: 0ddd pppp
  235. // Hex Range: 0x00 to 0x7F
  236. // - First 3 bits determine which DB (KRTN) (See datasheet)
  237. // - Last 4 bits corresond to the KSC signals (P13, P12, P11, P10 respectively)
  238. // Or, that can be read as, each key has it's own keycode (with NO release code)
  239. // Modifiers are treated differently
  240. // Add the key to the buffer, if it isn't already in the current Key Buffer
  241. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  242. {
  243. // Key isn't in the buffer yet
  244. if ( c == KeyIndex_BufferUsed )
  245. {
  246. bufferAdd( keyValue );
  247. break;
  248. }
  249. // Key already in the buffer
  250. if ( KeyIndex_Buffer[c] == keyValue )
  251. break;
  252. }
  253. // Special Internal Key Mapping/Functions
  254. switch ( keyValue )
  255. {
  256. // LED Test
  257. case 0x0A: // CALC
  258. setLED( 0x07, calcLED ); // 0x4F
  259. break;
  260. case 0x0B: // SCHED
  261. setLED( 0x0E, schedLED ); // 0x5D
  262. break;
  263. case 0x0C: // DRAW
  264. setLED( 0x0D, drawLED ); // 0x5B
  265. break;
  266. case 0x42: // SHIFT LOCK
  267. setLED( 0x0B, shiftLockLED ); // 0x57
  268. break;
  269. case 0x5E: // INSERT
  270. setLED( 0x02, insertLED ); // 0x45
  271. break;
  272. /*
  273. // TEST
  274. case 0x51:
  275. scan_resetKeyboard();
  276. break;
  277. case 0x52:
  278. scan_diagnostics();
  279. break;
  280. case 0x53:
  281. scan_setRepeatStart( 0x00 );
  282. break;
  283. case 0x54:
  284. scan_readSwitchStatus();
  285. break;
  286. case 0x55:
  287. scan_repeatControl( 0x00 );
  288. break;
  289. case 0x56:
  290. scan_repeatControl( 0x01 );
  291. break;
  292. case 0x57:
  293. scan_enableKeyboard( 0x00 );
  294. break;
  295. case 0x58:
  296. scan_enableKeyboard( 0x01 );
  297. break;
  298. case 0x59:
  299. scan_setRepeatRate( 0x00 );
  300. break;
  301. case 0x5A:
  302. scan_readLED();
  303. break;
  304. */
  305. }
  306. break;
  307. }
  308. }
  309. // Send data
  310. // See below functions for the input sequences for the Epson QX-10 Keyboard
  311. uint8_t scan_sendData( uint8_t dataPayload )
  312. {
  313. // Debug
  314. char tmpStr[6];
  315. hexToStr( dataPayload, tmpStr );
  316. info_dPrint( tmpStr, " " );
  317. UDR1 = dataPayload;
  318. return 0;
  319. }
  320. // Signal KeyIndex_Buffer that it has been properly read
  321. inline void scan_finishedWithBuffer( uint8_t sentKeys )
  322. {
  323. return;
  324. }
  325. // Signal that the keys have been properly sent over USB
  326. // For the Epson QX-10 only the modifier keys have release signals
  327. // Therefore, only 5 keys could possibly be assigned as a modifiers
  328. // The rest of the keys are single press (like the Kaypro keyboards)
  329. //
  330. // However, this differentiation causes complications on how the key signals are discarded and used
  331. // The single keypresses must be discarded immediately, while the modifiers must be kept
  332. inline void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  333. {
  334. uint8_t foundModifiers = 0;
  335. // Look for all of the modifiers present, there is a max of 8 (but only keys for 5 on the HASCI version)
  336. for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
  337. {
  338. // The modifier range is from 0x80 to 0x8F (well, the last bit is the ON/OFF signal, but whatever...)
  339. if ( KeyIndex_Buffer[c] <= 0x8F && KeyIndex_Buffer[c] >= 0x80 )
  340. {
  341. // Add the modifier back into the the Key Buffer
  342. KeyIndex_Buffer[foundModifiers] = KeyIndex_Buffer[c];
  343. foundModifiers++;
  344. }
  345. }
  346. // Adjust the size of the new Key Buffer
  347. KeyIndex_BufferUsed = foundModifiers;
  348. /* Non-working, too slow (too much traffic on the bus)
  349. // Poll the modifiers using an input command
  350. uint8_t oldBuffer = KeyIndex_BufferUsed;
  351. KeyIndex_BufferUsed = 0;
  352. if ( oldBuffer )
  353. scan_readSwitchStatus();
  354. */
  355. }
  356. // Reset/Hold keyboard
  357. // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
  358. // The Epson QX-10 Keyboards have a command used to lock the keyboard output
  359. void scan_lockKeyboard( void )
  360. {
  361. scan_enableKeyboard( 0x00 );
  362. }
  363. void scan_unlockKeyboard( void )
  364. {
  365. scan_enableKeyboard( 0x01 );
  366. }
  367. // Reset Keyboard
  368. // Does the following
  369. // - Clears the keycode buffer (32 characters)
  370. // - Validates repeat function (what does this do?)
  371. // - Sets repeat start time (500 ms)
  372. // - Sets repeat interval (50 ms)
  373. // - Turns off all LEDs
  374. void scan_resetKeyboard( void )
  375. {
  376. // Reset command for the QX-10 Keyboard
  377. scan_sendData( 0xE0 );
  378. // Empty buffer, now that keyboard has been reset
  379. KeyIndex_BufferUsed = 0;
  380. }
  381. // TODO Check
  382. // Runs Diagnostics on the keyboard
  383. // - First does a reset (see scan_resetKeyboard)
  384. // - Blinks all of the LEDs one after another
  385. // - Outputs 0x00 if no keys are pressed
  386. // - Outputs 0xFF if any keys are being pressed
  387. void scan_diagnostics( void )
  388. {
  389. // Send reset command with diagnositics
  390. scan_sendData( 0xE7 );
  391. }
  392. // TODO Check
  393. // Set Repeat Interval Start
  394. // 300 ms + n * 25 ms
  395. // Interval after which to start the repeated keys
  396. void scan_setRepeatStart( uint8_t n )
  397. {
  398. // Send command
  399. // Binary Representation: 000n nnnn
  400. // Hex boundaries 0x00 to 0x1F
  401. // 300 ms to 1075 ms (intervals of 25 ms)
  402. scan_sendData( n );
  403. }
  404. // Read Switch Status (preferential to actual keypress outputs)
  405. // 000 - N/A?
  406. // 001 - N/A?
  407. // 010 - Right SHIFT
  408. // 011 - Left SHIFT
  409. // 100 - N/A?
  410. // 101 - Left CTRL
  411. // 110 - GRPH SHIFT
  412. // 111 - Right CTRL
  413. void scan_readSwitchStatus( void )
  414. {
  415. scan_sendData( 0x80 );
  416. }
  417. // TODO Check
  418. // Repeat Control
  419. // 0x00 Stops repeat function
  420. // 0x01 Enables repeat function
  421. void scan_repeatControl( uint8_t on )
  422. {
  423. // Send command
  424. // Binary Representation: 101X XXXn
  425. // Hex options: 0xA0 or 0xA1
  426. scan_sendData( 0xA0 | on );
  427. }
  428. // TODO Check
  429. // Enable Sending Keyboard Data
  430. // 0x00 Stops keycode transmission
  431. // 0x01 Enables keycode transmission
  432. void scan_enableKeyboard( uint8_t enable )
  433. {
  434. // Send command
  435. // Binary Representation: 110X XXXn
  436. // Hex options: 0xC0 or 0xC1
  437. scan_sendData( 0xC0 | enable );
  438. }
  439. // Set Repeat Interval
  440. // 30 ms + n * 5 ms
  441. // Period between sending each repeated key after the initial interval
  442. void scan_setRepeatRate( uint8_t n )
  443. {
  444. // Send command
  445. // Binary Representation: 001n nnnn
  446. // Hex options: 0x00 to 0x1F
  447. // 30 ms to 185 ms (intervals of 5 ms)
  448. scan_sendData( 0x20 | n );
  449. }
  450. // Turn On/Off LED
  451. // 0x00 LED Off
  452. // 0x01 LED On
  453. //
  454. // 8 LEDs max (Note: 5 connected on my board, there is 1 position empty on the PCB for a total of 6)
  455. // 0 to 7 (0x0 to 0x7)
  456. void scan_setLED( uint8_t ledNumber, uint8_t on )
  457. {
  458. // Send command
  459. // Binary Representation: 010l llln
  460. // Hex options: 0x40 to 0x4F
  461. // The spec is NOT accurate (especially about the "don't care" bit)
  462. // llll n - Usage
  463. // 0000 X - N/A (1)
  464. // 0001 X - N/A (2)
  465. // 0010 1 - INSERT On
  466. // 0011 0 - SHIFT LOCK Off
  467. // 0100 X - N/A (3)
  468. // 0101 0 - DRAW Off
  469. // 0110 0 - SCHED Off
  470. // 0111 1 - CALC On
  471. // 1000 X - N/A (1)
  472. // 1001 X - N/A (2)
  473. // 1010 0 - INSERT Off
  474. // 1011 1 - SHIFT LOCK On
  475. // 1100 X - N/A (3)
  476. // 1101 1 - DRAW On
  477. // 1110 1 - SCHED On
  478. // 1111 0 - CALC Off
  479. uint8_t off = 0;
  480. if ( !on )
  481. {
  482. off = 0x10;
  483. }
  484. scan_sendData( ( 0x40 | (ledNumber << 1) | on ) ^ off );
  485. }
  486. // Read LED Status
  487. // High priority data output (may overwrite some keycode data)
  488. void scan_readLED( void )
  489. {
  490. scan_sendData( 0x7F );
  491. }