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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "RowBase.h"
  2. /*
  3. process() scans the row and calls any newly pressed or released keys.
  4. */
  5. void RowBase::process()
  6. {
  7. //these variables are all bitwise, one bit per key
  8. uint8_t rowState; //1 means pressed, 0 means released
  9. uint16_t rowEnd; //1 bit marks positioned after last key of row
  10. uint8_t debouncedChanged; //1 means debounced changed
  11. wait();
  12. rowState = scan(rowEnd);
  13. debouncedChanged = debounce(rowState, debounced);
  14. pressRelease(rowEnd, debouncedChanged);
  15. }
  16. /* wait() delay's scan to give switches time to debounce.
  17. This version of wait() is very simple. More sophisticated versions can override this one.
  18. For fastest response time, wait() should be placed before scan() or after pressRelease()
  19. (waiting between strobe and send would unnecessarily delay send).
  20. A keyboard with a faster scan rate responds faster.
  21. Follow these step to tune DELAY_MICROSECONDS for maximum scan rate for a given SAMPLE_COUNT:
  22. Initialize DELAY_MICROSECONDS in your sketch:
  23. const unsigned int RowBase::DELAY_MICROSECONDS = 1000;
  24. Add this to the sketch's loop() function:
  25. debug.print_microseconds_per_scan();
  26. Compile and load the sketch into the microcontroller; microseconds_per_scan is printed every second.
  27. Adjust the value of DELAY_MICROSECONDS and repeat until:
  28. debug.print_microseconds_per_scan() <= DEBOUNCE_TIME / SAMPLE_COUNT
  29. DEBOUNCE_TIME can be obtained from the switch's datasheet. Some switch bounce times are:
  30. Cherry MX specifies 5msec bounce time http://www.cherrycorp.com/english/switches/key/mx.htm
  31. hasu measured Cherry MX bounce times .3ms to 1.4ms http://geekhack.org/index.php?topic=42385.0
  32. Tactile switch MJTP series bounce 10 ms http://www.apem.com/files/apem/brochures/MJTP_6MM.pdf
  33. Avoid sampling the switch input at a rate synchronous to events in the environment
  34. that might create periodic EMI. For instance, 50 and 60 Hz.
  35. The largest allowable DELAY_MICROSECONDS is 65535 (.065535 seconds).
  36. Polling I2C may slow the scan rate enough so that no additional delay is needed:
  37. const unsigned int RowBase::DELAY_MICROSECONDS = 0;
  38. */
  39. void RowBase::wait()
  40. {
  41. delayMicroseconds(DELAY_MICROSECONDS); //delay between Row scans to debounce switches
  42. }
  43. /*
  44. pressRelease() calls key's press() or release() function if it was pressed or released.
  45. Both parameters are bitwise.
  46. rowEnd bit marks positioned immediatly after last key of row.
  47. */
  48. void RowBase::pressRelease(const uint16_t rowEnd, const uint8_t debouncedChanged)
  49. {
  50. uint8_t isFallingEdge; //1 means falling edge
  51. uint8_t isRisingEdge; //1 means rising edge
  52. uint8_t rowMask; //bitwise, active col bit is 1
  53. uint8_t col; //index for ptrsKeys[col] array
  54. //bit=1 if last debounced changed from 1 to 0, else bit=0
  55. isFallingEdge = debouncedChanged & ~debounced;
  56. //bit=1 if last debounced changed from 0 to 1, else bit=0
  57. isRisingEdge = debouncedChanged & debounced;
  58. for (rowMask=1, col=0; rowMask<rowEnd; rowMask<<=1, col++) //for each key in row
  59. {
  60. //release before press avoids impossible key sequence
  61. if (rowMask & isFallingEdge) //if key was released
  62. {
  63. ptrsKeys[col]->release();
  64. }
  65. if (rowMask & isRisingEdge) //if key was pressed
  66. {
  67. ptrsKeys[col]->press();
  68. keyWasPressed();
  69. }
  70. }
  71. }
  72. void RowBase::keyWasPressed()
  73. {
  74. //empty in RowBase class. To unstick sticky keys, override keyWasPressed() in derived class.
  75. }