Skip to content

Make setting device address actually do something. #3

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 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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 library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SparkFun AS7331 Arduino Library
version=2.0.0
version=2.1.0
author=SparkFun Electronics
maintainer=SparkFun Electronics
sentence=An Arduino library to make use of the Qwiic and Qwiic Mini AS7331 Spectral UV Sensor
Expand Down
37 changes: 36 additions & 1 deletion src/SparkFun_AS7331.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,17 @@ class SfeAS7331ArdI2C : public SfeAS7331Driver

setCommunicationBus(&_theI2CBus);

// If the address passed in isn't the default, set the new address.
if (kDefaultAS7331Addr != address)
{
if(!setDeviceAddress(address))
return false;
}

if(!isConnected())
return false;

return SfeAS7331Driver::begin(address);
return SfeAS7331Driver::begin();
}

/// @brief Checks to see if the AS7331 is connected.
Expand All @@ -63,6 +70,34 @@ class SfeAS7331ArdI2C : public SfeAS7331Driver
return (kDefaultAS7331DeviceID == getDeviceID());
}

/// @brief Sets the address that the bus uses to communicate with the sensor.
/// @param deviceAddress Device address to use.
/// @return True if a valid address is set.
bool setDeviceAddress(const uint8_t &deviceAddress)
{
switch(deviceAddress)
{
// If it's any of the allowed addresses, set it.
case kDefaultAS7331Addr:
case kSecondaryAS7331Addr:
case kTertiaryAS7331Addr:
case kQuaternaryAS7331Addr:
_theI2CBus.setAddress(deviceAddress);
break;
default: // If it's invalid, return false.
return false;
break;
}
return true;
}

/// @brief Gets the currently configured device address.
/// @return device address.
uint8_t getDeviceAddress(void)
{
return _theI2CBus.address();
}

private:
sfeTkArdI2C _theI2CBus;
};
27 changes: 1 addition & 26 deletions src/sfeAS7331.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sfeAS7331.h"

bool SfeAS7331Driver::begin(const uint8_t &deviceAddress, sfeTkIBus *theBus)
bool SfeAS7331Driver::begin(sfeTkIBus *theBus)
{
// Nullptr check.
if (!_theBus && !theBus)
Expand All @@ -10,10 +10,6 @@ bool SfeAS7331Driver::begin(const uint8_t &deviceAddress, sfeTkIBus *theBus)
if (theBus != nullptr)
setCommunicationBus(theBus);

// If the address passed in isn't the default, set the new address.
if (kDefaultAS7331Addr != deviceAddress)
setDeviceAddress(deviceAddress);

// Get the device setup and ready.
return runDefaultSetup();
}
Expand Down Expand Up @@ -65,27 +61,6 @@ void SfeAS7331Driver::setCommunicationBus(sfeTkIBus *theBus)
_theBus = theBus;
}

void SfeAS7331Driver::setDeviceAddress(const uint8_t &deviceAddress)
{
switch(deviceAddress)
{
// If it's any of the allowed addresses, set it.
case kDefaultAS7331Addr:
case kSecondaryAS7331Addr:
case kTertiaryAS7331Addr:
case kQuaternaryAS7331Addr:
_devAddress = deviceAddress;
break;
default: // Default to doing nothing.
break;
}
}

uint8_t SfeAS7331Driver::getDeviceAddress(void)
{
return _devAddress;
}

bool SfeAS7331Driver::runDefaultSetup(const bool &runSoftReset)
{
// Nullptr check.
Expand Down
16 changes: 3 additions & 13 deletions src/sfeAS7331.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ class SfeAS7331Driver
public:
// Default initialization values based on the datasheet. See SfeAS7331Driver::setDefaultSettings for
// an explanation of the values.
SfeAS7331Driver(uint8_t address = kDefaultAS7331Addr)
: _devAddress{address}, _theBus{nullptr}, _breakTime{25}, _numEdges{1}, _readyPinMode{false},
SfeAS7331Driver()
: _theBus{nullptr}, _breakTime{25}, _numEdges{1}, _readyPinMode{false},
_dividerEnabled{false}, _tempConvEnabled{true}, _indexMode{true}, _standbyState{false}, _startState{false},
_powerDownEnableState{true}, _opMode{DEVICE_MODE_CFG}, _sensorGain{GAIN_2}, _cclk{CCLK_1_024_MHZ},
_mmode{MEAS_MODE_CMD}, _conversionTime{TIME_64MS}, _dividerRange{DIV_2}, _uva{0.0f}, _uvb{0.0f}, _uvc{0.0f},
Expand All @@ -287,10 +287,9 @@ class SfeAS7331Driver

/// @brief This method is called to initialize the AS7331 device through the
/// specified bus.
/// @param deviceAddress I2C address for the device.
/// @param theBus Pointer to the bus object.
/// @return True if successful, false if it fails.
bool begin(const uint8_t &deviceAddress = kDefaultAS7331Addr, sfeTkIBus *theBus = nullptr);
bool begin(sfeTkIBus *theBus = nullptr);

/// @brief Requests the device ID from the sensor.
/// @return The device ID of the sensor.
Expand All @@ -300,14 +299,6 @@ class SfeAS7331Driver
/// @param theBus Bus to set as the communication devie.
void setCommunicationBus(sfeTkIBus *theBus);

/// @brief Sets the address that the bus uses to communicate with the sensor.
/// @param deviceAddress Device address to use.
void setDeviceAddress(const uint8_t &deviceAddress);

/// @brief Gets the currently configured device address.
/// @return device address.
uint8_t getDeviceAddress(void);

/// @brief Helper class that sets up the sensor and state in the POR
/// configuration.
/// @param runSoftReset Flag that runs the soft reset function to reset the
Expand Down Expand Up @@ -660,7 +651,6 @@ class SfeAS7331Driver
void setDefaultSettings(void);

sfeTkIBus *_theBus; // Pointer to bus device.
uint8_t _devAddress; // Device's I2C address.

uint8_t _breakTime; // Local config value. Value is in us/8. EX: _breakTime = 20 means 20*8 = 160us.
uint8_t _numEdges; // Local config value. Edges seen on SYN pin before ending conversion in SYND mode.
Expand Down