Skip to content

Commit f1d7ac6

Browse files
committed
move to a byte swap method in the toolkit
1 parent 3a7adea commit f1d7ac6

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/sfeTk/sfeToolkit.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2323
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525
#include "sfeToolkit.h"
26-
#include <cstdint>
2726

2827
/**
2928
* @brief C function - Runtime check for system byte order

src/sfeTk/sfeToolkit.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626

2727
#pragma once
2828

29+
#include <climits>
30+
#include <cstdint>
31+
#include <type_traits>
2932
/**
3033
@brief Common include file for the core of the SparkFun Electronics Toolkit
3134
*/
@@ -41,8 +44,19 @@ enum class sfeTKByteOrder : uint8_t
4144
// Use a namespace for the toolkit "utilities and helpers"
4245
namespace sfeToolkit
4346
{
44-
4547
// Function to determine the byte order of the system
4648
sfeTKByteOrder systemByteOrder(void);
4749

50+
// Method to swap the byte order of any unsigned integer - you pick the size. Uses constexpr so it's a compile time
51+
// operation/inline/optimized
52+
//
53+
// from
54+
// https://stackoverflow.com/questions/36936584/how-to-write-constexpr-swap-function-to-change-endianess-of-an-integer
55+
//
56+
template <class T>
57+
constexpr typename std::enable_if<std::is_unsigned<T>::value, T>::type byte_swap(T i, T j = 0u, std::size_t n = 0u)
58+
{
59+
return n == sizeof(T) ? j : byte_swap<T>(i >> CHAR_BIT, (j << CHAR_BIT) | (i & (T)(unsigned char)(-1)), n + 1);
60+
}
61+
4862
}; // namespace sfeToolkit

src/sfeTkArdI2C.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ sfeTkError_t sfeTkArdI2C::writeRegisterRegion(uint8_t devReg, const uint8_t *dat
209209
//
210210
sfeTkError_t sfeTkArdI2C::writeRegister16Region(uint16_t devReg, const uint8_t *data, size_t length)
211211
{
212-
devReg = ((devReg << 8) & 0xff00) | ((devReg >> 8) & 0x00ff);
212+
// devReg = ((devReg << 8) & 0xff00) | ((devReg >> 8) & 0x00ff);
213+
devReg = sfeToolkit::byte_swap(devReg);
213214
return writeRegisterRegionAddress((uint8_t *)&devReg, 2, data, length);
214215
}
215216

@@ -227,7 +228,8 @@ sfeTkError_t sfeTkArdI2C::writeRegister16Region16(uint16_t devReg, const uint16_
227228
return writeRegisterRegionAddress((uint8_t *)&devReg, 2, (uint8_t *)data, length * 2);
228229

229230
// okay, we need to swap
230-
devReg = ((devReg << 8) & 0xff00) | ((devReg >> 8) & 0x00ff);
231+
devReg = sfeToolkit::byte_swap(devReg);
232+
// devReg = ((devReg << 8) & 0xff00) | ((devReg >> 8) & 0x00ff);
231233
uint16_t data16[length];
232234
for (size_t i = 0; i < length; i++)
233235
data16[i] = ((data[i] << 8) & 0xff00) | ((data[i] >> 8) & 0x00ff);

0 commit comments

Comments
 (0)