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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Copyright (C) 2011 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. // Pinout Defines
  31. #define SPKR_PORT PORTD
  32. #define SPKR_DDR DDRD
  33. #define SPKR_POS 1
  34. #define POWR_PORT PORTC
  35. #define POWR_DDR DDRC
  36. #define POWR_POS 7
  37. // ----- Macros -----
  38. // Make sure we haven't overflowed the buffer
  39. #define bufferAdd(byte) \
  40. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  41. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  42. // ----- Variables -----
  43. // Buffer used to inform the macro processing module which keys have been detected as pressed
  44. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  45. volatile uint8_t KeyIndex_BufferUsed;
  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 <- 0.10450 ms per bit, thus 1000 / 0.10450 = 9569.4
  54. // Thus UBRR = 104.50
  55. // To deal with the 0.5, setting to double speed, which means UBRR = 209
  56. uint16_t baud = 209; // Max setting of 4095
  57. UBRR1H = (uint8_t)(baud >> 8);
  58. UBRR1L = (uint8_t)baud;
  59. // Enable Double Read Speed
  60. UCSR1A = 0x02;
  61. // Enable the receiver, transitter, and RX Complete Interrupt
  62. UCSR1B = 0x98;
  63. // Set frame format: 8 data, no stop bits or parity
  64. // Asynchrounous USART mode
  65. UCSR1C = 0x06;
  66. // Set Speaker Pin to Pull-Up gives a low-volume click (XXX no other setting does, why?)
  67. SPKR_DDR &= ~(1 << SPKR_POS);
  68. SPKR_PORT |= (1 << SPKR_POS);
  69. // Set Power Pin (I've traced this back to the "Power On" Switch, but I don't really know what it's for)
  70. // Configured as a Pull-up Input - This pin "can" be read as well, it will go to GND when the "Power On" switch is pressed, and will read ~5V otherwise
  71. // XXX Currently not used by the controller
  72. POWR_DDR &= ~(1 << POWR_POS);
  73. POWR_PORT |= (1 << POWR_POS);
  74. // Reset the keyboard before scanning, we might be in a wierd state
  75. scan_resetKeyboard();
  76. }
  77. // Main Detection Loop
  78. // Not needed for the Sony NEWS, this is just a busy loop
  79. inline uint8_t scan_loop()
  80. {
  81. return 0;
  82. }
  83. void processKeyValue( uint8_t keyValue )
  84. {
  85. // Detect release condition
  86. uint8_t release = keyValue & 0x80;
  87. // Finalize output buffer
  88. // Mask 8th bit
  89. keyValue &= 0x7F;
  90. // Key Release
  91. if ( release )
  92. {
  93. // Check for the released key, and shift the other keys lower on the buffer
  94. uint8_t c;
  95. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  96. {
  97. // Key to release found
  98. if ( KeyIndex_Buffer[c] == keyValue )
  99. {
  100. // Shift keys from c position
  101. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  102. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  103. // Decrement Buffer
  104. KeyIndex_BufferUsed--;
  105. break;
  106. }
  107. }
  108. // Error case (no key to release)
  109. if ( c == KeyIndex_BufferUsed + 1 )
  110. {
  111. errorLED( 1 );
  112. char tmpStr[6];
  113. hexToStr( keyValue, tmpStr );
  114. erro_dPrint( "Could not find key to release: ", tmpStr );
  115. }
  116. }
  117. // Press or Repeated Key
  118. else
  119. {
  120. // Make sure the key isn't already in the buffer
  121. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  122. {
  123. // Key isn't in the buffer yet
  124. if ( c == KeyIndex_BufferUsed )
  125. {
  126. bufferAdd( keyValue );
  127. break;
  128. }
  129. // Key already in the buffer
  130. if ( KeyIndex_Buffer[c] == keyValue )
  131. break;
  132. }
  133. }
  134. }
  135. // USART Receive Buffer Full Interrupt
  136. ISR(USART1_RX_vect)
  137. {
  138. cli(); // Disable Interrupts
  139. uint8_t keyValue = 0x00;
  140. // One scancode at a time (fastest interval ~3.95 ms - recorded, should still be ok for interrupt polling)
  141. // Read the raw packet from the USART
  142. keyValue = UDR1;
  143. // Debug
  144. char tmpStr[6];
  145. hexToStr( keyValue, tmpStr );
  146. dPrintStrs( tmpStr, " " );
  147. // Process the scancode
  148. if ( keyValue != 0x00 )
  149. processKeyValue( keyValue );
  150. sei(); // Re-enable Interrupts
  151. }
  152. // Send data to keyboard
  153. uint8_t scan_sendData( uint8_t dataPayload )
  154. {
  155. // Debug
  156. char tmpStr[6];
  157. hexToStr( dataPayload, tmpStr );
  158. info_dPrint( tmpStr, " " );
  159. UDR1 = dataPayload;
  160. return 0;
  161. }
  162. // Signal KeyIndex_Buffer that it has been properly read
  163. // Not needed as a signal is sent to remove key-presses
  164. void scan_finishedWithBuffer( uint8_t sentKeys )
  165. {
  166. return;
  167. }
  168. // Reset/Hold keyboard TODO
  169. // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
  170. void scan_lockKeyboard( void )
  171. {
  172. }
  173. void scan_unlockKeyboard( void )
  174. {
  175. }
  176. // Reset Keyboard
  177. void scan_resetKeyboard( void )
  178. {
  179. // Empty buffer, now that keyboard has been reset
  180. KeyIndex_BufferUsed = 0;
  181. }
  182. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  183. {
  184. return;
  185. }