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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 CLK_READ PIND
  32. #define CLK_PORT PORTD
  33. #define CLK_DDR DDRD
  34. #define CLK_PIN 1
  35. #define DATA_READ PIND
  36. #define DATA_PORT PORTD
  37. #define DATA_DDR DDRD
  38. #define DATA_PIN 0
  39. #define INTR_PORT PORTD
  40. #define INTR_DDR DDRD
  41. #define INTR_PIN 0
  42. // ----- Macros -----
  43. #define READ_CLK CLK_READ & (1 << CLK_PIN) ? 1 : 0
  44. #define READ_DATA DATA_READ & (1 << DATA_PIN) ? 0 : 1
  45. #define UNSET_INTR() INTR_DDR &= ~(1 << INTR_PIN)
  46. #define SET_INTR() INTR_DDR |= (1 << INTR_PIN)
  47. #define bufferAdd(byte) \
  48. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  49. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  50. // ----- Variables -----
  51. // Buffer used to inform the macro processing module which keys have been detected as pressed
  52. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  53. volatile uint8_t KeyIndex_BufferUsed;
  54. // Scan Code Retrieval Variables
  55. uint8_t inputData = 0xFF;
  56. uint8_t packet_index = 0;
  57. // ----- Functions -----
  58. // Setup
  59. inline void scan_setup()
  60. {
  61. // Initially reset the keyboard (just in case we are in a wierd state)
  62. scan_resetKeyboard();
  63. // Setup SPI for data input using the clock and data inputs
  64. // TODO
  65. /*
  66. // Setup inputs
  67. CLK_DDR &= ~(1 << CLK_PIN);
  68. DATA_DDR &= ~(1 << DATA_PIN);
  69. // Setup Pull-up's
  70. CLK_PORT &= ~(1 << CLK_PIN); // (CLK)
  71. DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
  72. */
  73. // Setup Keyboard Interrupt
  74. INTR_DDR &= ~(1 << INTR_PIN);
  75. INTR_PORT &= ~(1 << INTR_PIN);
  76. // Setup Keyboard Reset Line
  77. // TODO
  78. }
  79. // Main Detection Loop
  80. inline uint8_t scan_loop()
  81. {
  82. /*
  83. // Packet Read
  84. if ( packet_index == 8 )
  85. {
  86. // Disable Error LED, proper key found
  87. errorLED( 0 );
  88. //#ifdef MAX_DEBUG
  89. // Crazy Debug (Read the Scan Code)
  90. char tmpStr[3];
  91. hexToStr_op( inputData, tmpStr, 2 );
  92. dPrintStrsNL( "Read Data: 0x", tmpStr );
  93. //#endif
  94. // - Map the scan code to the index array -
  95. // If the 8th bit is high, remove the keypress, else, add the keypress
  96. // The lower 7 bits are the array index
  97. KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
  98. // Reset Containers
  99. packet_index = 0;
  100. inputData = 0xFF;
  101. }
  102. // Bad Packet
  103. else if ( packet_index > 8 )
  104. {
  105. // Signal Error
  106. errorLED( 1 );
  107. char tmpStr[3];
  108. int8ToStr( packet_index, tmpStr );
  109. erro_dPrint( "Big packet? Mismatched... ", tmpStr );
  110. packet_index = 0;
  111. inputData = 0xFF;
  112. }
  113. */
  114. // Disable keyboard interrupt (does nothing if already off)
  115. UNSET_INTR();
  116. /* XXX OLD CODE - Somewhat worked, has glitches, and is not compatible with the current API
  117. // Read the clock 8 times
  118. if ( READ_CLK )
  119. {
  120. // Mis-read packet, set back to 0
  121. if ( packet_index == -1 )
  122. packet_index = 0;
  123. // Append 1 bit of data
  124. inputData &= ~(READ_DATA << packet_index);
  125. packet_index++;
  126. // 8 Bits have been read
  127. if ( packet_index == 8 )
  128. {
  129. // Wait till clock edge falls
  130. while ( READ_CLK );
  131. // Sample both lines to make sure this is not a data value
  132. // and definitely the end of packet data blip
  133. uint16_t badDataCounter = 0;
  134. while ( !( READ_DATA ) && !( READ_CLK ) )
  135. badDataCounter++;
  136. if ( badDataCounter < 25 )
  137. {
  138. //#ifdef MAX_DEBUG
  139. // Crazy Debug (Read the Scan Code)
  140. char tmpStr[3];
  141. hexToStr_op( inputData, tmpStr, 2 );
  142. dbug_dPrint( "Read Data: 0x", tmpStr );
  143. //#endif
  144. // - Map the scan code to the index array -
  145. // If the 8th bit is high, remove the keypress, else, add the keypress
  146. // The lower 7 bits are the array index
  147. KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
  148. }
  149. // Even though this is a mis-read packet, we still know what the value is
  150. else
  151. {
  152. // Signal Error
  153. errorLED( 1 );
  154. char tmpStr[3];
  155. hexToStr_op( inputData, tmpStr, 2 );
  156. erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
  157. }
  158. // Reset Containers
  159. inputData = 0xFF;
  160. packet_index = 0;
  161. // Interrupt the keyboard, so we don't get packet pieces...
  162. SET_INTR();
  163. // Do not wait for next clock, let USB do it's thing (if desired)
  164. return packet_index;
  165. }
  166. // Wait till clock edge falls
  167. while ( READ_CLK );
  168. }
  169. */
  170. // Interrupt keyboard if there is no pending packet
  171. SET_INTR();
  172. return packet_index;
  173. }
  174. // Send data
  175. // XXX Not used with the Tandy1000
  176. uint8_t scan_sendData( uint8_t dataPayload )
  177. {
  178. return 0;
  179. }
  180. // Signal KeyIndex_Buffer that it has been properly read
  181. // TODO
  182. void scan_finishedWithBuffer( uint8_t sentKeys )
  183. {
  184. }
  185. // Signal that the keys have been properly sent over USB
  186. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  187. {
  188. }
  189. // Reset/Hold keyboard
  190. // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
  191. // The Tandy 1000 keyboard has a dedicated hold/processor interrupt line
  192. void scan_lockKeyboard( void )
  193. {
  194. UNSET_INTR();
  195. }
  196. void scan_unlockKeyboard( void )
  197. {
  198. SET_INTR();
  199. }
  200. // Reset Keyboard
  201. void scan_resetKeyboard( void )
  202. {
  203. // TODO Tandy1000 has a dedicated reset line
  204. }