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

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