2016-11-02 21:28:58 +00:00
|
|
|
#include "Scanner_ShiftRegsReadStrobed.h"
|
2016-09-25 04:27:06 +00:00
|
|
|
|
2016-11-02 21:28:58 +00:00
|
|
|
Scanner_ShiftRegsReadStrobed::Scanner_ShiftRegsReadStrobed(const bool strobeOn,
|
2016-09-25 04:27:06 +00:00
|
|
|
const uint8_t slaveSelect, const uint8_t byte_count)
|
|
|
|
: strobeOn(strobeOn), strobeOff(!strobeOn),
|
2016-09-28 20:37:40 +00:00
|
|
|
slaveSelect(slaveSelect), byte_count(byte_count)
|
2016-09-25 04:27:06 +00:00
|
|
|
{
|
|
|
|
pinMode(slaveSelect, OUTPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init() is called once for each row from Row constructor.
|
|
|
|
Configures controller to communicate with shift register matrix.
|
|
|
|
*/
|
2016-11-02 21:28:58 +00:00
|
|
|
void Scanner_ShiftRegsReadStrobed::init(const uint8_t strobePin)
|
2016-09-25 04:27:06 +00:00
|
|
|
{
|
|
|
|
pinMode(strobePin, OUTPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* begin() should be called once from sketch setup().
|
|
|
|
Initializes shift register's shift/load pin.
|
|
|
|
*/
|
2016-11-02 21:28:58 +00:00
|
|
|
void Scanner_ShiftRegsReadStrobed::begin()
|
2016-09-25 04:27:06 +00:00
|
|
|
{
|
2016-11-02 21:15:16 +00:00
|
|
|
digitalWrite(slaveSelect, HIGH); //initialize ??only needed for first scan
|
2016-10-31 23:43:53 +00:00
|
|
|
SPI.begin(); //todo move this to constructor or init()
|
2016-09-25 04:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* scan() strobes the row's strobePin and returns state of the shift register's input pins.
|
|
|
|
strobePin is Arduino pin number connected to this row.
|
|
|
|
Bit patterns are 1 bit per key.
|
2016-11-02 21:15:16 +00:00
|
|
|
|
2016-11-02 21:28:58 +00:00
|
|
|
Scanner_ShiftRegsReadStrobed class was tested on two sets of 74HC165 shift registers
|
2016-11-02 21:15:16 +00:00
|
|
|
and 74AHC1G126 tri-state buffer chips
|
|
|
|
|
|
|
|
74HC165 is not an SPI device.
|
|
|
|
The 74HC165 SH/LD polarity is the inverse of SPI slave-select convention.
|
|
|
|
Most SPI chips will high-Z their MISO pin when their slave select signal is high.
|
|
|
|
To use SPI-like protocol, 74HC*126 will high-Z the MISO pin when the slave select signal is low.
|
|
|
|
|
|
|
|
SPI.beginTransaction() and SPI.endTransaction() are not needed,
|
|
|
|
but would be needed if trackball uses interrupts.
|
2016-09-25 04:27:06 +00:00
|
|
|
*/
|
2016-11-02 21:28:58 +00:00
|
|
|
read_pins_t Scanner_ShiftRegsReadStrobed::scan(const uint8_t strobePin)
|
2016-09-25 04:27:06 +00:00
|
|
|
{
|
|
|
|
read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released
|
|
|
|
|
2016-11-02 21:15:16 +00:00
|
|
|
digitalWrite(strobePin, strobeOn); //strobe on
|
2016-10-30 08:30:13 +00:00
|
|
|
|
2016-11-02 21:15:16 +00:00
|
|
|
//SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz
|
2016-10-30 20:36:34 +00:00
|
|
|
|
2016-11-02 21:15:16 +00:00
|
|
|
delayMicroseconds(20); //photo-transistor at 3.3v needs 20 ms to stablize
|
2016-09-25 04:27:06 +00:00
|
|
|
|
2016-11-02 21:15:16 +00:00
|
|
|
digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output
|
2016-10-30 20:36:34 +00:00
|
|
|
|
|
|
|
digitalWrite(strobePin, strobeOff); //strobe off
|
2016-10-30 08:30:13 +00:00
|
|
|
|
2016-09-25 04:27:06 +00:00
|
|
|
SPI.transfer(&readState, byte_count);
|
2016-10-30 08:30:13 +00:00
|
|
|
|
2016-11-02 21:15:16 +00:00
|
|
|
//SPI.endTransaction();
|
|
|
|
|
|
|
|
digitalWrite(slaveSelect, LOW); //load parallel inputs to registers
|
2016-09-25 04:27:06 +00:00
|
|
|
|
|
|
|
return readState;
|
|
|
|
}
|
|
|
|
|