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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "Scanner_ShiftRegsPISOMultiRow.h"
  2. Scanner_ShiftRegsPISOMultiRow::Scanner_ShiftRegsPISOMultiRow(const bool strobeOn,
  3. const uint8_t slaveSelect, const uint8_t byte_count)
  4. : strobeOn(strobeOn), strobeOff(!strobeOn),
  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_ShiftRegsPISOMultiRow::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_ShiftRegsPISOMultiRow::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_ShiftRegsPISOMultiRow 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_ShiftRegsPISOMultiRow::scan(const uint8_t strobePin)
  37. {
  38. read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released
  39. digitalWrite(strobePin, strobeOn); //strobe on
  40. //SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz
  41. delayMicroseconds(20); //photo-transistor at 3.3v needs 20 ms to stablize
  42. digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output
  43. digitalWrite(strobePin, strobeOff); //strobe off
  44. SPI.transfer(&readState, byte_count);
  45. //SPI.endTransaction();
  46. digitalWrite(slaveSelect, LOW); //load parallel inputs to registers
  47. return readState;
  48. }