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.

Scanner_uC.cpp 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "Scanner_uC.h"
  2. /* constructor
  3. */
  4. Scanner_uC::Scanner_uC(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 read pins
  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 READ_PIN_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 Scanner_uC::scan()
  34. {
  35. read_pins_t rowState = 0; //bitwise, one col per bit, 1 means key is pressed
  36. read_pins_t readMask = 1; //bitwise, one col per bit, active col bit is 1
  37. //strobe row on
  38. digitalWrite(STROBE_PIN, STROBE_ON);
  39. delayMicroseconds(3); //time to stablize voltage
  40. //read all the read pins
  41. for (uint8_t i=0; i < READ_PIN_COUNT; i++)
  42. {
  43. if ( digitalRead(READ_PINS[i]) == STROBE_ON )
  44. {
  45. rowState |= readMask;
  46. }
  47. readMask <<= 1;
  48. }
  49. //strobe row off
  50. digitalWrite(STROBE_PIN, STROBE_OFF);
  51. // readPinCount = READ_PIN_COUNT;
  52. return rowState;
  53. }