Skip to content

v10 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
249ec0f
Adjusts example 1, adds additional function to ultrasonic class
edspark May 27, 2024
82ea547
Adds some examples that will be rolled in shortly
edspark May 28, 2024
8cf35e8
Adds write to getDistance
edspark May 28, 2024
64bff81
Adds I2C commands to relevant function
edspark May 28, 2024
11ad0f1
Updates examples, add comments, and licenses
edspark May 29, 2024
e0bc499
Removes unterminated comment
edspark May 29, 2024
ee168f5
Fixes missing include and indentation
edspark May 29, 2024
fe78ad5
Changes name of example 2 for camel case consistency
edspark May 29, 2024
89efacb
Uses the new writeBlock method, changes type for changeAddress functi…
edspark May 30, 2024
5131495
Removes bus files, adds bus files as submodule (may not work), adds w…
edspark May 30, 2024
3663b74
Fixes submodule path (maybe)
edspark May 30, 2024
00e5dd6
Adds toolkit as dependency for Arduino library manager
edspark May 30, 2024
4523063
Removes underscore in Toolkit listing
edspark May 30, 2024
6997329
Removes git submodule
edspark May 30, 2024
e5bf0b2
Updates workflow
edspark May 30, 2024
0b4ba7a
Fixes polarity of echo pin to reflect change to firmware
edspark May 31, 2024
787e410
Cleaning up last example
edspark Jun 3, 2024
b47b595
Re-adds dependency
edspark Jun 5, 2024
9e4e186
Re-adds original `changeAddress` function and changes the current on …
edspark Jun 6, 2024
c6415b9
Changes conversion function
edspark Jun 7, 2024
d5ecebc
Adds link to OLED library
edspark Jun 7, 2024
41d07b8
Adds updated method for changing address while commenting out older m…
edspark Jun 7, 2024
b8715e0
Reorders examples
edspark Jun 7, 2024
c4f3779
Adds example using trigger and echo pins
edspark Jun 7, 2024
ac1f217
Removes include
edspark Jun 14, 2024
35223a5
Adds new parameter to constructor, updates examples with new parameter
edspark Jun 18, 2024
fe2c0aa
Minor fix to function calls now used in the toolkit
edspark Jun 18, 2024
d100f66
Fix to firmware check
edspark Jun 18, 2024
5a36824
Fixes to comments and some formatting
edspark Jun 18, 2024
c58db09
Adds on more comment
edspark Jun 18, 2024
c51c6b0
Changes `getDistance()` back to its original name `triggerAndRead()`
edspark Jun 19, 2024
8668f36
Removes erroneous include
edspark Jun 19, 2024
4578da4
Fixes error when writing new address to the ultrasonic object forcing…
edspark Jun 21, 2024
2278ae6
Update README.md
loricrotser Jun 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/Example1_BasicReadings/Example1_BasicReadings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"

// Create an ultrasonic sensor object
QwiicUltrasonic myUltrasonic;
QwiicUltrasonic myUltrasonic(kQwiicUltrasonicFWLatest);

// Here we set the device address. Note that an older version of the Qwiic
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,
Expand Down
5 changes: 1 addition & 4 deletions examples/Example2_OLED_Distance/Example2_OLED_Distance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@
#include "res/qw_fnt_8x16.h"
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"

const int TRIG_PIN = 7;
const int ECHO_PIN = 8;

// Create an ultrasonic sensor object
QwiicUltrasonic myUltrasonic;
QwiicUltrasonic myUltrasonic(kQwiicUltrasonicFWLatest);
// Creat an OLED object
QwiicNarrowOLED myOLED;

Expand Down
4 changes: 2 additions & 2 deletions examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const int echoPin = 8; // Echo Pin of Ultrasonic Sensor
float distance = 0.0;
float duration = 0.0;
const float speedOfSound = 340.00; // Speed of sound in m/s
const float convMilli= 1000.00; // Speed of sound in m/s
const float millisPerSecond= 1000.00; // Number of milliseconds in a second

