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.3KB

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 bitwise, where 1 bit corrsiponds to place immediatly after last key of row.
  37. rowEnd and rowMask are larger type than portMask so that they can not overflow.
  38. */
  39. uint8_t RowScanner_BitManipulation::getRowState(uint16_t& rowEnd)
  40. {
  41. uint16_t rowMask = 1; //bitwise, one col per bit, active col bit is 1
  42. uint8_t rowState = 0; //bitwise, one key per bit, 1 means key is pressed
  43. for (uint8_t i=0; i < colPortCount; i++) //for each col port
  44. {
  45. //bitwise colPins, 1 means pin is connected to column
  46. uint8_t colPins = ptrsColPorts[i]->getColPins();
  47. //bitwise colPortState, pin values where set in ColPort::read(), get them now
  48. uint8_t colPortState = ptrsColPorts[i]->getPortState();
  49. if (activeHigh)
  50. {
  51. colPortState = ~colPortState;
  52. }
  53. for ( uint8_t portMask = 1; portMask > 0; portMask <<= 1 ) //shift portMask until overflow
  54. { //for each pin of col port
  55. if (portMask & colPins) //if pin is connected to column
  56. {
  57. if (portMask & ~colPortState) //if pin detected a key press
  58. {
  59. rowState |= rowMask; //set rowState bit for that key
  60. }
  61. rowMask <<= 1; //shift rowMask to next key
  62. }
  63. }
  64. }
  65. rowEnd = rowMask;
  66. return rowState;
  67. }