2016-09-25 03:03:08 +00:00
|
|
|
#ifndef PORT_MCP23S17_H
|
|
|
|
#define PORT_MCP23S17_H
|
2016-09-11 22:45:52 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <SPI.h>
|
2016-09-23 07:13:12 +00:00
|
|
|
#include <PortInterface.h>
|
2016-09-11 22:45:52 +00:00
|
|
|
|
2016-09-23 07:13:12 +00:00
|
|
|
/*
|
2016-09-24 19:04:52 +00:00
|
|
|
write pins are connected to matrix Row (strobe pin) or LED.
|
|
|
|
readPins are connected to matrix column to read which keys are pressed.
|
2016-09-23 07:13:12 +00:00
|
|
|
|
|
|
|
Slave Select is hardcoded to Arduino Pin 10.
|
|
|
|
Arduino Pin 10 avoids the speed penalty of digitalWrite.
|
|
|
|
|
|
|
|
Instantiation
|
|
|
|
------------
|
2016-09-25 03:03:08 +00:00
|
|
|
MCP23S17 datasheet identifies ports by letters, while class Port_MCP23S17 uses portNum
|
2016-09-24 17:26:08 +00:00
|
|
|
for port A, use portNum=0
|
|
|
|
for port B, use portNum=1
|
|
|
|
readPins parameter configures port's pins.
|
2016-09-23 07:13:12 +00:00
|
|
|
|
2016-09-24 17:26:08 +00:00
|
|
|
Example instantiation:
|
2016-09-24 19:04:52 +00:00
|
|
|
const uint8_t IOE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded
|
|
|
|
Port_MCP23S17 portB(IOE_ADDR, 1, 0); //all pins are set to output for strobes and LEDs
|
2016-09-28 19:56:10 +00:00
|
|
|
Port_MCP23S17 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //pin 0 and pin 1 are set to input for reading,
|
2016-09-24 17:26:08 +00:00
|
|
|
//remaining pins can be used for LEDs
|
2016-09-23 07:13:12 +00:00
|
|
|
|
|
|
|
Diode orientation
|
|
|
|
----------------
|
|
|
|
Diode orientation is explained in keybrd_library_user_guide.md > Diode orientation
|
|
|
|
|
|
|
|
MCP23S17 data sheet
|
|
|
|
------------------
|
|
|
|
http://www.onsemi.com/pub_link/Collateral/MCP23S17-D.PDF
|
|
|
|
*/
|
2016-09-25 03:03:08 +00:00
|
|
|
class Port_MCP23S17 : public PortInterface
|
2016-09-11 22:45:52 +00:00
|
|
|
{
|
2016-09-23 07:13:12 +00:00
|
|
|
private:
|
2016-09-24 18:08:47 +00:00
|
|
|
const uint8_t deviceAddr;
|
2016-09-24 17:26:08 +00:00
|
|
|
const uint8_t portNum; //port identification number
|
|
|
|
uint8_t outputVal; //bit pattern for strobe and LEDs
|
2016-09-24 19:04:52 +00:00
|
|
|
const uint8_t readPins; //bit pattern, IODIR 0=output, 1=input
|
2016-09-11 23:03:00 +00:00
|
|
|
uint8_t transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data);
|
2016-09-23 07:13:12 +00:00
|
|
|
public:
|
2016-09-25 03:03:08 +00:00
|
|
|
Port_MCP23S17(const uint8_t deviceAddr, const uint8_t portNum, const uint8_t readPins)
|
2016-09-24 18:08:47 +00:00
|
|
|
: deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
|
2016-09-24 14:56:02 +00:00
|
|
|
void beginProtocol();
|
2016-09-23 07:13:12 +00:00
|
|
|
void begin(const uint8_t strobeOn);
|
|
|
|
virtual void write(const uint8_t pin, const bool logicLevel);
|
|
|
|
virtual uint8_t read();
|
2016-09-11 22:45:52 +00:00
|
|
|
};
|
|
|
|
#endif
|