Kiibohd Controller
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

scan_loop.c 7.0KB

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