Kiibohd Controller
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

scan_loop.c 6.3KB

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