keybrd library is an open source library for creating custom-keyboard firmware.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

RowBase.cpp 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Row::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 Row::DELAY_MICROSECONDS = 0;
  38. Slow-scan trick for debug messages that print too fast and not aligned, add this to sketch loop():
  39. delay(500);
  40. Keyboard.println(F(""));
  41. That way debug messages are printed at a managable rate, and each scan starts a new line.
  42. */
  43. void RowBase::wait()
  44. {
  45. delayMicroseconds(DELAY_MICROSECONDS); //delay between Row scans to debounce switches
  46. }
  47. /*
  48. pressRelease() calls key's press() or release() function if it was pressed or released.
  49. Both parameters are bitwise.
  50. rowEnd bit marks positioned immediatly after last key of row.
  51. */
  52. void RowBase::pressRelease(const uint16_t rowEnd, const uint8_t debouncedChanged)
  53. {
  54. uint8_t isFallingEdge; //1 means falling edge
  55. uint8_t isRisingEdge; //1 means rising edge
  56. uint8_t rowMask; //bitwise, active col bit is 1
  57. uint8_t col; //index for ptrsKeys[col] array
  58. //bit=1 if last debounced changed from 1 to 0, else bit=0
  59. isFallingEdge = debouncedChanged & ~debounced;
  60. //bit=1 if last debounced changed from 0 to 1, else bit=0
  61. isRisingEdge = debouncedChanged & debounced;
  62. for (rowMask=1, col=0; rowMask<rowEnd; rowMask<<=1, col++) //for each key in row
  63. {
  64. //release before press avoids impossible key sequence
  65. if (rowMask & isFallingEdge) //if key was released
  66. {
  67. ptrsKeys[col]->release();
  68. }
  69. if (rowMask & isRisingEdge) //if key was pressed
  70. {
  71. ptrsKeys[col]->press();
  72. keyWasPressed();
  73. }
  74. }
  75. }
  76. void RowBase::keyWasPressed()
  77. {
  78. //empty in RowBase class. To unstick sticky keys, override keyWasPressed() in derived class.
  79. }