Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure conversion is complete; fix ads1015 scaling. #174

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 40 additions & 9 deletions diozero-core/src/main/java/com/diozero/devices/Ads1x15.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ public byte getMask() {
private DigitalInputDevice readyPin;
private float lastResult;

// for tracing
private boolean amTracing = false;

/**
*
* @param pgaConfig Programmable Gain Amplifier configuration - make sure this
Expand Down Expand Up @@ -353,6 +356,8 @@ private Ads1x15(int controller, Model model, Address address, PgaConfig pgaConfi
boardPinInfo = new Ads1x15BoardPinInfo(model, pgaConfig.getVoltage());
device = I2CDevice.builder(address.getValue()).setController(controller).setByteOrder(ByteOrder.BIG_ENDIAN)
.build();

amTracing = Logger.isTraceEnabled();
}

@Override
Expand Down Expand Up @@ -464,20 +469,48 @@ private void setDataRate(int dataRate, byte dataRateMask) {
this.dataRateMask = dataRateMask;
}

public void waitUntilReady() {
long t0 = 0;

if (amTracing) { // only want to hit currentTimeMillis() if necessary
Logger.trace("waitUntilReady hit");
t0 = System.currentTimeMillis();
}

SleepUtil.sleepMillis(dataRateSleepMillis);
int incrementalSleepMillis = Math.max(dataRateSleepMillis / 20, 1);

int i = 0;
while (true) {
short data = device.readShort(ADDR_POINTER_CONFIG);
if ((data & 0x8000) != 0) break;
SleepUtil.sleepMillis(incrementalSleepMillis);
i++;
}

if (amTracing) {
long t1 = System.currentTimeMillis();
Logger.trace("waitUntilReady done, took {} ms, {} iterations", t1 - t0, i);
}
}

public float getValue(int adcNumber) {
Logger.debug("Reading channel {}, mode={}", Integer.valueOf(adcNumber), mode);
Logger.debug("Reading channel {}, mode={}", adcNumber, mode);

// TODO Protect against concurrent reads

if (mode == Mode.SINGLE) {
setConfig(adcNumber);

SleepUtil.sleepMillis(dataRateSleepMillis);
waitUntilReady();
} else if (readyPin != null) {
return lastResult;
}

return RangeUtil.map(readConversionData(adcNumber), 0, Short.MAX_VALUE, 0, 1f);
short conversionData = readConversionData(adcNumber);
// need to set constrain to false: we *could* get a negative if doing differential
float rv = RangeUtil.map(conversionData, 0, 0x8000, 0, 1f, false);
Logger.trace ("mapped {} to {}", conversionData, rv);
return rv;
}

protected void setConfig(int adcNumber) {
Expand All @@ -486,18 +519,16 @@ protected void setConfig(int adcNumber) {
byte config_lsb = (byte) (dataRateMask | comparatorMode.getMask() | comparatorPolarity.getMask()
| (latchingComparator ? CONFIG_LSB_COMP_LATCHING : 0) | comparatorQueue.getMask());
device.writeI2CBlockData(ADDR_POINTER_CONFIG, config_msb, config_lsb);
Logger.trace("msb: 0x{}, lsb: 0x{}", Integer.toHexString(config_msb & 0xff),
Integer.toHexString(config_lsb & 0xff));
Logger.trace("setConfig: 0x{} 0x{}", Integer.toHexString(config_msb & 0xff),
Integer.toHexString(config_lsb & 0xff));
}

private short readConversionData(int adcNumber) {
// byte[] data = device.readI2CBlockDataByteArray(ADDR_POINTER_CONV, 2);
// short value = (short) ((data[0] & 0xff) << 8 | (data[1] & 0xff));
short value = device.readShort(ADDR_POINTER_CONV);

if (model == Model.ADS1015) {
value >>= 4;
}
Logger.trace("readConversionData: 0x{}, {} dec", Integer.toHexString(value), value);

return value;
}
Expand Down