Skip to content

Commit 273cd3b

Browse files
authored
Fix #44, byte order 16 bits API (#45)
- fix #44, add function to reverse the byte order of the 16 bit API. - update readme.md
1 parent 18ddde4 commit 273cd3b

File tree

7 files changed

+50
-10
lines changed

7 files changed

+50
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.9.0] - 2025-02-18
10+
- fix #44, add function to reverse the byte order of the 16 bit API.
11+
- update readme.md
12+
13+
----
14+
915
## [0.8.1] - 2025-01-20
1016
- Add PR #41 interrupt functions. Thanks to Klang
1117
- add link to keypad library

MCP23017.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MCP23017.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.8.1
4+
// VERSION: 0.9.0
55
// PURPOSE: Arduino library for I2C MCP23017 16 channel port expander
66
// DATE: 2019-10-12
77
// URL: https://github.com/RobTillaart/MCP23017_RT
@@ -60,6 +60,12 @@ uint8_t MCP23017::getAddress()
6060
}
6161

6262

63+
void MCP23017::reverse16ByteOrder(bool reverse)
64+
{
65+
_reverse16ByteOrder = reverse;
66+
}
67+
68+
6369
///////////////////////////////////////////////////////////////////
6470
//
6571
// single pin interface
@@ -871,6 +877,11 @@ bool MCP23017::writeReg16(uint8_t reg, uint16_t value)
871877
return false;
872878
}
873879

880+
if (_reverse16ByteOrder) // swap regA and regB
881+
{
882+
value = (value >> 8) | (value << 8);
883+
}
884+
874885
// start write
875886
_wire->beginTransmission(_address);
876887
_wire->write(reg);
@@ -887,8 +898,6 @@ bool MCP23017::writeReg16(uint8_t reg, uint16_t value)
887898

888899
uint16_t MCP23017::readReg16(uint8_t reg)
889900
{
890-
uint16_t rv = 0;
891-
892901
_error = MCP23017_OK;
893902

894903
if (reg > MCP23x17_OLAT_B)
@@ -912,9 +921,14 @@ uint16_t MCP23017::readReg16(uint8_t reg)
912921
_error = MCP23017_I2C_ERROR;
913922
return 0;
914923
}
915-
rv = _wire->read() << 8;
916-
rv += _wire->read();
917-
return rv;
924+
uint16_t regA = _wire->read();
925+
uint16_t regB = _wire->read();
926+
927+
if (_reverse16ByteOrder)
928+
{
929+
return (regB << 8) | regA;
930+
}
931+
return (regA << 8) | regB;
918932
}
919933

920934

MCP23017.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: MCP23017.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.8.1
5+
// VERSION: 0.9.0
66
// PURPOSE: Arduino library for I2C MCP23017 16 channel port expander
77
// DATE: 2019-10-12
88
// URL: https://github.com/RobTillaart/MCP23017_RT
@@ -16,7 +16,7 @@
1616
#include "MCP23x17_registers.h"
1717

1818

19-
#define MCP23017_LIB_VERSION (F("0.8.1"))
19+
#define MCP23017_LIB_VERSION (F("0.9.0"))
2020

2121
#define MCP23017_OK 0x00
2222
#define MCP23017_PIN_ERROR 0x81
@@ -36,6 +36,11 @@ class MCP23017
3636
bool isConnected();
3737
uint8_t getAddress();
3838

39+
// Fix #44, reverse the byte order of the 16 bit API.
40+
// reverse == false ==> backwards compatible (default)
41+
// reverse == true ==> swaps the A and B byte to be more intuitive.
42+
void reverse16ByteOrder(bool reverse = false);
43+
3944

4045
// single pin interface
4146
// mode = INPUT, OUTPUT, INPUT_PULLUP (= same as INPUT)
@@ -122,6 +127,8 @@ class MCP23017
122127
bool writeReg16(uint8_t reg, uint16_t value);
123128
uint16_t readReg16(uint8_t reg);
124129

130+
bool _reverse16ByteOrder = false;
131+
125132
uint8_t _address;
126133
TwoWire* _wire;
127134
uint8_t _error;

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ However no priority.
5050
Note that the MCP23S017 (SPI version) does not have this "feature" for **GPA7** and **GPB7**.
5151

5252

53+
## 0.9.0 Breaking change
54+
55+
Fix #44, **reverse16ByteOrder(bool)** is added to reverse the byte order of the 16 bit API.
56+
Not using or setting the parameter to false keeps the library backwards compatible.
57+
5358
### 0.8.0 Breaking change
5459

5560
Fix for #47 MCP23S17, bug in enable interrupt handling.
@@ -202,6 +207,12 @@ Returns true if successful.
202207

203208
Please note REVD remarks at top.
204209

210+
Note: since version 0.9.0 a function is added to reverse the byte order of the 16 bit API.
211+
212+
- **void reverse16ByteOrder(bool reverse = false)** reverse the byte order of the 16 bit API.
213+
Not using this function or setting the parameter to false keeps the library backwards compatible.
214+
215+
205216
- **bool pinMode16(uint16_t mask)** mask = 0..0xFFFF, returns true if successful.
206217
- **bool write16(uint16_t value)** value = 0..0xFFFF, returns true if successful.
207218
- **uint16_t read16()** reads 16 pins into an uint16_t.

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ begin KEYWORD2
99
isConnected KEYWORD2
1010
getAddress KEYWORD2
1111

12+
reverse16ByteOrder KEYWORD2
13+
1214
pinMode1 KEYWORD2
1315
write1 KEYWORD2
1416
read1 KEYWORD2

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/MCP23017_RT.git"
1717
},
18-
"version": "0.8.1",
18+
"version": "0.9.0",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MCP23017_RT
2-
version=0.8.1
2+
version=0.9.0
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for I2C MCP23017 16 channel port expander 16 IO-lines

0 commit comments

Comments
 (0)