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_ShiftRegs74HC165.cpp 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "Scanner_ShiftRegs74HC165.h"
  2. //constructor
  3. Scanner_ShiftRegs74HC165::Scanner_ShiftRegs74HC165(const uint8_t strobePin, uint8_t readPinCount)
  4. : strobePin(strobePin), byte_count(ceil (float(readPinCount)/8))
  5. {
  6. //configure controller to communicate with shift register matrix
  7. pinMode(strobePin, OUTPUT);
  8. pinMode(SHIFT_LOAD, OUTPUT);
  9. }
  10. void Scanner_ShiftRegs74HC165::begin()
  11. {
  12. //initialize shift register's shift/load pin
  13. digitalWrite(SHIFT_LOAD, HIGH);
  14. }
  15. /* scan() strobes the row's strobePin and retuns state of the shift register's input pins.
  16. Bitwise variables are 1 bit per key.
  17. */
  18. read_pins_t Scanner_ShiftRegs74HC165::scan()
  19. {
  20. read_pins_t readState = 0; //bitwise, 1 means key is pressed, 0 means released
  21. //strobe row on
  22. digitalWrite(strobePin, STROBE_ON);
  23. delayMicroseconds(3); //time to stablize voltage
  24. //read all the column pins
  25. digitalWrite(SHIFT_LOAD, LOW); //load parallel inputs to the register
  26. digitalWrite(SHIFT_LOAD, HIGH); //shift the data toward a serial output
  27. SPI.transfer(&readState, byte_count);
  28. //strobe row off
  29. digitalWrite(strobePin, STROBE_OFF);
  30. //for testing on breadboard, clear unpowered pins
  31. readState &= 0b11110001000100010001000100010001; //todo delete this line
  32. return readState;
  33. }