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.5KB

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