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

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. void removeKeyValue( uint8_t keyValue );
  50. // ----- Interrupt Functions -----
  51. // USART Receive Buffer Full Interrupt
  52. ISR(USART1_RX_vect)
  53. {
  54. cli(); // Disable Interrupts
  55. uint8_t keyValue = 0x00;
  56. uint8_t keyState = 0x00;
  57. // Read the scancode packet from the USART (1st to 8th bits)
  58. keyValue = UDR1;
  59. // Read the release/press bit (9th bit) XXX Unnecessary, and wrong it seems, parity bit? or something else?
  60. keyState = UCSR1B & 0x02;
  61. // High bit of keyValue, also represents press/release
  62. keyState = keyValue & 0x80 ? 0x00 : 0x02;
  63. // Debug
  64. char tmpStr[6];
  65. hexToStr( keyValue & 0x7F, tmpStr );
  66. // Process the scancode
  67. switch ( keyState )
  68. {
  69. case 0x00: // Released
  70. dPrintStrs( tmpStr, "R " ); // Debug
  71. // Remove key from press buffer
  72. removeKeyValue( keyValue & 0x7F );
  73. break;
  74. case 0x02: // Pressed
  75. dPrintStrs( tmpStr, "P " ); // Debug
  76. // New key to process
  77. processKeyValue( keyValue & 0x7F );
  78. break;
  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. // Interpret scan code
  115. switch ( keyValue )
  116. {
  117. default:
  118. // Make sure the key isn't already in the buffer
  119. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  120. {
  121. // Key isn't in the buffer yet
  122. if ( c == KeyIndex_BufferUsed )
  123. {
  124. bufferAdd( keyValue );
  125. break;
  126. }
  127. // Key already in the buffer
  128. if ( KeyIndex_Buffer[c] == keyValue )
  129. break;
  130. }
  131. break;
  132. }
  133. }
  134. void removeKeyValue( uint8_t keyValue )
  135. {
  136. // Check for the released key, and shift the other keys lower on the buffer
  137. uint8_t c;
  138. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  139. {
  140. // Key to release found
  141. if ( KeyIndex_Buffer[c] == keyValue )
  142. {
  143. // Shift keys from c position
  144. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  145. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  146. // Decrement Buffer
  147. KeyIndex_BufferUsed--;
  148. break;
  149. }
  150. }
  151. // Error case (no key to release)
  152. if ( c == KeyIndex_BufferUsed + 1 )
  153. {
  154. errorLED( 1 );
  155. char tmpStr[6];
  156. hexToStr( keyValue, tmpStr );
  157. erro_dPrint( "Could not find key to release: ", tmpStr );
  158. }
  159. }
  160. // Send data
  161. uint8_t scan_sendData( uint8_t dataPayload )
  162. {
  163. UDR1 = dataPayload;
  164. return 0;
  165. }
  166. // Signal KeyIndex_Buffer that it has been properly read
  167. // TODO
  168. void scan_finishedWithBuffer( void )
  169. {
  170. return;
  171. }
  172. // Signal that the keys have been properly sent over USB
  173. // TODO
  174. void scan_finishedWithUSBBuffer( void )
  175. {
  176. }
  177. // Reset/Hold keyboard
  178. // NOTE: Does nothing with the BETKB
  179. void scan_lockKeyboard( void )
  180. {
  181. }
  182. // NOTE: Does nothing with the BETKB
  183. void scan_unlockKeyboard( void )
  184. {
  185. }
  186. // Reset Keyboard
  187. // TODO?
  188. // - Holds the input read line high to flush the buffer
  189. // - This does not actually reset the keyboard, but always seems brings it to a sane state
  190. // - Won't work fully if keys are being pressed done at the same time
  191. void scan_resetKeyboard( void )
  192. {
  193. // Initiate data request line, but don't read the incoming data
  194. //REQUEST_DATA(); TODO
  195. // Not a calculated valued...
  196. _delay_ms( 50 );
  197. // Stop request line
  198. //STOP_DATA(); TODO
  199. }