#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 | |||||
} |
#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 |
pullUp = 0; | 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. | /* read() returns portState. |
#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" | ||||
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. | ||||
------------------ | ------------------ | ||||
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; |
#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. | ||||
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. | ||||
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 | |||||
} | } |
#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. | ||||
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(); |