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_ShiftRegsReadStrobed.cpp 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "Scanner_ShiftRegsReadStrobed.h"
  2. Scanner_ShiftRegsReadStrobed::Scanner_ShiftRegsReadStrobed(const bool activeState,
  3. const uint8_t slaveSelect, const uint8_t byte_count)
  4. : activeState(activeState),
  5. slaveSelect(slaveSelect), byte_count(byte_count)
  6. {
  7. pinMode(slaveSelect, OUTPUT);
  8. SPI.begin();
  9. }
  10. /* init() is called once for each row from Row constructor.
  11. Configures controller to communicate with shift register matrix.
  12. slaveSelect initialize not needed, only affects first scan, which is before USB is detected by OS.
  13. digitalWrite(slaveSelect, HIGH);
  14. */
  15. void Scanner_ShiftRegsReadStrobed::init(const uint8_t strobePin)
  16. {
  17. pinMode(strobePin, OUTPUT);
  18. }
  19. /* scan() strobes the row's strobePin and returns state of the shift register's input pins.
  20. strobePin is Arduino pin number connected to this row.
  21. Bit patterns are 1 bit per key.
  22. Scanner_ShiftRegsReadStrobed class was tested on two sets of 74HC165 shift registers
  23. and 74AHC1G126 tri-state buffer chips
  24. 74HC165 is not an SPI device.
  25. The 74HC165 SH/LD polarity is the inverse of SPI slave-select convention.
  26. Most SPI chips will high-Z their MISO pin when their slave select signal is high.
  27. To use SPI-like protocol, 74HC*126 will high-Z the MISO pin when the slave select signal is low.
  28. SPI.beginTransaction() and SPI.endTransaction() are not needed,
  29. but would be needed if trackball uses interrupts.
  30. */
  31. read_pins_t Scanner_ShiftRegsReadStrobed::scan(const uint8_t strobePin)
  32. {
  33. read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released
  34. //strobe off here NOT release continuously
  35. digitalWrite(strobePin, activeState); //strobe on
  36. //SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz
  37. //photo-transistor at 3.3v needs 20 ms to stabilize
  38. delayMicroseconds(20); //monitor trackball here
  39. digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output
  40. digitalWrite(strobePin, !activeState); //strobe off to preserve IR LED life
  41. SPI.transfer(&readState, byte_count);
  42. //SPI.endTransaction();
  43. digitalWrite(slaveSelect, LOW); //load parallel inputs to registers
  44. //strobe off here still releases continuously
  45. return readState;
  46. }