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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 RESET_PORT PORTB
  32. #define RESET_DDR DDRD
  33. #define RESET_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 UNSET_RESET() RESET_DDR &= ~(1 << RESET_PIN)
  40. #define SET_RESET() RESET_DDR |= (1 << RESET_PIN)
  41. // ----- Variables -----
  42. // Buffer used to inform the macro processing module which keys have been detected as pressed
  43. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  44. volatile uint8_t KeyIndex_BufferUsed;
  45. // Buffer Signals
  46. volatile uint8_t BufferReadyToClear;
  47. // ----- Functions -----
  48. // Setup
  49. inline void scan_setup()
  50. {
  51. // Setup the the USART interface for keyboard data input
  52. // NOTE: The input data signal needs to be inverted for the Teensy USART to properly work
  53. // Setup baud rate
  54. // 16 MHz / ( 16 * Baud ) = UBRR
  55. // Baud <- 0.82020 ms per bit, thus 1000 / 0.82020 = 1219.2
  56. // Thus baud setting = 820
  57. uint16_t baud = 820; // Max setting of 4095
  58. UBRR1H = (uint8_t)(baud >> 8);
  59. UBRR1L = (uint8_t)baud;
  60. // Enable the receiver, transitter, and RX Complete Interrupt
  61. UCSR1B = 0x98;
  62. // Set frame format: 8 data, no stop bits or parity
  63. // Asynchrounous USART mode
  64. // 8304 sends encoded scancodes (for example Alphanumeric 0-9 follows their keypad encoding scheme)
  65. // Separate line is for reset
  66. UCSR1C = 0x06;
  67. // Initially buffer doesn't need to be cleared (it's empty...)
  68. BufferReadyToClear = 0;
  69. // Reset the keyboard before scanning, we might be in a wierd state
  70. // Note: This should be run asap, but we need the USART setup to run this command on the 8304
  71. scan_resetKeyboard();
  72. }
  73. // Main Detection Loop
  74. // Not needed for the Micro Switch 8304, this is just a busy loop
  75. inline uint8_t scan_loop()
  76. {
  77. return 0;
  78. }
  79. void processKeyValue( uint8_t keyValue )
  80. {
  81. // Finalize output buffer
  82. // Mask 8th bit
  83. keyValue &= 0x7F;
  84. // Interpret scan code
  85. switch ( keyValue )
  86. {
  87. case 0x40: // Clear buffer command
  88. info_print("CLEAR!");
  89. BufferReadyToClear = 1;
  90. break;
  91. case 0x7F:
  92. scan_lockKeyboard();
  93. _delay_ms(3000);
  94. scan_unlockKeyboard();
  95. default:
  96. // Make sure the key isn't already in the buffer
  97. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  98. {
  99. // Key isn't in the buffer yet
  100. if ( c == KeyIndex_BufferUsed )
  101. {
  102. bufferAdd( keyValue );
  103. break;
  104. }
  105. // Key already in the buffer
  106. if ( KeyIndex_Buffer[c] == keyValue )
  107. break;
  108. }
  109. break;
  110. }
  111. }
  112. // USART Receive Buffer Full Interrupt
  113. ISR(USART1_RX_vect)
  114. {
  115. cli(); // Disable Interrupts
  116. uint8_t keyValue = 0x00;
  117. // The interrupt is always for the first item of the packet set, reset the buffer
  118. KeyIndex_BufferUsed = 0;
  119. // Only the first 7 bits have scancode data
  120. // The last packet of the packet set has the 8th bit high, all the others are low
  121. //
  122. // Interrupts are too slow for the rest of the packet set, poll for the rest
  123. while ( 1 )
  124. {
  125. // Read the raw packet from the USART
  126. keyValue = UDR1;
  127. // Debug
  128. char tmpStr[6];
  129. hexToStr( keyValue, tmpStr );
  130. dPrintStrs( tmpStr, " " );
  131. // Process the scancode
  132. processKeyValue( keyValue );
  133. // Last packet of the set
  134. if ( keyValue & 0x80 )
  135. {
  136. dPrintStrs( "**" );
  137. break;
  138. }
  139. // Delay enough so we don't run into the same packet (or the previous buffered packet)
  140. _delay_us(10000);
  141. }
  142. sei(); // Re-enable Interrupts
  143. }
  144. // Send data
  145. //
  146. // Keyboard Input Guide for Micro Switch 8304
  147. // 0xBX is for LED F1,F2,Over Type,Lock
  148. // 0xAX is for LED F3,F8,F9,F10
  149. // 0x92 resets keyboard (LED off, echo scancode mode off)
  150. // 0x9E sets echo scancode mode from (0x81 to 0xFF; translates to 0x01 to 0x7F)
  151. // Other echos: 0x15~0x19 send 0x15~0x19, 0x40 sends 0x40 (as well as 0x44,0x45, 0x80)
  152. // 0x8C Acks the keyboard and gets 0x70 sent back (delayed)
  153. uint8_t scan_sendData( uint8_t dataPayload )
  154. {
  155. UDR1 = dataPayload;
  156. return 0;
  157. }
  158. // Signal KeyIndex_Buffer that it has been properly read
  159. // In the case of the Micro Switch 8304, we leave the buffer alone until more scancode data comes in
  160. void scan_finishedWithBuffer( uint8_t sentKeys )
  161. {
  162. // We received a Clear code from the 8304, clear the buffer now that we've used it
  163. if ( BufferReadyToClear )
  164. {
  165. KeyIndex_BufferUsed = 0;
  166. BufferReadyToClear = 0;
  167. }
  168. }
  169. // Signal that the keys have been properly sent over USB
  170. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  171. {
  172. }
  173. // Reset/Hold keyboard
  174. // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
  175. // The Micro Switch 8304 has a dedicated reset line
  176. void scan_lockKeyboard( void )
  177. {
  178. UNSET_RESET();
  179. }
  180. void scan_unlockKeyboard( void )
  181. {
  182. SET_RESET();
  183. }
  184. // Reset Keyboard
  185. void scan_resetKeyboard( void )
  186. {
  187. // Reset command for the 8304
  188. scan_sendData( 0x92 );
  189. }