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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 activeState, const uint8_t readPins[], const uint8_t readPinCount)
  12. : activeState(activeState), readPins(readPins), readPinCount(readPinCount)
  13. {
  14. uint8_t mode;
  15. //configure read pins
  16. if (activeState == 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. /* init() is called once for each row from Row constructor.
  30. Configure row-strobe pin to output.
  31. */
  32. void Scanner_uC::init(const uint8_t strobePin)
  33. {
  34. pinMode(strobePin, OUTPUT);
  35. }
  36. /* scan() is called on every iteration of sketch loop().
  37. scan() strobes the row's strobePin and retuns state of readPins.
  38. Bit patterns are 1 bit per key.
  39. */
  40. read_pins_t Scanner_uC::scan(const uint8_t strobePin)
  41. {
  42. read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released
  43. read_pins_t readMask = 1; //bits, active bit is 1
  44. //strobe on
  45. digitalWrite(strobePin, activeState);
  46. delayMicroseconds(3); //time to stablize voltage
  47. //read all the read pins
  48. for (uint8_t i=0; i < readPinCount; i++)
  49. {
  50. if ( digitalRead(readPins[i]) == activeState )
  51. {
  52. readState |= readMask;
  53. }
  54. readMask <<= 1;
  55. }
  56. //strobe off
  57. digitalWrite(strobePin, !activeState);
  58. return readState;
  59. }