|
1 | 1 | /** |
2 | | - * @file sfeDevSoilMoisture.cpp |
| 2 | + * @file sfDevSoilMoisture.cpp |
3 | 3 | * @brief Implementation file for the soil moisture sensor class |
4 | 4 | * |
5 | 5 | * This file contains the implementation of the soil moisture sensor class, including |
|
9 | 9 | * @author SparkFun Electronics |
10 | 10 | * @date 2025 |
11 | 11 | * @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License. |
12 | | - * |
| 12 | + * |
13 | 13 | * SPDX-License-Identifier: MIT |
14 | 14 | */ |
15 | 15 |
|
16 | | - |
17 | | -#include "sfeDevSoilMoisture.h" |
| 16 | +#include "sfDevSoilMoisture.h" |
18 | 17 |
|
19 | 18 | // Impl for the core driver |
20 | 19 |
|
21 | 20 | // Define the communication commands for the soil moisture sensor (from the original zio firmware) |
22 | 21 | /** |
23 | 22 | * @brief Command to turn off the on-board LED |
24 | 23 | */ |
25 | | -#define kCommandLEDOff 0x00 |
| 24 | +const uint8_t kCommandLEDOff = 0x00; |
26 | 25 |
|
27 | 26 | /** |
28 | 27 | * @brief Command to turn on the on-board LED |
29 | 28 | */ |
30 | | -#define kCommandLEDOn 0x01 |
| 29 | +const uint8_t kCommandLEDOn = 0x01; |
31 | 30 |
|
32 | 31 | /** |
33 | 32 | * @brief Command to change the I2C address of the sensor |
34 | 33 | */ |
35 | | -#define kCommandChangeAddress 0x03 |
| 34 | +const uint8_t kCommandChangeAddress = 0x03; |
36 | 35 |
|
37 | 36 | /** |
38 | 37 | * @brief Command to get the moisture value from the sensor |
39 | 38 | */ |
40 | | -#define kCommandGetValue 0x05 |
| 39 | +const uint8_t kCommandGetValue = 0x05; |
41 | 40 |
|
42 | 41 | /** |
43 | 42 | * @brief Command indicating no new data is available |
44 | 43 | */ |
45 | | -#define kCommandNothingNew 0x99 |
| 44 | +const uint8_t kCommandNothingNew = 0x99; |
46 | 45 |
|
47 | 46 | //--------------------------------------------------------------------- |
48 | 47 | // Core object implementation |
49 | 48 | //--------------------------------------------------------------------- |
50 | 49 | // start up the sensor |
51 | | -sfeTkError_t sfeDevSoilMoisture::begin(sfeTkIBus *theBus) |
| 50 | +sfTkError_t sfDevSoilMoisture::begin(sfTkIBus *theBus) |
52 | 51 | { |
53 | 52 | // Nullptr check |
54 | 53 | if (theBus == nullptr) |
55 | | - return kSTkErrBusNotInit; |
| 54 | + return ksfTkErrBusNotInit; |
56 | 55 |
|
57 | 56 | // Set bus pointer |
58 | 57 | _theBus = theBus; |
59 | 58 |
|
60 | | - return kSTkErrOk; |
| 59 | + return ksfTkErrOk; |
61 | 60 | } |
62 | 61 |
|
63 | 62 | //---------------------------------------------------------------------------------------- |
64 | 63 | // LED off command |
65 | | -sfeTkError_t sfeDevSoilMoisture::LEDOff(void) |
| 64 | +sfTkError_t sfDevSoilMoisture::LEDOff(void) |
66 | 65 | { |
67 | 66 | if (_theBus == nullptr) |
68 | | - return kSTkErrBusNotInit; |
| 67 | + return ksfTkErrBusNotInit; |
69 | 68 |
|
70 | 69 | // Send the command to turn the LED off |
71 | | - return _theBus->writeByte(kCommandLEDOff); |
| 70 | + return _theBus->writeData(kCommandLEDOff); |
72 | 71 | } |
73 | 72 | //---------------------------------------------------------------------------------------- |
74 | 73 | // LED on command |
75 | | -sfeTkError_t sfeDevSoilMoisture::LEDOn(void) |
| 74 | +sfTkError_t sfDevSoilMoisture::LEDOn(void) |
76 | 75 | { |
77 | 76 | if (_theBus == nullptr) |
78 | | - return kSTkErrBusNotInit; |
| 77 | + return ksfTkErrBusNotInit; |
79 | 78 |
|
80 | 79 | // Send the command to turn the LED on |
81 | | - return _theBus->writeByte(kCommandLEDOn); |
| 80 | + return _theBus->writeData(kCommandLEDOn); |
82 | 81 | } |
83 | 82 |
|
84 | 83 | //---------------------------------------------------------------------------------------- |
85 | 84 | // Read the moisture value from the sensor - returns a resistance reading between 0 and 1023 |
86 | | -uint16_t sfeDevSoilMoisture::readMoistureValue(void) |
| 85 | +uint16_t sfDevSoilMoisture::readMoistureValue(void) |
87 | 86 | { |
88 | 87 | if (_theBus == nullptr) |
89 | 88 | return 0; |
90 | 89 |
|
91 | 90 | uint16_t value = 0; |
92 | | - if (_theBus->readRegisterWord(kCommandGetValue, value) != kSTkErrOk) |
| 91 | + if (_theBus->readRegister(kCommandGetValue, value) != ksfTkErrOk) |
93 | 92 | return 0; |
94 | 93 |
|
95 | 94 | return value; |
96 | 95 | } |
97 | 96 |
|
98 | 97 | //---------------------------------------------------------------------------------------- |
99 | 98 | // Returns the moisture ratio from the sensor (0 - 1.0) |
100 | | -float sfeDevSoilMoisture::readMoistureRatio(void) |
| 99 | +float sfDevSoilMoisture::readMoistureRatio(void) |
101 | 100 | { |
102 | 101 | if (_theBus == nullptr) |
103 | 102 | return 0.0; |
104 | 103 |
|
105 | | - return (((float)SFE_SOIL_MOISTURE_MAX_VALUE - (float)readMoistureValue()) / (float)SFE_SOIL_MOISTURE_MAX_VALUE); |
| 104 | + return (((float)SF_SOIL_MOISTURE_MAX_VALUE - (float)readMoistureValue()) / (float)SF_SOIL_MOISTURE_MAX_VALUE); |
106 | 105 | } |
107 | 106 |
|
108 | 107 | //---------------------------------------------------------------------------------------- |
109 | 108 | // Returns the moisture percentage from the sensor (0 - 100%) |
110 | | -float sfeDevSoilMoisture::readMoisturePercentage(void) |
| 109 | +float sfDevSoilMoisture::readMoisturePercentage(void) |
111 | 110 | { |
112 | 111 | return readMoistureRatio() * 100.0; |
113 | 112 | } |
114 | 113 | //---------------------------------------------------------------------------------------- |
115 | 114 | // Change the I2C address of the sensor |
116 | | -sfeTkError_t sfeDevSoilMoisture::setI2CAddress(uint8_t newAddress) |
| 115 | +sfTkError_t sfDevSoilMoisture::setI2CAddress(uint8_t newAddress) |
117 | 116 | { |
118 | 117 | if (_theBus == nullptr) |
119 | | - return kSTkErrBusNotInit; |
| 118 | + return ksfTkErrBusNotInit; |
120 | 119 |
|
121 | 120 | // Validate the new address |
122 | 121 | if (newAddress < 0x07 || newAddress > 0x78) |
123 | | - return kSTkErrFail; |
| 122 | + return ksfTkErrFail; |
124 | 123 |
|
125 | 124 | // If in I2C mode, is the address the same as the current address? |
126 | | - if (_theBus->type() == kBusTypeI2C && ((sfeTkII2C *)_theBus)->address() == newAddress) |
127 | | - return kSTkErrOk; |
| 125 | + if (_theBus->type() == ksfTkBusTypeI2C && ((sfTkII2C *)_theBus)->address() == newAddress) |
| 126 | + return ksfTkErrOk; |
128 | 127 |
|
129 | 128 | // Send the command to change the address. NOTE: Because of how the sensor works, |
130 | 129 | // the following will return an error (since the sensor side resets the bus) |
131 | | - (void)_theBus->writeRegisterByte(kCommandChangeAddress, newAddress); |
| 130 | + (void)_theBus->writeRegister(kCommandChangeAddress, newAddress); |
132 | 131 |
|
133 | | - return kSTkErrOk; |
| 132 | + return ksfTkErrOk; |
134 | 133 | } |
135 | 134 | //---------------------------------------------------------------------------------------- |
136 | 135 | // Return the address of the sensor bus. For I2C this is the address of the sensor, for |
137 | 136 | // SPI this is the CS pin |
138 | | -uint8_t sfeDevSoilMoisture::address(void) |
| 137 | +uint8_t sfDevSoilMoisture::address(void) |
139 | 138 | { |
140 | 139 | if (_theBus == nullptr) |
141 | 140 | return 0; |
142 | 141 |
|
143 | | - if (_theBus->type() == kBusTypeSPI) |
144 | | - return ((sfeTkISPI *)_theBus)->cs(); |
145 | | - else if (_theBus->type() == kBusTypeI2C) |
146 | | - return ((sfeTkII2C *)_theBus)->address(); |
| 142 | + if (_theBus->type() == ksfTkBusTypeSPI) |
| 143 | + return ((sfTkISPI *)_theBus)->cs(); |
| 144 | + else if (_theBus->type() == ksfTkBusTypeI2C) |
| 145 | + return ((sfTkII2C *)_theBus)->address(); |
147 | 146 |
|
148 | 147 | return 0; |
149 | 148 | } |
0 commit comments