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

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