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

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