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

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