From ae96a3d79ca130db607573796f5e5d98ca7843a4 Mon Sep 17 00:00:00 2001 From: wolfv6 Date: Sun, 11 Sep 2016 16:45:52 -0600 Subject: [PATCH] move PortWrite_MCP23S17::push() to PortMCP23S17 --- src/PortMCP23S17.cpp | 12 ++++++++++++ src/PortMCP23S17.h | 12 ++++++++++++ src/PortRead_MCP23S17.cpp | 14 +++----------- src/PortRead_MCP23S17.h | 5 +++-- src/PortWrite_MCP23S17.cpp | 15 ++------------- src/PortWrite_MCP23S17.h | 4 ++-- 6 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 src/PortMCP23S17.cpp create mode 100644 src/PortMCP23S17.h diff --git a/src/PortMCP23S17.cpp b/src/PortMCP23S17.cpp new file mode 100644 index 0000000..5ac4583 --- /dev/null +++ b/src/PortMCP23S17.cpp @@ -0,0 +1,12 @@ +#include "PortMCP23S17.h" + +/* push() writes data to registerAddr. +*/ +void PortMCP23S17::push(const uint8_t command, const uint8_t registerAddr, const uint8_t data) +{ + digitalWrite(SS, LOW); //enable Slave Select + SPI.transfer(command); //write command todo also read command? + SPI.transfer(registerAddr); //register address to write data to + SPI.transfer(data); //write the data + digitalWrite(SS, HIGH); //disable Slave Select +} diff --git a/src/PortMCP23S17.h b/src/PortMCP23S17.h new file mode 100644 index 0000000..0ea726b --- /dev/null +++ b/src/PortMCP23S17.h @@ -0,0 +1,12 @@ +#ifndef PORTMCP23S17_H +#define PORTMCP23S17_H +#include +#include +#include + +class PortMCP23S17 +{ + protected: + void push(const uint8_t command, const uint8_t registerAddr, const uint8_t data); +}; +#endif diff --git a/src/PortRead_MCP23S17.cpp b/src/PortRead_MCP23S17.cpp index 1071dc9..7c78008 100644 --- a/src/PortRead_MCP23S17.cpp +++ b/src/PortRead_MCP23S17.cpp @@ -14,17 +14,9 @@ void PortRead_MCP23S17::begin(const uint8_t strobeOn) pullUp = 0; } - digitalWrite(SS, LOW); //enable Slave Select - SPI.transfer(port.DEVICE_ADDR << 1); //write command - SPI.transfer(port.num); //configure IODIR - SPI.transfer(readPins); //0=output (for LED), 1=input (for read) - digitalWrite(SS, HIGH); //enable Slave Select - - digitalWrite(SS, LOW); //disable Slave Select - SPI.transfer(port.DEVICE_ADDR << 1); //write command - SPI.transfer(port.num + 0x0C); //configure GPPU - SPI.transfer(pullUp); //0=pull-up disabled (for LED), 1=pull-up enabled (for read) - digitalWrite(SS, HIGH); //disable Slave Select + push(port.DEVICE_ADDR << 1, port.num, readPins); //write, configure IODIR, 0=output, 1=input + push(port.DEVICE_ADDR << 1, port.num + 0x0C, pullUp); //write, configure GPPU, + //0=pull-up disabled, 1=pull-up enabled } /* read() returns portState. diff --git a/src/PortRead_MCP23S17.h b/src/PortRead_MCP23S17.h index 848bba9..f49a7b1 100644 --- a/src/PortRead_MCP23S17.h +++ b/src/PortRead_MCP23S17.h @@ -4,6 +4,7 @@ #include #include #include +#include "PortMCP23S17.h" #include "PortIOE.h" #include "Scanner_IOE.h" @@ -15,7 +16,7 @@ Arduino Pin 10 avoids the speed penalty of digitalWrite. Instantiation ------------ readPins parameter is port's bitwise pin configuration - 1=configure as input (for pins connected to column) + 1=configure as input (for read pins connected to column) 0=configure as output (for LED or not connected to a column) readPins are read from pin 0 on up. @@ -32,7 +33,7 @@ MCP23S17 data sheet ------------------ http://www.onsemi.com/pub_link/Collateral/MCP23S17-D.PDF */ -class PortRead_MCP23S17 : public PortReadInterface +class PortRead_MCP23S17 : public PortReadInterface, public PortMCP23S17 { private: PortIOE& port; diff --git a/src/PortWrite_MCP23S17.cpp b/src/PortWrite_MCP23S17.cpp index 6585e4c..954be01 100644 --- a/src/PortWrite_MCP23S17.cpp +++ b/src/PortWrite_MCP23S17.cpp @@ -1,16 +1,5 @@ #include "PortWrite_MCP23S17.h" -/* push() writes data to registerAddr. -*/ -void PortWrite_MCP23S17::push(const uint8_t registerAddr, const uint8_t data) -{ - digitalWrite(SS, LOW); //enable Slave Select - SPI.transfer(port.DEVICE_ADDR << 1); //write command - SPI.transfer(registerAddr); //register address to write data to - SPI.transfer(data); //write the data - digitalWrite(SS, HIGH); //disable Slave Select -} - /* begin() is called from Scanner_IOE::begin(). Initiates SPI bus and configures write pins to output. MCP23S17 SPI interface is 10 MHz max. @@ -23,7 +12,7 @@ void PortWrite_MCP23S17::begin() SPI.beginTransaction(SPISettings (5000000, MSBFIRST, SPI_MODE0)); //control SPI bus, 5 MHz //SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device. - push(port.num, 0); //configure port direction (port.num) to output (0) + push(port.DEVICE_ADDR << 1, port.num, 0); //configure port direction (port.num) to output (0) } /* write() sets pin output to logicLevel. @@ -42,5 +31,5 @@ void PortWrite_MCP23S17::write(const uint8_t pin, const bool logicLevel) port.outputVal |= pin; //set pin output to high } - push(port.num + 0x12, port.outputVal); //set GPIO port pin to outputVal + push(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port pin to outputVal } diff --git a/src/PortWrite_MCP23S17.h b/src/PortWrite_MCP23S17.h index 9cabb86..6337eca 100644 --- a/src/PortWrite_MCP23S17.h +++ b/src/PortWrite_MCP23S17.h @@ -4,6 +4,7 @@ #include #include #include +#include "PortMCP23S17.h" #include "PortIOE.h" /* One MCP23S17 I/O expander port connected to matrix rows. @@ -28,11 +29,10 @@ MCP23S17 data sheet http://www.onsemi.com/pub_link/Collateral/MCP23S17-D.PDF */ -class PortWrite_MCP23S17 : public PortWriteInterface +class PortWrite_MCP23S17 : public PortWriteInterface, public PortMCP23S17 { private: PortIOE& port; - void push(const uint8_t registerAddr, const uint8_t data); public: PortWrite_MCP23S17(PortIOE& port) : port(port) {} void begin();