Skip to content

Commit 3b54790

Browse files
committed
Fix setDeviceAddress
Rev to v2.1.0 since logic changed. Moved set and get address functions to interface-specific class. Removed address-related things from the driver class.
1 parent 15601fd commit 3b54790

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun AS7331 Arduino Library
2-
version=2.0.0
2+
version=2.1.0
33
author=SparkFun Electronics
44
maintainer=SparkFun Electronics
55
sentence=An Arduino library to make use of the Qwiic and Qwiic Mini AS7331 Spectral UV Sensor

src/SparkFun_AS7331.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@ class SfeAS7331ArdI2C : public SfeAS7331Driver
4747

4848
setCommunicationBus(&_theI2CBus);
4949

50+
// If the address passed in isn't the default, set the new address.
51+
if (kDefaultAS7331Addr != address)
52+
{
53+
if(!setDeviceAddress(address))
54+
return false;
55+
}
56+
5057
if(!isConnected())
5158
return false;
5259

53-
return SfeAS7331Driver::begin(address);
60+
return SfeAS7331Driver::begin();
5461
}
5562

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

73+
/// @brief Sets the address that the bus uses to communicate with the sensor.
74+
/// @param deviceAddress Device address to use.
75+
/// @return True if a valid address is set.
76+
bool setDeviceAddress(const uint8_t &deviceAddress)
77+
{
78+
switch(deviceAddress)
79+
{
80+
// If it's any of the allowed addresses, set it.
81+
case kDefaultAS7331Addr:
82+
case kSecondaryAS7331Addr:
83+
case kTertiaryAS7331Addr:
84+
case kQuaternaryAS7331Addr:
85+
_theI2CBus.setAddress(deviceAddress);
86+
break;
87+
default: // If it's invalid, return false.
88+
return false;
89+
break;
90+
}
91+
return true;
92+
}
93+
94+
/// @brief Gets the currently configured device address.
95+
/// @return device address.
96+
uint8_t getDeviceAddress(void)
97+
{
98+
return _theI2CBus.address();
99+
}
100+
66101
private:
67102
sfeTkArdI2C _theI2CBus;
68103
};

src/sfeAS7331.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "sfeAS7331.h"
22

3-
bool SfeAS7331Driver::begin(const uint8_t &deviceAddress, sfeTkIBus *theBus)
3+
bool SfeAS7331Driver::begin(sfeTkIBus *theBus)
44
{
55
// Nullptr check.
66
if (!_theBus && !theBus)
@@ -10,10 +10,6 @@ bool SfeAS7331Driver::begin(const uint8_t &deviceAddress, sfeTkIBus *theBus)
1010
if (theBus != nullptr)
1111
setCommunicationBus(theBus);
1212

13-
// If the address passed in isn't the default, set the new address.
14-
if (kDefaultAS7331Addr != deviceAddress)
15-
setDeviceAddress(deviceAddress);
16-
1713
// Get the device setup and ready.
1814
return runDefaultSetup();
1915
}
@@ -65,27 +61,6 @@ void SfeAS7331Driver::setCommunicationBus(sfeTkIBus *theBus)
6561
_theBus = theBus;
6662
}
6763

68-
void SfeAS7331Driver::setDeviceAddress(const uint8_t &deviceAddress)
69-
{
70-
switch(deviceAddress)
71-
{
72-
// If it's any of the allowed addresses, set it.
73-
case kDefaultAS7331Addr:
74-
case kSecondaryAS7331Addr:
75-
case kTertiaryAS7331Addr:
76-
case kQuaternaryAS7331Addr:
77-
_devAddress = deviceAddress;
78-
break;
79-
default: // Default to doing nothing.
80-
break;
81-
}
82-
}
83-
84-
uint8_t SfeAS7331Driver::getDeviceAddress(void)
85-
{
86-
return _devAddress;
87-
}
88-
8964
bool SfeAS7331Driver::runDefaultSetup(const bool &runSoftReset)
9065
{
9166
// Nullptr check.

src/sfeAS7331.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ class SfeAS7331Driver
276276
public:
277277
// Default initialization values based on the datasheet. See SfeAS7331Driver::setDefaultSettings for
278278
// an explanation of the values.
279-
SfeAS7331Driver(uint8_t address = kDefaultAS7331Addr)
280-
: _devAddress{address}, _theBus{nullptr}, _breakTime{25}, _numEdges{1}, _readyPinMode{false},
279+
SfeAS7331Driver()
280+
: _theBus{nullptr}, _breakTime{25}, _numEdges{1}, _readyPinMode{false},
281281
_dividerEnabled{false}, _tempConvEnabled{true}, _indexMode{true}, _standbyState{false}, _startState{false},
282282
_powerDownEnableState{true}, _opMode{DEVICE_MODE_CFG}, _sensorGain{GAIN_2}, _cclk{CCLK_1_024_MHZ},
283283
_mmode{MEAS_MODE_CMD}, _conversionTime{TIME_64MS}, _dividerRange{DIV_2}, _uva{0.0f}, _uvb{0.0f}, _uvc{0.0f},
@@ -287,10 +287,9 @@ class SfeAS7331Driver
287287

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

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

303-
/// @brief Sets the address that the bus uses to communicate with the sensor.
304-
/// @param deviceAddress Device address to use.
305-
void setDeviceAddress(const uint8_t &deviceAddress);
306-
307-
/// @brief Gets the currently configured device address.
308-
/// @return device address.
309-
uint8_t getDeviceAddress(void);
310-
311302
/// @brief Helper class that sets up the sensor and state in the POR
312303
/// configuration.
313304
/// @param runSoftReset Flag that runs the soft reset function to reset the
@@ -660,7 +651,6 @@ class SfeAS7331Driver
660651
void setDefaultSettings(void);
661652

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

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

0 commit comments

Comments
 (0)