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.h 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef ROWBASE_H
  2. #define ROWBASE_H
  3. #include <Arduino.h>
  4. #include <inttypes.h>
  5. #include <Key.h>
  6. #include <RowPort.h>
  7. #include <ColPort.h>
  8. #define SAMPLE_COUNT 4 //number of consecutive equal bits needed to change a debounced bit
  9. /*
  10. Instantiation
  11. ------------
  12. Example instantiation of a row:
  13. RowPort_AVR rowPortF(DDRF, PORTF);
  14. ColPort_AVR colPortB(DDRB, PORTB, PINB, 1<<0 | 1<<1 | 1<<2 | 1<<3 );
  15. ColPort_AVR colPortD(DDRD, PORTD, PIND, 1<<2 | 1<<3 );
  16. ColPort* const ptrsColPorts[] = { &colPortB, &colPortD };
  17. const uint8_t COL_PORTS_COUNT = sizeof(ptrsColPorts)/sizeof(*ptrsColPorts);
  18. const PROGMEM Key* const ptrsKeys_0[] = { &k_00, &k_01, &k_02, &k_03, &k_04, &k_05 };
  19. Row row_0(ptrsKeys_0, &rowPortF, 1<<0, ptrsColPorts, COL_PORTS_COUNT);
  20. Number of ColPort::colPins should equal number of keys in Row::ptrsKeys array
  21. if a pin is missing, a key will be unresposive
  22. if a Key pointer is missing, the keyboard will fail in an unprdictable way
  23. A keyboard with a faster scan rate is more resposive.
  24. Follow these step to tune DELAY_MICROSECONDS for maximum scan rate within debounce times:
  25. Initialize DELAY_MICROSECONDS in your sketch:
  26. const unsigned int Row::DELAY_MICROSECONDS = 1000;
  27. Add this to the sketche's loop() function:
  28. keybrd.print_microseconds_per_scan();
  29. Compile and load the sketch into the microcontroller, which will print the actual microseconds_per_scan
  30. Incrementaly adjust the DELAY_MICROSECONDS untill the printed microseconds_per_scan is near the switches bounce time
  31. A switche's debounce time can be obtained from the switche's datasheet
  32. Cherry MX has 5ms bounce time http://www.cherrycorp.com/english/switches/key/mx.htm
  33. hasu measured Cherry MX bounce times .3ms to 1.4ms http://geekhack.org/index.php?topic=42385.0
  34. Tactile switch MJTP series bounce 10 ms http://www.apem.com/files/apem/brochures/MJTP_6MM.pdf
  35. Optic switches 0 bounce time because optic doesn't bounce
  36. Slow-scan trick for debug message that print too fast
  37. Keyboard.print(F("debug message"));
  38. Change DELAY_MICROSECONDS to a large number like 10000
  39. That way printing debug messages is slowed to a managable rate
  40. */
  41. class RowBase
  42. {
  43. private:
  44. Key *const *const ptrsKeys; //array of Key pointers
  45. RowPort &refRowPort; //this row's IC port
  46. const uint8_t rowPin; //bitwise, 1 indicates IC pin connected to this row
  47. ColPort *const *const ptrsColPorts; //array of column ports
  48. const uint8_t colPortCount;
  49. void scan(const bool activeHigh);
  50. uint8_t getRowState(uint16_t& rowEnd, const bool activeHigh);
  51. virtual uint8_t debounce(const uint8_t rowState)=0; //debouncer and I2C error correction
  52. void detectEdge(uint8_t newDebounced, uint8_t& isFallingEdge, uint8_t& isRisingEdge);
  53. void pressRelease(const uint16_t rowEnd,
  54. const uint8_t isFallingEdge, const uint8_t isRisingEdge);
  55. virtual void keyWasPressed();
  56. protected:
  57. uint8_t debounced; //bitwise, one bit per key, debounced value of readings
  58. public:
  59. RowBase( RowPort &refRowPort, const uint8_t rowPin,
  60. ColPort *const ptrsColPorts[], const uint8_t colPortCount,
  61. Key *const ptrsKeys[])
  62. : ptrsKeys(ptrsKeys), refRowPort(refRowPort), rowPin(rowPin),
  63. ptrsColPorts(ptrsColPorts), colPortCount(colPortCount),
  64. debounced(0) { }
  65. //Key* getPtrKey(uint8_t col) const; todo delete, no longer needed 5/31/16
  66. void process(const bool activeHigh);
  67. };
  68. #endif