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 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "RowScanner_PinsArray.h"
  2. /* constructor
  3. */
  4. RowScanner_PinsArray::RowScanner_PinsArray(const uint8_t STROBE_PIN,
  5. const uint8_t READ_PINS[], const uint8_t READ_PIN_COUNT)
  6. : STROBE_PIN(STROBE_PIN), READ_PINS(READ_PINS), READ_PIN_COUNT(READ_PIN_COUNT)
  7. {
  8. uint8_t mode;
  9. //configure row
  10. pinMode(STROBE_PIN, OUTPUT);
  11. if (STROBE_ON == LOW) //if active low
  12. {
  13. mode = INPUT_PULLUP; //uses internal pull-up resistor
  14. }
  15. else
  16. {
  17. mode = INPUT; //requires external pull-down resistor
  18. }
  19. //configure cols
  20. for (uint8_t i=0; i < READ_PIN_COUNT; i++)
  21. {
  22. pinMode(READ_PINS[i], mode);
  23. }
  24. }
  25. /* scan() Strobes the row and reads the columns.
  26. Sets KEY_COUNT and returns rowState.
  27. https://www.arduino.cc/en/Tutorial/DigitalPins
  28. https://www.arduino.cc/en/Reference/PinMode
  29. https://www.arduino.cc/en/Reference/DigitalWrite
  30. https://www.arduino.cc/en/Reference/DigitalRead
  31. https://www.arduino.cc/en/Reference/Constants > Digital Pins modes: INPUT, INPUT_PULLUP, and OUTPUT
  32. */
  33. read_pins_t RowScanner_PinsArray::scan(uint8_t& keyCount)
  34. {
  35. read_pins_t rowState = 0; //bitwise, one col per bit, 1 means key is pressed
  36. read_pins_t rowMask = 1; //bitwise, one col per bit, active col bit is 1
  37. digitalWrite(STROBE_PIN, STROBE_ON);
  38. delayMicroseconds(3); //time to stablize voltage
  39. //read all the column pins
  40. for (uint8_t i=0; i < READ_PIN_COUNT; i++)
  41. {
  42. if ( digitalRead(READ_PINS[i]) == STROBE_ON )
  43. {
  44. rowState |= rowMask;
  45. }
  46. rowMask <<= 1;
  47. }
  48. digitalWrite(STROBE_PIN, STROBE_OFF);
  49. keyCount = READ_PIN_COUNT;
  50. return rowState;
  51. }