Skip to content

Commit

Permalink
Replaced integer power change condition with enum (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Sep 19, 2021
1 parent 8299aeb commit 399123a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
41 changes: 25 additions & 16 deletions src/main/java/com/dalsemi/onewire/adapter/DSPortAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@
* <LI> 1-Wire Speed and Power Selection
* <UL>
* <LI> {@link #setPowerDuration(int) setPowerDuration}
* <LI> {@link #startPowerDelivery(int) startPowerDelivery}
* <LI> {@link #startPowerDelivery(PowerChangeCondition)}
* <LI> {@link #setProgramPulseDuration(int) setProgramPulseDuration}
* <LI> {@link #startProgramPulse(int) startProgramPulse}
* <LI> {@link #startProgramPulse(PowerChangeCondition)}
* <LI> {@link #startBreak() startBreak}
* <LI> {@link #setPowerNormal() setPowerNormal}
* <LI> {@link #setSpeed(Speed)} setSpeed}
Expand Down Expand Up @@ -239,14 +239,21 @@ public enum ResetResult {
}
}

/** Condition for power state change, immediate */
public static final int CONDITION_NOW = 0;
/**
* Condition for power change.
*/
public enum PowerChangeCondition {

/** Condition for power state change, after next bit communication */
public static final int CONDITION_AFTER_BIT = 1;
NOW(0),
AFTER_NEXT_BIT(1),
AFTER_NEXT_BYTE(2);

public final int code;

/** Condition for power state change, after next byte communication */
public static final int CONDITION_AFTER_BYTE = 2;
PowerChangeCondition(int code) {
this.code = code;
}
}

/** Duration used in delivering power to the 1-Wire, 1/2 second */
public static final int DELIVERY_HALF_SECOND = 0;
Expand Down Expand Up @@ -1332,7 +1339,7 @@ public synchronized void excludeFamily(byte family[]) {
* @throws OneWireException on a setup error with the 1-Wire adapter
*/
public void setPowerDuration(int timeFactor) throws OneWireException {

// VT: FIXME: Replace with UnsupportedOperationException?
throw new OneWireException("Power delivery not supported by this adapter type");
}

Expand All @@ -1358,8 +1365,8 @@ public void setPowerDuration(int timeFactor) throws OneWireException {
* @throws OneWireIOException on a 1-Wire communication error
* @throws OneWireException on a setup error with the 1-Wire adapter
*/
public boolean startPowerDelivery(int changeCondition) throws OneWireException {

public boolean startPowerDelivery(PowerChangeCondition changeCondition) throws OneWireException {
// VT: FIXME: Replace with UnsupportedOperationException?
throw new OneWireException("Power delivery not supported by this adapter type");
}

Expand All @@ -1382,7 +1389,7 @@ public boolean startPowerDelivery(int changeCondition) throws OneWireException {
* @throws OneWireException on a setup error with the 1-Wire adapter
*/
public void setProgramPulseDuration(int timeFactor) throws OneWireException {

// VT: FIXME: Replace with UnsupportedOperationException?
throw new OneWireException("Program pulse delivery not supported by this adapter type");
}

Expand All @@ -1409,8 +1416,8 @@ public void setProgramPulseDuration(int timeFactor) throws OneWireException {
* @throws OneWireException on a setup error with the 1-Wire adapter or the
* adapter does not support this operation
*/
public boolean startProgramPulse(int changeCondition) throws OneWireException {

public boolean startProgramPulse(PowerChangeCondition changeCondition) throws OneWireException {
// VT: FIXME: Replace with UnsupportedOperationException?
throw new OneWireException("Program pulse delivery not supported by this adapter type");
}

Expand All @@ -1424,7 +1431,7 @@ public boolean startProgramPulse(int changeCondition) throws OneWireException {
* adapter does not support this operation
*/
public void startBreak() throws OneWireException {

// VT: FIXME: Replace with UnsupportedOperationException?
throw new OneWireException("Break delivery not supported by this adapter type");
}

Expand Down Expand Up @@ -1467,8 +1474,10 @@ public void setPowerNormal() throws OneWireException {
*/
public void setSpeed(Speed speed) throws OneWireException {

if (speed != Speed.REGULAR)
if (speed != Speed.REGULAR) {
// VT: FIXME: Replace with UnsupportedOperationException?
throw new OneWireException("Speed " + speed + " not supported by this adapter type");
}
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/dalsemi/onewire/adapter/USerialAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1605,20 +1605,20 @@ public void setPowerDuration(int timeFactor) throws OneWireIOException, OneWireE
* @throws OneWireIOException on a 1-Wire communication error
* @throws OneWireException on a setup error with the 1-Wire adapter
*/
public boolean startPowerDelivery(int changeCondition) throws OneWireIOException, OneWireException {
public boolean startPowerDelivery(PowerChangeCondition changeCondition) throws OneWireIOException, OneWireException {

try {

// acquire exclusive use of the port
beginLocalExclusive();

if (changeCondition == CONDITION_AFTER_BIT) {
if (changeCondition == PowerChangeCondition.AFTER_NEXT_BIT) {
owState.levelChangeOnNextBit = true;
owState.primedLevelValue = Level.POWER_DELIVERY;
} else if (changeCondition == CONDITION_AFTER_BYTE) {
} else if (changeCondition == PowerChangeCondition.AFTER_NEXT_BYTE) {
owState.levelChangeOnNextByte = true;
owState.primedLevelValue = Level.POWER_DELIVERY;
} else if (changeCondition == CONDITION_NOW) {
} else if (changeCondition == PowerChangeCondition.NOW) {

// make sure adapter is present
if (uAdapterPresent()) {
Expand Down Expand Up @@ -1711,14 +1711,14 @@ public void setProgramPulseDuration(int timeFactor) throws OneWireIOException, O
* @throws OneWireException on a setup error with the 1-Wire adapter or the
* adapter does not support this operation
*/
public boolean startProgramPulse(int changeCondition) throws OneWireIOException, OneWireException {
public boolean startProgramPulse(PowerChangeCondition changeCondition) throws OneWireIOException, OneWireException {

// check if adapter supports program
if (!uState.programVoltageAvailable)
throw new OneWireException("USerialAdapter: startProgramPulse, program voltage not available");

// check if correct change condition
if (changeCondition != CONDITION_NOW)
if (changeCondition != PowerChangeCondition.NOW)
throw new OneWireException("USerialAdapter: startProgramPulse, CONDITION_NOW only currently supported");

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public synchronized void copyScratchpad(byte[] es_data)

// provide strong pull-up for copy
ib.adapter.setPowerDuration(DSPortAdapter.DELIVERY_INFINITE);
ib.adapter.startPowerDelivery(DSPortAdapter.CONDITION_AFTER_BYTE);
ib.adapter.startPowerDelivery(DSPortAdapter.PowerChangeCondition.AFTER_NEXT_BYTE);
ib.adapter.putByte(send_block[3]);

// pause before checking result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ protected byte programByte(int addr, byte data, boolean writeContinue)
}

// send the pulse
ib.adapter.startProgramPulse(DSPortAdapter.CONDITION_NOW);
ib.adapter.startProgramPulse(DSPortAdapter.PowerChangeCondition.NOW);

// return the result
return ( byte ) ib.adapter.getByte();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public void doTemperatureConvert (byte[] state) throws OneWireException {

// Setup Power Delivery
adapter.setPowerDuration(adapter.DELIVERY_INFINITE);
adapter.startPowerDelivery(adapter.CONDITION_AFTER_BYTE);
adapter.startPowerDelivery(DSPortAdapter.PowerChangeCondition.AFTER_NEXT_BYTE);

// send the convert temperature command
adapter.putByte(CONVERT_TEMPERATURE_COMMAND);
Expand All @@ -370,6 +370,7 @@ public void doTemperatureConvert (byte[] state) throws OneWireException {
try {
Thread.sleep(750);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
logger.debug("Interrupted", ex);
}

Expand Down Expand Up @@ -828,7 +829,7 @@ private void copyScratchpad () throws OneWireException {

// Setup Power Delivery
adapter.setPowerDuration(adapter.DELIVERY_INFINITE);
adapter.startPowerDelivery(adapter.CONDITION_NOW);
adapter.startPowerDelivery(DSPortAdapter.PowerChangeCondition.NOW);

// delay for 10 ms
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public void doTemperatureConvert(byte[] state) throws OneWireIOException, OneWir

// Setup Power Delivery
adapter.setPowerDuration(adapter.DELIVERY_INFINITE);
adapter.startPowerDelivery(adapter.CONDITION_AFTER_BYTE);
adapter.startPowerDelivery(DSPortAdapter.PowerChangeCondition.AFTER_NEXT_BYTE);
// send the convert temperature command
adapter.putByte(CONVERT_TEMPERATURE_COMMAND);

Expand Down Expand Up @@ -813,7 +813,7 @@ public void copyScratchpad() throws OneWireIOException, OneWireException {

// apply the power delivery
adapter.setPowerDuration(adapter.DELIVERY_INFINITE);
adapter.startPowerDelivery(adapter.CONDITION_AFTER_BYTE);
adapter.startPowerDelivery(DSPortAdapter.PowerChangeCondition.AFTER_NEXT_BYTE);

// send the convert temperature command
adapter.putByte(COPY_SCRATCHPAD_COMMAND);
Expand All @@ -822,6 +822,7 @@ public void copyScratchpad() throws OneWireIOException, OneWireException {
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
logger.debug("Interrupted", ex);
}

Expand Down

0 comments on commit 399123a

Please sign in to comment.