Skip to content

Commit 35223a5

Browse files
committed
Adds new parameter to constructor, updates examples with new parameter
* With new parameter, `updateAddress` was no longer needed
1 parent ac1f217 commit 35223a5

File tree

7 files changed

+73
-74
lines changed

7 files changed

+73
-74
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
1616

1717
// Create an ultrasonic sensor object
18-
QwiicUltrasonic myUltrasonic;
18+
QwiicUltrasonic myUltrasonic(kQwiicUltrasonicFWLatest);
1919

2020
// Here we set the device address. Note that an older version of the Qwiic
2121
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,

examples/Example2_OLED_Distance/Example2_OLED_Distance.ino

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@
2323
#include "res/qw_fnt_8x16.h"
2424
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
2525

26-
const int TRIG_PIN = 7;
27-
const int ECHO_PIN = 8;
28-
2926
// Create an ultrasonic sensor object
30-
QwiicUltrasonic myUltrasonic;
27+
QwiicUltrasonic myUltrasonic(kQwiicUltrasonicFWLatest);
3128
// Creat an OLED object
3229
QwiicNarrowOLED myOLED;
3330

examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const int echoPin = 8; // Echo Pin of Ultrasonic Sensor
3131
float distance = 0.0;
3232
float duration = 0.0;
3333
const float speedOfSound = 340.00; // Speed of sound in m/s
34-
const float convMilli= 1000.00; // Speed of sound in m/s
34+
const float millisPerSecond= 1000.00; // Number of milliseconds in a second
3535

3636
void setup() {
3737

@@ -64,7 +64,7 @@ void loop() {
6464
// Time until sound detected * speed of sound * conversion to mm
6565
// Divide by two because we only want the time the wave traveled to the object,
6666
// not to the object and back.
67-
distance = (duration * speedOfSound * convMilli) / 2;
67+
distance = (duration * speedOfSound * millisPerSecond) / 2;
6868

6969
// Print measurement
7070
Serial.print("Distance (mm): ");

examples/Example4_ChangeAddress/Example4_ChangeAddress.ino

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
1818

1919
// Create an ultrasonic sensor object
20-
QwiicUltrasonic myUltrasonic;
20+
QwiicUltrasonic myUltrasonic(kQwiicUltrasonicFWLatest);
2121

2222
// Here we set the device address. Note that an older version of the Qwiic
2323
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,
@@ -27,9 +27,9 @@ uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
2727
// uint8_t deviceAddress = 0x00;
2828

2929
// New addres is 7-bit unshifted.
30-
uint8_t NEW_ADDR = 0x1E;
30+
uint8_t newAddr = 0x20;
3131
//If using an older version of the Qwiic Ultrasonic, your address range is: 0x20 - 0x2F
32-
//uint8_t NEW_ADDR = 0x2F;
32+
//uint8_t newAddr = 0x2F;
3333

3434

3535
void setup()
@@ -55,13 +55,11 @@ void setup()
5555
delay(1000);
5656

5757
Serial.print("Changing Address To: ");
58-
Serial.println(NEW_ADDR, HEX);
58+
Serial.println(newAddr, HEX);
5959

6060

6161
// Call change address.....
62-
sfeTkError_t err = myUltrasonic.updateAddress(NEW_ADDR);
63-
// If you have an older version of the Qwiic Ultrasonic, you'll need to use the following:
64-
//sfeTkError_t err = myUltrasonic.changeAddress(NEW_ADDR);
62+
sfeTkError_t err = myUltrasonic.changeAddress(newAddr);
6563

6664
if(err)
6765
{
@@ -74,11 +72,8 @@ void setup()
7472
}
7573
delay(1000);
7674

77-
// I
78-
79-
8075
Serial.print("Load up example 1 with the new address at: ");
81-
Serial.println(NEW_ADDR, HEX);
76+
Serial.println(newAddr, HEX);
8277
Serial.println("Freezing....");
8378
while(1)
8479
;

library.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ paragraph=
77
category=Sensors
88
url=https://github.com/sparkfun/SparkFun_Qwiic_Ultrasonic_Arduino_Library
99
architectures=*
10-
depends=SparkFun Toolkit

src/sfeQwiicUltrasonic.cpp

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ sfeTkError_t sfeQwiicUltrasonic::begin(sfeTkII2C *theBus)
1919
return kSTkErrFail;
2020

2121
// Check the device address
22-
if (theBus->address() < kQwiicUltrasonicMinAddress || theBus->address() > kQwiicUltrasonicMaxAddress)
22+
if(_fwVersion == kQwiicUltrasonicFWOld)
2323
{
24-
// An older version of the firmware used 0x00 as the default address.
25-
// It's not really a valid address, but we need to allow it. Any other
26-
// address can't be used
27-
if (theBus->address() != 0x00)
28-
return kSTkErrFail;
29-
}
3024

25+
if (theBus->address() < kQwiicUltrasonicMinAddress || theBus->address() > kQwiicUltrasonicMaxAddress)
26+
{
27+
// An older version of the firmware used 0x00 as the default address.
28+
// It's not really a valid address, but we need to allow it. Any other
29+
// address can't be used
30+
if (theBus->address() != 0x00)
31+
return kSTkErrFail;
32+
}
33+
}
3134
// Set bus pointer
3235
_theBus = theBus;
3336

@@ -48,7 +51,8 @@ sfeTkError_t sfeQwiicUltrasonic::getDistance(uint16_t &distance)
4851
uint8_t rawData[2] = {};
4952

5053
// Get the distance
51-
sfeTkError_t err = _theBus->readBlock(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);
54+
//sfeTkError_t err = _theBus->readBlock(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);
55+
sfeTkError_t err = _theBus->readRegisterRegion(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);
5256

5357
// Check whether the read was successful
5458
if (err != kSTkErrOk)
@@ -61,41 +65,40 @@ sfeTkError_t sfeQwiicUltrasonic::getDistance(uint16_t &distance)
6165
return kSTkErrOk;
6266
}
6367

