Skip to content

Commit 7ebbe68

Browse files
committed
Added pullup and inverted to portMode and pinMode signature.
Reversed iodir logic to match other registers #4
1 parent 45e3405 commit 7ebbe68

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

examples/RegistersDumper/RegistersDumper.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ void setup() {
88
mcp.init();
99

1010
Serial.begin(115200);
11+
1112
uint8_t conf = mcp.readRegister(MCP23017_REGISTER::IODIRA);
1213
Serial.print("IODIRA : ");
1314
Serial.print(conf, BIN);

src/MCP23017.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,43 @@ void MCP23017::init()
2222
writeRegister(MCP23017_REGISTER::GPPUA, 0xFF, 0xFF);
2323
}
2424

25-
void MCP23017::portMode(MCP23017_PORT port, uint8_t value)
25+
void MCP23017::portMode(MCP23017_PORT port, uint8_t directions, uint8_t pullups, uint8_t inverted)
2626
{
27-
writeRegister(MCP23017_REGISTER::IODIRA + port, value);
27+
writeRegister(MCP23017_REGISTER::IODIRA + port, directions);
28+
writeRegister(MCP23017_REGISTER::GPPUA + port, pullups);
29+
writeRegister(MCP23017_REGISTER::IPOLA + port, inverted);
2830
}
2931

30-
void MCP23017::pinMode(uint8_t pin, uint8_t mode)
32+
void MCP23017::pinMode(uint8_t pin, uint8_t mode, bool inverted)
3133
{
3234
MCP23017_REGISTER iodirreg = MCP23017_REGISTER::IODIRA;
33-
uint8_t iodir;
35+
MCP23017_REGISTER pullupreg = MCP23017_REGISTER::GPPUA;
36+
MCP23017_REGISTER polreg = MCP23017_REGISTER::IPOLA;
37+
uint8_t iodir, pol, pull;
38+
3439
if(pin > 7)
3540
{
3641
iodirreg = MCP23017_REGISTER::IODIRB;
42+
pullupreg = MCP23017_REGISTER::GPPUB;
43+
polreg = MCP23017_REGISTER::IPOLB;
3744
pin -= 8;
3845
}
3946

4047
iodir = readRegister(iodirreg);
41-
if(mode == OUTPUT) iodir &= ~_BV(pin);
42-
else iodir |= _BV(pin);
48+
if(mode == INPUT || mode == INPUT_PULLUP) iodir |= _BV(pin);
49+
else iodir &= ~_BV(pin);
50+
51+
pull = readRegister(pullupreg);
52+
if(mode == INPUT_PULLUP) pull |= _BV(pin);
53+
else pull &= ~_BV(pin);
54+
55+
pol = readRegister(polreg);
56+
if(inverted) pol |= _BV(pin);
57+
else pol &= ~_BV(pin);
4358

4459
writeRegister(iodirreg, iodir);
60+
writeRegister(pullupreg, pull);
61+
writeRegister(polreg, pol);
4562
}
4663

4764
void MCP23017::digitalWrite(uint8_t pin, uint8_t state)

src/MCP23017.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class MCP23017
8585
*
8686
* See "3.5.1 I/O Direction register".
8787
*/
88-
void portMode(MCP23017_PORT port, uint8_t value);
88+
void portMode(MCP23017_PORT port, uint8_t directions, uint8_t pullups = 0xFF, uint8_t inverted = 0x00);
8989
/**
9090
* Controls a single pin direction.
9191
* Pin 0-7 for port A, 8-15 fo port B.
@@ -94,8 +94,11 @@ class MCP23017
9494
* 0 = Pin is configured as an output.
9595
*
9696
* See "3.5.1 I/O Direction register".
97+
*
98+
* Beware! Mode here behave as the standard arduino pinMode :
99+
* [ OUTPUT | INPUT | INPUT_PULLUP ]
97100
*/
98-
void pinMode(uint8_t pin, uint8_t mode);
101+
void pinMode(uint8_t pin, uint8_t mode, bool inverted = false);
99102

100103
/**
101104
* Writes a single pin state.

0 commit comments

Comments
 (0)