#include "Scanner_ShiftRegsPISOSingleRow.h" | |||||
#include "Scanner_ShiftRegsRead.h" | |||||
/* constructor | /* constructor | ||||
Parameter strobeOn is not used. | Parameter strobeOn is not used. | ||||
*/ | */ | ||||
Scanner_ShiftRegsPISOSingleRow::Scanner_ShiftRegsPISOSingleRow(const bool strobeOn, | |||||
Scanner_ShiftRegsRead::Scanner_ShiftRegsRead(const bool strobeOn, | |||||
const uint8_t slaveSelect, const uint8_t byte_count) | const uint8_t slaveSelect, const uint8_t byte_count) | ||||
: slaveSelect(slaveSelect), byte_count(byte_count) | : slaveSelect(slaveSelect), byte_count(byte_count) | ||||
{ | { | ||||
/* init() is called once for each row from Row constructor. | /* init() is called once for each row from Row constructor. | ||||
*/ | */ | ||||
void Scanner_ShiftRegsPISOSingleRow::init(const uint8_t strobePin) | |||||
void Scanner_ShiftRegsRead::init(const uint8_t strobePin) | |||||
{ | { | ||||
//empty function | //empty function | ||||
} | } | ||||
/* begin() should be called once from sketch setup(). | /* begin() should be called once from sketch setup(). | ||||
Initializes shift register's shift/load pin. | Initializes shift register's shift/load pin. | ||||
*/ | */ | ||||
void Scanner_ShiftRegsPISOSingleRow::begin() | |||||
void Scanner_ShiftRegsRead::begin() | |||||
{ | { | ||||
SPI.begin(); | SPI.begin(); | ||||
digitalWrite(slaveSelect, HIGH); | digitalWrite(slaveSelect, HIGH); | ||||
No strobe pin is needed, the shift register is wired so the strobe is effectivley always "on". | No strobe pin is needed, the shift register is wired so the strobe is effectivley always "on". | ||||
Bit patterns are 1 bit per key. | Bit patterns are 1 bit per key. | ||||
*/ | */ | ||||
read_pins_t Scanner_ShiftRegsPISOSingleRow::scan(const uint8_t strobePin) | |||||
read_pins_t Scanner_ShiftRegsRead::scan(const uint8_t strobePin) | |||||
{ | { | ||||
read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released | read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released | ||||
#include <SPI.h> | #include <SPI.h> | ||||
#include <ScannerInterface.h> | #include <ScannerInterface.h> | ||||
/* Scanner_ShiftRegsPISOSingleRow reads shift registers. | |||||
/* Scanner_ShiftRegsRead reads shift registers. | |||||
This was tested on 74HC165 shift registers, which are Parallel-In-Serial-Out (PISO). | This was tested on 74HC165 shift registers, which are Parallel-In-Serial-Out (PISO). | ||||
Upto 4 shift registers can be in a daisy chained for a total of 32 read pins. | Upto 4 shift registers can be in a daisy chained for a total of 32 read pins. | ||||
Example instantiation: | Example instantiation: | ||||
Row row_R0(scanner_R, 0, ptrsKeys_R0, sizeof(ptrsKeys_R0)/sizeof(*ptrsKeys_R0)); | Row row_R0(scanner_R, 0, ptrsKeys_R0, sizeof(ptrsKeys_R0)/sizeof(*ptrsKeys_R0)); | ||||
Scanner_ShiftRegsPISOSingleRow scanner_R(HIGH, SS, 4); | |||||
Scanner_ShiftRegsRead scanner_R(HIGH, SS, 4); | |||||
The Row "strobePin" parameter is ignored. | The Row "strobePin" parameter is ignored. | ||||
In the above example, the "strobePin" argument is 0, but it doesn't matter what value is given. | In the above example, the "strobePin" argument is 0, but it doesn't matter what value is given. | ||||
There are three Scanner_ShiftRegsPISOSingleRow parameters. | |||||
There are three Scanner_ShiftRegsRead parameters. | |||||
"strobeOn" paramter is ignored, but should be active state HIGH or LOW required by ScannerInterface. | "strobeOn" paramter is ignored, but should be active state HIGH or LOW required by ScannerInterface. | ||||
"slaveSelect" paramter can be any controller pin connected to shift register's SHIFT-LOAD pin. | "slaveSelect" paramter can be any controller pin connected to shift register's SHIFT-LOAD pin. | ||||
Switches connect grouned row to parallel-input pins. | Switches connect grouned row to parallel-input pins. | ||||
Controller's MISO pin is connected to shift register's serial output (QH) pin | Controller's MISO pin is connected to shift register's serial output (QH) pin | ||||
*/ | */ | ||||
class Scanner_ShiftRegsPISOSingleRow : public ScannerInterface | |||||
class Scanner_ShiftRegsRead : public ScannerInterface | |||||
{ | { | ||||
private: | private: | ||||
const uint8_t slaveSelect;//controller pin number connected to shift register SHIFT-LOAD pin | const uint8_t slaveSelect;//controller pin number connected to shift register SHIFT-LOAD pin | ||||
const uint8_t byte_count; //number of bytes to read from shift registers | const uint8_t byte_count; //number of bytes to read from shift registers | ||||
public: | public: | ||||
Scanner_ShiftRegsPISOSingleRow(const bool strobeOn, | |||||
Scanner_ShiftRegsRead(const bool strobeOn, | |||||
const uint8_t slaveSelect, const uint8_t byte_count); | const uint8_t slaveSelect, const uint8_t byte_count); | ||||
void init(const uint8_t strobePin); | void init(const uint8_t strobePin); | ||||
void begin(); | void begin(); |
#include "Scanner_ShiftRegsPISOMultiRow.h" | |||||
#include "Scanner_ShiftRegsReadStrobed.h" | |||||
Scanner_ShiftRegsPISOMultiRow::Scanner_ShiftRegsPISOMultiRow(const bool strobeOn, | |||||
Scanner_ShiftRegsReadStrobed::Scanner_ShiftRegsReadStrobed(const bool strobeOn, | |||||
const uint8_t slaveSelect, const uint8_t byte_count) | const uint8_t slaveSelect, const uint8_t byte_count) | ||||
: strobeOn(strobeOn), strobeOff(!strobeOn), | : strobeOn(strobeOn), strobeOff(!strobeOn), | ||||
slaveSelect(slaveSelect), byte_count(byte_count) | slaveSelect(slaveSelect), byte_count(byte_count) | ||||
/* init() is called once for each row from Row constructor. | /* init() is called once for each row from Row constructor. | ||||
Configures controller to communicate with shift register matrix. | Configures controller to communicate with shift register matrix. | ||||
*/ | */ | ||||
void Scanner_ShiftRegsPISOMultiRow::init(const uint8_t strobePin) | |||||
void Scanner_ShiftRegsReadStrobed::init(const uint8_t strobePin) | |||||
{ | { | ||||
pinMode(strobePin, OUTPUT); | pinMode(strobePin, OUTPUT); | ||||
} | } | ||||
/* begin() should be called once from sketch setup(). | /* begin() should be called once from sketch setup(). | ||||
Initializes shift register's shift/load pin. | Initializes shift register's shift/load pin. | ||||
*/ | */ | ||||
void Scanner_ShiftRegsPISOMultiRow::begin() | |||||
void Scanner_ShiftRegsReadStrobed::begin() | |||||
{ | { | ||||
digitalWrite(slaveSelect, HIGH); //initialize ??only needed for first scan | digitalWrite(slaveSelect, HIGH); //initialize ??only needed for first scan | ||||
SPI.begin(); //todo move this to constructor or init() | SPI.begin(); //todo move this to constructor or init() | ||||
strobePin is Arduino pin number connected to this row. | strobePin is Arduino pin number connected to this row. | ||||
Bit patterns are 1 bit per key. | Bit patterns are 1 bit per key. | ||||
Scanner_ShiftRegsPISOMultiRow class was tested on two sets of 74HC165 shift registers | |||||
Scanner_ShiftRegsReadStrobed class was tested on two sets of 74HC165 shift registers | |||||
and 74AHC1G126 tri-state buffer chips | and 74AHC1G126 tri-state buffer chips | ||||
74HC165 is not an SPI device. | 74HC165 is not an SPI device. | ||||
SPI.beginTransaction() and SPI.endTransaction() are not needed, | SPI.beginTransaction() and SPI.endTransaction() are not needed, | ||||
but would be needed if trackball uses interrupts. | but would be needed if trackball uses interrupts. | ||||
*/ | */ | ||||
read_pins_t Scanner_ShiftRegsPISOMultiRow::scan(const uint8_t strobePin) | |||||
read_pins_t Scanner_ShiftRegsReadStrobed::scan(const uint8_t strobePin) | |||||
{ | { | ||||
read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released | read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released | ||||
#include <SPI.h> | #include <SPI.h> | ||||
#include <ScannerInterface.h> | #include <ScannerInterface.h> | ||||
/* Scanner_ShiftRegsPISOMultiRow reads shift registers. | |||||
/* Scanner_ShiftRegsReadStrobed reads shift registers. | |||||
This was tested on 74HC165 shift registers, which are Parallel-In-Serial-Out (PISO). | This was tested on 74HC165 shift registers, which are Parallel-In-Serial-Out (PISO). | ||||
Shift registers can be daisy chained for a total of 32 read pins. | Shift registers can be daisy chained for a total of 32 read pins. | ||||
Example instantiation: | Example instantiation: | ||||
Scanner_ShiftRegsPISOMultiRow scanner_R(HIGH, SS, 4); | |||||
Scanner_ShiftRegsReadStrobed scanner_R(HIGH, SS, 4); | |||||
There are three Scanner_ShiftRegsPISOMultiRow parameters. | |||||
There are three Scanner_ShiftRegsReadStrobed parameters. | |||||
"strobeOn" paramter is active state HIGH or LOW. | "strobeOn" paramter is active state HIGH or LOW. | ||||
"slaveSelect" paramter can be any controller pin connected to shift register's SHIFT-LOAD pin. | "slaveSelect" paramter can be any controller pin connected to shift register's SHIFT-LOAD pin. | ||||
Orient diodes with cathode (banded end) towards the read pins. | Orient diodes with cathode (banded end) towards the read pins. | ||||
Controller's MISO pin is connected to shift register's serial output (QH) pin | Controller's MISO pin is connected to shift register's serial output (QH) pin | ||||
*/ | */ | ||||
class Scanner_ShiftRegsPISOMultiRow : public ScannerInterface | |||||
class Scanner_ShiftRegsReadStrobed : public ScannerInterface | |||||
{ | { | ||||
private: | private: | ||||
const bool strobeOn; //logic level of strobe on, active state HIGH or LOW | const bool strobeOn; //logic level of strobe on, active state HIGH or LOW | ||||
const uint8_t slaveSelect;//controller pin number connected to shift register SHIFT-LOAD pin | const uint8_t slaveSelect;//controller pin number connected to shift register SHIFT-LOAD pin | ||||
const uint8_t byte_count; //number of bytes to read from shift registers | const uint8_t byte_count; //number of bytes to read from shift registers | ||||
public: | public: | ||||
Scanner_ShiftRegsPISOMultiRow(const bool strobeOn, | |||||
Scanner_ShiftRegsReadStrobed(const bool strobeOn, | |||||
const uint8_t slaveSelect, const uint8_t byte_count); | const uint8_t slaveSelect, const uint8_t byte_count); | ||||
virtual void init(const uint8_t strobePin); | virtual void init(const uint8_t strobePin); | ||||
virtual void begin(); | virtual void begin(); |
#include <Row.h> | #include <Row.h> | ||||
//Right matrix | //Right matrix | ||||
#include <Scanner_ShiftRegsPISOSingleRow.h> | |||||
#include <Scanner_ShiftRegsRead.h> | |||||
// =============== CONFIGURATION =============== | // =============== CONFIGURATION =============== | ||||
ScanDelay scanDelay(9000); | ScanDelay scanDelay(9000); | ||||
Scanner_uC scanner_L(LOW, readPins_L, readPinCount_L); //active LOW | Scanner_uC scanner_L(LOW, readPins_L, readPinCount_L); //active LOW | ||||
// --------------- RIGHT SCANNER --------------- | // --------------- RIGHT SCANNER --------------- | ||||
Scanner_ShiftRegsPISOSingleRow scanner_R(HIGH, 10, 2); //active HIGH | |||||
Scanner_ShiftRegsRead scanner_R(HIGH, 10, 2); //active HIGH | |||||
// =================== ROWS ==================== | // =================== ROWS ==================== | ||||
// ----------------- LEFT ROWS ----------------- | // ----------------- LEFT ROWS ----------------- |