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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "RowScanner_BitManipulation.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_BitManipulation::scan(uint16_t& rowEnd)
  7. {
  8. //strobe row on
  9. if (activeHigh)
  10. {
  11. refRowPort.setActivePinHigh(rowPin);
  12. }
  13. else //activeLow
  14. {
  15. refRowPort.setActivePinLow(rowPin);
  16. }
  17. //read all the column ports
  18. for (uint8_t i=0; i < colPortCount; i++)
  19. {
  20. ptrsColPorts[i]->read();
  21. }
  22. //strobe row off
  23. if (activeHigh)
  24. {
  25. refRowPort.setActivePinLow(rowPin);
  26. }
  27. else //activeLow
  28. {
  29. refRowPort.setActivePinHigh(rowPin);
  30. }
  31. return getRowState(rowEnd);
  32. }
  33. /*
  34. Copies column pins to rowState. Unused column pins are not copied.
  35. Sets rowEnd and returns rowState.
  36. rowEnd is a bitwise row mask, one col per bit, where active col bit is 1.
  37. At end of function, 1 bit marks place immediatly after last key of row.
  38. rowEnd is a larger type than portMask so that it can not overflow.
  39. */
  40. uint8_t RowScanner_BitManipulation::getRowState(uint16_t& rowEnd)
  41. {
  42. uint8_t rowState = 0; //bitwise, one key per bit, 1 means key is pressed
  43. rowEnd = 1;
  44. for (uint8_t i=0; i < colPortCount; i++) //for each col port
  45. {
  46. //bitwise colPins, 1 means pin is connected to column
  47. uint8_t colPins = ptrsColPorts[i]->getColPins();
  48. //bitwise colPortState, pin values where set in ColPort::read(), get them now
  49. uint8_t colPortState = ptrsColPorts[i]->getPortState();
  50. if (activeHigh)
  51. {
  52. colPortState = ~colPortState;
  53. }
  54. for ( uint8_t portMask = 1; portMask > 0; portMask <<= 1 ) //shift portMask until overflow
  55. { //for each pin of col port
  56. if (portMask & colPins) //if pin is connected to column
  57. {
  58. if (portMask & ~colPortState) //if pin detected a key press
  59. {
  60. rowState |= rowEnd; //set rowState bit for that key
  61. }
  62. rowEnd <<= 1; //shift rowEnd to next key
  63. }
  64. }
  65. }
  66. return rowState;
  67. }