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.

133 lines
8.3 KiB

  1. # Contact bounce / contact chatter
  2. Mechanical switches often don't have a clean single transition between pressed and unpressed states.
  3. In an ideal world, when you press a switch, you would expect the digital pin to see something like this:
  4. (X axis showing time
  5. ```
  6. voltage +----------------------
  7. ^ |
  8. | |
  9. | ------------------+
  10. ----> time
  11. ```
  12. However in the real world you will actually see contact bounce, which will look like multiple 1->0 and 0->1 transitions,
  13. until the value finally settles.
  14. ```
  15. +-+ +--+ +-------------
  16. | | | | |
  17. | | | | |
  18. +-----------------+ +-+ +-+
  19. ```
  20. The time it takes for the switch to settle might vary with switch type, age, and even pressing technique.
  21. If the device chooses not to mitigate contact bounce, then often actions that happen when the switch is pressed are repeated
  22. multiple times.
  23. There are many ways to handle contact bounce ("Debouncing"). Some include employing additional hardware, for example an RC filter,
  24. while there are various ways to do debouncing in software too, often called debounce algorithms. This page discusses software
  25. debouncing methods available in QMK.
  26. While technically not considered contact bounce/contact chatter, some switch technologies are susceptible to noise, meaning,
  27. while the key is not changing state, sometimes short random 0->1 or 1->0 transitions might be read by the digital circuit, for example:
  28. ```
  29. +-+
  30. | |
  31. | |
  32. +-----------------+ +--------------------
  33. ```
  34. Many debounce methods (but not all) will also make the device resistant to noise. If you are working with a technology that is
  35. susceptible to noise, you must choose a debounce method that will also mitigate noise for you.
  36. ## Types of debounce algorithms
  37. 1) Unit of time: Timestamp (milliseconds) vs Cycles (scans)
  38. * Debounce algorithms often have a 'debounce time' parameter, that specifies the maximum settling time of the switch contacts.
  39. This time might be measured in various units:
  40. * Cycles-based debouncing waits n cycles (scans), decreasing count by one each matrix_scan
  41. * Timestamp-based debouncing stores the millisecond timestamp a change occurred, and does substraction to figure out time elapsed.
  42. * Timestamp-based debouncing is usually superior, especially in the case of noise-resistant devices because settling times of physical
  43. switches is specified in units of time, and should not depend on the matrix scan-rate of the keyboard.
  44. * Cycles-based debouncing is sometimes considered inferior, because the settling time that it is able to compensate for depends on the
  45. performance of the matrix scanning code. If you use cycles-based debouncing, and you significantly improve the performance of your scanning
  46. code, you might end up with less effective debouncing. A situation in which cycles-based debouncing might be preferable is when
  47. noise is present, and the scanning algorithm is slow, or variable speed. Even if your debounce algorithm is fundamentally noise-resistant,
  48. if the scanning is slow, and you are using a timestamp-based algorithm, you might end up making a debouncing decision based on only two
  49. sampled values, which will limit the noise-resistance of the algorithm.
  50. * Currently all built-in debounce algorithms support timestamp-based debouncing only. In the future we might
  51. implement cycles-based debouncing, and it will be selectable via a ```config.h``` macro.
  52. 2) Symmetric vs Asymmetric
  53. * Symmetric - apply the same debouncing algorithm, to both key-up and key-down events.
  54. * Recommended naming convention: ```sym_*```
  55. * Asymmetric - apply different debouncing algorithms to key-down and key-up events. E.g. Eager key-down, Defer key-up.
  56. * Recommended naming convention: ```asym_*``` followed by details of the type of algorithm in use, in order, for key-down and then key-up
  57. 3) Eager vs Defer
  58. * Eager - any key change is reported immediately. All further inputs for DEBOUNCE ms are ignored.
  59. * Eager algorithms are not noise-resistant.
  60. * Recommended naming conventions:
  61. * ```sym_eager_*```
  62. * ```asym_eager_*_*```: key-down is using eager algorithm
  63. * ```asym_*_eager_*```: key-up is using eager algorithm
  64. * Defer - wait for no changes for DEBOUNCE ms before reporting change.
  65. * Defer algorithms are noise-resistant
  66. * Recommended naming conventions:
  67. * ```sym_defer_*```
  68. * ```asym_defer_*_*```: key-down is using defer algorithm
  69. * ```asym_*_defer_*```: key-up is using defer algorithm
  70. 4) Global vs Per-Key vs Per-Row
  71. * Global - one timer for all keys. Any key change state affects global timer
  72. * Recommended naming convention: ```*_g```
  73. * Per-key - one timer per key
  74. * Recommended naming convention: ```*_pk```
  75. * Per-row - one timer per row
  76. * Recommended naming convention: ```*_pr```
  77. * Per-key and per-row algorithms consume more resources (in terms of performance,
  78. and ram usage), but fast typists might prefer them over global.
  79. ## Debounce algorithms supported by QMK
  80. QMK supports multiple debounce algorithms through its debounce API.
  81. ### Debounce selection
  82. | DEBOUNCE_TYPE | Description | What else is needed |
  83. | ------------- | --------------------------------------------------- | ----------------------------- |
  84. | Not defined | Use the default algorithm, currently sym_defer_g | Nothing |
  85. | custom | Use your own debounce code | ```SRC += debounce.c``` add your own debounce.c and implement necessary functions |
  86. | Anything Else | Use another algorithm from quantum/debounce/* | Nothing |
  87. **Regarding split keyboards**:
  88. The debounce code is compatible with split keyboards.
  89. ### Selecting an included debouncing method
  90. Keyboards may select one of the already implemented debounce methods, by adding to ```rules.mk``` the following line:
  91. ```
  92. DEBOUNCE_TYPE = <name of algorithm>
  93. ```
  94. Where name of algorithm is one of:
  95. * ```sym_defer_g``` - debouncing per keyboard. On any state change, a global timer is set. When ```DEBOUNCE``` milliseconds of no changes has occurred, all input changes are pushed.
  96. * This is the current default algorithm. This is the highest performance algorithm with lowest memory usage, and it's also noise-resistant.
  97. * ```sym_eager_pr``` - debouncing per row. On any state change, response is immediate, followed by locking the row ```DEBOUNCE``` milliseconds of no further input for that row.
  98. For use in keyboards where refreshing ```NUM_KEYS``` 8-bit counters is computationally expensive / low scan rate, and fingers usually only hit one row at a time. This could be
  99. appropriate for the ErgoDox models; the matrix is rotated 90°, and hence its "rows" are really columns, and each finger only hits a single "row" at a time in normal use.
  100. * ```sym_eager_pk``` - debouncing per key. On any state change, response is immediate, followed by ```DEBOUNCE``` milliseconds of no further input for that key
  101. * ```sym_defer_pk``` - debouncing per key. On any state change, a per-key timer is set. When ```DEBOUNCE``` milliseconds of no changes have occurred on that key, the key status change is pushed.
  102. * ```asym_eager_defer_pk``` - debouncing per key. On a key-down state change, response is immediate, followed by ```DEBOUNCE``` milliseconds of no further input for that key. On a key-up state change, a per-key timer is set. When ```DEBOUNCE``` milliseconds of no changes have occurred on that key, the key-up status change is pushed.
  103. ### A couple algorithms that could be implemented in the future:
  104. * ```sym_defer_pr```
  105. * ```sym_eager_g```
  106. ### Use your own debouncing code
  107. You have the option to implement you own debouncing algorithm. To do this:
  108. * Set ```DEBOUNCE_TYPE = custom``` in ```rules.mk```.
  109. * Add ```SRC += debounce.c``` in ```rules.mk```
  110. * Add your own ```debounce.c```. Look at current implementations in ```quantum/debounce``` for examples.
  111. * Debouncing occurs after every raw matrix scan.
  112. * Use num_rows rather than MATRIX_ROWS, so that split keyboards are supported correctly.
  113. * If the algorithm might be applicable to other keyboards, please consider adding it to ```quantum/debounce```