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.

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