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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "Scanner_uC.h"
  2. /* Scanner_uC functions call Arduino's Digital Pins functions
  3. https://www.arduino.cc/en/Tutorial/DigitalPins
  4. https://www.arduino.cc/en/Reference/PinMode
  5. https://www.arduino.cc/en/Reference/DigitalWrite
  6. https://www.arduino.cc/en/Reference/DigitalRead
  7. https://www.arduino.cc/en/Reference/Constants > Digital Pins modes: INPUT, INPUT_PULLUP, and OUTPUT
  8. */
  9. /* constructor
  10. */
  11. Scanner_uC::Scanner_uC(const bool strobeOn, const uint8_t readPins[], const uint8_t readPinCount)
  12. : strobeOn(strobeOn), strobeOff(!strobeOn), readPins(readPins), readPinCount(readPinCount)
  13. {
  14. uint8_t mode;
  15. //configure read pins
  16. if (strobeOn == LOW) //if active low
  17. {
  18. mode = INPUT_PULLUP; //use internal pull-up resistor
  19. }
  20. else //if active high
  21. {
  22. mode = INPUT; //requires external pull-down resistor
  23. }
  24. for (uint8_t i=0; i < readPinCount; i++)
  25. {
  26. pinMode(readPins[i], mode);
  27. }
  28. }
  29. /*
  30. Configure row-strobe pin to output.
  31. */
  32. void Scanner_uC::begin(const uint8_t strobePin)
  33. {
  34. pinMode(strobePin, OUTPUT);
  35. }
  36. /* scan() strobes the row's strobePin and retuns state of readPins.
  37. Bitwise variables are 1 bit per key.
  38. */
  39. read_pins_t Scanner_uC::scan(const uint8_t strobePin)
  40. {
  41. read_pins_t readState = 0; //bitwise, 1 means key is pressed, 0 means released
  42. read_pins_t readMask = 1; //bitwise, active bit is 1
  43. //strobe row on
  44. digitalWrite(strobePin, strobeOn);
  45. delayMicroseconds(3); //time to stablize voltage
  46. //read all the read pins
  47. for (uint8_t i=0; i < readPinCount; i++)
  48. {
  49. if ( digitalRead(readPins[i]) == strobeOn )
  50. {
  51. readState |= readMask;
  52. }
  53. readMask <<= 1;
  54. }
  55. //strobe row off
  56. digitalWrite(strobePin, strobeOff);
  57. return readState;
  58. }