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_PinsArray.cpp 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "RowScanner_PinsArray.h"
  2. /* constructor
  3. */
  4. RowScanner_PinsArray::RowScanner_PinsArray(const uint8_t strobePin,
  5. const uint8_t readPins[], const uint8_t READ_PIN_COUNT)
  6. : strobePin(strobePin), readPins(readPins), READ_PIN_COUNT(READ_PIN_COUNT)
  7. {
  8. uint8_t mode;
  9. //configure row
  10. pinMode(strobePin, OUTPUT);
  11. if (activeHigh)
  12. {
  13. mode = INPUT; //requires external pull-down resistor
  14. }
  15. else
  16. {
  17. mode = INPUT_PULLUP; //uses internal pull-up resistor
  18. }
  19. //configure cols
  20. for (uint8_t i=0; i < READ_PIN_COUNT; i++)
  21. {
  22. pinMode(readPins[i], mode);
  23. }
  24. }
  25. /* scan() Strobes the row and reads the columns.
  26. Sets rowEnd and returns rowState.
  27. rowEnd is a bitwise row mask, one col per bit, where active col bit is 1.
  28. At end of function, 1 bit marks place immediatly after last key of row.
  29. rowEnd is a larger type than portMask so that it can not overflow.
  30. https://www.arduino.cc/en/Tutorial/DigitalPins
  31. https://www.arduino.cc/en/Reference/PinMode
  32. https://www.arduino.cc/en/Reference/DigitalWrite
  33. https://www.arduino.cc/en/Reference/DigitalRead
  34. https://www.arduino.cc/en/Reference/Constants > Digital Pins modes: INPUT, INPUT_PULLUP, and OUTPUT
  35. */
  36. read_pins_t RowScanner_PinsArray::scan(read_pins_mask_t& rowEnd)
  37. {
  38. read_pins_t rowState = 0; //bitwise
  39. rowEnd = 1;
  40. //strobe row on
  41. if (activeHigh)
  42. {
  43. digitalWrite(strobePin, HIGH);
  44. }
  45. else //activeLow
  46. {
  47. digitalWrite(strobePin, LOW);
  48. }
  49. delayMicroseconds(3); //time to stablize voltage
  50. //read all the column pins
  51. for (uint8_t i=0; i < READ_PIN_COUNT; i++)
  52. {
  53. if ( digitalRead(readPins[i]) == activeHigh )
  54. {
  55. rowState |= rowEnd;
  56. }
  57. rowEnd <<= 1;
  58. }
  59. //strobe row off
  60. if (activeHigh)
  61. {
  62. digitalWrite(strobePin, LOW);
  63. }
  64. else //activeLow
  65. {
  66. digitalWrite(strobePin, HIGH);
  67. }
  68. return rowState;
  69. }