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

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