move PortWrite_MCP23S17::push() to PortMCP23S17
This commit is contained in:
parent
c2c0c02208
commit
ae96a3d79c
12
src/PortMCP23S17.cpp
Normal file
12
src/PortMCP23S17.cpp
Normal file
@ -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
|
||||||
|
}
|
12
src/PortMCP23S17.h
Normal file
12
src/PortMCP23S17.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef PORTMCP23S17_H
|
||||||
|
#define PORTMCP23S17_H
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <SPI.h>
|
||||||
|
|
||||||
|
class PortMCP23S17
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
void push(const uint8_t command, const uint8_t registerAddr, const uint8_t data);
|
||||||
|
};
|
||||||
|
#endif
|
@ -14,17 +14,9 @@ void PortRead_MCP23S17::begin(const uint8_t strobeOn)
|
|||||||
pullUp = 0;
|
pullUp = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
digitalWrite(SS, LOW); //enable Slave Select
|
push(port.DEVICE_ADDR << 1, port.num, readPins); //write, configure IODIR, 0=output, 1=input
|
||||||
SPI.transfer(port.DEVICE_ADDR << 1); //write command
|
push(port.DEVICE_ADDR << 1, port.num + 0x0C, pullUp); //write, configure GPPU,
|
||||||
SPI.transfer(port.num); //configure IODIR
|
//0=pull-up disabled, 1=pull-up enabled
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read() returns portState.
|
/* read() returns portState.
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <PortReadInterface.h>
|
#include <PortReadInterface.h>
|
||||||
|
#include "PortMCP23S17.h"
|
||||||
#include "PortIOE.h"
|
#include "PortIOE.h"
|
||||||
#include "Scanner_IOE.h"
|
#include "Scanner_IOE.h"
|
||||||
|
|
||||||
@ -15,7 +16,7 @@ Arduino Pin 10 avoids the speed penalty of digitalWrite.
|
|||||||
Instantiation
|
Instantiation
|
||||||
------------
|
------------
|
||||||
readPins parameter is port's bitwise pin configuration
|
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)
|
0=configure as output (for LED or not connected to a column)
|
||||||
readPins are read from pin 0 on up.
|
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
|
http://www.onsemi.com/pub_link/Collateral/MCP23S17-D.PDF
|
||||||
*/
|
*/
|
||||||
class PortRead_MCP23S17 : public PortReadInterface
|
class PortRead_MCP23S17 : public PortReadInterface, public PortMCP23S17
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
PortIOE& port;
|
PortIOE& port;
|
||||||
|
@ -1,16 +1,5 @@
|
|||||||
#include "PortWrite_MCP23S17.h"
|
#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().
|
/* begin() is called from Scanner_IOE::begin().
|
||||||
Initiates SPI bus and configures write pins to output.
|
Initiates SPI bus and configures write pins to output.
|
||||||
MCP23S17 SPI interface is 10 MHz max.
|
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.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.
|
//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.
|
/* 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
|
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
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <PortWriteInterface.h>
|
#include <PortWriteInterface.h>
|
||||||
|
#include "PortMCP23S17.h"
|
||||||
#include "PortIOE.h"
|
#include "PortIOE.h"
|
||||||
|
|
||||||
/* One MCP23S17 I/O expander port connected to matrix rows.
|
/* 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
|
http://www.onsemi.com/pub_link/Collateral/MCP23S17-D.PDF
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class PortWrite_MCP23S17 : public PortWriteInterface
|
class PortWrite_MCP23S17 : public PortWriteInterface, public PortMCP23S17
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
PortIOE& port;
|
PortIOE& port;
|
||||||
void push(const uint8_t registerAddr, const uint8_t data);
|
|
||||||
public:
|
public:
|
||||||
PortWrite_MCP23S17(PortIOE& port) : port(port) {}
|
PortWrite_MCP23S17(PortIOE& port) : port(port) {}
|
||||||
void begin();
|
void begin();
|
||||||
|
Reference in New Issue
Block a user