Skip to content

Commit 6226419

Browse files
committed
Code review updates
Change constants to Hungarian notation Move _theBus initializer into header Change changeAddress() to take a `const uint8_t&` Remove under read check, since that will be caught by previous check of err Add address getter
1 parent 93a4710 commit 6226419

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
22

3-
// Create an Ultrasonic sensor object
3+
// Create an ultrasonic sensor object
44
QwiicUltrasonic myUltrasonic;
55

66
// Here we set the device address. Note that an older version of the Qwiic
77
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,
88
// you'll need to change the address below. It is also recommended to run
99
// Example 2 to change the address to the new default.
10-
uint8_t deviceAddress = QWIIC_ULTRASONIC_DEFAULT_ADDRESS;
10+
uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
1111
// uint8_t deviceAddress = 0x00;
1212

1313
void setup()
@@ -19,7 +19,7 @@ void setup()
1919
Wire.begin();
2020

2121
// Attempt to begin the sensor
22-
while(myUltrasonic.begin(deviceAddress) == false)
22+
while (myUltrasonic.begin(deviceAddress) == false)
2323
{
2424
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
2525
delay(1000);

examples/Example2_ChangeAddress/Example2_ChangeAddress.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
22

3-
// Create an Ultrasonic sensor object
3+
// Create an ultrasonic sensor object
44
QwiicUltrasonic myUltrasonic;
55

66
// Here we set the device address. Note that an older version of the Qwiic
77
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,
88
// you'll need to change the address below. It is also recommended to run
99
// Example 2 to change the address to the new default.
10-
uint8_t deviceAddress = QWIIC_ULTRASONIC_DEFAULT_ADDRESS;
11-
// uint8_t deviceAddress = 0x20;
10+
uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
11+
// uint8_t deviceAddress = 0x00;
1212

1313
void setup()
1414
{
@@ -55,7 +55,7 @@ void setup()
5555
Serial.println(newAddress, HEX);
5656

5757
// Check if the address is valid
58-
if (newAddress < QWIIC_ULTRASONIC_MIN_ADDRESS || newAddress > QWIIC_ULTRASONIC_MAX_ADDRESS)
58+
if (newAddress < kQwiicUltrasonicMinAddress || newAddress > kQwiicUltrasonicMaxAddress)
5959
{
6060
Serial.println("Invalid address!");
6161
continue;

src/SparkFun_Qwiic_Ultrasonic_Arduino_Library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class QwiicUltrasonic : public sfeQwiicUltrasonic
1111
/// @param address I2C device address to use for the sensor
1212
/// @param wirePort Wire port to use for I2C communication
1313
/// @return True if successful, false otherwise
14-
bool begin(uint8_t address = QWIIC_ULTRASONIC_DEFAULT_ADDRESS, TwoWire &wirePort = Wire)
14+
bool begin(uint8_t address = kQwiicUltrasonicDefaultAddress, TwoWire &wirePort = Wire)
1515
{
1616
// Setup Arudino I2C bus
1717
_theI2CBus.init(wirePort, address);

src/sfeQwiicUltrasonic.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
#include "sfeQwiicUltrasonic.h"
22

3-
sfeQwiicUltrasonic::sfeQwiicUltrasonic()
4-
{
5-
_theBus = nullptr;
6-
}
7-
83
sfeTkError_t sfeQwiicUltrasonic::begin(sfeTkII2C *theBus)
94
{
105
// Nullptr check
116
if (theBus == nullptr)
127
return kSTkErrFail;
138

149
// Check the device address
15-
if (theBus->address() < QWIIC_ULTRASONIC_MIN_ADDRESS || theBus->address() > QWIIC_ULTRASONIC_MAX_ADDRESS)
10+
if (theBus->address() < kQwiicUltrasonicMinAddress || theBus->address() > kQwiicUltrasonicMaxAddress)
1611
{
1712
// An older version of the firmware used 0x00 as the default address.
1813
// It's not really a valid address, but we need to allow it. Any other
@@ -40,34 +35,27 @@ sfeTkError_t sfeQwiicUltrasonic::triggerAndRead(uint16_t &distance)
4035
uint8_t rawData[2] = {0, 0};
4136

4237
// Attempt to read the distance
43-
sfeTkError_t err = _theBus->readRegisterRegion(QWIIC_ULTRASONIC_REGISTER_TRIGGER, rawData, 2, bytesRead);
38+
sfeTkError_t err = _theBus->readRegisterRegion(kQwiicUltrasonicRegisterTrigger, rawData, 2, bytesRead);
4439

4540
// Check whether the read was successful
4641
if (err)
4742
return err;
4843

49-
// Check whether all data was read
50-
if (bytesRead != 2)
51-
return kSTkErrFail;
52-
5344
// Store raw data
5445
distance = (rawData[0] << 8) | rawData[1];
5546

5647
// Done!
5748
return kSTkErrOk;
5849
}
5950

60-
sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t address)
51+
sfeTkError_t sfeQwiicUltrasonic::changeAddress(const uint8_t &address)
6152
{
6253
// Check whether the address is valid
63-
if (address < QWIIC_ULTRASONIC_MIN_ADDRESS || address > QWIIC_ULTRASONIC_MAX_ADDRESS)
54+
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
6455
return kSTkErrFail;
6556

66-
// The first bit of the address must be set to 1
67-
address |= 0x80;
68-
69-
// Write the new address to the device
70-
sfeTkError_t err = _theBus->writeByte(address);
57+
// Write the new address to the device. The first bit must be set to 1
58+
sfeTkError_t err = _theBus->writeByte(address | 0x80);
7159

7260
// Check whether the write was successful
7361
if (err)
@@ -79,3 +67,8 @@ sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t address)
7967
// Done!
8068
return kSTkErrOk;
8169
}
70+
71+
uint8_t sfeQwiicUltrasonic::getAddress()
72+
{
73+
return _theBus->address();
74+
}

src/sfeQwiicUltrasonic.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
#include <stdint.h>
55

66
// Available I2C addresses of the Qwiic Ultrasonic
7-
const uint8_t QWIIC_ULTRASONIC_ADDRESSES[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
8-
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
9-
const uint8_t QWIIC_ULTRASONIC_NUM_ADDRESSES = sizeof(QWIIC_ULTRASONIC_ADDRESSES);
10-
const uint8_t QWIIC_ULTRASONIC_MIN_ADDRESS = QWIIC_ULTRASONIC_ADDRESSES[0];
11-
const uint8_t QWIIC_ULTRASONIC_MAX_ADDRESS = QWIIC_ULTRASONIC_ADDRESSES[15];
12-
const uint8_t QWIIC_ULTRASONIC_DEFAULT_ADDRESS = 0x2F;
7+
const uint8_t kQwiicUltrasonicAddresses[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
8+
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
9+
const uint8_t kQwiicUltrasonicNumAddresses = sizeof(kQwiicUltrasonicAddresses);
10+
const uint8_t kQwiicUltrasonicMinAddress = kQwiicUltrasonicAddresses[0];
11+
const uint8_t kQwiicUltrasonicMaxAddress = kQwiicUltrasonicAddresses[15];
12+
const uint8_t kQwiicUltrasonicDefaultAddress = 0x2F;
1313

1414
// Register to trigger a new measuremnt and read the previous one
15-
const uint8_t QWIIC_ULTRASONIC_REGISTER_TRIGGER = 0x01;
15+
const uint8_t kQwiicUltrasonicRegisterTrigger = 0x01;
1616

1717
class sfeQwiicUltrasonic
1818
{
1919
public:
2020
/// @brief Default constructor
21-
sfeQwiicUltrasonic();
21+
sfeQwiicUltrasonic() : _theBus(nullptr)
22+
{
23+
}
2224

2325
/// @brief Begins the Qwiic Ultrasonic sensor
2426
/// @param theBus I2C bus to use for communication
@@ -37,7 +39,11 @@ class sfeQwiicUltrasonic
3739
/// @brief Changes the I2C address of the Qwiic Ultrasonic sensor
3840
/// @param address New address, must be in the range 0x20 to 0x2F
3941
/// @return 0 for succuss, negative for errors, positive for warnings
40-
sfeTkError_t changeAddress(uint8_t address);
42+
sfeTkError_t changeAddress(const uint8_t &address);
43+
44+
/// @brief Gets the current I2C address of the Qwiic Ultrasonic sensor
45+
/// @return The current I2C address, 7-bit unshifted
46+
uint8_t getAddress();
4147

4248
protected:
4349
sfeTkII2C *_theBus;

0 commit comments

Comments
 (0)