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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // 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. // Make sure we haven't overflowed the buffer
  32. #define bufferAdd(byte) \
  33. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  34. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  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. // ----- Function Declarations -----
  40. void processKeyValue( uint8_t valueType );
  41. void removeKeyValue( uint8_t keyValue );
  42. // ----- Interrupt Functions -----
  43. // UART Receive Buffer Full Interrupt
  44. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  45. ISR(USART1_RX_vect)
  46. #elif defined(_mk20dx128_) // ARM
  47. void uart0_status_isr(void)
  48. #endif
  49. {
  50. cli(); // Disable Interrupts
  51. // Read part of the scan code (3 8bit chunks) from USART
  52. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  53. uint8_t tmp = UDR1;
  54. #elif defined(_mk20dx128_) // ARM
  55. // TODO
  56. uint8_t tmp = 0;
  57. #endif
  58. // Debug
  59. char tmpStr[6];
  60. hexToStr( tmp, tmpStr );
  61. dPrintStrs( tmpStr, " " ); // Debug
  62. // TODO
  63. sei(); // Re-enable Interrupts
  64. }
  65. // ----- Functions -----
  66. // Setup
  67. inline void scan_setup()
  68. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  69. {
  70. // Setup the the USART interface for keyboard data input
  71. // TODO
  72. // Setup baud rate
  73. // 16 MHz / ( 16 * Baud ) = UBRR
  74. // Baud: 4817 -> 16 MHz / ( 16 * 4817 ) = 207.5981
  75. // Thus baud setting = 208
  76. uint16_t baud = 208; // Max setting of 4095
  77. UBRR1H = (uint8_t)(baud >> 8);
  78. UBRR1L = (uint8_t)baud;
  79. // Enable the receiver, transmitter, and RX Complete Interrupt
  80. UCSR1B = 0x98;
  81. // Set frame format: 8 data, 1 stop bit, odd parity
  82. // Asynchrounous USART mode
  83. UCSR1C = 0x36;
  84. // Reset the keyboard before scanning, we might be in a wierd state
  85. scan_resetKeyboard();
  86. }
  87. #elif defined(_mk20dx128_) // ARM
  88. {
  89. // Setup the the UART interface for keyboard data input
  90. // Setup baud rate
  91. // TODO
  92. // Reset the keyboard before scanning, we might be in a wierd state
  93. scan_resetKeyboard();
  94. }
  95. #endif
  96. // Main Detection Loop
  97. inline uint8_t scan_loop()
  98. {
  99. return 0;
  100. }
  101. void processKeyValue( uint8_t keyValue )
  102. {
  103. // TODO Process ASCII
  104. // Make sure the key isn't already in the buffer
  105. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  106. {
  107. // Key isn't in the buffer yet
  108. if ( c == KeyIndex_BufferUsed )
  109. {
  110. bufferAdd( keyValue );
  111. break;
  112. }
  113. // Key already in the buffer
  114. if ( KeyIndex_Buffer[c] == keyValue )
  115. break;
  116. }
  117. }
  118. void removeKeyValue( uint8_t keyValue )
  119. {
  120. // Check for the released key, and shift the other keys lower on the buffer
  121. uint8_t c;
  122. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  123. {
  124. // Key to release found
  125. if ( KeyIndex_Buffer[c] == keyValue )
  126. {
  127. // Shift keys from c position
  128. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  129. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  130. // Decrement Buffer
  131. KeyIndex_BufferUsed--;
  132. break;
  133. }
  134. }
  135. // Error case (no key to release)
  136. if ( c == KeyIndex_BufferUsed + 1 )
  137. {
  138. errorLED( 1 );
  139. char tmpStr[6];
  140. hexToStr( keyValue, tmpStr );
  141. erro_dPrint( "Could not find key to release: ", tmpStr );
  142. }
  143. }
  144. // Send data
  145. uint8_t scan_sendData( uint8_t dataPayload )
  146. {
  147. // Debug
  148. char tmpStr[6];
  149. hexToStr( dataPayload, tmpStr );
  150. info_dPrint( "Sending - ", tmpStr );
  151. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  152. UDR1 = dataPayload;
  153. #elif defined(_mk20dx128_) // ARM
  154. // TODO
  155. #endif
  156. return 0;
  157. }
  158. // Signal KeyIndex_Buffer that it has been properly read
  159. void scan_finishedWithBuffer( uint8_t sentKeys )
  160. {
  161. }
  162. // Signal that the keys have been properly sent over USB
  163. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  164. {
  165. }
  166. // Reset/Hold keyboard
  167. // NOTE: Does nothing with the FACOM6684
  168. void scan_lockKeyboard( void )
  169. {
  170. }
  171. // NOTE: Does nothing with the FACOM6684
  172. void scan_unlockKeyboard( void )
  173. {
  174. }
  175. // Reset Keyboard
  176. void scan_resetKeyboard( void )
  177. {
  178. // Not a calculated valued...
  179. _delay_ms( 50 );
  180. KeyIndex_BufferUsed = 0;
  181. }