void setup() {

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

// Print measurement
Serial.print("Distance (mm): ");
Expand Down
17 changes: 6 additions & 11 deletions examples/Example4_ChangeAddress/Example4_ChangeAddress.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"

// Create an ultrasonic sensor object
QwiicUltrasonic myUltrasonic;
QwiicUltrasonic myUltrasonic(kQwiicUltrasonicFWLatest);

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

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


void setup()
Expand All @@ -55,13 +55,11 @@ void setup()
delay(1000);

Serial.print("Changing Address To: ");
Serial.println(NEW_ADDR, HEX);
Serial.println(newAddr, HEX);


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

if(err)
{
Expand All @@ -74,11 +72,8 @@ void setup()
}
delay(1000);

// I


Serial.print("Load up example 1 with the new address at: ");
Serial.println(NEW_ADDR, HEX);
Serial.println(newAddr, HEX);
Serial.println("Freezing....");
while(1)
;
Expand Down
1 change: 0 additions & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ paragraph=
category=Sensors
url=https://github.com/sparkfun/SparkFun_Qwiic_Ultrasonic_Arduino_Library
architectures=*
depends=SparkFun Toolkit
75 changes: 39 additions & 36 deletions src/sfeQwiicUltrasonic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ sfeTkError_t sfeQwiicUltrasonic::begin(sfeTkII2C *theBus)
return kSTkErrFail;

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

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

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

// Get the distance
sfeTkError_t err = _theBus->readBlock(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);
//sfeTkError_t err = _theBus->readBlock(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);
sfeTkError_t err = _theBus->readRegisterRegion(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);

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

sfeTkError_t sfeQwiicUltrasonic::changeAddress(const uint8_t &address)
sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t &address)
{
// Check whether the address is valid
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
return kSTkErrFail;

// Write the new address to the device. The first bit must be set to 1
sfeTkError_t err = _theBus->writeByte(address | 0x80);

// Check whether the write was successful
if (err != kSTkErrOk)
return err;

// Update the address in the bus
_theBus->setAddress(address);
sfeTkError_t err;

// Done!
return kSTkErrOk;
}
if(_fwVersion == kQwiicUltrasonicFWOld)
{
// Old firmware only supports a limited range of addresses.
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
return kSTkErrFail;

sfeTkError_t sfeQwiicUltrasonic::updateAddress(uint8_t &address)
{
// Check whether the address is valid
sfeTkError_t err;
size_t numBytes = 2;
// We want to shift the address left before we send it.

address <<= 1;
const uint8_t toWrite[2] = {kUltrasonicAddressChangeCommand, address};
// Write the new address to the device. The first bit must be set to 1
err = _theBus->writeByte(address | 0x80);
}
else if(_fwVersion == kQwiicUltrasonicFWLatest)
{
size_t numBytes = 2;
// Latest firmware versions supports all of the available I2C addresses.
if (address < kQwiicUltrasonicI2CAddressMin || address > kQwiicUltrasonicI2CAddressMax)
return kSTkErrFail;

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

// Write the new address to the device.
err = _theBus->writeBlock(toWrite, numBytes);
//Write the new address to the device
err = _theBus->writeBlock(toWrite, numBytes);
}
else {
//There was some error setting the version in the constructor
//return an error.
return kSTkErrOk;
}

// Check whether the write was successful
if (err != kSTkErrOk)
Expand Down
43 changes: 24 additions & 19 deletions src/sfeQwiicUltrasonic.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* SparkFun Ulrasonic Distance Sensor
*
* Product:
/* SparkFun Ulrasonic Distance Sensor
*
* Product:
* * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-1XXXX)
* * https://www.sparkfun.com/1XXXX
*
*
* SPDX-License-Identifier: MIT
*
* Copyright (c) 2024 SparkFun Electronics
Expand All @@ -12,20 +12,21 @@
#pragma once

#include "SparkFun_Toolkit.h"
#include <stdint.h>

// Available I2C addresses of the Qwiic Ultrasonic
const uint8_t kQwiicUltrasonicDefaultAddress = 0x2F;

const uint8_t kQwiicUltrasonicFWLatest = 0x01;
const uint8_t kQwiicUltrasonicFWOld = 0x10;

// These addresses are the min and max (respectively) of valid I2C addresses that can
// be used for the newest revision of the Qwiic Ultrasonic sensor.
const uint8_t kQwiicI2CAddressMin = 0x08;
const uint8_t kQwiicI2CAddressMax = 0x7F;
const uint8_t kQwiicUltrasonicI2CAddressMin = 0x08;
const uint8_t kQwiicUltrasonicI2CAddressMax = 0x7F;
// Available I2C addresses of the Qwiic Ultrasonic
const uint8_t kQwiicUltrasonicAddresses[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
const uint8_t kQwiicUltrasonicNumAddresses = sizeof(kQwiicUltrasonicAddresses) / sizeof(uint8_t);
const uint8_t kQwiicUltrasonicMinAddress = kQwiicUltrasonicAddresses[0];
const uint8_t kQwiicUltrasonicMaxAddress = kQwiicUltrasonicAddresses[15];;
const uint8_t kQwiicUltrasonicMinAddress = 0x20;
const uint8_t kQwiicUltrasonicMaxAddress = 0x2F;
;
// I2C commands
const uint8_t kUltrasonicDistanceReadCommand = 0x01;
const uint8_t kUltrasonicAddressChangeCommand = 0x04;
Expand All @@ -34,10 +35,16 @@ class sfeQwiicUltrasonic
{
public:
/// @brief Default constructor
sfeQwiicUltrasonic() : _theBus(nullptr)
sfeQwiicUltrasonic() : _theBus(nullptr), _fwVersion(kQwiicUltrasonicFWLatest)
{
}

/// @brief Default constructor
sfeQwiicUltrasonic(uint8_t fwVersion) : _theBus(nullptr), _fwVersion(fwVersion)
{
}


/// @brief Begins the Qwiic Ultrasonic sensor
/// @param theBus I2C bus to use for communication
/// @return 0 for succuss, negative for errors, positive for warnings
Expand All @@ -55,17 +62,15 @@ class sfeQwiicUltrasonic
/// @brief Changes the I2C address of older Qwiic Ultrasonic sensors.
/// @param address New address, must be in the range 0x20 to 0x2F
/// @return 0 for succuss, negative for errors, positive for warnings
sfeTkError_t changeAddress(const uint8_t &address);

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

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

protected:
sfeTkII2C *_theBus;

private:
uint8_t _fwVersion = 0x00;
};
Loading