keybrd library is an open source library for creating custom-keyboard firmware.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

RowScanner_PinsBitwise.cpp 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "RowScanner_PinsBitwise.h"
  2. /*
  3. Strobes the row and reads the columns.
  4. Sets rowEnd and returns rowState.
  5. */
  6. read_pins_t RowScanner_PinsBitwise::scan(read_pins_mask_t& rowEnd)
  7. {
  8. //strobe row on
  9. if (activeHigh)
  10. {
  11. refRowPort.setActivePinHigh(strobePin);
  12. }
  13. else //activeLow
  14. {
  15. refRowPort.setActivePinLow(strobePin);
  16. }
  17. delayMicroseconds(3); //time to stablize voltage
  18. //read the port pins
  19. refColPort.read();
  20. //strobe row off
  21. if (activeHigh)
  22. {
  23. refRowPort.setActivePinLow(strobePin);
  24. }
  25. else //activeLow
  26. {
  27. refRowPort.setActivePinHigh(strobePin);
  28. }
  29. return getRowState(rowEnd);
  30. }
  31. /*
  32. Copies column pins to rowState. Unused column pins are not copied.
  33. Sets rowEnd and returns rowState.
  34. rowEnd is a bitwise row mask, one col per bit, where active col bit is 1.
  35. At end of function, 1 bit marks place immediatly after last key of row.
  36. rowEnd is a larger type than portMask so that it can not overflow.
  37. */
  38. uint8_t RowScanner_PinsBitwise::getRowState(read_pins_mask_t& rowEnd)
  39. {
  40. uint8_t rowState = 0; //bitwise, one key per bit, 1 means key is pressed
  41. uint8_t portMask; //bitwise, 1 bit is a colPortState position
  42. rowEnd = 1;
  43. //bitwise colPins, 1 means pin is connected to column
  44. uint8_t colPins = refColPort.getColPins();
  45. //bitwise colPortState, pin values where set in ColPort::read(), get them now
  46. uint8_t colPortState = refColPort.getPortState();
  47. if (activeHigh)
  48. {
  49. colPortState = ~colPortState;
  50. }
  51. for ( portMask = 1; portMask > 0; portMask <<= 1 ) //shift portMask until overflow
  52. { //for each pin of col port
  53. if (portMask & colPins) //if pin is connected to column
  54. {
  55. if (portMask & ~colPortState) //if pin detected a key press
  56. {
  57. rowState |= rowEnd; //set rowState bit for that key
  58. }
  59. rowEnd <<= 1; //shift rowEnd to next key
  60. }
  61. }
  62. return rowState;
  63. }