64-
sfeTkError_t sfeQwiicUltrasonic::changeAddress(const uint8_t &address)
68+
sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t &address)
6569
{
6670
// Check whether the address is valid
67-
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
68-
return kSTkErrFail;
69-
70-
// Write the new address to the device. The first bit must be set to 1
71-
sfeTkError_t err = _theBus->writeByte(address | 0x80);
72-
73-
// Check whether the write was successful
74-
if (err != kSTkErrOk)
75-
return err;
7671

77-
// Update the address in the bus
78-
_theBus->setAddress(address);
72+
sfeTkError_t err;
7973

80-
// Done!
81-
return kSTkErrOk;
82-
}
74+
if(_fwVersion == kQwiicUltrasonicFWOld)
75+
{
76+
// Old firmware only supports a limited range of addresses.
77+
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
78+
return kSTkErrFail;
8379

84-
sfeTkError_t sfeQwiicUltrasonic::updateAddress(uint8_t &address)
85-
{
86-
// Check whether the address is valid
87-
sfeTkError_t err;
88-
size_t numBytes = 2;
89-
// We want to shift the address left before we send it.
90-
91-
address <<= 1;
92-
const uint8_t toWrite[2] = {kUltrasonicAddressChangeCommand, address};
80+
// Write the new address to the device. The first bit must be set to 1
81+
err = _theBus->writeByte(address | 0x80);
82+
}
83+
else if(_fwVersion == kQwiicUltrasonicFWLatest)
84+
{
85+
size_t numBytes = 2;
86+
// Latest firmware versions supports all of the available I2C addresses.
87+
if (address < kQwiicUltrasonicI2CAddressMin || address > kQwiicUltrasonicI2CAddressMax)
88+
return kSTkErrFail;
9389

94-
if (address < kQwiicI2CAddressMin|| address > kQwiicI2CAddressMax)
95-
return kSTkErrFail;
90+
// We want to shift the address left before we send it.
91+
address <<= 1;
92+
const uint8_t toWrite[2] = {kUltrasonicAddressChangeCommand, address};
9693

97-
// Write the new address to the device.
98-
err = _theBus->writeBlock(toWrite, numBytes);
94+
//Write the new address to the device
95+
err = _theBus->writeBlock(toWrite, numBytes);
96+
}
97+
else {
98+
//There was some error setting the version in the constructor
99+
//return an error.
100+
return kSTkErrOk;
101+
}
99102

100103
// Check whether the write was successful
101104
if (err != kSTkErrOk)

src/sfeQwiicUltrasonic.h

Lines changed: 24 additions & 19 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
@@ -12,20 +12,21 @@
1212
#pragma once
1313

1414
#include "SparkFun_Toolkit.h"
15-
#include <stdint.h>
1615

1716
// Available I2C addresses of the Qwiic Ultrasonic
1817
const uint8_t kQwiicUltrasonicDefaultAddress = 0x2F;
18+
19+
const uint8_t kQwiicUltrasonicFWLatest = 0x01;
20+
const uint8_t kQwiicUltrasonicFWOld = 0x10;
21+
1922
// These addresses are the min and max (respectively) of valid I2C addresses that can
2023
// be used for the newest revision of the Qwiic Ultrasonic sensor.
21-
const uint8_t kQwiicI2CAddressMin = 0x08;
22-
const uint8_t kQwiicI2CAddressMax = 0x7F;
24+
const uint8_t kQwiicUltrasonicI2CAddressMin = 0x08;
25+
const uint8_t kQwiicUltrasonicI2CAddressMax = 0x7F;
2326
// Available I2C addresses of the Qwiic Ultrasonic
24-
const uint8_t kQwiicUltrasonicAddresses[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
25-
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
26-
const uint8_t kQwiicUltrasonicNumAddresses = sizeof(kQwiicUltrasonicAddresses) / sizeof(uint8_t);
27-
const uint8_t kQwiicUltrasonicMinAddress = kQwiicUltrasonicAddresses[0];
28-
const uint8_t kQwiicUltrasonicMaxAddress = kQwiicUltrasonicAddresses[15];;
27+
const uint8_t kQwiicUltrasonicMinAddress = 0x20;
28+
const uint8_t kQwiicUltrasonicMaxAddress = 0x2F;
29+
;
2930
// I2C commands
3031
const uint8_t kUltrasonicDistanceReadCommand = 0x01;
3132
const uint8_t kUltrasonicAddressChangeCommand = 0x04;
@@ -34,10 +35,16 @@ class sfeQwiicUltrasonic
3435
{
3536
public:
3637
/// @brief Default constructor
37-
sfeQwiicUltrasonic() : _theBus(nullptr)
38+
sfeQwiicUltrasonic() : _theBus(nullptr), _fwVersion(kQwiicUltrasonicFWLatest)
3839
{
3940
}
4041

42+
/// @brief Default constructor
43+
sfeQwiicUltrasonic(uint8_t fwVersion) : _theBus(nullptr), _fwVersion(fwVersion)
44+
{
45+
}
46+
47+
4148
/// @brief Begins the Qwiic Ultrasonic sensor
4249
/// @param theBus I2C bus to use for communication
4350
/// @return 0 for succuss, negative for errors, positive for warnings
@@ -55,17 +62,15 @@ class sfeQwiicUltrasonic
5562
/// @brief Changes the I2C address of older Qwiic Ultrasonic sensors.
5663
/// @param address New address, must be in the range 0x20 to 0x2F
5764
/// @return 0 for succuss, negative for errors, positive for warnings
58-
sfeTkError_t changeAddress(const uint8_t &address);
59-
60-
/// @brief Changes the I2C address of the latest revision of the Qwiic Ultrasonic sensor.
61-
/// @param address New address, must be in the range 0x20 to 0x2F
62-
/// @return 0 for succuss, negative for errors, positive for warnings
63-
sfeTkError_t updateAddress(uint8_t &address);
65+
sfeTkError_t changeAddress(uint8_t &address);
6466

6567
/// @brief Gets the current I2C address being used by the library for the Qwiic Ultrasonic sensor
6668
/// @return The current I2C address, 7-bit unshifted
6769
uint8_t getAddress();
6870

6971
protected:
7072
sfeTkII2C *_theBus;
73+
74+
private:
75+
uint8_t _fwVersion = 0x00;
7176
};

0 commit comments

Comments
 (0)