Skip to content

Commit 25a8677

Browse files
committed
Changes virutal class name to "SfeBus", changes i2cAddr param to devAddr
* Formats files
1 parent b2c5d93 commit 25a8677

File tree

2 files changed

+148
-156
lines changed

2 files changed

+148
-156
lines changed

src/sfe_bus.cpp

Lines changed: 74 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1-
// sfe_bus.cpp
2-
//
3-
// The MIT License (MIT)
4-
//
5-
// Copyright (c) 2022 SparkFun Electronics
6-
// Permission is hereby granted, free of charge, to any person obtaining a
7-
// copy of this software and associated documentation files (the "Software"),
8-
// to deal in the Software without restriction, including without limitation
9-
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
10-
// and/or sell copies of the Software, and to permit persons to whom the
11-
// Software is furnished to do so, subject to the following conditions: The
12-
// above copyright notice and this permission notice shall be included in all
13-
// copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
14-
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15-
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16-
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17-
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18-
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19-
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20-
21-
22-
// The following classes specify the behavior for communicating
23-
// over the respective data buses: Inter-Integrated Circuit (I2C)
24-
// and Serial Peripheral Interface (SPI).
1+
/*
2+
sfe_bus.cpp
3+
4+
The MIT License (MIT)
5+
6+
Copyright (c) 2022 SparkFun Electronics
7+
Permission is hereby granted, free of charge, to any person obtaining a
8+
copy of this software and associated documentation files (the "Software"),
9+
to deal in the Software without restriction, including without limitation
10+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
and/or sell copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following conditions: The
13+
above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
15+
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
The following classes specify the behavior for communicating
23+
over the respective data buses: Inter-Integrated Circuit (I2C)
24+
and Serial Peripheral Interface (SPI).
25+
*/
2526

2627
#include "sfe_bus.h"
2728

2829
#define maxI2CBufferLength 32
2930
#define SPI_READ 0x80
3031

3132
// What we use for transfer chunk size
32-
const static uint16_t buffSize = maxI2CBufferLength
33+
const static uint16_t buffSize = maxI2CBufferLength;
3334

3435
//////////////////////////////////////////////////////////////////////////////////////////////////
3536
// Constructor
3637
//
3738

3839
// To repeatedly use this bus toolkit, it will need its own namespace
39-
//namespace sfe_XXX {
40-
SfeI2C::SfeI2C(void) : _i2cPort{nullptr}
40+
// namespace sfe_XXX {
41+
SfeI2C::SfeI2C(void)
42+
: _i2cPort{nullptr}
4143
{
4244
}
4345

@@ -50,14 +52,14 @@ bool SfeI2C::init(TwoWire &wirePort, bool bInit)
5052
{
5153

5254
// if we don't have a wire port already
53-
if( !_i2cPort )
55+
if (!_i2cPort)
5456
{
5557
_i2cPort = &wirePort;
5658

57-
if( bInit )
59+
if (bInit)
5860
_i2cPort->begin();
5961
}
60-
62+
6163
return true;
6264
}
6365

@@ -68,25 +70,23 @@ bool SfeI2C::init(TwoWire &wirePort, bool bInit)
6870
// will use the default
6971
bool SfeI2C::init()
7072
{
71-
if( !_i2cPort )
72-
return init(Wire);
73-
else
74-
return false;
73+
if (!_i2cPort)
74+
return init(Wire);
75+
else
76+
return false;
7577
}
7678

77-
78-
7979
//////////////////////////////////////////////////////////////////////////////////////////////////
8080
// ping()
8181
//
8282
// Is a device connected?
83-
bool SfeI2C::ping(uint8_t i2cAddr)
83+
bool SfeI2C::ping(uint8_t devAddr)
8484
{
8585

86-
if( !_i2cPort )
86+
if (!_i2cPort)
8787
return false;
8888

89-
_i2cPort->beginTransmission(i2cAddr);
89+
_i2cPort->beginTransmission(devAddr);
9090
return _i2cPort->endTransmission() == 0;
9191
}
9292

@@ -95,29 +95,27 @@ bool SfeI2C::ping(uint8_t i2cAddr)
9595
//
9696
// Write a byte to a devRegister
9797

98-
bool SfeI2C::writeRegisterByte(uint8_t i2cAddr, uint8_t devReg, uint8_t dataToWrite)
98+
bool SfeI2C::writeRegisterByte(uint8_t devAddr, uint8_t devReg, uint8_t dataToWrite)
9999
{
100100

101101
if (!_i2cPort)
102102
return false;
103103

104-
_i2cPort->beginTransmission(i2cAddr);
104+
_i2cPort->beginTransmission(devAddr);
105105
_i2cPort->write(devReg);
106106
_i2cPort->write(dataToWrite);
107107
return _i2cPort->endTransmission() == 0;
108108
}
109109

