diff --git a/src/PortMCP23S17.cpp b/src/PortMCP23S17.cpp index 5ac4583..7de52cf 100644 --- a/src/PortMCP23S17.cpp +++ b/src/PortMCP23S17.cpp @@ -1,12 +1,16 @@ #include "PortMCP23S17.h" -/* push() writes data to registerAddr. +/* transfer() writes data to registerAddr, reads portSate from registerAddr, and returns portState. */ -void PortMCP23S17::push(const uint8_t command, const uint8_t registerAddr, const uint8_t data) +uint8_t PortMCP23S17::transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data) { + uint8_t portState; //bit wise + 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 + portState = SPI.transfer(data); //write data, read portState digitalWrite(SS, HIGH); //disable Slave Select + + return portState; } diff --git a/src/PortMCP23S17.h b/src/PortMCP23S17.h index 0ea726b..bd28611 100644 --- a/src/PortMCP23S17.h +++ b/src/PortMCP23S17.h @@ -7,6 +7,6 @@ class PortMCP23S17 { protected: - void push(const uint8_t command, const uint8_t registerAddr, const uint8_t data); + uint8_t transfer(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 7c78008..b015903 100644 --- a/src/PortRead_MCP23S17.cpp +++ b/src/PortRead_MCP23S17.cpp @@ -14,8 +14,8 @@ void PortRead_MCP23S17::begin(const uint8_t strobeOn) pullUp = 0; } - 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, + transfer(port.DEVICE_ADDR << 1, port.num, readPins); //write, configure IODIR, 0=output, 1=input + transfer(port.DEVICE_ADDR << 1, port.num + 0x0C, pullUp); //write, configure GPPU, //0=pull-up disabled, 1=pull-up enabled } @@ -24,13 +24,5 @@ Only portState bits of readPins are valid. */ uint8_t PortRead_MCP23S17::read() { - uint8_t portState; //bit wise - - digitalWrite(SS, LOW); //enable Slave Select - SPI.transfer( (port.DEVICE_ADDR << 1) | 1); //read command - SPI.transfer(port.num + 0x12); //GPIO register address to read data from - portState = SPI.transfer(0); //save the data (0 is dummy data to send) - digitalWrite(SS, HIGH); //disable Slave Select - - return portState; + return transfer( (port.DEVICE_ADDR << 1) | 1, port.num + 0x12, 0); //read from GPIO } diff --git a/src/PortWrite_MCP23S17.cpp b/src/PortWrite_MCP23S17.cpp index 954be01..6d93b05 100644 --- a/src/PortWrite_MCP23S17.cpp +++ b/src/PortWrite_MCP23S17.cpp @@ -12,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.DEVICE_ADDR << 1, port.num, 0); //configure port direction (port.num) to output (0) + transfer(port.DEVICE_ADDR << 1, port.num, 0); //configure port direction (port.num) to output (0) } /* write() sets pin output to logicLevel. @@ -31,5 +31,5 @@ void PortWrite_MCP23S17::write(const uint8_t pin, const bool logicLevel) port.outputVal |= pin; //set pin output to high } - push(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port pin to outputVal + transfer(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port to outputVal }