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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (C) 2011-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. // 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. #include "matrix_scan.h"
  30. // ----- Defines -----
  31. // Debouncing Defines
  32. // Old
  33. //#define SAMPLE_THRESHOLD 110
  34. //#define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
  35. #define SAMPLE_THRESHOLD 6
  36. #define MAX_SAMPLES 10 // Max is 127, reaching 128 is very bad
  37. // ----- Macros -----
  38. // Make sure we haven't overflowed the buffer
  39. #define bufferAdd(byte) \
  40. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  41. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  42. // ----- Variables -----
  43. // Buffer used to inform the macro processing module which keys have been detected as pressed
  44. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  45. volatile uint8_t KeyIndex_BufferUsed;
  46. volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
  47. // Keeps track of the number of scans, so we only do a debounce assess when it would be valid (as it throws away data)
  48. uint8_t scan_count = 0;
  49. // This is where the matrix scan data is held, as well as debouncing is evaluated to, which (depending on the read value) is handled
  50. // by the macro module
  51. uint8_t KeyIndex_Array[KEYBOARD_KEYS + 1];
  52. // ----- Functions -----
  53. // Setup
  54. inline void scan_setup()
  55. {
  56. matrix_pinSetup( (uint8_t*)matrix_pinout, scanMode );
  57. }
  58. // Main Detection Loop
  59. inline uint8_t scan_loop()
  60. {
  61. // Check count to see if the sample threshold may have been reached, otherwise collect more data
  62. if ( scan_count < MAX_SAMPLES )
  63. {
  64. matrix_scan( (uint8_t*)matrix_pinout, KeyIndex_Array );
  65. // scanDual requires 2 passes, and thus needs more memory per matrix_scan pass
  66. #if scanMode == scanDual
  67. scan_count += 2;
  68. #else
  69. scan_count++;
  70. #endif
  71. // Signal Main Detection Loop to continue scanning
  72. return 0;
  73. }
  74. // Reset Sample Counter
  75. scan_count = 0;
  76. // Assess debouncing sample table
  77. // Loop over all of the sampled keys of the given array
  78. // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
  79. // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
  80. for ( uint8_t key = 1; key < KeyIndex_Size + 1; key++ ) if ( ( KeyIndex_Array[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD )
  81. {
  82. // Debug output (keypress detected)
  83. char tmpStr[6];
  84. hexToStr( key, tmpStr );
  85. dPrintStrs( tmpStr, " " );
  86. // Add the key to the buffer, if it isn't already in the current Key Buffer
  87. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  88. {
  89. // Key isn't in the buffer yet
  90. if ( c == KeyIndex_BufferUsed )
  91. {
  92. bufferAdd( key );
  93. break;
  94. }
  95. // Key already in the buffer
  96. if ( KeyIndex_Buffer[c] == key )
  97. break;
  98. }
  99. KeyIndex_Array[key] = (1 << 7);
  100. }
  101. else
  102. {
  103. // Remove the key from the buffer only if it was previously known to be pressed
  104. if ( KeyIndex_Array[key] & (1 << 7 ) )
  105. {
  106. // Check for the released key, and shift the other keys lower on the buffer
  107. for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
  108. {
  109. // Key to release found
  110. if ( KeyIndex_Buffer[c] == key )
  111. {
  112. // Shift keys from c position
  113. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  114. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  115. // Decrement Buffer
  116. KeyIndex_BufferUsed--;
  117. break;
  118. }
  119. }
  120. }
  121. KeyIndex_Array[key] = 0x00;
  122. }
  123. // Ready to allow for USB send
  124. return 1;
  125. }
  126. // Signal that the keys have been properly sent over USB
  127. inline void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  128. {
  129. return;
  130. }
  131. // Signal KeyIndex_Buffer that it has been fully scanned using the macro module
  132. inline void scan_finishedWithBuffer( uint8_t sentKeys )
  133. {
  134. return;
  135. }
  136. // Send data to keyboard
  137. // Not used in this module
  138. uint8_t scan_sendData( uint8_t dataPayload )
  139. {
  140. return 0;
  141. }