From 15ebb4b6cfb877fe6024f877142a692550fe27fc Mon Sep 17 00:00:00 2001 From: wolfv6 Date: Wed, 7 Sep 2016 02:59:01 -0600 Subject: [PATCH] add Scanner_ShiftRegsPISO, update keybrd_4b_split_keyboard_with_shift_registers.ino --- src/Scanner_ShiftRegs74HC165.cpp | 51 ---------------- src/Scanner_ShiftRegs74HC165.h | 49 --------------- src/Scanner_ShiftRegsPISOMultiRow.cpp | 51 ++++++++++++++++ src/Scanner_ShiftRegsPISOMultiRow.h | 58 ++++++++++++++++++ src/Scanner_ShiftRegsPISOSingleRow.cpp | 42 +++++++++++++ src/Scanner_ShiftRegsPISOSingleRow.h | 60 +++++++++++++++++++ ...4b_split_keyboard_with_shift_registers.ino | 6 +- 7 files changed, 214 insertions(+), 103 deletions(-) delete mode 100644 src/Scanner_ShiftRegs74HC165.cpp delete mode 100644 src/Scanner_ShiftRegs74HC165.h create mode 100644 src/Scanner_ShiftRegsPISOMultiRow.cpp create mode 100644 src/Scanner_ShiftRegsPISOMultiRow.h create mode 100644 src/Scanner_ShiftRegsPISOSingleRow.cpp create mode 100644 src/Scanner_ShiftRegsPISOSingleRow.h diff --git a/src/Scanner_ShiftRegs74HC165.cpp b/src/Scanner_ShiftRegs74HC165.cpp deleted file mode 100644 index b822ff3..0000000 --- a/src/Scanner_ShiftRegs74HC165.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "Scanner_ShiftRegs74HC165.h" - -Scanner_ShiftRegs74HC165::Scanner_ShiftRegs74HC165(const bool strobeOn, const uint8_t slaveSelect, - const uint8_t readPinCount) - : strobeOn(strobeOn), strobeOff(!strobeOn), slaveSelect(slaveSelect), - byte_count( ceil(float(readPinCount)/8) ) -{ - pinMode(slaveSelect, OUTPUT); -} - -/* init() is called once for each row from Row constructor. -Configure controller to communicate with shift register matrix. -*/ -void Scanner_ShiftRegs74HC165::init(const uint8_t strobePin) -{ - pinMode(strobePin, OUTPUT); -} - -/* begin() should be called once from sketch setup(). -*/ -void Scanner_ShiftRegs74HC165::begin() -{ - //initialize shift register's shift/load pin - digitalWrite(slaveSelect, HIGH); -} - -/* 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 -Bitwise variables are 1 bit per key. -*/ -read_pins_t Scanner_ShiftRegs74HC165::scan(const uint8_t strobePin) -{ - read_pins_t readState = 0; //bitwise, 1 means key is pressed, 0 means released - - //strobe row on - digitalWrite(strobePin, strobeOn); - //delayMicroseconds(3); //time to stablize voltage - delayMicroseconds(333); //todo - - //read all the column pins - digitalWrite(slaveSelect, LOW); //load parallel inputs to the register - digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output - //SPI.transfer(&readState, byte_count); - SPI.transfer(&readState, 4);//todo - - //strobe row off - digitalWrite(strobePin, strobeOff); - - return readState; -} - diff --git a/src/Scanner_ShiftRegs74HC165.h b/src/Scanner_ShiftRegs74HC165.h deleted file mode 100644 index e2f184b..0000000 --- a/src/Scanner_ShiftRegs74HC165.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef ROWSCANNER_SHIFTREGS74HC165_H -#define ROWSCANNER_SHIFTREGS74HC165_H - -#include -#include -#include -#include -#include -#include -#include - -/* Scanner_ShiftRegs74HC165 reads shift registers. -shift registers 74HC165 are Parallel-In-Serial-Out (PISO) -Upto 4 shift registers can be in a daisy chained for a total of 32 read pins. - -For active low: -Shift register parallel input pins have 10k pull-up resistors powered -Orient diodes with cathode (banded end) towards the write pins (row) -Controller's MISO pin is connected to shift register's complementary serial output (/QH) pin -Use these two lines in the sketch: - const bool Scanner_ShiftRegs74HC165::strobeOn = LOW; - const bool Scanner_ShiftRegs74HC165::strobeOff = HIGH; - -For active high: -Shift register parallel input pins have 10k pull-down resistors grounded -Orient diodes with cathode (banded end) towards the read pins. -Controller's MISO pin is connected to shift register's serial output (QH) pin -Use these two lines in the sketch: - const bool Scanner_ShiftRegs74HC165::strobeOn = HIGH; - const bool Scanner_ShiftRegs74HC165::strobeOff = LOW; - -In addition, each row needs to be connected to a strobe pin from the controller. -*/ -class Scanner_ShiftRegs74HC165 : public ScannerInterface -{ - private: - const bool strobeOn; //logic level of strobe on, active state HIGH or LOW - const bool strobeOff; //logic level of strobe off, complement of active state - const uint8_t slaveSelect; //controller's pin number that is - // connected to shift register's SHIFT-LOAD pin - const uint8_t byte_count; //number of bytes to read from shift registers - public: - Scanner_ShiftRegs74HC165(const bool strobeOn, const uint8_t slaveSelect, - const uint8_t readPinCount); - void init(const uint8_t strobePin); - void begin(); - virtual read_pins_t scan(const uint8_t strobePin); -}; -#endif diff --git a/src/Scanner_ShiftRegsPISOMultiRow.cpp b/src/Scanner_ShiftRegsPISOMultiRow.cpp new file mode 100644 index 0000000..34f1a97 --- /dev/null +++ b/src/Scanner_ShiftRegsPISOMultiRow.cpp @@ -0,0 +1,51 @@ +#include "Scanner_ShiftRegsPISOMultiRow.h" + +/* constructor +*/ +Scanner_ShiftRegsPISOMultiRow::Scanner_ShiftRegsPISOMultiRow(const bool strobeOn, + const uint8_t slaveSelect, const uint8_t byte_count) + : strobeOn(strobeOn), strobeOff(!strobeOn), + slaveSelect(slaveSelect), byte_count(byte_count) +{ + pinMode(slaveSelect, OUTPUT); +} + +/* init() is called once for each row from Row constructor. +Configures controller to communicate with shift register matrix. +*/ +void Scanner_ShiftRegsPISOMultiRow::init(const uint8_t strobePin) +{ + pinMode(strobePin, OUTPUT); +} + +/* begin() should be called once from sketch setup(). +Initializes shift register's shift/load pin. +*/ +void Scanner_ShiftRegsPISOMultiRow::begin() +{ + digitalWrite(slaveSelect, HIGH); +} + +/* 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. +Bitwise variables are 1 bit per key. +*/ +read_pins_t Scanner_ShiftRegsPISOMultiRow::scan(const uint8_t strobePin) +{ + read_pins_t readState = 0; //bitwise, 1 means key is pressed, 0 means released + + //strobe row on + digitalWrite(strobePin, strobeOn); + delayMicroseconds(3); //time to stablize voltage + + //read all the column pins + digitalWrite(slaveSelect, LOW); //load parallel inputs to the register + digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output + SPI.transfer(&readState, byte_count); + + //strobe row off + digitalWrite(strobePin, strobeOff); + + return readState; +} + diff --git a/src/Scanner_ShiftRegsPISOMultiRow.h b/src/Scanner_ShiftRegsPISOMultiRow.h new file mode 100644 index 0000000..1e825c4 --- /dev/null +++ b/src/Scanner_ShiftRegsPISOMultiRow.h @@ -0,0 +1,58 @@ +#ifndef ROWSCANNER_SHIFTREGSPISOMULTIROW_H +#define ROWSCANNER_SHIFTREGSPISOMULTIROW_H + +#include +#include +#include +#include +#include +#include +#include + +/* Scanner_ShiftRegsPISOMultiRow reads shift registers. +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. + +Example instantiation: + Scanner_ShiftRegsPISOMultiRow scanner_R(HIGH, SS, 4); + +There are three Scanner_ShiftRegsPISOMultiRow parameters. +"strobeOn" paramter is active state HIGH or LOW. + +"slaveSelect" paramter can be any controller pin connected to shift register's SHIFT-LOAD pin. +slaveSelect pin SS (Arduino pin 10) has the fastest scan. + +"byte_count" is the number of bytes to read from shift registers (1 to 4). +byte_count should cover all the row's keys: + byte_count*8 >= row's keyCount + +Hardware setup: +Each row needs to be connected to a strobe pin from the controller. +Switche and diode in series are connected to shift-register parallel-input pins and strobed row. + +For active low: +Shift-register parallel-input pins need 10k pull-up resistors powered. +Orient diodes with cathode (banded end) towards the write pins (row) +Controller's MISO pin is connected to shift register's complementary serial output (/QH) pin + +For active high: +Shift-register parallel-input pins need 10k pull-down resistors grounded. +Orient diodes with cathode (banded end) towards the read pins. +Controller's MISO pin is connected to shift register's serial output (QH) pin +*/ +class Scanner_ShiftRegsPISOMultiRow : public ScannerInterface +{ + private: + const bool strobeOn; //logic level of strobe on, active state HIGH or LOW + const bool strobeOff; //logic level of strobe off, complement of strobeOn + const uint8_t slaveSelect; //controller's pin number that is + // connected to shift register's SHIFT-LOAD pin + const uint8_t byte_count; //number of bytes to read from shift registers + public: + Scanner_ShiftRegsPISOMultiRow(const bool strobeOn, + const uint8_t slaveSelect, const uint8_t byte_count); + virtual void init(const uint8_t strobePin); + virtual void begin(); + virtual read_pins_t scan(const uint8_t strobePin); +}; +#endif diff --git a/src/Scanner_ShiftRegsPISOSingleRow.cpp b/src/Scanner_ShiftRegsPISOSingleRow.cpp new file mode 100644 index 0000000..da00687 --- /dev/null +++ b/src/Scanner_ShiftRegsPISOSingleRow.cpp @@ -0,0 +1,42 @@ +#include "Scanner_ShiftRegsPISOSingleRow.h" + +/* constructor +*/ +Scanner_ShiftRegsPISOSingleRow::Scanner_ShiftRegsPISOSingleRow(const bool strobeOn, + const uint8_t slaveSelect, const uint8_t byte_count) + : slaveSelect(slaveSelect), byte_count(byte_count) +{ + pinMode(slaveSelect, OUTPUT); +} + +/* init() is called once for each row from Row constructor. +*/ +void Scanner_ShiftRegsPISOSingleRow::init(const uint8_t strobePin) +{ + //empty function +} + +/* begin() should be called once from sketch setup(). +Initializes shift register's shift/load pin. +*/ +void Scanner_ShiftRegsPISOSingleRow::begin() +{ + digitalWrite(slaveSelect, HIGH); +} + +/* scan() returns state of the shift register's input pins. +No strobe pin is needed, the shift register is wired so the strobe is effectivley always "on". +Bitwise variables are 1 bit per key. +*/ +read_pins_t Scanner_ShiftRegsPISOSingleRow::scan(const uint8_t strobePin) +{ + read_pins_t readState = 0; //bitwise, 1 means key is pressed, 0 means released + + //read all the column pins + digitalWrite(slaveSelect, LOW); //load parallel inputs to the register + digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output + SPI.transfer(&readState, byte_count); + + return readState; +} + diff --git a/src/Scanner_ShiftRegsPISOSingleRow.h b/src/Scanner_ShiftRegsPISOSingleRow.h new file mode 100644 index 0000000..1483659 --- /dev/null +++ b/src/Scanner_ShiftRegsPISOSingleRow.h @@ -0,0 +1,60 @@ +#ifndef ROWSCANNER_SHIFTREGSPISOSINGLEROW_H +#define ROWSCANNER_SHIFTREGSPISOSINGLEROW_H + +#include +#include +#include +#include +#include +#include +#include + +/* Scanner_ShiftRegsPISOSingleRow reads shift registers. +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. + +Example instantiation: + Row row_R0(scanner_R, 0, ptrsKeys_R0, sizeof(ptrsKeys_R0)/sizeof(*ptrsKeys_R0)); + Scanner_ShiftRegsPISOSingleRow scanner_R(HIGH, SS, 4); + +The Row "strobePin" parameter is ignored. +In the above example, the "strobePin" argument is 0, but it doesn't matter what value is given. + +There are three Scanner_ShiftRegsPISOSingleRow parameters. +"strobeOn" paramter is active state HIGH or LOW. + +"slaveSelect" paramter can be any controller pin connected to shift register's SHIFT-LOAD pin. +slaveSelect pin SS (Arduino pin 10) has the fastest scan. + +"byte_count" is the number of bytes to read from shift registers (1 to 4). +byte_count should cover all the row's keys: + byte_count*8 >= row's keyCount + +Hardware setup: +There is only one row, and it is permanently active. +Switches are connected to shift-register parallel-input pins (diodes are not needed) and row. + +For active low: +Shift-register parallel-input pins need 10k pull-up resistors powered. +Switches connect powered row to parallel-input pins. +Controller's MISO pin is connected to shift register's complementary serial output (/QH) pin + +For active high: +Shift-register parallel-input pins need 10k pull-down resistors grounded. +Switches connect grouned row to parallel-input pins. +Controller's MISO pin is connected to shift register's serial output (QH) pin +*/ +class Scanner_ShiftRegsPISOSingleRow : public ScannerInterface +{ + private: + const uint8_t slaveSelect; //controller's pin number that is + // connected to shift register's SHIFT-LOAD pin + const uint8_t byte_count; //number of bytes to read from shift registers + public: + Scanner_ShiftRegsPISOSingleRow(const bool strobeOn, + const uint8_t slaveSelect, const uint8_t byte_count); + void init(const uint8_t strobePin); + void begin(); + virtual read_pins_t scan(const uint8_t strobePin); +}; +#endif diff --git a/tutorials/keybrd_4b_split_keyboard_with_shift_registers/keybrd_4b_split_keyboard_with_shift_registers.ino b/tutorials/keybrd_4b_split_keyboard_with_shift_registers/keybrd_4b_split_keyboard_with_shift_registers.ino index d41cc1f..a21cfaa 100644 --- a/tutorials/keybrd_4b_split_keyboard_with_shift_registers/keybrd_4b_split_keyboard_with_shift_registers.ino +++ b/tutorials/keybrd_4b_split_keyboard_with_shift_registers/keybrd_4b_split_keyboard_with_shift_registers.ino @@ -19,8 +19,8 @@ The right matrix has 2 shift registers daisy chained. #include //Right matrix -#include //needed?? todo -#include +//#include //needed?? todo +#include // =============== CONFIGURATION =============== ScanDelay scanDelay(9000); @@ -62,7 +62,7 @@ Row row_L1(scanner_L, 1, ptrsKeys_L1, KEY_COUNT_L1); // =============== RIGHT MATRIX ================ //use slaveSelect pin SS (Arduino pin 10) for fastest scan -Scanner_ShiftRegs74HC165 scanner_R(HIGH, SS, 14); //active HIGH +Scanner_ShiftRegsPISOSingleRow scanner_R(HIGH, SS, 2); //active HIGH //rows Key* ptrsKeys_R0[] = { &s_6, &s_5, &s_4, &s_3, //shift register on right