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.9KB

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