From a45efab44f8741490c400a0e58c39aef000fe4be Mon Sep 17 00:00:00 2001 From: wolfv6 Date: Sun, 30 Oct 2016 17:25:44 -0600 Subject: [PATCH] add PortWriteInterface, Port_ShiftRegs --- src/LED_Port.h | 1 - src/LED_ShiftReg.cpp | 11 +++++++++ src/LED_ShiftReg.h | 22 ++++++++++++++++++ src/LED_ShiftRegs.cpp | 33 --------------------------- src/LED_ShiftRegs.h | 22 ------------------ src/PortWriteInterface.h | 11 +++++++++ src/Port_ShiftRegs.cpp | 31 +++++++++++++++++++++++++ src/Port_ShiftRegs.h | 21 +++++++++++++++++ src/Scanner_ShiftRegsPISOMultiRow.cpp | 2 -- 9 files changed, 96 insertions(+), 58 deletions(-) create mode 100644 src/LED_ShiftReg.cpp create mode 100644 src/LED_ShiftReg.h delete mode 100644 src/LED_ShiftRegs.cpp delete mode 100644 src/LED_ShiftRegs.h create mode 100644 src/PortWriteInterface.h create mode 100644 src/Port_ShiftRegs.cpp create mode 100644 src/Port_ShiftRegs.h diff --git a/src/LED_Port.h b/src/LED_Port.h index ab1b586..c541751 100644 --- a/src/LED_Port.h +++ b/src/LED_Port.h @@ -2,7 +2,6 @@ #define LED_PORT_H #include #include -#include #include #include diff --git a/src/LED_ShiftReg.cpp b/src/LED_ShiftReg.cpp new file mode 100644 index 0000000..cfb3b17 --- /dev/null +++ b/src/LED_ShiftReg.cpp @@ -0,0 +1,11 @@ +#include "LED_ShiftReg.h" + +void LED_ShiftReg::on() +{ + refPort.write(pin, HIGH); +} + +void LED_ShiftReg::off() +{ + refPort.write(pin, LOW); +} diff --git a/src/LED_ShiftReg.h b/src/LED_ShiftReg.h new file mode 100644 index 0000000..d4a9acf --- /dev/null +++ b/src/LED_ShiftReg.h @@ -0,0 +1,22 @@ +#ifndef LED_SHIFTREG_H +#define LED_SHIFTREG_H +#include +#include +#include +#include + +/* LED_ShiftReg turns LED on and off. +shift register RCLK pin a.k.a. SS or ST +*/ +class LED_ShiftReg: public LEDInterface +{ + private: + PortWriteInterface& refPort; + const uint8_t pin; //bit pattern, 1 is shift-register pin connected to an LED + public: + LED_ShiftReg(PortWriteInterface& refPort, uint8_t pin) + : refPort(refPort), pin(pin) {} + virtual void on(); + virtual void off(); +}; +#endif diff --git a/src/LED_ShiftRegs.cpp b/src/LED_ShiftRegs.cpp deleted file mode 100644 index e4fd549..0000000 --- a/src/LED_ShiftRegs.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "LED_ShiftRegs.h" - -/* constructor -*/ -LED_ShiftRegs::LED_ShiftRegs(const uint8_t slaveSelect, const uint8_t pin) - :slaveSelect(slaveSelect), pin(pin) -{ - pinMode(slaveSelect, OUTPUT); -} - -/* begin() should be called once from sketch setup(). -Initializes shift register's shift/load pin. -*/ -void LED_ShiftRegs::begin() -{ - SPI.begin(); - digitalWrite(slaveSelect, HIGH); -} - -//todo preserve other LED values, similar to Port_PCA9655E outputVal -void LED_ShiftRegs::on() -{ - digitalWrite(slaveSelect, LOW); - SPI.transfer(pin); - digitalWrite (slaveSelect, HIGH); -} - -void LED_ShiftRegs::off() -{ - digitalWrite(slaveSelect, LOW); - SPI.transfer(0); - digitalWrite (slaveSelect, HIGH); -} diff --git a/src/LED_ShiftRegs.h b/src/LED_ShiftRegs.h deleted file mode 100644 index 9bc14e6..0000000 --- a/src/LED_ShiftRegs.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef LED_SHIFTREGS_H -#define LED_SHIFTREGS_H -#include -#include -#include -#include - -/* A LED_ShiftRegs turns LED on and off. -shift register RCLK pin a.k.a. SS or ST -*/ -class LED_ShiftRegs: public LEDInterface -{ - private: - const uint8_t slaveSelect;//controller pin number connected to shift register RCLK - const uint8_t pin; //bit pattern, shift register pin that is connected to an LED - public: - LED_ShiftRegs(const uint8_t slaveSelect, const uint8_t pin); - void begin(); - virtual void on(); - virtual void off(); -}; -#endif diff --git a/src/PortWriteInterface.h b/src/PortWriteInterface.h new file mode 100644 index 0000000..037be89 --- /dev/null +++ b/src/PortWriteInterface.h @@ -0,0 +1,11 @@ +#ifndef PORTWRITEINTERFACE_H +#define PORTWRITEINTERFACE_H +#include +#include + +class PortWriteInterface +{ + public: + virtual void write(const uint8_t pin, const bool pinLogicLevel)=0; +}; +#endif diff --git a/src/Port_ShiftRegs.cpp b/src/Port_ShiftRegs.cpp new file mode 100644 index 0000000..15ae6bd --- /dev/null +++ b/src/Port_ShiftRegs.cpp @@ -0,0 +1,31 @@ +#include "Port_ShiftRegs.h" + +Port_ShiftRegs::Port_ShiftRegs(const uint8_t slaveSelect) : slaveSelect(slaveSelect) +{ + pinMode(slaveSelect, OUTPUT); +} + +/* begin() should be called once from sketch setup(). +Initializes shift register's shift/load pin. +*/ +void Port_ShiftRegs::begin() +{ + digitalWrite(slaveSelect, HIGH); + SPI.begin(); +} + +void Port_ShiftRegs::write(const uint8_t pin, const bool logicLevel) +{ + if (logicLevel == LOW) + { + outputVal &= ~pin; //set pin output to low + } + else + { + outputVal |= pin; //set pin output to high + } + + digitalWrite(slaveSelect, LOW); + SPI.transfer(outputVal); + digitalWrite (slaveSelect, HIGH); +} diff --git a/src/Port_ShiftRegs.h b/src/Port_ShiftRegs.h new file mode 100644 index 0000000..f21f432 --- /dev/null +++ b/src/Port_ShiftRegs.h @@ -0,0 +1,21 @@ +#ifndef PORT_SHIFTREGS_H +#define PORT_SHIFTREGS_H +#include +#include +#include +#include + +/* Port_ShiftRegs +shift register RCLK pin a.k.a. SS or ST +*/ +class Port_ShiftRegs : public PortWriteInterface +{ + private: + const uint8_t slaveSelect; //controller-pin number connected to shift register RCLK + uint8_t outputVal; //bit pattern for LEDs + public: + Port_ShiftRegs(const uint8_t slaveSelect); + void begin(); + void write(const uint8_t pin, const bool logicLevel); +}; +#endif diff --git a/src/Scanner_ShiftRegsPISOMultiRow.cpp b/src/Scanner_ShiftRegsPISOMultiRow.cpp index b868002..e29972b 100644 --- a/src/Scanner_ShiftRegsPISOMultiRow.cpp +++ b/src/Scanner_ShiftRegsPISOMultiRow.cpp @@ -1,7 +1,5 @@ #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),