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.

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