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.

RowScanner_PinsBitwise.cpp 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "RowScanner_PinsBitwise.h"
  2. /*
  3. Strobes the row and reads the columns.
  4. Strobe is on for shortest possible time to preserve IR LED on DodoHand's optic switch.
  5. */
  6. uint8_t RowScanner_PinsBitwise::scan(uint16_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 all the port pins
  19. refColPort.read();
  20. /* shows strobing pin 1 and 2, but realy stobing 0 and 1 todo
  21. Keyboard.print(F(" strobePin="));
  22. Keyboard.print(strobePin);
  23. Keyboard.print(F(", "));
  24. */
  25. //strobe row off
  26. if (activeHigh)
  27. {
  28. refRowPort.setActivePinLow(strobePin);
  29. }
  30. else //activeLow
  31. {
  32. refRowPort.setActivePinHigh(strobePin);
  33. }
  34. return getRowState(rowEnd);
  35. }
  36. /*
  37. Copies column pins to rowState. Unused column pins are not copied.
  38. Sets rowEnd and returns rowState.
  39. rowEnd is a bitwise row mask, one col per bit, where active col bit is 1.
  40. At end of function, 1 bit marks place immediatly after last key of row.
  41. rowEnd is a larger type than portMask so that it can not overflow.
  42. */
  43. uint8_t RowScanner_PinsBitwise::getRowState(uint16_t& rowEnd)
  44. {
  45. uint8_t rowState = 0; //bitwise, one key per bit, 1 means key is pressed
  46. uint8_t portMask; //bitwise, 1 bit is a colPortState position
  47. rowEnd = 1;
  48. //bitwise colPins, 1 means pin is connected to column
  49. uint8_t colPins = refColPort.getColPins();
  50. //bitwise colPortState, pin values where set in ColPort::read(), get them now
  51. uint8_t colPortState = refColPort.getPortState();
  52. if (activeHigh)
  53. {
  54. colPortState = ~colPortState;
  55. }
  56. for ( portMask = 1; portMask > 0; portMask <<= 1 ) //shift portMask until overflow
  57. { //for each pin of col port
  58. if (portMask & colPins) //if pin is connected to column
  59. {
  60. if (portMask & ~colPortState) //if pin detected a key press
  61. {
  62. rowState |= rowEnd; //set rowState bit for that key
  63. }
  64. rowEnd <<= 1; //shift rowEnd to next key
  65. }
  66. }
  67. //todo Keyboard.print(rowState);
  68. return rowState;
  69. }