Fixed formatting
This commit is contained in:
@@ -3,78 +3,78 @@
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
I2C::I2C()
|
||||
I2C::I2C()
|
||||
{
|
||||
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
I2C::~I2C()
|
||||
{
|
||||
Wire.end();
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
const bool I2C::Read(const uint8_t deviceAddr, const uint8_t deviceReg, const uint8_t len, std::vector<uint8_t> &data)
|
||||
{
|
||||
busy.try_lock();
|
||||
Wire.beginTransmission(deviceAddr);
|
||||
Wire.write(deviceReg);
|
||||
switch (Wire.endTransmission(true))
|
||||
{
|
||||
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
|
||||
isInitialized = true;
|
||||
case 0:
|
||||
break; // no error, break switch
|
||||
case 1:
|
||||
log_e("Data to long to fit in buffer: [%d]", len);
|
||||
case 2:
|
||||
log_e("Received NAK on address transmit");
|
||||
case 3:
|
||||
log_e("Received NAK on data transmit");
|
||||
case 4:
|
||||
log_e("Unknown Error");
|
||||
return false;
|
||||
}
|
||||
const uint8_t nBytes = Wire.requestFrom(deviceAddr, len);
|
||||
if (nBytes < len)
|
||||
{
|
||||
log_w("Received data is less than expected: len[%d], nBytes[%d]", len, nBytes);
|
||||
}
|
||||
data.clear();
|
||||
data.resize(nBytes); // resize out buffer to received data len, no check if data len is correct
|
||||
for (auto i = 0; i < nBytes; i++)
|
||||
{
|
||||
data[i] = static_cast<uint8_t>(Wire.read());
|
||||
}
|
||||
busy.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool I2C::Write(const uint8_t deviceAddr, const uint8_t deviceReg, const std::vector<uint8_t> &data)
|
||||
{
|
||||
busy.lock();
|
||||
Wire.beginTransmission(deviceAddr);
|
||||
Wire.write(deviceReg);
|
||||
for (auto d : data)
|
||||
{
|
||||
Wire.write(d);
|
||||
}
|
||||
|
||||
I2C::~I2C()
|
||||
switch (Wire.endTransmission(true))
|
||||
{
|
||||
Wire.end();
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
const bool I2C::Read(const uint8_t deviceAddr, const uint8_t deviceReg, const uint8_t len, std::vector<uint8_t> &data)
|
||||
{
|
||||
busy.try_lock();
|
||||
Wire.beginTransmission(deviceAddr);
|
||||
Wire.write(deviceReg);
|
||||
switch (Wire.endTransmission(true))
|
||||
{
|
||||
case 0:
|
||||
break; // no error, break switch
|
||||
case 1:
|
||||
log_e("Data to long to fit in buffer: [%d]", len);
|
||||
case 2:
|
||||
log_e("Received NAK on address transmit");
|
||||
case 3:
|
||||
log_e("Received NAK on data transmit");
|
||||
case 4:
|
||||
log_e("Unknown Error");
|
||||
return false;
|
||||
}
|
||||
const uint8_t nBytes = Wire.requestFrom(deviceAddr, len);
|
||||
if (nBytes < len)
|
||||
{
|
||||
log_w("Received data is less than expected: len[%d], nBytes[%d]", len, nBytes);
|
||||
}
|
||||
data.clear();
|
||||
data.resize(nBytes); // resize out buffer to received data len, no check if data len is correct
|
||||
for (auto i = 0; i < nBytes; i++)
|
||||
{
|
||||
data[i] = static_cast<uint8_t>(Wire.read());
|
||||
}
|
||||
busy.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool I2C::Write(const uint8_t deviceAddr, const uint8_t deviceReg, const std::vector<uint8_t> &data)
|
||||
{
|
||||
busy.lock();
|
||||
Wire.beginTransmission(deviceAddr);
|
||||
Wire.write(deviceReg);
|
||||
for (auto d : data)
|
||||
{
|
||||
Wire.write(d);
|
||||
}
|
||||
|
||||
switch (Wire.endTransmission(true))
|
||||
{
|
||||
case 0:
|
||||
break; // no error, break switch
|
||||
case 1:
|
||||
log_e("Data to long to fit in buffer: [%d]", data.size());
|
||||
case 2:
|
||||
log_e("Received NAK on address transmit");
|
||||
case 3:
|
||||
log_e("Received NAK on data transmit");
|
||||
case 4:
|
||||
log_e("Unknown Error");
|
||||
return false;
|
||||
}
|
||||
busy.unlock();
|
||||
return true;
|
||||
case 0:
|
||||
break; // no error, break switch
|
||||
case 1:
|
||||
log_e("Data to long to fit in buffer: [%d]", data.size());
|
||||
case 2:
|
||||
log_e("Received NAK on address transmit");
|
||||
case 3:
|
||||
log_e("Received NAK on data transmit");
|
||||
case 4:
|
||||
log_e("Unknown Error");
|
||||
return false;
|
||||
}
|
||||
busy.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace drivers
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
#pragma once
|
||||
#include <Wire.h>
|
||||
#include <Wire.h>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#define I2C_SCL_PIN 41
|
||||
#define I2C_SDA_PIN 42
|
||||
#define I2C_SCL_PIN 41
|
||||
#define I2C_SDA_PIN 42
|
||||
|
||||
namespace drivers {
|
||||
|
||||
class I2C {
|
||||
private:
|
||||
bool isInitialized = false;
|
||||
std::mutex busy;
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
public:
|
||||
I2C(void);
|
||||
~I2C(void);
|
||||
class I2C
|
||||
{
|
||||
private:
|
||||
bool isInitialized = false;
|
||||
std::mutex busy;
|
||||
|
||||
const bool Read(const uint8_t deviceAddr, const uint8_t deviceReg, const uint8_t len, std::vector<uint8_t> &data);
|
||||
const bool Write(const uint8_t deviceAddr, const uint8_t deviceReg, const std::vector<uint8_t> &data);
|
||||
public:
|
||||
I2C(void);
|
||||
~I2C(void);
|
||||
|
||||
const bool Read(const uint8_t deviceAddr, const uint8_t deviceReg, const uint8_t len, std::vector<uint8_t> &data);
|
||||
const bool Write(const uint8_t deviceAddr, const uint8_t deviceReg, const std::vector<uint8_t> &data);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user