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.

RowBase.cpp 3.2KB

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