110-
111-
112110
//////////////////////////////////////////////////////////////////////////////////////////////////
113111
// writeRegisterRegion()
114112
//
115113
// Write a block of data to a device.
116114

117-
int SfeI2C::writeRegisterRegion(uint8_t i2cAddr, uint8_t devReg, const uint8_t *data, uint16_t length)
115+
int SfeI2C::writeRegisterRegion(uint8_t devAddr, uint8_t devReg, const uint8_t *data, uint16_t length)
118116
{
119117

120-
_i2cPort->beginTransmission(i2cAddr);
118+
_i2cPort->beginTransmission(devAddr);
121119
_i2cPort->write(devReg);
122120
_i2cPort->write(data, (int)length);
123121

@@ -132,7 +130,7 @@ int SfeI2C::writeRegisterRegion(uint8_t i2cAddr, uint8_t devReg, const uint8_t *
132130
// For large buffers, the data is chuncked over KMaxI2CBufferLength at a time
133131
//
134132
//
135-
int SfeI2C::readRegisterRegion(uint8_t i2cAddr, uint8_t devReg, uint8_t *data, uint16_t numBytes)
133+
int SfeI2C::readRegisterRegion(uint8_t devAddr, uint8_t devReg, uint8_t *data, uint16_t numBytes)
136134
{
137135
uint8_t nChunk;
138136
uint16_t nReturned;
@@ -145,7 +143,7 @@ int SfeI2C::readRegisterRegion(uint8_t i2cAddr, uint8_t devReg, uint8_t *data, u
145143

146144
while (numBytes > 0)
147145
{
148-
_i2cPort->beginTransmission(i2cAddr);
146+
_i2cPort->beginTransmission(devAddr);
149147

150148
if (bFirstInter)
151149
{
@@ -159,7 +157,7 @@ int SfeI2C::readRegisterRegion(uint8_t i2cAddr, uint8_t devReg, uint8_t *data, u
159157
// We're chunking in data - keeping the max chunk to kMaxI2CBufferLength
160158
nChunk = numBytes > buffSize ? buffSize : numBytes;
161159

162-
nReturned = _i2cPort->requestFrom((int)i2cAddr, (int)nChunk, (int)true);
160+
nReturned = _i2cPort->requestFrom((int)devAddr, (int)nChunk, (int)true);
163161

164162
// No data returned, no dice
165163
if (nReturned == 0)
@@ -169,7 +167,7 @@ int SfeI2C::readRegisterRegion(uint8_t i2cAddr, uint8_t devReg, uint8_t *data, u
169167
for (i = 0; i < nReturned; i++)
170168
{
171169
*data++ = _i2cPort->read();
172-
}
170+
}
173171

174172
// Decrement the amount of data recieved from the overall data request amount
175173
numBytes = numBytes - nReturned;
@@ -179,8 +177,6 @@ int SfeI2C::readRegisterRegion(uint8_t i2cAddr, uint8_t devReg, uint8_t *data, u
179177
return 0; // Success
180178
}
181179

182-
183-
184180
//////////////////////////////////////////////////////////////////////////////////////////////////
185181
// Constructor
186182
//
@@ -193,27 +189,26 @@ SfeSPI::SfeSPI(void) : _spiPort{nullptr}
193189
//
194190
// Methods to init/setup this device. The caller can provide a SPI Port, or this class
195191
// will use the default
196-
bool SfeSPI::init(SPIClass &spiPort, SPISettings& ismSPISettings, uint8_t cs, bool bInit)
192+
bool SfeSPI::init(SPIClass &spiPort, SPISettings &ismSPISettings, uint8_t cs, bool bInit)
197193
{
198194

199195
// if we don't have a SPI port already
200-
if( !_spiPort )
196+
if (!_spiPort)
201197
{
202198
_spiPort = &spiPort;
203199

204-
if( bInit )
200+
if (bInit)
205201
_spiPort->begin();
206202
}
207203

208-
209204
// SPI settings are needed for every transaction
210-
_sfeSPISettings = ismSPISettings;
205+
_sfeSPISettings = ismSPISettings;
211206

212207
// The chip select pin can vary from platform to platform and project to project
213-
// and so it must be given by the user.
214-
if( !cs )
215-
return false;
216-
208+
// and so it must be given by the user.
209+
if (!cs)
210+
return false;
211+
217212
_cs = cs;
218213

219214
return true;
@@ -224,40 +219,37 @@ bool SfeSPI::init(SPIClass &spiPort, SPISettings& ismSPISettings, uint8_t cs, b
224219
//
225220
// Methods to init/setup this device. The caller can provide a SPI Port, or this class
226221
// will use the default
227-
bool SfeSPI::init(uint8_t cs, bool bInit)
222+
bool SfeSPI::init(uint8_t cs, bool bInit)
228223
{
229224

230-
//If the transaction settings are not provided by the user they are built here.
231-
SPISettings spiSettings = SPISettings(3000000, MSB_FIRST, SPI_MODE3);
225+
// If the transaction settings are not provided by the user they are built here.
226+
SPISettings spiSettings = SPISettings(3000000, MSB_FIRST, SPI_MODE3);
232227

233-
//In addition of the port is not provided by the user, it defaults to SPI here.
228+
// In addition of the port is not provided by the user, it defaults to SPI here.
234229
return init(SPI, spiSettings, cs, bInit);
235-
236230
}
237231

238-
239232
//////////////////////////////////////////////////////////////////////////////////////////////////
240233
// ping()
241234
//
242235
// Is a device connected? The SPI ping is not relevant but is defined here to keep consistency with
243236
// I2C class i.e. provided for the interface.
244237
//
245238

246-
247-
bool SfeSPI::ping(uint8_t i2cAddr)
239+
bool SfeSPI::ping(uint8_t devAddr)
248240
{
249-
return true;
241+
return true;
250242
}
251243

252244
//////////////////////////////////////////////////////////////////////////////////////////////////
253245
// writeRegisterByte()
254246
//
255247
// Write a byte to a devRegister
256248

257-
bool SfeSPI::writeRegisterByte(uint8_t i2cAddr, uint8_t devReg, uint8_t dataToWrite)
249+
bool SfeSPI::writeRegisterByte(uint8_t devAddr, uint8_t devReg, uint8_t dataToWrite)
258250
{
259251

260-
if( !_spiPort )
252+
if (!_spiPort)
261253
return false;
262254

263255
// Apply settings
@@ -272,34 +264,33 @@ bool SfeSPI::writeRegisterByte(uint8_t i2cAddr, uint8_t devReg, uint8_t dataToWr
272264
digitalWrite(_cs, HIGH);
273265
_spiPort->endTransaction();
274266

275-
return true;
267+
return true;
276268
}
277269

278-
279270
//////////////////////////////////////////////////////////////////////////////////////////////////
280271
// writeRegisterRegion()
281272
//
282273
// Write a block of data to a device.
283-
int SfeSPI::writeRegisterRegion(uint8_t i2cAddr, uint8_t devReg, const uint8_t *data, uint16_t length)
274+
int SfeSPI::writeRegisterRegion(uint8_t devAddr, uint8_t devReg, const uint8_t *data, uint16_t length)
284275
{
285276

286277
int i;
287278

288-
// Apply settings
279+
// Apply settings
289280
_spiPort->beginTransaction(_sfeSPISettings);
290-
// Signal communication start
281+
// Signal communication start
291282
digitalWrite(_cs, LOW);
292283
_spiPort->transfer(devReg);
293284

294-
for(i = 0; i < length; i++)
285+
for (i = 0; i < length; i++)
295286
{
296287
_spiPort->transfer(*data++);
297288
}
298289

299290
// End communication
300291
digitalWrite(_cs, HIGH);
301292
_spiPort->endTransaction();
302-
return 0;
293+
return 0;
303294
}
304295

305296
////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -309,31 +300,30 @@ int SfeSPI::writeRegisterRegion(uint8_t i2cAddr, uint8_t devReg, const uint8_t *
309300
//
310301
//
311302
//
312-
int SfeSPI::readRegisterRegion(uint8_t i2cAddr, uint8_t devReg, uint8_t *data, uint16_t numBytes)
303+
int SfeSPI::readRegisterRegion(uint8_t devAddr, uint8_t devReg, uint8_t *data, uint16_t numBytes)
313304
{
314305
if (!_spiPort)
315306
return -1;
316307

317-
int i;
308+
int i;
318309

319310
// Apply settings
320-
_spiPort->beginTransaction(_sfeSPISettings);
311+
_spiPort->beginTransaction(_sfeSPISettings);
321312
// Signal communication start
322313
digitalWrite(_cs, LOW);
323314
// A leading "1" must be added to transfer with devRegister to indicate a "read"
324315
devReg = (devReg | SPI_READ);
325316
_spiPort->transfer(devReg);
326317

327-
for(i = 0; i < numBytes; i++)
318+
for (i = 0; i < numBytes; i++)
328319
{
329320
*data++ = _spiPort->transfer(0x00);
330321
}
331322

332323
// End transaction
333324
digitalWrite(_cs, HIGH);
334325
_spiPort->endTransaction();
335-
return 0;
336-
326+
return 0;
337327
}
338328

339329
//} namespace sfe_XXX

0 commit comments

Comments
 (0)