keybrd library is an open source library for creating custom-keyboard firmware.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

RowBase.cpp 3.3KB

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