@@ -113,20 +113,24 @@ void SX1509::pinDir(byte pin, byte inOut, byte initialLevel)
113
113
// 0: IO is configured as an output
114
114
// 1: IO is configured as an input
115
115
byte modeBit;
116
- if ((inOut == OUTPUT) || (inOut == ANALOG_OUTPUT)) {
116
+ if ((inOut == OUTPUT) || (inOut == ANALOG_OUTPUT))
117
+ {
117
118
uint16_t tempRegData = readWord (REG_DATA_B);
118
- if (initialLevel == LOW) {
119
- tempRegData &= ~(1 <<pin);
120
- writeWord (REG_DATA_B, tempRegData);
121
- }
122
- modeBit = 0 ;
123
- } else {
119
+ if (initialLevel == LOW)
120
+ {
121
+ tempRegData &= ~(1 << pin);
122
+ writeWord (REG_DATA_B, tempRegData);
123
+ }
124
+ modeBit = 0 ;
125
+ }
126
+ else
127
+ {
124
128
modeBit = 1 ;
125
- }
126
-
129
+ }
130
+
127
131
uint16_t tempRegDir = readWord (REG_DIR_B);
128
- if (modeBit)
129
- tempRegDir |= (1 << pin);
132
+ if (modeBit)
133
+ tempRegDir |= (1 << pin);
130
134
else
131
135
tempRegDir &= ~(1 << pin);
132
136
@@ -149,31 +153,33 @@ void SX1509::pinMode(byte pin, byte inOut, byte initialLevel)
149
153
150
154
bool SX1509::writePin (byte pin, byte highLow)
151
155
{
152
-
156
+
153
157
uint16_t tempRegDir = readWord (REG_DIR_B);
154
-
155
- if ((0xFFFF ^ tempRegDir) & (1 << pin)) // If the pin is an output, write high/low
158
+
159
+ if ((0xFFFF ^ tempRegDir) & (1 << pin)) // If the pin is an output, write high/low
156
160
{
157
161
uint16_t tempRegData = readWord (REG_DATA_B);
158
- if (highLow) tempRegData |= (1 << pin);
159
- else tempRegData &= ~(1 << pin);
162
+ if (highLow)
163
+ tempRegData |= (1 << pin);
164
+ else
165
+ tempRegData &= ~(1 << pin);
160
166
return writeWord (REG_DATA_B, tempRegData);
161
167
}
162
168
else // Otherwise the pin is an input, pull-up/down
163
169
{
164
170
uint16_t tempPullUp = readWord (REG_PULL_UP_B);
165
171
uint16_t tempPullDown = readWord (REG_PULL_DOWN_B);
166
-
167
- if (highLow) // if HIGH, do pull-up, disable pull-down
172
+
173
+ if (highLow) // if HIGH, do pull-up, disable pull-down
168
174
{
169
- tempPullUp |= (1 << pin);
170
- tempPullDown &= ~(1 << pin);
175
+ tempPullUp |= (1 << pin);
176
+ tempPullDown &= ~(1 << pin);
171
177
return writeWord (REG_PULL_UP_B, tempPullUp) && writeWord (REG_PULL_DOWN_B, tempPullDown);
172
178
}
173
179
else // If LOW do pull-down, disable pull-up
174
180
{
175
- tempPullDown |= (1 << pin);
176
- tempPullUp &= ~(1 << pin);
181
+ tempPullDown |= (1 << pin);
182
+ tempPullUp &= ~(1 << pin);
177
183
return writeWord (REG_PULL_UP_B, tempPullUp) && writeWord (REG_PULL_DOWN_B, tempPullDown);
178
184
}
179
185
}
@@ -187,43 +193,50 @@ bool SX1509::digitalWrite(byte pin, byte highLow)
187
193
byte SX1509::readPin (byte pin)
188
194
{
189
195
uint16_t tempRegDir = readWord (REG_DIR_B);
190
-
191
- if (tempRegDir & (1 << pin)) // If the pin is an input
196
+
197
+ if (tempRegDir & (1 << pin)) // If the pin is an input
192
198
{
193
199
uint16_t tempRegData = readWord (REG_DATA_B);
194
- if (tempRegData & (1 << pin))
200
+ if (tempRegData & (1 << pin))
195
201
return 1 ;
196
- } else {
197
- // log_d("Pin %d not INPUT, REG_DIR_B: %d", pin, tempRegDir);
198
- }
199
-
202
+ }
203
+ else
204
+ {
205
+ // log_d("Pin %d not INPUT, REG_DIR_B: %d", pin, tempRegDir);
206
+ }
207
+
200
208
return 0 ;
201
209
}
202
210
203
- bool SX1509::readPin (const byte pin, bool * value)
211
+ bool SX1509::readPin (const byte pin, bool * value)
204
212
{
205
- uint16_t tempRegDir;
206
- if (readWord (REG_DIR_B, &tempRegDir)) {
207
- if (tempRegDir & (1 <<pin)) { // If the pin is an input
208
- uint16_t tempRegData;
209
- if (readWord (REG_DATA_B, &tempRegData)) {
210
- *value = (tempRegData & (1 <<pin)) != 0 ;
211
- return true ;
212
- };
213
- } else {
214
- *value = false ;
215
- return true ;
216
- }
217
- }
218
- return false ;
213
+ uint16_t tempRegDir;
214
+ if (readWord (REG_DIR_B, &tempRegDir))
215
+ {
216
+ if (tempRegDir & (1 << pin))
217
+ { // If the pin is an input
218
+ uint16_t tempRegData;
219
+ if (readWord (REG_DATA_B, &tempRegData))
220
+ {
221
+ *value = (tempRegData & (1 << pin)) != 0 ;
222
+ return true ;
223
+ };
224
+ }
225
+ else
226
+ {
227
+ *value = false ;
228
+ return true ;
229
+ }
230
+ }
231
+ return false ;
219
232
}
220
233
221
234
byte SX1509::digitalRead (byte pin)
222
235
{
223
236
return readPin (pin);
224
237
}
225
238
226
- bool SX1509::digitalRead (byte pin, bool * value)
239
+ bool SX1509::digitalRead (byte pin, bool * value)
227
240
{
228
241
return readPin (pin, value);
229
242
}
@@ -573,7 +586,7 @@ void SX1509::enableInterrupt(byte pin, byte riseFall)
573
586
{
574
587
// Set REG_INTERRUPT_MASK
575
588
uint16_t tempWord = readWord (REG_INTERRUPT_MASK_B);
576
- tempWord &= ~(1 << pin); // 0 = event on IO will trigger interrupt
589
+ tempWord &= ~(1 << pin); // 0 = event on IO will trigger interrupt
577
590
writeWord (REG_INTERRUPT_MASK_B, tempWord);
578
591
579
592
byte sensitivity = 0 ;
@@ -750,9 +763,9 @@ uint16_t SX1509::readWord(byte registerAddress)
750
763
return readValue;
751
764
}
752
765
753
- bool SX1509::readByte (byte registerAddress, byte* value)
766
+ bool SX1509::readByte (byte registerAddress, byte * value)
754
767
{
755
- return readBytes (registerAddress, value, 1 );
768
+ return readBytes (registerAddress, value, 1 );
756
769
}
757
770
758
771
// readWord(byte registerAddress)
@@ -761,15 +774,16 @@ bool SX1509::readByte(byte registerAddress, byte* value)
761
774
// - The msb of the return value will contain the value read from registerAddress
762
775
// - The lsb of the return value will contain the value read from registerAddress + 1
763
776
// - Return boolean true if succesfull
764
- bool SX1509::readWord (byte registerAddress, uint16_t * value)
777
+ bool SX1509::readWord (byte registerAddress, uint16_t * value)
765
778
{
766
- byte dest[2 ];
767
- if (readBytes (registerAddress, dest, 2 )) {
768
- value[0 ] = dest[1 ];
769
- value[1 ] = dest[0 ];
770
- return true ;
771
- }
772
- return false ;
779
+ byte dest[2 ];
780
+ if (readBytes (registerAddress, dest, 2 ))
781
+ {
782
+ value[0 ] = dest[1 ];
783
+ value[1 ] = dest[0 ];
784
+ return true ;
785
+ }
786
+ return false ;
773
787
}
774
788
775
789
// readBytes(byte firstRegisterAddress, byte * destination, byte length)
@@ -778,28 +792,30 @@ bool SX1509::readWord(byte registerAddress, uint16_t* value)
778
792
// - destination is an array of bytes where the read values will be stored into
779
793
// - length is the number of bytes to be read
780
794
// - Return boolean true if succesfull
781
- bool SX1509::readBytes (byte firstRegisterAddress, byte * destination, byte length)
795
+ bool SX1509::readBytes (byte firstRegisterAddress, byte *destination, byte length)
782
796
{
783
797
_i2cPort->beginTransmission (deviceAddress);
784
798
_i2cPort->write (firstRegisterAddress);
785
799
uint8_t endResult = _i2cPort->endTransmission ();
786
800
bool result = (endResult == I2C_ERROR_OK) && (_i2cPort->requestFrom (deviceAddress, length) == length);
787
-
788
- if (result) {
789
- unsigned int timeout = RECEIVE_TIMEOUT_VALUE * length;
790
- while ((_i2cPort->available () < length) && (timeout-- != 0 ))
791
- ;
792
-
793
- if (timeout == 0 ) {
794
- return false ;
795
- }
796
-
797
- for (int i=0 ; i<length; i++)
798
- {
799
- destination[i] = _i2cPort->read ();
800
- }
801
- }
802
- return result;
801
+
802
+ if (result)
803
+ {
804
+ unsigned int timeout = RECEIVE_TIMEOUT_VALUE * length;
805
+ while ((_i2cPort->available () < length) && (timeout-- != 0 ))
806
+ ;
807
+
808
+ if (timeout == 0 )
809
+ {
810
+ return false ;
811
+ }
812
+
813
+ for (int i = 0 ; i < length; i++)
814
+ {
815
+ destination[i] = _i2cPort->read ();
816
+ }
817
+ }
818
+ return result;
803
819
}
804
820
805
821
// writeByte(byte registerAddress, byte writeValue)
@@ -810,9 +826,9 @@ bool SX1509::readBytes(byte firstRegisterAddress, byte * destination, byte lengt
810
826
bool SX1509::writeByte (byte registerAddress, byte writeValue)
811
827
{
812
828
_i2cPort->beginTransmission (deviceAddress);
813
- bool result = _i2cPort->write (registerAddress) && _i2cPort->write (writeValue);
814
- uint8_t endResult = _i2cPort->endTransmission ();
815
- return result && (endResult == I2C_ERROR_OK);
829
+ bool result = _i2cPort->write (registerAddress) && _i2cPort->write (writeValue);
830
+ uint8_t endResult = _i2cPort->endTransmission ();
831
+ return result && (endResult == I2C_ERROR_OK);
816
832
}
817
833
818
834
// writeWord(byte registerAddress, ungisnged int writeValue)
@@ -822,13 +838,13 @@ bool SX1509::writeByte(byte registerAddress, byte writeValue)
822
838
// - Return value: true if succeeded, false if failed
823
839
bool SX1509::writeWord (byte registerAddress, uint16_t writeValue)
824
840
{
825
- byte msb, lsb;
826
- msb = ((writeValue & 0xFF00 ) >> 8 );
827
- lsb = (writeValue & 0x00FF );
828
- _i2cPort->beginTransmission (deviceAddress);
829
- bool result = _i2cPort->write (registerAddress) && _i2cPort->write (msb) && _i2cPort->write (lsb);
830
- uint8_t endResult = _i2cPort->endTransmission ();
831
- return result && (endResult == I2C_ERROR_OK);
841
+ byte msb, lsb;
842
+ msb = ((writeValue & 0xFF00 ) >> 8 );
843
+ lsb = (writeValue & 0x00FF );
844
+ _i2cPort->beginTransmission (deviceAddress);
845
+ bool result = _i2cPort->write (registerAddress) && _i2cPort->write (msb) && _i2cPort->write (lsb);
846
+ uint8_t endResult = _i2cPort->endTransmission ();
847
+ return result && (endResult == I2C_ERROR_OK);
832
848
}
833
849
834
850
// writeBytes(byte firstRegisterAddress, byte * writeArray, byte length)
@@ -838,14 +854,15 @@ bool SX1509::writeWord(byte registerAddress, uint16_t writeValue)
838
854
// - writeArray should be an array of byte values to be written.
839
855
// - length should be the number of bytes to be written.
840
856
// - Return value: true if succeeded, false if failed
841
- bool SX1509::writeBytes (byte firstRegisterAddress, byte * writeArray, byte length)
842
- {
843
- _i2cPort->beginTransmission (deviceAddress);
844
- bool result = _i2cPort->write (firstRegisterAddress);
845
- int i = 0 ;
846
- while (result && i < length) {
847
- result = _i2cPort->write (writeArray[i++]);
848
- }
849
- uint8_t endResult = _i2cPort->endTransmission ();
850
- return result && (endResult == I2C_ERROR_OK);
857
+ bool SX1509::writeBytes (byte firstRegisterAddress, byte *writeArray, byte length)
858
+ {
859
+ _i2cPort->beginTransmission (deviceAddress);
860
+ bool result = _i2cPort->write (firstRegisterAddress);
861
+ int i = 0 ;
862
+ while (result && i < length)
863
+ {
864
+ result = _i2cPort->write (writeArray[i++]);
865
+ }
866
+ uint8_t endResult = _i2cPort->endTransmission ();
867
+ return result && (endResult == I2C_ERROR_OK);
851
868
}
0 commit comments