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.

Row.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef ROW_H
  2. #define ROW_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 Row
  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. static const unsigned int DELAY_MICROSECONDS; //delay between each Row scan for debouncing
  50. uint8_t samples[SAMPLE_COUNT]; //bitwise, one bit per key, most resent readings
  51. uint8_t samplesIndex; //samples[] current write index
  52. uint8_t debounced; //bitwise, one bit per key, debounced value of readings
  53. virtual void keyWasPressed();
  54. public:
  55. Row( RowPort &refRowPort, const uint8_t rowPin,
  56. ColPort *const ptrsColPorts[], const uint8_t colPortCount,
  57. Key *const ptrsKeys[])
  58. : ptrsKeys(ptrsKeys), refRowPort(refRowPort), rowPin(rowPin),
  59. ptrsColPorts(ptrsColPorts), colPortCount(colPortCount),
  60. samplesIndex(0), debounced(0) { }
  61. Key* getPtrKey(uint8_t col) const;
  62. void process(const bool activeHigh);
  63. void scan(const bool activeHigh);
  64. uint8_t getRowState(uint16_t& rowEnd, const bool activeHigh);
  65. uint8_t debounce(const uint8_t rowState); //switch debouncer and I2C error correction
  66. void detectEdge(uint8_t newDebounced, uint8_t& isFallingEdge, uint8_t& isRisingEdge);
  67. void pressRelease(const uint16_t rowEnd, const uint8_t isFallingEdge, const uint8_t isRisingEdge);
  68. };
  69. #endif