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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright (C) 2012 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. // ----- 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. // ----- Function Declarations -----
  48. void processKeyValue( uint8_t keyValue );
  49. // ----- Interrupt Functions -----
  50. // USART Receive Buffer Full Interrupt
  51. ISR(USART1_RX_vect)
  52. {
  53. cli(); // Disable Interrupts
  54. uint8_t keyValue = 0x00;
  55. // The interrupt is always for the first item of the packet set, reset the buffer
  56. KeyIndex_BufferUsed = 0;
  57. // Only the first 7 bits have scancode data
  58. // The last packet of the packet set has the 8th bit high, all the others are low
  59. //
  60. // Interrupts are too slow for the rest of the packet set, poll for the rest
  61. while ( 1 )
  62. {
  63. // Read the raw packet from the USART
  64. keyValue = UDR1;
  65. // Debug
  66. char tmpStr[6];
  67. hexToStr( keyValue, tmpStr );
  68. dPrintStrs( tmpStr, " " );
  69. // Process the scancode
  70. processKeyValue( keyValue );
  71. // Last packet of the set
  72. if ( keyValue & 0x80 )
  73. {
  74. dPrintStrs( "**" );
  75. break;
  76. }
  77. // Delay enough so we don't run into the same packet (or the previous buffered packet)
  78. _delay_us(10000);
  79. }
  80. sei(); // Re-enable Interrupts
  81. }
  82. // ----- Functions -----
  83. // Setup
  84. inline void scan_setup()
  85. {
  86. // Setup the the USART interface for keyboard data input
  87. // NOTE: The input data signal needs to be inverted for the Teensy USART to properly work
  88. // Setup baud rate
  89. // 16 MHz / ( 16 * Baud ) = UBRR
  90. // Baud <- 0.823284 ms per bit, thus 1000 / 0.823284 = 1214.65004 -> 823.2824
  91. // Thus baud setting = 823
  92. uint16_t baud = 823; // Max setting of 4095
  93. UBRR1H = (uint8_t)(baud >> 8);
  94. UBRR1L = (uint8_t)baud;
  95. // Enable the receiver, transitter, and RX Complete Interrupt as well as 9 bit data
  96. UCSR1B = 0x9C;
  97. // Set frame format: 9 data, 1 stop bit, no parity
  98. // Asynchrounous USART mode
  99. UCSR1C = 0x06;
  100. // Initially buffer doesn't need to be cleared (it's empty...)
  101. BufferReadyToClear = 0;
  102. // Reset the keyboard before scanning, we might be in a wierd state
  103. scan_resetKeyboard();
  104. }
  105. // Main Detection Loop
  106. // Not needed for the BETKB, this is just a busy loop
  107. inline uint8_t scan_loop()
  108. {
  109. return 0;
  110. }
  111. // TODO
  112. void processKeyValue( uint8_t keyValue )
  113. {
  114. // Finalize output buffer
  115. // Mask 8th bit
  116. keyValue &= 0x7F;
  117. // Interpret scan code
  118. switch ( keyValue )
  119. {
  120. case 0x40: // Clear buffer command
  121. info_print("CLEAR!");
  122. BufferReadyToClear = 1;
  123. break;
  124. case 0x7F:
  125. scan_lockKeyboard();
  126. _delay_ms(3000);
  127. scan_unlockKeyboard();
  128. default:
  129. // Make sure the key isn't already in the buffer
  130. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  131. {
  132. // Key isn't in the buffer yet
  133. if ( c == KeyIndex_BufferUsed )
  134. {
  135. bufferAdd( keyValue );
  136. break;
  137. }
  138. // Key already in the buffer
  139. if ( KeyIndex_Buffer[c] == keyValue )
  140. break;
  141. }
  142. break;
  143. }
  144. }
  145. // Send data
  146. uint8_t scan_sendData( uint8_t dataPayload )
  147. {
  148. UDR1 = dataPayload;
  149. return 0;
  150. }
  151. // Signal KeyIndex_Buffer that it has been properly read
  152. // TODO
  153. void scan_finishedWithBuffer( void )
  154. {
  155. return;
  156. }
  157. // Signal that the keys have been properly sent over USB
  158. // TODO
  159. void scan_finishedWithUSBBuffer( void )
  160. {
  161. /*
  162. uint8_t foundModifiers = 0;
  163. // Look for all of the modifiers present, there is a max of 8 (but only keys for 5 on the HASCI version)
  164. for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
  165. {
  166. // The modifier range is from 0x80 to 0x8F (well, the last bit is the ON/OFF signal, but whatever...)
  167. if ( KeyIndex_Buffer[c] <= 0x8F && KeyIndex_Buffer[c] >= 0x80 )
  168. {
  169. // Add the modifier back into the the Key Buffer
  170. KeyIndex_Buffer[foundModifiers] = KeyIndex_Buffer[c];
  171. foundModifiers++;
  172. }
  173. }
  174. // Adjust the size of the new Key Buffer
  175. KeyIndex_BufferUsed = foundModifiers;
  176. */
  177. }
  178. // Reset/Hold keyboard
  179. // NOTE: Does nothing with the BETKB
  180. void scan_lockKeyboard( void )
  181. {
  182. }
  183. // NOTE: Does nothing with the BETKB
  184. void scan_unlockKeyboard( void )
  185. {
  186. }
  187. // Reset Keyboard
  188. // TODO?
  189. // - Holds the input read line high to flush the buffer
  190. // - This does not actually reset the keyboard, but always seems brings it to a sane state
  191. // - Won't work fully if keys are being pressed done at the same time
  192. void scan_resetKeyboard( void )
  193. {
  194. // Initiate data request line, but don't read the incoming data
  195. //REQUEST_DATA(); TODO
  196. // Not a calculated valued...
  197. _delay_ms( 50 );
  198. // Stop request line
  199. //STOP_DATA(); TODO
  200. }