|
| 1 | +/* |
| 2 | +***************************************************************************** |
| 3 | +@file Deneyap_ToprakNemiOlcer.cpp |
| 4 | +@mainpage Deneyap Soil Moisture Sensor based ICM7555CBA Library |
| 5 | + Source File |
| 6 | +@maintainer RFtek Electronics <[email protected]> |
| 7 | +@version v1.0.0 |
| 8 | +@date June 21, 2022 |
| 9 | +@brief Includes functions to control Deneyap Soil Moisture Sensor |
| 10 | + based ICM7555CBA Arduino library |
| 11 | +
|
| 12 | +Library includes: |
| 13 | +--> Configuration functions |
| 14 | +--> Data manipulation functions |
| 15 | +--> I2C communication functions |
| 16 | +***************************************************************************** |
| 17 | +*/ |
| 18 | +#include "Deneyap_ToprakNemiOlcer.h" |
| 19 | + |
| 20 | +/* Device Status Functions ---------------------------------------------------*/ |
| 21 | + |
| 22 | +/** |
| 23 | + * @brief Configures I2C connection with user defined address and port |
| 24 | + * @param address: I2C address of the device, port: I2C port number (0 or 1) |
| 25 | + * @retval connection status (1 --> connected, 0 --> not connected) |
| 26 | + */ |
| 27 | +bool SoilMoisture::begin(uint8_t address, TwoWire &port) { |
| 28 | + Wire.begin(); |
| 29 | +#if defined(ARDUINO_DYM) |
| 30 | + Wire.setClock(100000); |
| 31 | +#else |
| 32 | + Wire.setClock(50000); |
| 33 | +#endif |
| 34 | + _i2cAddress = address; |
| 35 | + _i2cPort = &port; |
| 36 | + _dataPacket = {0}; |
| 37 | + |
| 38 | + return isConnected(); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * @brief Checks whether I2C connection established |
| 43 | + * @param None |
| 44 | + * @retval connection status (1 --> connected, 0 --> not connected) |
| 45 | + */ |
| 46 | +bool SoilMoisture::isConnected() { |
| 47 | + _i2cPort->beginTransmission(_i2cAddress); |
| 48 | + |
| 49 | + if (_i2cPort->endTransmission() == 0) |
| 50 | + return true; |
| 51 | + return false; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @brief Requests firmware version of the device |
| 56 | + * @param None |
| 57 | + * @retval Device firmware version |
| 58 | + */ |
| 59 | +uint16_t SoilMoisture::getFwVersion() { |
| 60 | + _dataPacket.command = SOIL_MOISTURE_REQUEST_FW_VERSION; |
| 61 | + _dataPacket.dataSize = 0; |
| 62 | + return I2C_ReadFirmwareData16bit(&_dataPacket); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * @brief Sets (changes) I2C address of the device |
| 67 | + * @param address: I2C address to be set |
| 68 | + * @retval Transmission status (1 --> No error, Otherwise --> Transmission error) |
| 69 | + */ |
| 70 | + |
| 71 | +bool SoilMoisture::setI2Caddress(uint8_t newAddress) { |
| 72 | + _dataPacket.command = SOIL_MOISTURE_CHANGE_ADDR; |
| 73 | + _dataPacket.dataSize = 1; |
| 74 | + _dataPacket.data[0] = newAddress; |
| 75 | + |
| 76 | + bool status = I2C_SendDataPacket(&_dataPacket); |
| 77 | + |
| 78 | + if (status == true) { |
| 79 | + _i2cAddress = newAddress; |
| 80 | + return true; |
| 81 | + } |
| 82 | + else { |
| 83 | + return false; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/* I2C Data Manipulation Functions -------------------------------------------*/ |
| 88 | + |
| 89 | +/** |
| 90 | + * @brief Reads the SOIL_MOISTURE pin |
| 91 | + * @param None |
| 92 | + * @retval uint16_t : SOIL_MOISTURE pin value |
| 93 | + */ |
| 94 | +uint16_t SoilMoisture::ReadSoilMoisture() { |
| 95 | + _dataPacket.command = (uint8_t)READ_SOIL_MOISTURE; |
| 96 | + _dataPacket.dataSize = 0; |
| 97 | + return I2C_ReadData16bit(&_dataPacket); |
| 98 | +} |
| 99 | + |
| 100 | +/* I2C Data Transaction Funstions --------------------------------------------*/ |
| 101 | + |
| 102 | +/** |
| 103 | + * @brief Reads 8bit data from I2C interface |
| 104 | + * @param dataPacket: includes protocol to request data |
| 105 | + * @retval I2C 8bit data |
| 106 | + */ |
| 107 | +uint8_t SoilMoisture::I2C_ReadData8bit(SoilMoisture_DataPacket_TypeDef *dataPacket) { |
| 108 | + _i2cPort->beginTransmission(_i2cAddress); |
| 109 | + _i2cPort->write(dataPacket->command); |
| 110 | + _i2cPort->endTransmission(); |
| 111 | + |
| 112 | + if (_i2cPort->requestFrom(_i2cAddress, static_cast<uint8_t>(1)) != 0) |
| 113 | + return _i2cPort->read(); |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief Reads 16bit data from I2C interface |
| 119 | + * @param dataPacket: includes protocol to request data |
| 120 | + * @retval I2C 16bit data |
| 121 | + */ |
| 122 | +uint16_t SoilMoisture::I2C_ReadData16bit(SoilMoisture_DataPacket_TypeDef *dataPacket) { |
| 123 | + _i2cPort->beginTransmission(_i2cAddress); |
| 124 | + _i2cPort->write(dataPacket->command); |
| 125 | + _i2cPort->endTransmission(); |
| 126 | + |
| 127 | + if (_i2cPort->requestFrom(_i2cAddress, static_cast<uint8_t>(2)) != 0) { |
| 128 | + uint16_t i2cData = _i2cPort->read(); |
| 129 | + i2cData |= (_i2cPort->read() << 8); |
| 130 | + return i2cData; |
| 131 | + } |
| 132 | + return 0; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * @brief Reads 16bit data from I2C interface |
| 137 | + * @param dataPacket: includes protocol to request data |
| 138 | + * @retval I2C 16bit data |
| 139 | + */ |
| 140 | +uint16_t SoilMoisture::I2C_ReadFirmwareData16bit(SoilMoisture_DataPacket_TypeDef *dataPacket) { |
| 141 | + _i2cPort->beginTransmission(_i2cAddress); |
| 142 | + _i2cPort->write(dataPacket->command); |
| 143 | + _i2cPort->endTransmission(); |
| 144 | + |
| 145 | + if (_i2cPort->requestFrom(_i2cAddress, static_cast<uint8_t>(2)) != 0) { |
| 146 | + i2cData2 = _i2cPort->read(); |
| 147 | + i2cData1 = _i2cPort->read(); |
| 148 | + Serial.print("v"); Serial.print(i2cData1); Serial.print("."); Serial.print(i2cData2); |
| 149 | + } |
| 150 | + return 0; |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * @brief Sends data packet based on a custom defined protocol |
| 155 | + * @param dataPacket: includes all related data |
| 156 | + * @retval Transmission status (1 --> No error, Otherwise --> Transmission error) |
| 157 | + */ |
| 158 | +bool SoilMoisture::I2C_SendDataPacket(SoilMoisture_DataPacket_TypeDef *dataPacket) { |
| 159 | + _i2cPort->beginTransmission(_i2cAddress); |
| 160 | + _i2cPort->write(dataPacket->command); |
| 161 | + _i2cPort->write(dataPacket->dataSize); |
| 162 | + |
| 163 | + for (uint8_t i = 0; i < _dataPacket.dataSize; i++) |
| 164 | + _i2cPort->write(_dataPacket.data[i]); |
| 165 | + |
| 166 | + if (_i2cPort->endTransmission() == 0) |
| 167 | + return true; |
| 168 | + return false; |
| 169 | +} |
0 commit comments