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

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