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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // ----- Includes -----
  22. // AVR Includes
  23. #include <avr/interrupt.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. #define SAMPLE_THRESHOLD 110
  33. #define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
  34. // ----- Macros -----
  35. // Make sure we haven't overflowed the buffer
  36. #define bufferAdd(byte) \
  37. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  38. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  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. // Keeps track of the number of scans, so we only do a debounce assess when it would be valid (as it throws away data)
  44. uint8_t scan_count = 0;
  45. // This is where the matrix scan data is held, as well as debouncing is evaluated to, which (depending on the read value) is handled
  46. // by the macro module
  47. uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
  48. // ----- Functions -----
  49. // Setup
  50. inline void scan_setup()
  51. {
  52. matrix_pinSetup( (uint8_t*)matrix_pinout, scanMode );
  53. }
  54. // Main Detection Loop
  55. inline uint8_t scan_loop()
  56. {
  57. // Check count to see if the sample threshold may have been reached, otherwise collect more data
  58. if ( scan_count < MAX_SAMPLES )
  59. {
  60. matrix_scan( (uint8_t*)matrix_pinout, KeyIndex_Array );
  61. // scanDual requires 2 passes, and thus needs more memory per matrix_scan pass
  62. #if scanMode == scanDual
  63. scan_count += 2;
  64. #else
  65. scan_count++;
  66. #endif
  67. // Signal Main Detection Loop to continue scanning
  68. return 0;
  69. }
  70. // Reset Sample Counter
  71. scan_count = 0;
  72. // Assess debouncing sample table
  73. // Loop over all of the sampled keys of the given array
  74. // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
  75. // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
  76. for ( uint8_t key = 1; key < KeyIndex_Size + 1; key++ ) if ( ( KeyIndex_Array[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD )
  77. {
  78. bufferAdd( key );
  79. KeyIndex_Array[key] = (1 << 7);
  80. }
  81. else
  82. {
  83. KeyIndex_Array[key] = 0x00;
  84. }
  85. // Ready to allow for USB send
  86. return 1;
  87. }