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

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