Skip to content

Commit 9c6f870

Browse files
committed
holy cow - this thing sensor supports SPI -- added SPI support
1 parent 533497e commit 9c6f870

File tree

7 files changed

+49
-11
lines changed

7 files changed

+49
-11
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ At the start of your sketch, the library header file is included using the follo
3838

3939
Before the arduino ```setup()``` function, create a Soil Sensor object in your file with the following declaration:
4040

41+
For I2C bus:
4142
```c++
42-
SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor class
43+
SparkFunSoilMoistureSensorI2C mySoilSensor; // Create an instance of the sensor class
4344
```
4445

46+
For SPI bus:
47+
```c++
48+
SparkFunSoilMoistureSensorSPI mySoilSensor; // Create an instance of the sensor class
49+
```
50+
51+
4552
### Initialization
4653

4754
In the Arduino ```setup()``` function, initialize the sensor by calling the begin method. This method is called after the Arduino `Wire` (I2C) library is initialized.

examples/Example_01_BasicReadings/Example_01_BasicReadings.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "SparkFun_Soil_Moisture_Sensor.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_Soil_Moisture_Sensor
1212
#include <Wire.h>
1313

14-
SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor class
14+
SparkFunSoilMoistureSensorI2C mySoilSensor; // Create an instance of the sensor class
1515

1616
// Some Dev boards have their QWIIC connector on Wire or Wire1
1717
// This #ifdef will help this sketch work across more products

examples/Example_02_ReadingsAndLED/Example_02_ReadingsAndLED.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "SparkFun_Soil_Moisture_Sensor.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_Soil_Moisture_Sensor
1212
#include <Wire.h>
1313

14-
SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor class
14+
SparkFunSoilMoistureSensorI2C mySoilSensor; // Create an instance of the sensor class
1515

1616
// Some Dev boards have their QWIIC connector on Wire or Wire1
1717
// This #ifdef will help this sketch work across more products

examples/Example_03_LEDFlashMoisture/Example_03_LEDFlashMoisture.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "SparkFun_Soil_Moisture_Sensor.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_Soil_Moisture_Sensor
1515
#include <Wire.h>
1616

17-
SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor class
17+
SparkFunSoilMoistureSensorI2C mySoilSensor; // Create an instance of the sensor class
1818

1919
// The plan:
2020
// The value of the sensor has the following range:

src/SparkFun_Soil_Moisture_Sensor.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818
#include <SparkFun_Toolkit.h>
1919
#include "sfeTk/sfeDevSoilMoisture.h"
2020

21-
// Define our Arduino Object
22-
class SparkFunSoilMoistureSensor : public sfeDevSoilMoisture
21+
// Note:
22+
// The core of the implementation for this device library is in the SparkFun Toolkit object sfeDevSoilMoisture,
23+
// 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.
26+
//
27+
//-----------------------------------------------------------------------------------------------
28+
// Define our Arduino Object - I2C version
29+
class SparkFunSoilMoistureSensorI2C : public sfeDevSoilMoisture
2330
{
2431
public:
2532
/// @brief Begins the Device
@@ -45,3 +52,27 @@ class SparkFunSoilMoistureSensor : public sfeDevSoilMoisture
4552
private:
4653
sfeTkArdI2C _theI2CBus;
4754
};
55+
56+
//-----------------------------------------------------------------------------------------------
57+
// Define our Arduino Object - SPI version
58+
class SparkFunSoilMoistureSensorSPI : public sfeDevSoilMoisture
59+
{
60+
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))
67+
{
68+
69+
// Setup Arduino SPI bus
70+
_theSPIBus.init(spiPort, spiSettings, csPin, true);
71+
72+
// Begin the sensor and make sure it's connected.
73+
return (sfeDevSoilMoisture::begin(&_theSPIBus) == kSTkErrOk );
74+
}
75+
76+
private:
77+
sfeTkArdSPI _theSPIBus;
78+
};

src/sfeTk/sfeDevSoilMoisture.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//---------------------------------------------------------------------
2222
// Core object implementation
2323
//---------------------------------------------------------------------
24-
sfeTkError_t sfeDevSoilMoisture::begin(sfeTkII2C *theBus)
24+
sfeTkError_t sfeDevSoilMoisture::begin(sfeTkIBus *theBus)
2525
{
2626
// Nullptr check
2727
if (theBus == nullptr)
@@ -30,7 +30,7 @@ sfeTkError_t sfeDevSoilMoisture::begin(sfeTkII2C *theBus)
3030
// Set bus pointer
3131
_theBus = theBus;
3232

33-
return _theBus->ping();
33+
return kSTkErrOk;
3434
}
3535

3636
//----------------------------------------------------------------------------------------

src/sfeTk/sfeDevSoilMoisture.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class sfeDevSoilMoisture
3333
/// @brief Start the driver/begin connecting/comms to the device
3434
/// @param theBus A pointer to an I2C toolkit object
3535
/// @return kSTkErrOk if successful, otherwise an error code
36-
sfeTkError_t begin(sfeTkII2C *theBus = nullptr);
36+
sfeTkError_t begin(sfeTkIBus *theBus = nullptr);
3737

3838
/// @brief Turns off the on-board LED
3939
/// @return kSTkErrOk if successful, otherwise an error code
@@ -61,7 +61,7 @@ class sfeDevSoilMoisture
6161
sfeTkError_t changeSensorAddress(uint8_t newAddress);
6262

6363
protected:
64-
// The I2C bus the sensor is connected to
65-
sfeTkII2C *_theBus;
64+
// The toolkit bus the sensor is connected to
65+
sfeTkIBus *_theBus;
6666

6767
}; // class sfeDevSoilMoisture

0 commit comments

Comments
 (0)