Kiibohd Controller
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

scan_loop.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // 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 HOLD_PORT PORTD
  32. #define HOLD_DDR DDRD
  33. #define HOLD_PIN 3
  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. // ----- Variables -----
  40. // Buffer used to inform the macro processing module which keys have been detected as pressed
  41. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  42. volatile uint8_t KeyIndex_BufferUsed;
  43. volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
  44. // Buffer Signals
  45. volatile uint8_t BufferReadyToClear;
  46. // ----- Function Declarations -----
  47. void processKeyValue( uint8_t keyValue );
  48. void removeKeyValue( 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. uint8_t keyState = 0x00;
  56. // Read the scancode packet from the USART (1st to 8th bits)
  57. keyValue = UDR1;
  58. // Read the release/press bit (9th bit) XXX Unnecessary, and wrong it seems, parity bit? or something else?
  59. keyState = UCSR1B & 0x02;
  60. // High bit of keyValue, also represents press/release
  61. keyState = keyValue & 0x80 ? 0x00 : 0x02;
  62. // Debug
  63. char tmpStr[6];
  64. hexToStr( keyValue & 0x7F, tmpStr );
  65. // Process the scancode
  66. switch ( keyState )
  67. {
  68. case 0x00: // Released
  69. dPrintStrs( tmpStr, "R " ); // Debug
  70. // Remove key from press buffer
  71. removeKeyValue( keyValue & 0x7F );
  72. break;
  73. case 0x02: // Pressed
  74. dPrintStrs( tmpStr, "P " ); // Debug
  75. // New key to process
  76. processKeyValue( keyValue & 0x7F );
  77. break;
  78. }
  79. sei(); // Re-enable Interrupts
  80. }
  81. // ----- Functions -----
  82. // Setup
  83. inline void scan_setup()
  84. {
  85. // Setup the the USART interface for keyboard data input
  86. // NOTE: The input data signal needs to be inverted for the Teensy USART to properly work
  87. // Setup baud rate
  88. // 16 MHz / ( 16 * Baud ) = UBRR
  89. // Baud <- 0.823284 ms per bit, thus 1000 / 0.823284 = 1214.65004 -> 823.2824
  90. // Thus baud setting = 823
  91. uint16_t baud = 823; // Max setting of 4095
  92. UBRR1H = (uint8_t)(baud >> 8);
  93. UBRR1L = (uint8_t)baud;
  94. // Enable the receiver, and RX Complete Interrupt as well as 9 bit data
  95. UCSR1B = 0x94;
  96. // The transmitter is only to be enabled when needed
  97. // Set the pin to be pull-up otherwise (use the lowered voltage inverter in order to sink)
  98. HOLD_DDR &= ~(1 << HOLD_PIN);
  99. HOLD_PORT |= (1 << HOLD_PIN);
  100. // Set frame format: 9 data, 1 stop bit, no parity
  101. // Asynchrounous USART mode
  102. UCSR1C = 0x06;
  103. // Initially buffer doesn't need to be cleared (it's empty...)
  104. BufferReadyToClear = 0;
  105. // InputSignal is off by default
  106. KeyIndex_Add_InputSignal = 0x00;
  107. // Reset the keyboard before scanning, we might be in a wierd state
  108. scan_resetKeyboard();
  109. }
  110. // Main Detection Loop
  111. // Not needed for the BETKB, this is just a busy loop
  112. inline uint8_t scan_loop()
  113. {
  114. return 0;
  115. }
  116. void processKeyValue( uint8_t keyValue )
  117. {
  118. // Interpret scan code
  119. switch ( keyValue )
  120. {
  121. case 0x00: // Break code from input?
  122. break;
  123. default:
  124. // Make sure the key isn't already in the buffer
  125. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  126. {
  127. // Key isn't in the buffer yet
  128. if ( c == KeyIndex_BufferUsed )
  129. {
  130. bufferAdd( keyValue );
  131. // Only send data if enabled
  132. if ( KeyIndex_Add_InputSignal )
  133. scan_sendData( KeyIndex_Add_InputSignal );
  134. break;
  135. }
  136. // Key already in the buffer
  137. if ( KeyIndex_Buffer[c] == keyValue )
  138. break;
  139. }
  140. break;
  141. }
  142. }
  143. void removeKeyValue( uint8_t keyValue )
  144. {
  145. // Check for the released key, and shift the other keys lower on the buffer
  146. uint8_t c;
  147. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  148. {
  149. // Key to release found
  150. if ( KeyIndex_Buffer[c] == keyValue )
  151. {
  152. // Shift keys from c position
  153. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  154. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  155. // Decrement Buffer
  156. KeyIndex_BufferUsed--;
  157. break;
  158. }
  159. }
  160. // Error case (no key to release)
  161. if ( c == KeyIndex_BufferUsed + 1 )
  162. {
  163. errorLED( 1 );
  164. char tmpStr[6];
  165. hexToStr( keyValue, tmpStr );
  166. erro_dPrint( "Could not find key to release: ", tmpStr );
  167. }
  168. }
  169. // Send data
  170. uint8_t scan_sendData( uint8_t dataPayload )
  171. {
  172. // Enable the USART Transmitter
  173. UCSR1B |= (1 << 3);
  174. // Debug
  175. char tmpStr[6];
  176. hexToStr( dataPayload, tmpStr );
  177. info_dPrint( "Sending - ", tmpStr );
  178. UDR1 = dataPayload;
  179. // Wait for the payload
  180. _delay_us( 800 );
  181. // Disable the USART Transmitter
  182. UCSR1B &= ~(1 << 3);
  183. return 0;
  184. }
  185. // Signal KeyIndex_Buffer that it has been properly read
  186. void scan_finishedWithBuffer( uint8_t sentKeys )
  187. {
  188. }
  189. // Signal that the keys have been properly sent over USB
  190. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  191. {
  192. }
  193. // Reset/Hold keyboard
  194. // NOTE: Does nothing with the BETKB
  195. void scan_lockKeyboard( void )
  196. {
  197. }
  198. // NOTE: Does nothing with the BETKB
  199. void scan_unlockKeyboard( void )
  200. {
  201. }
  202. // Reset Keyboard
  203. void scan_resetKeyboard( void )
  204. {
  205. // Not a calculated valued...
  206. _delay_ms( 50 );
  207. }