Skip to content

Commit 9e4e186

Browse files
committed
Re-adds original changeAddress function and changes the current on in this branch to:
`updateAddress` * Re-adds original address list * Adds constants for general min and max i2c addresses * Updates language around the address related functions
1 parent b47b595 commit 9e4e186

File tree

5 files changed

+42
-31
lines changed

5 files changed

+42
-31
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ void setup()
3333
Wire.begin();
3434

3535
// Attempt to begin the sensor
36-
if (myUltrasonic.begin(deviceAddress) == false)
36+
while (myUltrasonic.begin(deviceAddress) == false)
3737
{
3838
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
39-
while(1)
40-
;
39+
delay(2000);
4140
}
4241

4342
Serial.println("Ultrasonic sensor connected!");

examples/Example2_BasicDistanceTrigger/Example2_BasicDistanceTrigger.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ void setup() {
3838
pinMode(echoPin, INPUT);
3939

4040
// Attempt to begin the sensor
41-
if (myUltrasonic.begin(deviceAddress) == false)
41+
while (myUltrasonic.begin(deviceAddress) == false)
4242
{
4343
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
44-
while(1)
45-
;
44+
delay(2000);
4645
}
4746

4847
Serial.println("Ultrasonic sensor connected!");

examples/Example3_ChangeAddress/Example3_ChangeAddress.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ void setup()
4242
;
4343

4444
// Attempt to begin the sensor
45-
if (myUltrasonic.begin(kQwiicUltrasonicDefaultAddress) == false )
45+
while (myUltrasonic.begin(deviceAddress) == false)
4646
{
4747
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
48+
delay(2000);
4849
}
4950

5051
Serial.println("Ready to change address.");

src/sfeQwiicUltrasonic.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111

1212
#include "sfeQwiicUltrasonic.h"
13+
#include "sfeTk/sfeTkError.h"
14+
#include <cstdint>
1315

1416
sfeTkError_t sfeQwiicUltrasonic::begin(sfeTkII2C *theBus)
1517
{
@@ -42,12 +44,12 @@ sfeTkError_t sfeQwiicUltrasonic::isConnected()
4244

4345
sfeTkError_t sfeQwiicUltrasonic::getDistance(uint16_t &distance)
4446
{
45-
size_t bytesRead = 0;
46-
uint8_t rawData[2] = {0, 0};
47-
sfeTkError_t err;
47+
size_t bytesRead;
48+
size_t numBytes = 2;
49+
uint8_t rawData[2] = {};
4850

49-
_theBus->writeByte(kUltrasonicDistanceReadCommand);
50-
err = _theBus->readRegisterRegion(_theBus->address(), rawData, 2, bytesRead);
51+
// Get the distance
52+
sfeTkError_t err = _theBus->readBlock(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);
5153

5254
// Check whether the read was successful
5355
if (err != kSTkErrOk)
@@ -60,39 +62,40 @@ sfeTkError_t sfeQwiicUltrasonic::getDistance(uint16_t &distance)
6062
return kSTkErrOk;
6163
}
6264

63-
sfeTkError_t sfeQwiicUltrasonic::getTriggeredDistance(uint16_t &distance)
65+
sfeTkError_t sfeQwiicUltrasonic::changeAddress(const uint8_t &address)
6466
{
65-
size_t bytesRead = 0;
66-
uint8_t rawData[2] = {0, 0};
67+
// Check whether the address is valid
68+
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
69+
return kSTkErrFail;
6770

68-
// Attempt to read the distance
69-
sfeTkError_t err = _theBus->readRegisterRegion(_theBus->address(), rawData, 2, bytesRead);
71+
// Write the new address to the device. The first bit must be set to 1
72+
sfeTkError_t err = _theBus->writeByte(address | 0x80);
7073

71-
// Check whether the read was successful
74+
// Check whether the write was successful
7275
if (err != kSTkErrOk)
7376
return err;
7477

75-
// Store raw data
76-
distance = (rawData[0] << 8) | rawData[1];
78+
// Update the address in the bus
79+
_theBus->setAddress(address);
7780

7881
// Done!
7982
return kSTkErrOk;
8083
}
8184

82-
sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t &address)
85+
sfeTkError_t sfeQwiicUltrasonic::updateAddress(uint8_t &address)
8386
{
8487
// Check whether the address is valid
8588
sfeTkError_t err;
8689
size_t numBytes = 2;
8790
// We want to shift the address left before we send it.
91+
8892
address <<= 1;
8993
const uint8_t toWrite[2] = {kUltrasonicAddressChangeCommand, address};
9094

91-
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
95+
if (address < kQwiicI2CAddressMin|| address > kQwiicI2CAddressMax)
9296
return kSTkErrFail;
9397

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

98101
// Check whether the write was successful

src/sfeQwiicUltrasonic.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@
1616

1717
// Available I2C addresses of the Qwiic Ultrasonic
1818
const uint8_t kQwiicUltrasonicDefaultAddress = 0x2F;
19-
const uint8_t kQwiicUltrasonicMinAddress = 0x08;
20-
const uint8_t kQwiicUltrasonicMaxAddress = 0x7F;
19+
// These addresses are the min and max (respectively) of valid I2C addresses that can
20+
// be used for the newest revision of the Qwiic Ultrasonic sensor.
21+
const uint8_t kQwiicI2CAddressMin = 0x08;
22+
const uint8_t kQwiicI2CAddressMax = 0x7F;
23+
// Available I2C addresses of the Qwiic Ultrasonic
24+
const uint8_t kQwiicUltrasonicDefaultAddress = 0x2F;
25+
const uint8_t kQwiicUltrasonicAddresses[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
26+
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
27+
const uint8_t kQwiicUltrasonicNumAddresses = sizeof(kQwiicUltrasonicAddresses) / sizeof(uint8_t);
28+
const uint8_t kQwiicUltrasonicMinAddress = kQwiicUltrasonicAddresses[0];
29+
const uint8_t kQwiicUltrasonicMaxAddress = kQwiicUltrasonicAddresses[15];;
2130
// I2C commands
2231
const uint8_t kUltrasonicDistanceReadCommand = 0x01;
2332
const uint8_t kUltrasonicAddressChangeCommand = 0x04;
@@ -44,15 +53,15 @@ class sfeQwiicUltrasonic
4453
/// @return 0 for succuss, negative for errors, positive for warnings
4554
sfeTkError_t getDistance(uint16_t &distance);
4655

47-
/// @brief Triggers a new measurement and reads the previous one
48-
/// @param distance Distance in mm
56+
/// @brief Changes the I2C address of older Qwiic Ultrasonic sensors.
57+
/// @param address New address, must be in the range 0x20 to 0x2F
4958
/// @return 0 for succuss, negative for errors, positive for warnings
50-
sfeTkError_t getTriggeredDistance(uint16_t &distance);
59+
sfeTkError_t changeAddress(const uint8_t &address);
5160

52-
/// @brief Changes the I2C address of the Qwiic Ultrasonic sensor
61+
/// @brief Changes the I2C address of the latest revision of the Qwiic Ultrasonic sensor.
5362
/// @param address New address, must be in the range 0x20 to 0x2F
5463
/// @return 0 for succuss, negative for errors, positive for warnings
55-
sfeTkError_t changeAddress(uint8_t &address);
64+
sfeTkError_t updateAddress(uint8_t &address);
5665

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

0 commit comments

Comments
 (0)