Kiibohd Controller
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

scan_loop.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include <stdint.h>
  22. #include <usb_keyboard_debug.h>
  23. #include <keymap.h>
  24. // Debouncing Defines
  25. #define SAMPLE_THRESHOLD 110
  26. #define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
  27. // Loop over all of the sampled keys of the given array
  28. // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
  29. // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
  30. #define DEBOUNCE_ASSESS(table,size) \
  31. for ( uint8_t key = 1; key < size + 1; key++ ) {\
  32. table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
  33. } \
  34. // NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
  35. // Other Bits: Pressed state sample counter
  36. #define KEYBOARD_SIZE 23
  37. uint8_t keyboardDetectArray[KEYBOARD_SIZE + 1];
  38. // Interrupt Variable
  39. volatile uint8_t sendKeypresses = 0;
  40. // USB Data Send
  41. void usb_send( uint8_t validKeys )
  42. {
  43. // TODO undo potentially old keys
  44. for ( uint8_t c = validKeys; c < 6; c++ )
  45. keyboard_keys[c] = 0;
  46. // Send keypresses
  47. usb_keyboard_send();
  48. // Clear sendKeypresses Flag
  49. sendKeypresses = 0;
  50. // Clear modifiers
  51. keyboard_modifier_keys = 0;
  52. }
  53. // Given a sampling array, and the current number of detected keypress
  54. // Add as many keypresses from the sampling array to the USB key send array as possible.
  55. void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map ) {
  56. for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
  57. if ( keys[key] & (1 << 7) ) {
  58. pint8( key );
  59. //print(" ");
  60. uint8_t modFound = 0;
  61. // Determine if the key is a modifier
  62. for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
  63. // Modifier found
  64. if ( modifiers[mod] == key ) {
  65. keyboard_modifier_keys |= map[key];
  66. modFound = 1;
  67. break;
  68. }
  69. }
  70. if ( modFound )
  71. continue;
  72. // Too many keys
  73. if ( *validKeys == 6 )
  74. break;
  75. // Allow ignoring keys with 0's
  76. if ( map[key] != 0 )
  77. keyboard_keys[(*validKeys)++] = map[key];
  78. }
  79. }
  80. }
  81. // Main Detection Loop
  82. void scan_loop( void )
  83. {
  84. //matrix_pinSetup( matrix_pinout );
  85. uint8_t count = 0;
  86. for ( ;; ) {
  87. //matrix_scan( matrix_pinout, keyboardDetectArray );
  88. // Check count to see if the sample threshold may have been reached, otherwise collect more data
  89. if ( count++ < MAX_SAMPLES )
  90. continue;
  91. // Reset Sample Counter
  92. count = 0;
  93. // Assess debouncing sample table
  94. //DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
  95. // Send keypresses over USB if the ISR has signalled that it's time
  96. if ( !sendKeypresses )
  97. continue;
  98. // Layout Setup
  99. uint8_t validKeys = 0;
  100. uint8_t *keyboard_MODMASK = keyboard_modifierMask;
  101. uint8_t keyboard_NUMMODS = MODIFIERS_KEYBOARD;
  102. uint8_t *keyboard_MAP = defaultMap;
  103. // TODO Layout Switching
  104. // TODO Macro Processing
  105. // Debounce Sampling Array to USB Data Array
  106. keyPressDetection( keyboardDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_MODMASK, keyboard_NUMMODS, keyboard_MAP );
  107. // Send USB Data
  108. usb_send( validKeys );
  109. }
  110. }