Skip to content

Commit 75d9823

Browse files
committed
refactor of design
1 parent 1bc808f commit 75d9823

File tree

8 files changed

+367
-72
lines changed

8 files changed

+367
-72
lines changed

src/SparkFun_Toolkit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ SOFTWARE.
2929

3030
// Just include the toolkit headers
3131

32-
#include "sfeTkBusI2C.h"
33-
#include "sfeTkBusSPI.h"
32+
#include "sfeTkArdI2C.h"
33+
#include "sfeTkArdSPI.h"

src/sfeTk/sfeTkIBus.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2727

2828
#include <cstdint>
2929

30-
class sfeTkBus
30+
class sfeTkIBus
3131
{
3232
public:
33+
/// @brief Write a single byte to the given register
3334
///
34-
/// @brief A simple ping of the device at the given address.
35-
/// @param devAddr Address/Pin of the device
35+
/// @param devReg The device's register's address.
36+
/// @param data Data to write.
3637
///
37-
/// @retval bool - true on success, false on failure
38+
/// @retval bool - true on successful execution.
3839
///
39-
virtual bool ping(uint8_t devAddr) = 0;
40+
virtual bool writeRegisterByte(uint8_t devReg, uint8_t data) = 0;
4041

41-
/// @brief Write a single byte to the given register
42+
/// @brief Write a single word (16 bit) to the given register
4243
///
43-
/// @param devAddr The device's address/pin -- depending on implementation
4444
/// @param devReg The device's register's address.
4545
/// @param data Data to write.
4646
///
4747
/// @retval bool - true on successful execution.
4848
///
49-
virtual bool writeRegisterByte(uint8_t devAddr, uint8_t devReg, uint8_t data) = 0;
49+
virtual bool writeRegisterWord(uint8_t devReg, uint16_t data) = 0;
5050

5151
/// @brief Writes a number of bytes starting at the given register's address.
5252
///
@@ -56,7 +56,25 @@ class sfeTkBus
5656
///
5757
/// @retval int returns the number of bytes written, < 0 on error
5858
///
59-
virtual int writeRegisterRegion(uint8_t devAddr, uint8_t devReg, const uint8_t *data, uint16_t length) = 0;
59+
virtual int writeRegisterRegion(uint8_t devReg, const uint8_t *data, uint16_t length) = 0;
60+
61+
/// @brief Read a single byte from the given register
62+
///
63+
/// @param devReg The device's register's address.
64+
/// @param data Data to read.
65+
///
66+
/// @retval bool - true on successful execution.
67+
///
68+
virtual bool readRegisterByte(uint8_t devReg, uint8_t &data) = 0;
69+
70+
/// @brief Read a single word (16 bit) from the given register
71+
///
72+
/// @param devReg The device's register's address.
73+
/// @param data Data to read.
74+
///
75+
/// @retval bool - true on successful execution.
76+
///
77+
virtual bool readRegisterWord(uint8_t devReg, uint16_t &data) = 0;
6078

6179
/// @brief Reads a block of data from the given register.
6280
///
@@ -66,7 +84,7 @@ class sfeTkBus
6684
///
6785
/// @retval int returns 0 on success, or error code
6886
///
69-
virtual int readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t numBytes) = 0;
87+
virtual int readRegisterRegion(uint8_t reg, uint8_t *data, uint16_t numBytes) = 0;
7088
};
7189

7290
//};

src/sfeTk/sfeTkII2C.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
// sfeTkII2C.h
3+
//
4+
// Defines the I2C communication bus interface for the SparkFun Electronics Toolkit
5+
/*
6+
7+
The MIT License (MIT)
8+
9+
Copyright (c) 2022 SparkFun Electronics
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions: The
16+
above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
18+
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
19+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
#pragma once
27+
28+
#include "sfeTkIBus.h"
29+
30+
class sfeTkI2C : public sfeTkIBus
31+
{
32+
public:
33+
sfeTkI2C() : _address{kNoAddress}
34+
{
35+
}
36+
sfeTkI2C(uint8_t addr) : _address{addr}
37+
{
38+
}
39+
40+
///
41+
/// @brief A simple ping of the device at the set address
42+
///
43+
/// @retval bool - true on success, false on failure
44+
///
45+
virtual bool ping() = 0;
46+
47+
/// @brief setter for the I2C address
48+
///
49+
/// @param devAddr The device's address
50+
///
51+
virtual void setAddress(uint8_t devAddr)
52+
{
53+
_address = devAddr;
54+
}
55+
56+
/// @brief getter for the I2C address
57+
///
58+
/// @retval uint8_t returns the address for the device
59+
///
60+
virtual uint8_t address(void)
61+
{
62+
return _address;
63+
}
64+
65+
static constexpr uint8_t kNoAddress = 0;
66+
67+
private:
68+
uint8_t _address;
69+
};
70+
71+
//};

src/sfeTk/sfeTkISPI.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
// sfeTkISPI.h
3+
//
4+
// Defines the SPI communication bus interface for the SparkFun Electronics Toolkit
5+
/*
6+
7+
The MIT License (MIT)
8+
9+
Copyright (c) 2022 SparkFun Electronics
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions: The
16+
above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
18+
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
19+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
#pragma once
27+
28+
#include "sfeTkIBus.h"
29+
30+
class sfeTkSPI : public sfeTkIBus
31+
{
32+
public:
33+
sfeTkI2C() : _csPin{kNoCSPin}
34+
{
35+
}
36+
37+
/// @brief setter for the CS Pin
38+
///
39+
/// @param devCS The device's CS Pin
40+
///
41+
virtual void setCS(uint8_t devCS)
42+
{
43+
_cs = devCS;
44+
}
45+
46+
/// @brief getter for the cs pin
47+
///
48+
/// @retval uint8_t returns the CS pin for the device
49+
///
50+
virtual uint8_t cs(void)
51+
{
52+
return _cs;
53+
}
54+
55+
static constexpr uint8_t kNoCSPin = 0;
56+
57+
private:
58+
uint8_t _cs;
59+
};
60+
61+
//};

0 commit comments

Comments
 (0)