Skip to content

Commit a7c9317

Browse files
committed
Added initialLevel parameter to pinMode() and pinDir()
1 parent f94a3b5 commit a7c9317

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

Diff for: src/SparkFunSX1509.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,22 @@ void SX1509::reset(bool hardware)
105105
}
106106
}
107107

108-
void SX1509::pinDir(byte pin, byte inOut)
108+
void SX1509::pinDir(byte pin, byte inOut, byte initialLevel)
109109
{
110110
// The SX1509 RegDir registers: REG_DIR_B, REG_DIR_A
111111
// 0: IO is configured as an output
112112
// 1: IO is configured as an input
113113
byte modeBit;
114-
if ((inOut == OUTPUT) || (inOut == ANALOG_OUTPUT))
115-
modeBit = 0;
116-
else
114+
if ((inOut == OUTPUT) || (inOut == ANALOG_OUTPUT)) {
115+
unsigned int tempRegData = readWord(REG_DATA_B);
116+
if (initialLevel == LOW) {
117+
tempRegData &= ~(1<<pin);
118+
writeWord(REG_DATA_B, tempRegData);
119+
}
120+
modeBit = 0;
121+
} else {
117122
modeBit = 1;
123+
}
118124

119125
unsigned int tempRegDir = readWord(REG_DIR_B);
120126
if (modeBit)
@@ -134,9 +140,9 @@ void SX1509::pinDir(byte pin, byte inOut)
134140
}
135141
}
136142

137-
void SX1509::pinMode(byte pin, byte inOut)
143+
void SX1509::pinMode(byte pin, byte inOut, byte initialLevel)
138144
{
139-
pinDir(pin, inOut);
145+
pinDir(pin, inOut, initialLevel);
140146
}
141147

142148
void SX1509::writePin(byte pin, byte highLow)
@@ -737,8 +743,6 @@ unsigned int SX1509::readWord(byte registerAddress)
737743
// - No return value.
738744
void SX1509::readBytes(byte firstRegisterAddress, byte * destination, byte length)
739745
{
740-
byte readValue;
741-
742746
Wire.beginTransmission(deviceAddress);
743747
Wire.write(firstRegisterAddress);
744748
Wire.endTransmission();

Diff for: src/SparkFunSX1509.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ class SX1509
117117
// - inOut: The Arduino INPUT and OUTPUT constants should be used for the
118118
// inOut parameter. They do what they say!
119119
// -----------------------------------------------------------------------------
120-
void pinMode(byte pin, byte inOut);
121-
void pinDir(byte pin, byte inOut); // Legacy - use pinMode
120+
void pinMode(byte pin, byte inOut, byte initialLevel = HIGH);
121+
void pinDir(byte pin, byte inOut, byte initialLevel = HIGH); // Legacy - use pinMode
122122

123123
// -----------------------------------------------------------------------------
124124
// digitalWrite(byte pin, byte highLow): This function writes a pin to either high

0 commit comments

Comments
 (0)