Skip to content

Commit

Permalink
Merge pull request #1 from Lutemi/feature/multiple-i2c
Browse files Browse the repository at this point in the history
Add support for multiple I2C buses
  • Loading branch information
vzahradnik committed Feb 2, 2023
2 parents 9377f6e + 647b2ce commit de99c27
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
51 changes: 22 additions & 29 deletions src/MCP342x.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "Wire.h"
#include "Arduino.h"
#include "MCP342x.h"

Expand Down Expand Up @@ -27,23 +26,23 @@ const MCP342x::Gain MCP342x::gain8 = Gain(0x03);

uint8_t MCP342x::generalCallReset(void)
{
Wire.beginTransmission(0x00);
Wire.write(0x06);
return Wire.endTransmission();
wire->beginTransmission(0x00);
wire->write(0x06);
return wire->endTransmission();
}

uint8_t MCP342x::generalCallLatch(void)
{
Wire.beginTransmission(0x00);
Wire.write(0x04);
return Wire.endTransmission();
wire->beginTransmission(0x00);
wire->write(0x04);
return wire->endTransmission();
}

uint8_t MCP342x::generalCallConversion(void)
{
Wire.beginTransmission(0x00);
Wire.write(0x08);
return Wire.endTransmission();
wire->beginTransmission(0x00);
wire->write(0x08);
return wire->endTransmission();
}

void MCP342x::normalise(long &result, Config config)
Expand All @@ -62,23 +61,17 @@ void MCP342x::normalise(long &result, Config config)
*/
result <<= (21 - int(config.getResolution()) - config.getGain().log2());
}


MCP342x::MCP342x(void) : address(0x68)
{
;
}

MCP342x::MCP342x(uint8_t add) : address(add)

MCP342x::MCP342x(uint8_t address, WireType wireImpl) : address(address), wire(wireImpl)
{
;
}

bool MCP342x::autoprobe(const uint8_t *addressList, uint8_t len)
{
for (uint8_t i = 0; i < len; ++i) {
Wire.requestFrom(addressList[i], (uint8_t)1);
if (Wire.available()) {
wire->requestFrom(addressList[i], (uint8_t)1);
if (wire->available()) {
address = addressList[i];
return true;
}
Expand All @@ -96,19 +89,19 @@ MCP342x::error_t MCP342x::convert(Channel channel, Mode mode, Resolution resolut

MCP342x::error_t MCP342x::configure(const Config &config) const
{
Wire.beginTransmission(address);
Wire.write(config.val);
if (Wire.endTransmission())
wire->beginTransmission(address);
wire->write(config.val);
if (wire->endTransmission())
return errorConfigureFailed;
else
return errorNone;
}

MCP342x::error_t MCP342x::convert(const Config &config) const
{
Wire.beginTransmission(address);
Wire.write(config.val | newConversionMask);
if (Wire.endTransmission())
wire->beginTransmission(address);
wire->write(config.val | newConversionMask);
if (wire->endTransmission())
return errorConvertFailed;
else
return errorNone;
Expand All @@ -121,12 +114,12 @@ MCP342x::error_t MCP342x::read(long &result, Config& status) const
// most appropriate configuration value (ready may have changed).
const uint8_t len = 4;
uint8_t buffer[len] = {};
Wire.requestFrom(address, len);
if (Wire.available() != len)
wire->requestFrom(address, len);
if (wire->available() != len)
return errorReadFailed;

for (uint8_t i = 0; i < len; ++i)
buffer[i] = Wire.read();
buffer[i] = wire->read();

uint8_t dataBytes;
if ((buffer[3] & 0x0c) == 0x0c) {
Expand Down
13 changes: 8 additions & 5 deletions src/MCP342x.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef MCP342X_h
#define MCP342X_h

#include "Wire.h"
typedef TwoWire* WireType;

#define MCP342X_VERSION "1.0.4"

class MCP342x {
Expand Down Expand Up @@ -48,15 +51,14 @@ class MCP342x {
errorConfigureFailed,
};

static uint8_t generalCallReset(void);
static uint8_t generalCallLatch(void);
static uint8_t generalCallConversion(void);
uint8_t generalCallReset(void);
uint8_t generalCallLatch(void);
uint8_t generalCallConversion(void);

// Adjust result to account for gain and resolution settings
static void normalise(long &result, Config config);

MCP342x(void);
MCP342x(uint8_t address);
MCP342x(uint8_t address = 0x68, WireType wireImpl = &Wire);

bool autoprobe(const uint8_t *addressList, uint8_t len);

Expand Down Expand Up @@ -113,6 +115,7 @@ class MCP342x {

private:
uint8_t address;
WireType wire;
// For easy readout need to know whether 18 bit mode was selected
// uint8_t config;
};
Expand Down

0 comments on commit de99c27

Please sign in to comment.