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