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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Copyright (C) 2011-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. // Known signals
  36. static uint8_t cmd_clickOFF = 0x0A; // Short beep, turns off clicker
  37. static uint8_t cmd_clickON = 0x04; // Long beep, turns on clicker
  38. static uint8_t cmd_ACK_AA = 0x10; // Keyboard will send ack (0xAA) back to PC
  39. // Other known signals
  40. // 0x02 turns on clicker but with short beep
  41. // ----- Functions -----
  42. // Setup
  43. inline void Scan_setup()
  44. {
  45. // Setup the the USART interface for keyboard data input
  46. // Setup baud rate
  47. // 16 MHz / ( 16 * Baud ) = UBRR
  48. // Baud <- 3.358 ms per bit, thus 1000 / 3.358 = 297.80
  49. // Thus USBRR = 3357
  50. uint16_t baud = 3357; // Max setting of 4095
  51. UBRR1H = (uint8_t)(baud >> 8);
  52. UBRR1L = (uint8_t)baud;
  53. // Enable the receiver, transitter, and RX Complete Interrupt
  54. UCSR1B = 0x98;
  55. // Set frame format: 8 data, no stop bits or parity
  56. // Asynchrounous USART mode
  57. // Kaypro sends ASCII codes (mostly standard) with 1 start bit and 8 data bits, with no trailing stop or parity bits
  58. UCSR1C = 0x06;
  59. }
  60. // Main Detection Loop
  61. // Nothing is needed here for the Kaypro, but the function is available as part of the api to be called in a polling fashion
  62. // TODO
  63. // - Add songs :D
  64. inline uint8_t Scan_loop()
  65. {
  66. // We *could* do extra offline processing here, but, it's not really needed for the Kaypro 1 keyboard
  67. return 0;
  68. }
  69. // USART Receive Buffer Full Interrupt
  70. ISR(USART1_RX_vect)
  71. {
  72. cli(); // Disable Interrupts
  73. // Get key from USART
  74. uint8_t keyValue = UDR1;
  75. //#ifdef MAX_DEBUG
  76. // Debug print key
  77. char tmpStr1[6];
  78. hexToStr( keyValue, tmpStr1 );
  79. dPrintStrs( tmpStr1, " " );
  80. //#endif
  81. // Add key(s) to processing buffer
  82. // First split out Shift and Ctrl
  83. // Reserved Codes:
  84. // Shift - 0xF5
  85. // Ctrl - 0xF6
  86. switch ( keyValue )
  87. {
  88. // - Ctrl Keys -
  89. // Exception keys
  90. case 0x08: // ^H
  91. case 0x09: // ^I
  92. case 0x0D: // ^M
  93. case 0x1B: // ^[
  94. Macro_bufferAdd( keyValue );
  95. break;
  96. // 0x40 Offset Keys
  97. // Add Ctrl key and offset to the lower alphabet
  98. case 0x00: // ^@
  99. case 0x1C: // "^\"
  100. case 0x1D: // ^]
  101. case 0x1E: // ^^
  102. case 0x1F: // ^_
  103. Macro_bufferAdd( 0xF6 );
  104. Macro_bufferAdd( keyValue + 0x40 );
  105. break;
  106. // - Add Shift key and offset to non-shifted key -
  107. // 0x10 Offset Keys
  108. case 0x21: // !
  109. case 0x23: // #
  110. case 0x24: // $
  111. case 0x25: // %
  112. Macro_bufferAdd( 0xF5 );
  113. Macro_bufferAdd( keyValue + 0x10 );
  114. break;
  115. // 0x11 Offset Keys
  116. case 0x26: // &
  117. case 0x28: // (
  118. Macro_bufferAdd( 0xF5 );
  119. Macro_bufferAdd( keyValue + 0x11 );
  120. break;
  121. // 0x07 Offset Keys
  122. case 0x29: // )
  123. Macro_bufferAdd( 0xF5 );
  124. Macro_bufferAdd( keyValue + 0x07 );
  125. break;
  126. // -0x0E Offset Keys
  127. case 0x40: // @
  128. Macro_bufferAdd( 0xF5 );
  129. Macro_bufferAdd( keyValue - 0x0E );
  130. break;
  131. // 0x0E Offset Keys
  132. case 0x2A: // *
  133. Macro_bufferAdd( 0xF5 );
  134. Macro_bufferAdd( keyValue + 0x0E );
  135. break;
  136. // 0x12 Offset Keys
  137. case 0x2B: // +
  138. Macro_bufferAdd( 0xF5 );
  139. Macro_bufferAdd( keyValue + 0x12 );
  140. break;
  141. // 0x05 Offset Keys
  142. case 0x22: // "
  143. Macro_bufferAdd( 0xF5 );
  144. Macro_bufferAdd( keyValue + 0x05 );
  145. break;
  146. // 0x01 Offset Keys
  147. case 0x3A: // :
  148. Macro_bufferAdd( 0xF5 );
  149. Macro_bufferAdd( keyValue + 0x01 );
  150. break;
  151. // -0x10 Offset Keys
  152. case 0x3C: // <
  153. case 0x3E: // >
  154. case 0x3F: // ?
  155. Macro_bufferAdd( 0xF5 );
  156. Macro_bufferAdd( keyValue - 0x10 );
  157. break;
  158. // -0x28 Offset Keys
  159. case 0x5E: // ^
  160. Macro_bufferAdd( 0xF5 );
  161. Macro_bufferAdd( keyValue - 0x28 );
  162. break;
  163. // -0x32 Offset Keys
  164. case 0x5F: // _
  165. Macro_bufferAdd( 0xF5 );
  166. Macro_bufferAdd( keyValue - 0x32 );
  167. break;
  168. // -0x20 Offset Keys
  169. case 0x7B: // {
  170. case 0x7C: // |
  171. case 0x7D: // }
  172. Macro_bufferAdd( 0xF5 );
  173. Macro_bufferAdd( keyValue - 0x20 );
  174. break;
  175. // -0x1E Offset Keys
  176. case 0x7E: // ~
  177. Macro_bufferAdd( 0xF5 );
  178. Macro_bufferAdd( keyValue - 0x1E );
  179. break;
  180. // All other keys
  181. default:
  182. // Ctrl Characters are from 0x00 to 0x1F, excluding:
  183. // 0x08 - Backspace
  184. // 0x09 - [Horizontal] Tab
  185. // 0x0D - [Carriage] Return
  186. // 0x1B - Escape
  187. // 0x7F - Delete (^?) (Doesn't need to be split out)
  188. // 0x60 Offset Keys
  189. // Add Ctrl key and offset to the lower alphabet
  190. if ( keyValue >= 0x00 && keyValue <= 0x1F )
  191. {
  192. Macro_bufferAdd( 0xF6 );
  193. Macro_bufferAdd( keyValue + 0x60 );
  194. }
  195. // Shift Characters are from 0x41 to 0x59
  196. // No exceptions here :D
  197. // Add Shift key and offset to the lower alphabet
  198. else if ( keyValue >= 0x41 && keyValue <= 0x5A )
  199. {
  200. Macro_bufferAdd( 0xF5 );
  201. Macro_bufferAdd( keyValue + 0x20 );
  202. }
  203. // Everything else
  204. else
  205. {
  206. Macro_bufferAdd( keyValue );
  207. }
  208. break;
  209. }
  210. // Special keys - For communication to the keyboard
  211. // TODO Try to push this functionality into the macros...somehow
  212. switch ( keyValue )
  213. {
  214. case 0xC3: // Keypad Enter
  215. print("\n");
  216. info_print("BEEEEP! - Clicker on");
  217. scan_sendData( cmd_clickON );
  218. break;
  219. case 0xB2: // Keypad Decimal
  220. print("\n");
  221. info_print("BEEP! - Clicker off");
  222. scan_sendData( cmd_clickOFF );
  223. break;
  224. case 0x0A: // Line Feed
  225. print("\n");
  226. info_print("ACK!!");
  227. scan_sendData( cmd_ACK_AA );
  228. break;
  229. }
  230. sei(); // Re-enable Interrupts
  231. }
  232. // Send data
  233. uint8_t Scan_sendData( uint8_t dataPayload )
  234. {
  235. UDR1 = dataPayload;
  236. return 0;
  237. }
  238. // Signal KeyIndex_Buffer that it has been properly read
  239. void Scan_finishedWithBuffer( uint8_t sentKeys )
  240. {
  241. }
  242. // Signal that the keys have been properly sent over USB
  243. void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
  244. {
  245. }
  246. // Reset/Hold keyboard
  247. // NOTE: Does nothing with the BETKB
  248. void Scan_lockKeyboard( void )
  249. {
  250. }
  251. // NOTE: Does nothing with the BETKB
  252. void Scan_unlockKeyboard( void )
  253. {
  254. }
  255. // Reset Keyboard
  256. void Scan_resetKeyboard( void )
  257. {
  258. }