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

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