Skip to content

Commit 64bff81

Browse files
committed
Adds I2C commands to relevant function
1 parent 8cf35e8 commit 64bff81

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void setup()
1414
{
1515
// Start serial
1616
Serial.begin(115200);
17-
Serial.println("Qwiic Ultrasonic Example 1 - Basic Readings");
17+
Serial.println("Ultrasonic Distance Sensor Example 1 - Basic Distance Sensing");
1818

1919
Wire.begin();
2020

@@ -38,6 +38,12 @@ void loop()
3838
Serial.print("Distance (mm): ");
3939
Serial.println(distance);
4040

41+
//Serial.println("Distance (cm): ");
42+
//Serial.print((distance / 10.0), 2);
43+
44+
//Serial.println("Distace (in): ");
45+
//Serial.print((distance / 25.4), 2);
46+
4147
// Wait a bit
4248
delay(250);
4349
}

src/sfeQwiicUltrasonic.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ sfeTkError_t sfeQwiicUltrasonic::getDistace(uint16_t &distance)
3333
{
3434
size_t bytesRead = 0;
3535
uint8_t rawData[2] = {0, 0};
36+
sfeTkError_t err;
3637

37-
// Attempt to read the distance
38-
sfeTkError_t err = _theBus->write(kQwiicUltrasonicRegisterTrigger, rawData, 2, bytesRead);
39-
sfeTkError_t err = _theBus->readRegisterRegion(kQwiicUltrasonicRegisterTrigger, rawData, 2, bytesRead);
38+
_theBus->writeRegisterByte(theBus->address(), kUltrasonicDistanceReadCommand);
39+
delay(10);
40+
err = _theBus->readRegisterRegion(kQwiicUltrasonicRegisterTrigger, rawData, 2, bytesRead);
4041

4142
// Check whether the read was successful
4243
if (err != kSTkErrOk)
@@ -71,11 +72,14 @@ sfeTkError_t sfeQwiicUltrasonic::getTriggeredDistance(uint16_t &distance)
7172
sfeTkError_t sfeQwiicUltrasonic::changeAddress(const uint8_t &address)
7273
{
7374
// Check whether the address is valid
75+
sfeTkError_t err;
76+
7477
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
7578
return kSTkErrFail;
7679

7780
// Write the new address to the device. The first bit must be set to 1
78-
sfeTkError_t err = _theBus->writeByte(address | 0x80);
81+
_theBus->writeRegisterByte(_theBus->address(), kUltrasonicAddressChangeCommand);
82+
err = _theBus->writeRegisterByte(_theBus->address(), address << 1);
7983

8084
// Check whether the write was successful
8185
if (err != kSTkErrOk)

src/sfeQwiicUltrasonic.h

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

66
// Available I2C addresses of the Qwiic Ultrasonic
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) / sizeof(uint8_t);
10-
const uint8_t kQwiicUltrasonicMinAddress = kQwiicUltrasonicAddresses[0];
11-
const uint8_t kQwiicUltrasonicMaxAddress = kQwiicUltrasonicAddresses[15];
127
const uint8_t kQwiicUltrasonicDefaultAddress = 0x2F;
13-
14-
// Register to trigger a new measuremnt and read the previous one
15-
const uint8_t kQwiicUltrasonicRegisterTrigger = 0x01;
8+
const uint8_t kQwiicUltrasonicMinAddress = 0x08;
9+
const uint8_t kQwiicUltrasonicMaxAddress = 0x7F;
10+
// I2C commands
11+
const uint8_t kUltrasonicDistanceReadCommand = 0x01;
12+
const uint8_t kUltrasonicAddressChangeCommand = 0x04;
1613

1714
class sfeQwiicUltrasonic
1815
{
@@ -46,7 +43,7 @@ class sfeQwiicUltrasonic
4643
/// @return 0 for succuss, negative for errors, positive for warnings
4744
sfeTkError_t changeAddress(const uint8_t &address);
4845

49-
/// @brief Gets the current I2C address of the Qwiic Ultrasonic sensor
46+
/// @brief Gets the current I2C address being used by the library for the Qwiic Ultrasonic sensor
5047
/// @return The current I2C address, 7-bit unshifted
5148
uint8_t getAddress();
5249

0 commit comments

Comments
 (0)