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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* Copyright (C) 2012 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 DATA_PORT PORTC
  34. #define DATA_DDR DDRC
  35. #define DATA_PIN 7
  36. #define CLOCK_PORT PORTC
  37. #define CLOCK_DDR DDRC
  38. #define CLOCK_PIN 6
  39. #define RESET_PORT PORTF
  40. #define RESET_DDR DDRF
  41. #define RESET_PIN 7
  42. // ----- Macros -----
  43. // Make sure we haven't overflowed the buffer
  44. #define bufferAdd(byte) \
  45. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  46. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  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. volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
  52. volatile uint8_t currentWaveState = 0;
  53. // Buffer Signals
  54. volatile uint8_t BufferReadyToClear;
  55. // ----- Function Declarations -----
  56. void processKeyValue( uint8_t keyValue );
  57. void removeKeyValue( uint8_t keyValue );
  58. // ----- Interrupt Functions -----
  59. // Generates a constant external clock
  60. ISR( TIMER1_COMPA_vect )
  61. {
  62. if ( currentWaveState )
  63. {
  64. CLOCK_PORT &= ~(1 << CLOCK_PIN);
  65. currentWaveState--;
  66. }
  67. else
  68. {
  69. CLOCK_PORT |= (1 << CLOCK_PIN);
  70. currentWaveState++;
  71. }
  72. }
  73. // ----- Functions -----
  74. // Setup
  75. inline void scan_setup()
  76. {
  77. // Setup Timer Pulse (16 bit)
  78. // TODO Clock can be adjusted to whatever (read chip datasheets for limits)
  79. // 16 MHz / (2 * Prescaler * (1 + OCR1A)) = 1200.1 baud
  80. // Prescaler is 1
  81. // Twice every 1200 baud (actually 1200.1, timer isn't accurate enough)
  82. // This is close to 820 us, but a bit slower
  83. cli();
  84. TCCR1B = 0x09;
  85. OCR1AH = 0x01;
  86. OCR1AL = 0x09;
  87. TIMSK1 = (1 << OCIE1A);
  88. CLOCK_DDR = (1 << CLOCK_PIN);
  89. sei();
  90. // Initially buffer doesn't need to be cleared (it's empty...)
  91. BufferReadyToClear = 0;
  92. // InputSignal is off by default
  93. KeyIndex_Add_InputSignal = 0x00;
  94. // Reset the keyboard before scanning, we might be in a wierd state
  95. scan_resetKeyboard();
  96. }
  97. // Main Detection Loop
  98. // Since this function is non-interruptable, we can do checks here on what stage of the
  99. // output clock we are at (0 or 1)
  100. // We are looking for a start of packet
  101. // If detected, all subsequent bits are then logged into a variable
  102. // Once the end of the packet has been detected (always the same length), decode the pressed keys
  103. inline uint8_t scan_loop()
  104. {
  105. return 0;
  106. }
  107. void processKeyValue( uint8_t keyValue )
  108. {
  109. // Interpret scan code
  110. switch ( keyValue )
  111. {
  112. case 0x00: // Break code from input?
  113. break;
  114. default:
  115. // Make sure the key isn't already in the buffer
  116. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  117. {
  118. // Key isn't in the buffer yet
  119. if ( c == KeyIndex_BufferUsed )
  120. {
  121. bufferAdd( keyValue );
  122. // Only send data if enabled
  123. if ( KeyIndex_Add_InputSignal )
  124. scan_sendData( KeyIndex_Add_InputSignal );
  125. break;
  126. }
  127. // Key already in the buffer
  128. if ( KeyIndex_Buffer[c] == keyValue )
  129. break;
  130. }
  131. break;
  132. }
  133. }
  134. void removeKeyValue( uint8_t keyValue )
  135. {
  136. // Check for the released key, and shift the other keys lower on the buffer
  137. uint8_t c;
  138. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  139. {
  140. // Key to release found
  141. if ( KeyIndex_Buffer[c] == keyValue )
  142. {
  143. // Shift keys from c position
  144. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  145. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  146. // Decrement Buffer
  147. KeyIndex_BufferUsed--;
  148. break;
  149. }
  150. }
  151. // Error case (no key to release)
  152. if ( c == KeyIndex_BufferUsed + 1 )
  153. {
  154. errorLED( 1 );
  155. char tmpStr[6];
  156. hexToStr( keyValue, tmpStr );
  157. erro_dPrint( "Could not find key to release: ", tmpStr );
  158. }
  159. }
  160. // Send data
  161. uint8_t scan_sendData( uint8_t dataPayload )
  162. {
  163. return 0;
  164. }
  165. // Signal KeyIndex_Buffer that it has been properly read
  166. void scan_finishedWithBuffer( void )
  167. {
  168. }
  169. // Signal that the keys have been properly sent over USB
  170. void scan_finishedWithUSBBuffer( void )
  171. {
  172. }
  173. // Reset/Hold keyboard
  174. // NOTE: Does nothing with the BETKB
  175. void scan_lockKeyboard( void )
  176. {
  177. }
  178. // NOTE: Does nothing with the BETKB
  179. void scan_unlockKeyboard( void )
  180. {
  181. }
  182. // Reset Keyboard
  183. void scan_resetKeyboard( void )
  184. {
  185. // TODO Determine the scan period, and the interval to scan each bit
  186. }