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

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