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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Copyright (C) 2013-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. // ----- Macros -----
  31. // ----- Variables -----
  32. // Buffer used to inform the macro processing module which keys have been detected as pressed
  33. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  34. volatile uint8_t KeyIndex_BufferUsed;
  35. volatile uint8_t KeyBufferRemove[6];
  36. volatile uint8_t KeyBufferRemoveCount = 0;
  37. static uint8_t KeyBuffer[3];
  38. volatile static uint8_t KeyBufferCount = 0;
  39. // ----- Function Declarations -----
  40. void processKeyValue( uint8_t valueType, uint8_t keyValue );
  41. void removeKeyValue( uint8_t keyValue );
  42. // ----- Interrupt Functions -----
  43. // USART Receive Buffer Full Interrupt
  44. ISR(USART1_RX_vect)
  45. {
  46. cli(); // Disable Interrupts
  47. // Read part of the scan code (3 8bit chunks) from USART
  48. KeyBuffer[KeyBufferCount] = UDR1;
  49. if ( KeyBufferCount >= 2 )
  50. {
  51. // Debug
  52. for ( uint8_t c = 0; c <= 2; c++ )
  53. {
  54. // Debug
  55. char tmpStr[6];
  56. hexToStr( KeyBuffer[c], tmpStr );
  57. dPrintStrs( tmpStr, " " ); // Debug
  58. }
  59. print("\n");
  60. processKeyValue( KeyBuffer[1], KeyBuffer[2] );
  61. KeyBufferCount = 0;
  62. }
  63. else
  64. {
  65. KeyBufferCount++;
  66. }
  67. sei(); // Re-enable Interrupts
  68. }
  69. // ----- Functions -----
  70. // Setup
  71. inline void Scan_setup()
  72. {
  73. // Setup the the USART interface for keyboard data input
  74. // Setup baud rate
  75. // 16 MHz / ( 16 * Baud ) = UBRR
  76. // Baud: 4817 -> 16 MHz / ( 16 * 4817 ) = 207.5981
  77. // Thus baud setting = 208
  78. uint16_t baud = 208; // Max setting of 4095
  79. UBRR1H = (uint8_t)(baud >> 8);
  80. UBRR1L = (uint8_t)baud;
  81. // Enable the receiver, transmitter, and RX Complete Interrupt
  82. UCSR1B = 0x98;
  83. // Set frame format: 8 data, 1 stop bit, odd parity
  84. // Asynchrounous USART mode
  85. UCSR1C = 0x36;
  86. // Reset the keyboard before scanning, we might be in a wierd state
  87. scan_resetKeyboard();
  88. }
  89. // Main Detection Loop
  90. inline uint8_t Scan_loop()
  91. {
  92. // Remove any "released keys", this is delayed due to buffer release synchronization issues
  93. for ( uint8_t c = 0; c < KeyBufferRemoveCount; c++ )
  94. {
  95. removeKeyValue( KeyBufferRemove[c] );
  96. }
  97. KeyBufferRemoveCount = 0;
  98. return 0;
  99. }
  100. void processKeyValue( uint8_t valueType, uint8_t keyValue )
  101. {
  102. switch ( valueType )
  103. {
  104. // Single Key Press
  105. case 0x00:
  106. break;
  107. // Repeated Key Press
  108. case 0x01:
  109. break;
  110. // Modifier Key Release
  111. case 0x02:
  112. KeyBufferRemove[KeyBufferRemoveCount++] = keyValue;
  113. return;
  114. }
  115. // Make sure the key isn't already in the buffer
  116. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  117. {
  118. // Key isn't in the buffer yet
  119. if ( c == KeyIndex_BufferUsed )
  120. {
  121. Macro_bufferAdd( keyValue );
  122. break;
  123. }
  124. // Key already in the buffer
  125. if ( KeyIndex_Buffer[c] == keyValue )
  126. break;
  127. }
  128. }
  129. void removeKeyValue( uint8_t keyValue )
  130. {
  131. // Check for the released key, and shift the other keys lower on the buffer
  132. uint8_t c;
  133. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  134. {
  135. // Key to release found
  136. if ( KeyIndex_Buffer[c] == keyValue )
  137. {
  138. // Shift keys from c position
  139. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  140. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  141. // Decrement Buffer
  142. KeyIndex_BufferUsed--;
  143. break;
  144. }
  145. }
  146. // Error case (no key to release)
  147. if ( c == KeyIndex_BufferUsed + 1 )
  148. {
  149. errorLED( 1 );
  150. char tmpStr[6];
  151. hexToStr( keyValue, tmpStr );
  152. erro_dPrint( "Could not find key to release: ", tmpStr );
  153. }
  154. }
  155. // Send data
  156. uint8_t Scan_sendData( uint8_t dataPayload )
  157. {
  158. // Debug
  159. char tmpStr[6];
  160. hexToStr( dataPayload, tmpStr );
  161. info_dPrint( "Sending - ", tmpStr );
  162. UDR1 = dataPayload;
  163. return 0;
  164. }
  165. // Signal KeyIndex_Buffer that it has been properly read
  166. void Scan_finishedWithBuffer( uint8_t sentKeys )
  167. {
  168. // Make sure we aren't in the middle of a receiving a new scancode
  169. while ( KeyBufferCount != 0 );
  170. cli(); // Disable Interrupts
  171. // Count for number of modifiers to maintain in the buffer
  172. uint8_t filled = 0;
  173. uint8_t latched = 0;
  174. uint8_t latchBuffer[13]; // There are only 13 keys that can possibly be latched at the same time...
  175. uint8_t normal = 0;
  176. uint8_t prevBuffer = KeyIndex_BufferUsed;
  177. // Clean out all keys except "special" keys (designated modifiers)
  178. uint8_t key;
  179. for ( key = 0; key < sentKeys; key++ )
  180. {
  181. switch ( KeyIndex_Buffer[key] )
  182. {
  183. // Dedicated Modifier Keys
  184. // NOTE: Both shifts are represented as the same scan code
  185. case 0x04:
  186. case 0x05:
  187. case 0x12:
  188. KeyIndex_Buffer[filled++] = KeyIndex_Buffer[key];
  189. break;
  190. // Latched Keys, only released if a non-modifier is pressed along with it
  191. // NOTE: This keys do not have a built in repeating
  192. case 0x00:
  193. case 0x01:
  194. case 0x03:
  195. //case 0x0B: // XXX Being used as an alternate Enter, since it is labelled as such
  196. case 0x22:
  197. case 0x10:
  198. case 0x11:
  199. case 0x20:
  200. case 0x21:
  201. case 0x30:
  202. case 0x31:
  203. case 0x40:
  204. //case 0x41: // XXX Being used as ESC
  205. latchBuffer[latched++] = KeyIndex_Buffer[key];
  206. break;
  207. // Allow the scancode to be removed, normal keys
  208. default:
  209. normal++;
  210. break;
  211. }
  212. }
  213. // Reset the buffer counter
  214. KeyIndex_BufferUsed = filled;
  215. // Add back lost keys, so they are processed on the next USB send
  216. for ( ; key < prevBuffer; key++ )
  217. {
  218. Macro_bufferAdd( KeyIndex_Buffer[key] );
  219. info_print("Re-appending lost key after USB send...");
  220. }
  221. // Only "re-add" the latched keys if they weren't used
  222. if ( latched > 0 && normal == 0 )
  223. {
  224. for ( uint8_t c = 0; c < latched; c++ )
  225. {
  226. Macro_bufferAdd( latchBuffer[c] );
  227. }
  228. }
  229. sei(); // Re-enable Interrupts
  230. }
  231. // Signal that the keys have been properly sent over USB
  232. void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
  233. {
  234. }
  235. // Reset/Hold keyboard
  236. // NOTE: Does nothing with the FACOM6684
  237. void Scan_lockKeyboard( void )
  238. {
  239. }
  240. // NOTE: Does nothing with the FACOM6684
  241. void Scan_unlockKeyboard( void )
  242. {
  243. }
  244. // Reset Keyboard
  245. void Scan_resetKeyboard( void )
  246. {
  247. // Not a calculated valued...
  248. _delay_ms( 50 );
  249. KeyBufferCount = 0;
  250. KeyBufferRemoveCount = 0;
  251. KeyIndex_BufferUsed = 0;
  252. }