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.

преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <macro.h>
  27. #include <print.h>
  28. // Local Includes
  29. #include "scan_loop.h"
  30. // ----- Defines -----
  31. // ----- Macros -----
  32. // ----- Variables -----
  33. // Buffer used to inform the macro processing module which keys have been detected as pressed
  34. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  35. volatile uint8_t KeyIndex_BufferUsed;
  36. // ----- Function Declarations -----
  37. void processKeyValue( uint8_t valueType );
  38. void removeKeyValue( uint8_t keyValue );
  39. // ----- Interrupt Functions -----
  40. // UART Receive Buffer Full Interrupt
  41. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  42. ISR(USART1_RX_vect)
  43. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  44. void uart0_status_isr(void)
  45. #endif
  46. {
  47. cli(); // Disable Interrupts
  48. // Variable for UART data read
  49. uint8_t keyValue = 0x00;
  50. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  51. keyValue = UDR1;
  52. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  53. // UART0_S1 must be read for the interrupt to be cleared
  54. if ( UART0_S1 & UART_S1_RDRF )
  55. {
  56. // Only doing single byte FIFO here
  57. keyValue = UART0_D;
  58. }
  59. #endif
  60. // Debug
  61. char tmpStr[6];
  62. hexToStr( keyValue, tmpStr );
  63. dPrintStrs( tmpStr, " " ); // Debug
  64. // Decipher scan value
  65. processKeyValue( keyValue );
  66. sei(); // Re-enable Interrupts
  67. }
  68. // ----- Functions -----
  69. // Reset Keyboard
  70. void Scan_resetKeyboard( void )
  71. {
  72. // Not a calculated valued...
  73. _delay_ms( 50 );
  74. KeyIndex_BufferUsed = 0;
  75. }
  76. // Setup
  77. inline void Scan_setup()
  78. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  79. {
  80. // Setup the the USART interface for keyboard data input
  81. // Setup baud rate - 1205 Baud
  82. // 16 MHz / ( 16 * Baud ) = UBRR
  83. // Baud: 1205 -> 16 MHz / ( 16 * 1205 ) = 829.8755
  84. // Thus baud setting = 830
  85. uint16_t baud = 830; // Max setting of 4095
  86. UBRR1H = (uint8_t)(baud >> 8);
  87. UBRR1L = (uint8_t)baud;
  88. // Enable the receiver, and RX Complete Interrupt
  89. UCSR1B = 0x90;
  90. // Set frame format: 8 data, 1 stop bit, even parity
  91. // Asynchrounous USART mode
  92. UCSR1C = 0x26;
  93. // Reset the keyboard before scanning, we might be in a wierd state
  94. Scan_resetKeyboard();
  95. }
  96. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  97. {
  98. // Setup the the UART interface for keyboard data input
  99. SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
  100. // Pin Setup for UART0
  101. PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  102. PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  103. // Setup baud rate - 1205 Baud
  104. // 48 MHz / ( 16 * Baud ) = BDH/L
  105. // Baud: 1205 -> 48 MHz / ( 16 * 1205 ) = 2489.6266
  106. // Thus baud setting = 2490
  107. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  108. uint16_t baud = 2490; // Max setting of 8191
  109. UART0_BDH = (uint8_t)(baud >> 8);
  110. UART0_BDL = (uint8_t)baud;
  111. // 8 bit, Even Parity, Idle Character bit after stop
  112. // NOTE: For 8 bit with Parity you must enable 9 bit transmission (pg. 1065)
  113. // You only need to use UART0_D for 8 bit reading/writing though
  114. // UART_C1_M UART_C1_PE UART_C1_PT UART_C1_ILT
  115. UART0_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
  116. // Number of bytes in FIFO before TX Interrupt
  117. UART0_TWFIFO = 1;
  118. // Number of bytes in FIFO before RX Interrupt
  119. UART0_RWFIFO = 1;
  120. // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
  121. // TX/RX FIFO Size:
  122. // 0x0 - 1 dataword
  123. // 0x1 - 4 dataword
  124. // 0x2 - 8 dataword
  125. //UART0_PFIFO = UART_PFIFO_TXFE | /*TXFIFOSIZE*/ (0x0 << 4) | UART_PFIFO_RXFE | /*RXFIFOSIZE*/ (0x0);
  126. // Reciever Inversion Disabled, LSBF
  127. // UART_S2_RXINV UART_S2_MSBF
  128. UART0_S2 |= 0x00;
  129. // Transmit Inversion Disabled
  130. // UART_C3_TXINV
  131. UART0_C3 |= 0x00;
  132. // TX Disabled, RX Enabled, RX Interrupt Enabled
  133. // UART_C2_TE UART_C2_RE UART_C2_RIE
  134. UART0_C2 = UART_C2_RE | UART_C2_RIE;
  135. // Add interrupt to the vector table
  136. NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
  137. // Reset the keyboard before scanning, we might be in a wierd state
  138. Scan_resetKeyboard();
  139. }
  140. #endif
  141. // Main Detection Loop
  142. inline uint8_t Scan_loop()
  143. {
  144. return 0;
  145. }
  146. void processKeyValue( uint8_t keyValue )
  147. {
  148. // XXX NOTE: The key processing is not complete for this keyboard
  149. // Mostly due to laziness, and that the keyboard can't really be useful on a modern computer
  150. // Basic typing will work, but some of the keys and the Graph mode changes things around
  151. // Add key(s) to processing buffer
  152. // First split out Shift and Ctrl
  153. // Reserved Codes:
  154. // Shift - 0xF5
  155. // Ctrl - 0xF6
  156. switch ( keyValue )
  157. {
  158. // - Ctrl Keys -
  159. // Exception keys
  160. case 0x08: // ^H
  161. case 0x09: // ^I
  162. case 0x0D: // ^M
  163. case 0x1B: // ^[
  164. Macro_bufferAdd( keyValue );
  165. break;
  166. // 0x40 Offset Keys
  167. // Add Ctrl key and offset to the lower alphabet
  168. case 0x00: // ^@
  169. case 0x1C: // "^\"
  170. case 0x1D: // ^]
  171. case 0x1E: // ^^
  172. case 0x1F: // ^_
  173. Macro_bufferAdd( 0xF6 );
  174. Macro_bufferAdd( keyValue + 0x40 );
  175. break;
  176. // - Add Shift key and offset to non-shifted key -
  177. // 0x10 Offset Keys
  178. case 0x21: // !
  179. case 0x23: // #
  180. case 0x24: // $
  181. case 0x25: // %
  182. Macro_bufferAdd( 0xF5 );
  183. Macro_bufferAdd( keyValue + 0x10 );
  184. break;
  185. // 0x11 Offset Keys
  186. case 0x26: // &
  187. case 0x28: // (
  188. Macro_bufferAdd( 0xF5 );
  189. Macro_bufferAdd( keyValue + 0x11 );
  190. break;
  191. // 0x07 Offset Keys
  192. case 0x29: // )
  193. Macro_bufferAdd( 0xF5 );
  194. Macro_bufferAdd( keyValue + 0x07 );
  195. break;
  196. // -0x0E Offset Keys
  197. case 0x40: // @
  198. Macro_bufferAdd( 0xF5 );
  199. Macro_bufferAdd( keyValue - 0x0E );
  200. break;
  201. // 0x0E Offset Keys
  202. case 0x2A: // *
  203. Macro_bufferAdd( 0xF5 );
  204. Macro_bufferAdd( keyValue + 0x0E );
  205. break;
  206. // 0x12 Offset Keys
  207. case 0x2B: // +
  208. Macro_bufferAdd( 0xF5 );
  209. Macro_bufferAdd( keyValue + 0x12 );
  210. break;
  211. // 0x05 Offset Keys
  212. case 0x22: // "
  213. Macro_bufferAdd( 0xF5 );
  214. Macro_bufferAdd( keyValue + 0x05 );
  215. break;
  216. // 0x01 Offset Keys
  217. case 0x3A: // :
  218. Macro_bufferAdd( 0xF5 );
  219. Macro_bufferAdd( keyValue + 0x01 );
  220. break;
  221. // -0x10 Offset Keys
  222. case 0x3C: // <
  223. case 0x3E: // >
  224. case 0x3F: // ?
  225. Macro_bufferAdd( 0xF5 );
  226. Macro_bufferAdd( keyValue - 0x10 );
  227. break;
  228. // -0x28 Offset Keys
  229. case 0x5E: // ^
  230. Macro_bufferAdd( 0xF5 );
  231. Macro_bufferAdd( keyValue - 0x28 );
  232. break;
  233. // -0x32 Offset Keys
  234. case 0x5F: // _
  235. Macro_bufferAdd( 0xF5 );
  236. Macro_bufferAdd( keyValue - 0x32 );
  237. break;
  238. // -0x20 Offset Keys
  239. case 0x7B: // {
  240. case 0x7C: // |
  241. case 0x7D: // }
  242. Macro_bufferAdd( 0xF5 );
  243. Macro_bufferAdd( keyValue - 0x20 );
  244. break;
  245. // -0x1E Offset Keys
  246. case 0x7E: // ~
  247. Macro_bufferAdd( 0xF5 );
  248. Macro_bufferAdd( keyValue - 0x1E );
  249. break;
  250. // All other keys
  251. default:
  252. // Ctrl Characters are from 0x00 to 0x1F, excluding:
  253. // 0x08 - Backspace
  254. // 0x09 - [Horizontal] Tab
  255. // 0x0D - [Carriage] Return
  256. // 0x1B - Escape
  257. // 0x7F - Delete (^?) (Doesn't need to be split out)
  258. // 0x60 Offset Keys
  259. // Add Ctrl key and offset to the lower alphabet
  260. if ( keyValue >= 0x00 && keyValue <= 0x1F )
  261. {
  262. Macro_bufferAdd( 0xF6 );
  263. Macro_bufferAdd( keyValue + 0x60 );
  264. }
  265. // Shift Characters are from 0x41 to 0x59
  266. // No exceptions here :D
  267. // Add Shift key and offset to the lower alphabet
  268. else if ( keyValue >= 0x41 && keyValue <= 0x5A )
  269. {
  270. Macro_bufferAdd( 0xF5 );
  271. Macro_bufferAdd( keyValue + 0x20 );
  272. }
  273. // Everything else
  274. else
  275. {
  276. Macro_bufferAdd( keyValue );
  277. }
  278. break;
  279. }
  280. }
  281. // Send data
  282. // NOTE: Example only, MBC-55X cannot receive user data
  283. uint8_t Scan_sendData( uint8_t dataPayload )
  284. {
  285. // Debug
  286. char tmpStr[6];
  287. hexToStr( dataPayload, tmpStr );
  288. info_dPrint( "Sending - ", tmpStr );
  289. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  290. UDR1 = dataPayload;
  291. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  292. UART0_D = dataPayload;
  293. #endif
  294. return 0;
  295. }
  296. // Signal KeyIndex_Buffer that it has been properly read
  297. void Scan_finishedWithBuffer( uint8_t sentKeys )
  298. {
  299. }
  300. // Signal that the keys have been properly sent over USB
  301. void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
  302. {
  303. cli(); // Disable Interrupts
  304. // Reset the buffer counter
  305. KeyIndex_BufferUsed = 0;
  306. sei(); // Re-enable Interrupts
  307. }
  308. // Reset/Hold keyboard
  309. // NOTE: Does nothing with the MBC-55x
  310. void Scan_lockKeyboard( void )
  311. {
  312. }
  313. // NOTE: Does nothing with the MBC-55x
  314. void Scan_unlockKeyboard( void )
  315. {
  316. }