Skip to content

Commit 68fb460

Browse files
committed
change error code on no-bus condition; formatter
1 parent 517a880 commit 68fb460

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

src/SparkFun_Soil_Moisture_Sensor.h

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,37 @@
99
*---------------------------------------------------------------------------------
1010
*/
1111

12-
// A simple wrapper around the SparkFun Soil Moisture Sensor - based on the original example driver /example from Zio
13-
// from here https://github.com/sparkfun/Zio-Qwiic-Soil-Moisture-Sensor
12+
// A simple wrapper around the SparkFun Soil Moisture Sensor - based on the original example driver /example from Zio
13+
// from here https://github.com/sparkfun/Zio-Qwiic-Soil-Moisture-Sensor
1414

1515
#pragma once
1616

1717
// To make the Arduino machine happy, include the toolkit header before the core implementation for this device
1818
#include <SparkFun_Toolkit.h>
1919
#include "sfeTk/sfeDevSoilMoisture.h"
2020

21+
2122
// Note:
2223
// The core of the implementation for this device library is in the SparkFun Toolkit object sfeDevSoilMoisture,
2324
// contained in the directory sfeTk. This object implements all functionality independent of the bus type, I2C or SPI.
24-
// The SparkFunSoilMoistureSensorI2C and SparkFunSoilMoistureSensorSPI classes below are the Arduino-specific, bus-specific
25-
// implementations that inherit from the core toolkit class.
25+
// The SparkFunSoilMoistureSensorI2C and SparkFunSoilMoistureSensorSPI classes below are the Arduino-specific,
26+
// bus-specific implementations that inherit from the core toolkit class.
2627
//
2728
//-----------------------------------------------------------------------------------------------
2829
// Define our Arduino Object - I2C version
2930
class SparkFunSoilMoistureSensorI2C : public sfeDevSoilMoisture
3031
{
3132
public:
32-
/// @brief Begins the Device
33-
/// @param address I2C device address to use for the sensor
34-
/// @param wirePort Wire port to use for I2C communication
35-
/// @return True if successful, false otherwise
33+
/**
34+
* @brief Begins the Device
35+
*
36+
* Initializes the soil moisture sensor for I2C communication using the specified
37+
* I2C address and wire port.
38+
*
39+
* @param address I2C device address to use for the sensor (default is SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS)
40+
* @param wirePort Wire port to use for I2C communication (default is Wire)
41+
* @return true if successful, false otherwise
42+
*/
3643
bool begin(const uint8_t address = SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS, TwoWire &wirePort = Wire)
3744
{
3845
// Setup Arduino I2C bus
@@ -42,8 +49,14 @@ class SparkFunSoilMoistureSensorI2C : public sfeDevSoilMoisture
4249
return sfeDevSoilMoisture::begin(&_theI2CBus) == kSTkErrOk ? isConnected() : false;
4350
}
4451

45-
/// @brief Checks if the Device is connected
46-
/// @return True if the sensor is connected, false otherwise
52+
/**
53+
* @brief Checks if the device is connected and responding
54+
*
55+
* Tests communication with the sensor by attempting to ping it over I2C.
56+
* A successful ping indicates the sensor is properly connected and responding.
57+
*
58+
* @return true if the sensor responds to communication attempts, false otherwise
59+
*/
4760
bool isConnected()
4861
{
4962
return _theI2CBus.ping() == kSTkErrOk;
@@ -58,19 +71,27 @@ class SparkFunSoilMoistureSensorI2C : public sfeDevSoilMoisture
5871
class SparkFunSoilMoistureSensorSPI : public sfeDevSoilMoisture
5972
{
6073
public:
61-
/// @brief Begins the Device with SPI as the communication bus
62-
/// @param csPin The chip select pin for the sensor
63-
/// @param spiPort The SPI port to use for communication
64-
/// @param spiSettings The SPI settings to use for communication
65-
/// @return True if successful, false otherwise
66-
bool begin(const uint8_t csPin, SPIClass &spiPort = SPI, SPISettings spiSettings = SPISettings(100000, MSBFIRST, SPI_MODE0))
74+
/**
75+
* @brief Begins the Device with SPI as the communication bus
76+
*
77+
* Initializes the soil moisture sensor for SPI communication using the specified
78+
* chip select pin, SPI port, and SPI settings.
79+
*
80+
* @param csPin The chip select pin for the sensor
81+
* @param spiPort The SPI port to use for communication (default is SPI)
82+
* @param spiSettings The SPI settings to use for communication (default is SPISettings(100000, MSBFIRST,
83+
* SPI_MODE0))
84+
* @return true if successful, false otherwise
85+
*/
86+
bool begin(const uint8_t csPin, SPIClass &spiPort = SPI,
87+
SPISettings spiSettings = SPISettings(100000, MSBFIRST, SPI_MODE0))
6788
{
6889

6990
// Setup Arduino SPI bus
7091
_theSPIBus.init(spiPort, spiSettings, csPin, true);
7192

7293
// Begin the sensor with the SPI bus connection
73-
return (sfeDevSoilMoisture::begin(&_theSPIBus) == kSTkErrOk );
94+
return (sfeDevSoilMoisture::begin(&_theSPIBus) == kSTkErrOk);
7495
}
7596

7697
private:

src/sfeTk/sfeDevSoilMoisture.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sfeTkError_t sfeDevSoilMoisture::begin(sfeTkIBus *theBus)
2727
{
2828
// Nullptr check
2929
if (theBus == nullptr)
30-
return kSTkErrFail;
30+
return kSTkErrBusNotInit;
3131

3232
// Set bus pointer
3333
_theBus = theBus;
@@ -40,7 +40,7 @@ sfeTkError_t sfeDevSoilMoisture::begin(sfeTkIBus *theBus)
4040
sfeTkError_t sfeDevSoilMoisture::LEDOff(void)
4141
{
4242
if (_theBus == nullptr)
43-
return kSTkErrFail;
43+
return kSTkErrBusNotInit;
4444

4545
// Send the command to turn the LED off
4646
return _theBus->writeByte(kCommandLEDOff);
@@ -50,7 +50,7 @@ sfeTkError_t sfeDevSoilMoisture::LEDOff(void)
5050
sfeTkError_t sfeDevSoilMoisture::LEDOn(void)
5151
{
5252
if (_theBus == nullptr)
53-
return kSTkErrFail;
53+
return kSTkErrBusNotInit;
5454

5555
// Send the command to turn the LED on
5656
return _theBus->writeByte(kCommandLEDOn);
@@ -91,7 +91,7 @@ float sfeDevSoilMoisture::readMoisturePercentage(void)
9191
sfeTkError_t sfeDevSoilMoisture::setI2CAddress(uint8_t newAddress)
9292
{
9393
if (_theBus == nullptr)
94-
return kSTkErrFail;
94+
return kSTkErrBusNotInit;
9595

9696
// Validate the new address
9797
if (newAddress < 0x07 || newAddress > 0x78)
@@ -101,9 +101,9 @@ sfeTkError_t sfeDevSoilMoisture::setI2CAddress(uint8_t newAddress)
101101
if (_theBus->type() == kBusTypeI2C && ((sfeTkII2C *)_theBus)->address() == newAddress)
102102
return kSTkErrOk;
103103

104-
// Send the command to change the address. NOTE: Because of how the sensor works,
104+
// Send the command to change the address. NOTE: Because of how the sensor works,
105105
// the following will return an error (since the sensor side resets the bus)
106-
(void) _theBus->writeRegisterByte(kCommandChangeAddress, newAddress);
106+
(void)_theBus->writeRegisterByte(kCommandChangeAddress, newAddress);
107107

108108
return kSTkErrOk;
109109
}
@@ -119,6 +119,6 @@ uint8_t sfeDevSoilMoisture::address(void)
119119
return ((sfeTkISPI *)_theBus)->cs();
120120
else if (_theBus->type() == kBusTypeI2C)
121121
return ((sfeTkII2C *)_theBus)->address();
122-
122+
123123
return 0;
124124
}

0 commit comments

Comments
 (0)