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

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