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.

Debouncer_Samples.cpp 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. SPI, 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 SPI, I2C, or TWI.
  9. This class was tested on split keyboard with a 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_MACRO 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_MACRO, between button press and debounced signal.
  19. samples[SAMPLE_COUNT_MACRO] is a ring buffer. samplesIndex is it's current write index.
  20. SAMPLE_COUNT_MACRO is the number of consecutive equal samples needed to debounce.
  21. SAMPLE_COUNT_MACRO is a macro because it defines samples[SAMPLE_COUNT_MACRO] array size at compile time.
  22. SAMPLE_COUNT_MACRO is defined in config_keybrd.h and should be at lease 1.
  23. SAMPLE_COUNT_MACRO = 4 is very reliable for a keyboard.
  24. Split keyboards with a long connecting wire or in environment with
  25. strong electromagnetic interference (EMI) may need a larger SAMPLE_COUNT_MACRO for reliability.
  26. */
  27. #include "Debouncer_Samples.h"
  28. /* debounce() sets debounced and returns debouncedChanged.
  29. All parameters and variables are bit patterns.
  30. For parameters, 1 means pressed, 0 means released.
  31. For return, 1 means debounced changed.
  32. */
  33. read_pins_t Debouncer_Samples::debounce(const read_pins_t rawSignal, read_pins_t& debounced)
  34. {
  35. read_pins_t previousDebounced; //bits, 1 means pressed, 0 means released
  36. read_pins_t all_1 = ~0; //bits
  37. read_pins_t all_0 = 0; //bits
  38. samples[samplesIndex] = rawSignal; //insert rawSignal into samples[] ring buffer
  39. if (++samplesIndex >= SAMPLE_COUNT_MACRO) //if end of ring buffer
  40. {
  41. samplesIndex = 0; //wrap samplesIndex to beginning of ring buffer
  42. }
  43. for (uint8_t j = 0; j < SAMPLE_COUNT_MACRO; j++) //traverse the sample[] ring buffer
  44. {
  45. all_1 &= samples[j]; //1 if all samples are 1
  46. all_0 |= samples[j]; //0 if all samples are 0
  47. }
  48. previousDebounced = debounced;
  49. // update debounced if all the samples agree with one another
  50. // if all samples=1 then debounced=1
  51. // elseif all samples=0 then debounced=0
  52. // else debounced=previousDebounced i.e. no change
  53. debounced = all_1 | (all_0 & previousDebounced);
  54. return debounced xor previousDebounced;
  55. }