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.cpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "Row.h"
  2. /* constructor
  3. init() is called once for each row, to set scanner's uC strobePin to output.
  4. */
  5. Row::Row(ScannerInterface& refScanner, const uint8_t strobePin,
  6. Key* const ptrsKeys[], const uint8_t keyCount)
  7. : refScanner(refScanner), strobePin(strobePin),
  8. ptrsKeys(ptrsKeys), keyCount(keyCount), debounced(0)
  9. {
  10. refScanner.init(strobePin);
  11. }
  12. /* process() scans the row and calls any newly pressed or released keys.
  13. Bit pattern variables are 1 bit per key.
  14. */
  15. void Row::process()
  16. {
  17. read_pins_t readState; //bits, 1 means key is pressed, 0 means released
  18. read_pins_t debouncedChanged; //bits, 1 means debounced changed
  19. readState = refScanner.scan(strobePin);
  20. debouncedChanged = debouncer.debounce(readState, debounced);
  21. send(keyCount, debouncedChanged);
  22. }
  23. /*
  24. send() calls key's press() or release() function if key was pressed or released.
  25. Parameter debouncedChanged is bit a pattern.
  26. */
  27. void Row::send(const uint8_t keyCount, const read_pins_t debouncedChanged)
  28. {
  29. read_pins_t isFallingEdge; //bits, 1 means falling edge
  30. read_pins_t isRisingEdge; //bits, 1 means rising edge
  31. read_pins_t readPosition; //bits, active bit is 1
  32. uint8_t i; //index for ptrsKeys[i] array
  33. //bit=1 if last debounced changed from 1 to 0, else bit=0
  34. isFallingEdge = debouncedChanged & ~debounced;
  35. //bit=1 if last debounced changed from 0 to 1, else bit=0
  36. isRisingEdge = debouncedChanged & debounced;
  37. for (readPosition=1, i=0; i < keyCount; readPosition<<=1, i++) //for each key in row
  38. {
  39. //release before press avoids impossible key sequence
  40. if (readPosition & isFallingEdge) //if key was released
  41. {
  42. ptrsKeys[i]->release();
  43. }
  44. if (readPosition & isRisingEdge) //if key was pressed
  45. {
  46. ptrsKeys[i]->press();
  47. keyWasPressed();
  48. }
  49. }
  50. }
  51. void Row::keyWasPressed()
  52. {
  53. //empty in Row class. To unstick sticky keys, override keyWasPressed() in derived Row class.
  54. }