keybrd library is an open source library for creating custom-keyboard firmware.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

Scanner_uC.cpp 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 uint8_t strobePin,
  12. const uint8_t readPins[], const uint8_t readPinCount)
  13. : strobePin(strobePin), readPins(readPins), readPinCount(readPinCount)
  14. {
  15. uint8_t mode;
  16. //configure row
  17. pinMode(strobePin, OUTPUT);
  18. if (STROBE_ON == LOW) //if active low
  19. {
  20. mode = INPUT_PULLUP; //use internal pull-up resistor
  21. }
  22. else //if active high
  23. {
  24. mode = INPUT; //requires external pull-down resistor
  25. }
  26. //configure read pins
  27. for (uint8_t i=0; i < readPinCount; i++)
  28. {
  29. pinMode(readPins[i], mode);
  30. }
  31. }
  32. /* scan() strobes the row's strobePin and retuns state of readPins.
  33. Bitwise variables are 1 bit per key.
  34. */
  35. read_pins_t Scanner_uC::scan()
  36. {
  37. read_pins_t readState = 0; //bitwise, 1 means key is pressed, 0 means released
  38. read_pins_t readMask = 1; //bitwise, active bit is 1
  39. //strobe row on
  40. digitalWrite(strobePin, STROBE_ON);
  41. delayMicroseconds(3); //time to stablize voltage
  42. //read all the read pins
  43. for (uint8_t i=0; i < readPinCount; i++)
  44. {
  45. if ( digitalRead(readPins[i]) == STROBE_ON )
  46. {
  47. readState |= readMask;
  48. }
  49. readMask <<= 1;
  50. }
  51. //strobe row off
  52. digitalWrite(strobePin, STROBE_OFF);
  53. return readState;
  54. }