Skip to content

Commit 3a7adea

Browse files
committed
cleanup, re-org of byte order logic -move helpers into a namespace, made Arduino like consts for order types
1 parent 9846401 commit 3a7adea

File tree

7 files changed

+66
-28
lines changed

7 files changed

+66
-28
lines changed

src/SparkFun_Toolkit.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131
// Arduino Libraries.
3232

3333
// Just include the toolkit headers
34-
35-
#include <sfeTk/sfeToolkit.h>
3634
#include "sfeTkArdI2C.h"
37-
#include "sfeTkArdSPI.h"
35+
#include "sfeTkArdSPI.h"
36+
#include "sfeTkArduino.h"

src/sfeTk/sfeToolkit.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ 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 <stdint.h>
26+
#include <cstdint>
2727

2828
/**
2929
* @brief C function - Runtime check for system byte order
3030
*/
31-
sfeTKByteOrder_t systemByteOrder(void)
31+
sfeTKByteOrder sfeToolkit::systemByteOrder(void)
3232
{
3333
uint16_t i = 1;
34-
return *((uint8_t *)&i) == 0 ? SFETK_BIG_ENDIAN : SFETK_LITTLE_ENDIAN;
34+
return *((uint8_t *)&i) == 0 ? sfeTKByteOrder::BigEndian : sfeTKByteOrder::LittleEndian;
3535
}

src/sfeTk/sfeToolkit.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,18 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131
*/
3232
#include "sfeTkError.h"
3333

34-
/**
35-
@brief The byte order of the system
36-
*/
37-
typedef enum
34+
// byte order types/enum
35+
enum class sfeTKByteOrder : uint8_t
3836
{
39-
SFETK_BIG_ENDIAN = 0x01,
40-
SFETK_LITTLE_ENDIAN = 0x00
41-
} sfeTKByteOrder_t;
37+
BigEndian = 0x01,
38+
LittleEndian = 0x02
39+
};
4240

43-
// Export our byte order function as a C function
44-
#ifdef __cplusplus
45-
extern "C"
41+
// Use a namespace for the toolkit "utilities and helpers"
42+
namespace sfeToolkit
4643
{
47-
#endif
48-
// Runtime check for system byte order
49-
sfeTKByteOrder_t systemByteOrder(void);
50-
#ifdef __cplusplus
51-
}
52-
#endif
44+
45+
// Function to determine the byte order of the system
46+
sfeTKByteOrder systemByteOrder(void);
47+
48+
}; // namespace sfeToolkit

src/sfeTkArdI2C.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ sfeTkError_t sfeTkArdI2C::writeRegister16Region(uint16_t devReg, const uint8_t *
223223
sfeTkError_t sfeTkArdI2C::writeRegister16Region16(uint16_t devReg, const uint16_t *data, size_t length)
224224
{
225225
// if the system byte order is the same as the desired order, just send the buffer
226-
if (systemByteOrder() == _byteOrder)
226+
if (sfeToolkit::systemByteOrder() == _byteOrder)
227227
return writeRegisterRegionAddress((uint8_t *)&devReg, 2, (uint8_t *)data, length * 2);
228228

229229
// okay, we need to swap
@@ -389,13 +389,13 @@ sfeTkError_t sfeTkArdI2C::readRegister16Region(uint16_t devReg, uint8_t *data, s
389389
sfeTkError_t sfeTkArdI2C::readRegister16Region16(uint16_t devReg, uint16_t *data, size_t numBytes, size_t &readBytes)
390390
{
391391
// if the system byte order is the same as the desired order, flip the address
392-
if (systemByteOrder() != _byteOrder)
392+
if (sfeToolkit::systemByteOrder() != _byteOrder)
393393
devReg = ((devReg << 8) & 0xff00) | ((devReg >> 8) & 0x00ff);
394394

395395
sfeTkError_t status = readRegisterRegionAnyAddress((uint8_t *)&devReg, 2, (uint8_t *)data, numBytes * 2, readBytes);
396396

397397
// Do we need to flip the byte order?
398-
if (status == kSTkErrOk && systemByteOrder() != _byteOrder)
398+
if (status == kSTkErrOk && sfeToolkit::systemByteOrder() != _byteOrder)
399399
{
400400
for (size_t i = 0; i < numBytes; i++)
401401
data[i] = ((data[i] << 8) & 0xff00) | ((data[i] >> 8) & 0x00ff);

src/sfeTkArdI2C.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ over Inter-Integrated Circuit (I2C) in Arduino
3131
#include <Wire.h>
3232

3333
// Include our platform I2C interface definition.
34+
#include "sfeTkArduino.h"
3435
#include <sfeTk/sfeTkII2C.h>
3536

3637
/**
@@ -44,7 +45,7 @@ class sfeTkArdI2C : public sfeTkII2C
4445
@brief Constructor
4546
*/
4647

47-
sfeTkArdI2C(void) : _i2cPort(nullptr), _bufferChunkSize{kDefaultBufferChunk}, _byteOrder{SFETK_LITTLE_ENDIAN}
48+
sfeTkArdI2C(void) : _i2cPort(nullptr), _bufferChunkSize{kDefaultBufferChunk}, _byteOrder{SFTK_LSBFIRST}
4849
{
4950
}
5051
/**
@@ -295,8 +296,10 @@ class sfeTkArdI2C : public sfeTkII2C
295296
/**
296297
* @brief Set the byte order for multi-byte data transfers
297298
*
299+
* @param order The byte order to set - set to either SFTK_MSBFIRST or SFTK_LSBFIRST. The default is SFTK_LSBFIRST
300+
*
298301
*/
299-
void setByteOrder(sfeTKByteOrder_t order)
302+
void setByteOrder(sfeTKByteOrder order)
300303
{
301304
_byteOrder = order;
302305
}
@@ -321,5 +324,5 @@ class sfeTkArdI2C : public sfeTkII2C
321324
size_t _bufferChunkSize;
322325

323326
/** flag to manage byte swapping */
324-
sfeTKByteOrder_t _byteOrder;
327+
sfeTKByteOrder _byteOrder;
325328
};

src/sfeTkArdSPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2727

2828
#pragma once
2929

30+
#include "sfeTkArduino.h"
3031
#include <SPI.h>
3132
#include <sfeTk/sfeTkISPI.h>
3233

src/sfeTkArduino.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
/*
3+
@brief sfeTkArduino.h
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2023 SparkFun Electronics
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a
10+
copy of this software and associated documentation files (the "Software"),
11+
to deal in the Software without restriction, including without limitation
12+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
13+
and/or sell copies of the Software, and to permit persons to whom the
14+
Software is furnished to do so, subject to the following conditions: The
15+
above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
17+
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
18+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
*/
25+
26+
#pragma once
27+
28+
// Purpose:
29+
//
30+
// "Arduino-ized" value of toolkit values constants"
31+
32+
// Just include the toolkit headers
33+
34+
#include <sfeTk/sfeToolkit.h>
35+
36+
// Arduino-ize our byte order types
37+
38+
const sfeTKByteOrder SFTK_MSBFIRST = sfeTKByteOrder::BigEndian;
39+
const sfeTKByteOrder SFTK_LSBFIRST = sfeTKByteOrder::LittleEndian;

0 commit comments

Comments
 (0)