Skip to content

Commit 89efacb

Browse files
committed
Uses the new writeBlock method, changes type for changeAddress function so that I can shift the address within
1 parent fe78ad5 commit 89efacb

File tree

6 files changed

+109
-10
lines changed

6 files changed

+109
-10
lines changed

examples/Example3_ChangeAddress/Example3_ChangeAddress.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ QwiicUltrasonic myUltrasonic;
2525
// Example 2 to change the address to the new default.
2626
uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
2727
// uint8_t deviceAddress = 0x00;
28-
const uint8_t NEW_ADDR = 0x1E;
28+
29+
// New addres is 7-bit unshifted.
30+
uint8_t NEW_ADDR = 0x1E;
2931

3032
void setup()
3133
{

src/sfeQwiicUltrasonic.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/* SparkFun Ulrasonic Distance Sensor
2-
*
3-
* Product:
1+
/* SparkFun Ulrasonic Distance Sensor
2+
*
3+
* Product:
44
* * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-1XXXX)
55
* * https://www.sparkfun.com/1XXXX
6-
*
6+
*
77
* SPDX-License-Identifier: MIT
88
*
99
* Copyright (c) 2024 SparkFun Electronics
@@ -80,16 +80,20 @@ sfeTkError_t sfeQwiicUltrasonic::getTriggeredDistance(uint16_t &distance)
8080
return kSTkErrOk;
8181
}
8282

83-
sfeTkError_t sfeQwiicUltrasonic::changeAddress(const uint8_t &address)
83+
sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t &address)
8484
{
8585
// Check whether the address is valid
8686
sfeTkError_t err;
87+
size_t numBytes = 2;
88+
address <<= 1;
89+
const uint8_t toWrite[2] = {kUltrasonicAddressChangeCommand, address};
8790

8891
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
8992
return kSTkErrFail;
9093

9194
// Write the new address to the device. The first bit must be set to 1
92-
err = _theBus->writeRegisterByte(kUltrasonicAddressChangeCommand, (address<< 1));
95+
// err = _theBus->writeRegisterByte(kUltrasonicAddressChangeCommand, (address<< 1));
96+
err = _theBus->writeBlock(toWrite, numBytes);
9397

9498
// Check whether the write was successful
9599
if (err != kSTkErrOk)

src/sfeQwiicUltrasonic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class sfeQwiicUltrasonic
5252
/// @brief Changes the I2C address of the Qwiic Ultrasonic sensor
5353
/// @param address New address, must be in the range 0x20 to 0x2F
5454
/// @return 0 for succuss, negative for errors, positive for warnings
55-
sfeTkError_t changeAddress(const uint8_t &address);
55+
sfeTkError_t changeAddress(uint8_t &address);
5656

5757
/// @brief Gets the current I2C address being used by the library for the Qwiic Ultrasonic sensor
5858
/// @return The current I2C address, 7-bit unshifted

src/sfeTk/sfeTkIBus.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ class sfeTkIBus
9595
*/
9696
virtual sfeTkError_t writeByte(uint8_t data) = 0;
9797

98+
/**--------------------------------------------------------------------------
99+
* @brief Write a single byte to the device*
100+
* @param data Data to write.
101+
*
102+
* @retval sfeTkError_t - kSTkErrOk on successful execution.
103+
*
104+
*/
105+
virtual sfeTkError_t writeWord(uint16_t data) = 0;
106+
107+
/**--------------------------------------------------------------------------
108+
* @brief Write a single byte to the device*
109+
* @param data Data to write.
110+
*
111+
* @retval sfeTkError_t - kSTkErrOk on successful execution.
112+
*
113+
*/
114+
virtual sfeTkError_t writeBlock(const uint8_t *data, size_t length) = 0;
115+
98116
/**--------------------------------------------------------------------------
99117
* @brief Write a single byte to the given register
100118
*

src/sfeTkArdI2C.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
/*
22
sfeTkArdI2C.cpp
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2023 SparkFun Electronics
6+
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+
322
*/
423

524
#include "sfeTkArdI2C.h"
25+
#include <cstdint>
626

727
//---------------------------------------------------------------------------------
828
// init()
@@ -68,7 +88,7 @@ sfeTkError_t sfeTkArdI2C::ping()
6888
//---------------------------------------------------------------------------------
6989
// writeByte()
7090
//
71-
// Writes a single byte to the device.
91+
// Sends a single byte to the device.
7292
//
7393
// Returns true on success, false on failure
7494
//
@@ -83,6 +103,41 @@ sfeTkError_t sfeTkArdI2C::writeByte(uint8_t dataToWrite)
83103
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
84104
}
85105

106+
//---------------------------------------------------------------------------------
107+
// writeWord()
108+
//
109+
// Sends a word to the device.
110+
//
111+
// Returns true on success, false on failure
112+
//
113+
sfeTkError_t sfeTkArdI2C::writeWord(uint16_t dataToWrite)
114+
{
115+
if (!_i2cPort)
116+
return kSTkErrBusNotInit;
117+
118+
return writeBlock((uint8_t *)&dataToWrite, sizeof(uint16_t));
119+
}
120+
121+
//---------------------------------------------------------------------------------
122+
// writeBlock()
123+
//
124+
// Sends an array of data to the device.
125+
//
126+
// Returns true on success, false on failure
127+
//
128+
sfeTkError_t sfeTkArdI2C::writeBlock(const uint8_t *data, size_t length)
129+
{
130+
int nData = 0;
131+
if (!_i2cPort)
132+
return kSTkErrBusNotInit;
133+
134+
// do the Arduino I2C work
135+
_i2cPort->beginTransmission(address());
136+
_i2cPort->write(data, (int)length);
137+
138+
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
139+
}
140+
86141
//---------------------------------------------------------------------------------
87142
// writeRegisterByte()
88143
//

src/sfeTkArdI2C.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class sfeTkArdI2C : public sfeTkII2C
109109
sfeTkError_t ping();
110110

111111
/**
112-
@brief Write a single byte to the device
112+
@brief Sends a single byte to the device
113113
@note sfeTkIBus interface method
114114
115115
@param data Data to write.
@@ -118,6 +118,26 @@ class sfeTkArdI2C : public sfeTkII2C
118118
*/
119119
sfeTkError_t writeByte(uint8_t data);
120120

121+
/**
122+
@brief Sends a word to the device.
123+
@note sfeTkIBus interface method
124+
125+
@param data Data to write.
126+
127+
@retval returns kStkErrOk on success
128+
*/
129+
sfeTkError_t writeWord(uint16_t data);
130+
131+
/**
132+
@brief Sends a block of data to the device.
133+
@note sfeTkIBus interface method
134+
135+
@param data Data to write.
136+
137+
@retval returns kStkErrOk on success
138+
*/
139+
sfeTkError_t writeBlock(const uint8_t *data, size_t length);
140+
121141
/**
122142
@brief Write a single byte to the given register
123143
@note sfeTkIBus interface method

0 commit comments

Comments
 (0)