keybrd library is an open source library for creating custom-keyboard firmware.
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* debounce() function
  2. Debounce uses multiple samples to debounces switch states,
  3. where each sample contains the switch states for a row of switches, one bit per switch.
  4. Debounce uses Dr. Marty's debounce algorithm from
  5. http://drmarty.blogspot.com.br/2009/05/best-switch-debounce-routine-ever.html
  6. I2C and TWI protocols do not include any Packet Error Checking (PEC).
  7. The goal of Marty's debounce routine is to reject spurious signals,
  8. which is useful when connecting split keyboards with a cable using I2C or TWI.
  9. Was tested on split keyboard with 3-meter long telephone wire to I/O expander
  10. Dr. Marty's debounce algorithm:
  11. Periodically read samples and update the state when a number consecutive sample bits are equal.
  12. Output from keybrd/examples/debounce_unit_test.cpp with SAMPLE_COUNT 4:
  13. button pressed: 100000001111111110000
  14. bouncy signal: 100001001111011110000
  15. debounced signal: 000000000001111111110
  16. isFallingEdge: 000000000000000000001
  17. isRisingEdge: 000000000001000000000
  18. There is a latency equal to SAMPLE_COUNT, between button press and debounced signal.
  19. samples[SAMPLE_COUNT] is a ring buffer. samplesIndex is it's current write index.
  20. SAMPLE_COUNT is the number of consecutive equal samples needed to debounce.
  21. SAMPLE_COUNT is a macro because it defines samples[SAMPLE_COUNT] array size at compile time.
  22. SAMPLE_COUNT should be at lease 1.
  23. Keyboards with a long I2C wire or in environment with strong electromagnetic interference (EMI)
  24. need a larger SAMPLE_COUNT for reliability.
  25. Larger SAMPLE_COUNTs are more reliable but consume more memory, where
  26. SAMPLE_COUNT*ROW_COUNT = bytes of memory consumed by keyboard
  27. SAMPLE_COUNT = 4 is very reliable for a keyboard.
  28. */
  29. /* debounce() function
  30. Parameter rowState is bitwise, 1 means pressed, 0 means released.
  31. Returns bitwise debouncedChanged.
  32. */
  33. #include "Row.h"
  34. uint8_t Row::debounce(const uint8_t rowState)
  35. {
  36. uint8_t debounced; //bitwise, 1 means pressed, 0 means released
  37. uint8_t debouncedChanged; //bitwise, 1 means debounced changed
  38. uint8_t all_1 = ~0; //bitwise
  39. uint8_t all_0 = 0; //bitwise
  40. samples[samplesIndex] = rowState; //insert rowState into samples[] ring buffer
  41. if (++samplesIndex >= SAMPLE_COUNT) //if end of ring buffer
  42. {
  43. samplesIndex = 0; //wrap samplesIndex to beginning of ring buffer
  44. }
  45. for (uint8_t j = 0; j < SAMPLE_COUNT; j++) //traverse the sample[] ring buffer
  46. {
  47. all_1 &= samples[j]; //1 if all samples are 1
  48. all_0 |= samples[j]; //0 if all samples are 0
  49. }
  50. // update newDebounce if all the samples agree with one another
  51. // if all samples=1 then debounced=1
  52. // elseif all samples=0 then debounced=0
  53. // else debounced=previousDebounced i.e. no change
  54. debounced = all_1 | (all_0 & previousDebounced);
  55. debouncedChanged = debounced xor previousDebounced;
  56. previousDebounced = debounced;
  57. return debouncedChanged;
  58. }