Skip to content

Commit

Permalink
Merge pull request #259 from topherbuckley/silentAssertionErrors
Browse files Browse the repository at this point in the history
Replaced assert statment with error check and AssertionError throw
  • Loading branch information
hannesa2 authored Jun 1, 2021
2 parents cc38d9f + ac982f6 commit 754e891
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
10 changes: 6 additions & 4 deletions IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,27 @@ public interface PwmOutput extends Closeable {
*
* @param dutyCycle The duty cycle, as a real value from 0.0 to 1.0.
* @throws ConnectionLostException The connection to the IOIO has been lost.
* @throws RuntimeException dutyCycle must take values from 0 to 1 (inclusive).
* @see #setPulseWidth(int)
*/
void setDutyCycle(float dutyCycle) throws ConnectionLostException;
void setDutyCycle(float dutyCycle) throws ConnectionLostException, RuntimeException;

/**
* Sets the pulse width of the PWM output. The pulse width is duration of
* the high-time within a single period of the signal. For relative control
* of the pulse with, consider using {@link #setDutyCycle(float)}.
*
* @param pulseWidthUs The pulse width, in microsecond units.
* @param pulseWidthUs The pulse width, in microsecond units. Must be larger than zero.
* @throws ConnectionLostException The connection to the IOIO has been lost.
* @throws RuntimeException pulseWidthUs must take values larger than zero.
* @see #setDutyCycle(float)
*/
void setPulseWidth(int pulseWidthUs) throws ConnectionLostException;
void setPulseWidth(int pulseWidthUs) throws ConnectionLostException, RuntimeException;

/**
* The same as {@link #setPulseWidth(int)}, but with sub-microsecond
* precision.
*/
void setPulseWidth(float pulseWidthUs)
throws ConnectionLostException;
throws ConnectionLostException, RuntimeException;
}
4 changes: 3 additions & 1 deletion IOIOLibCore/src/main/java/ioio/lib/impl/AnalogInputImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public float getReference() {
@Override
synchronized public void setValue(int value) {
// Log.v("AnalogInputImpl", "Pin " + pinNum_ + " value is " + value);
assert (value >= 0 && value < 1024);
if (value < 0 || value >= 1024) {
throw new RuntimeException("value must be between 0 (inclusive) and 1024 (exclusive). A value of " + value + " was given.");
}
value_ = value;
++sampleCount_;
bufferPush((short) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class DigitalInputImpl extends AbstractPin implements DigitalInput,
@Override
synchronized public void setValue(int value) {
// Log.v("DigitalInputImpl", "Pin " + pinNum_ + " value is " + value);
assert (value == 0 || value == 1);
if (value != 0 && value != 1) {
throw new RuntimeException("value must be 0 or 1. A value of " + value + " was given.");
}
value_ = (value == 1);
if (!valid_) {
valid_ = true;
Expand Down
14 changes: 9 additions & 5 deletions IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,24 @@ public synchronized void close() {
}

@Override
public void setDutyCycle(float dutyCycle) throws ConnectionLostException {
assert (dutyCycle <= 1 && dutyCycle >= 0);
public void setDutyCycle(float dutyCycle) throws ConnectionLostException, RuntimeException {
if (dutyCycle > 1 || dutyCycle < 0) {
throw new RuntimeException("dutyCycle must be between 0 and 1. A dutyCycle of " + dutyCycle + " was given.");
}
setPulseWidthInClocks(period_ * dutyCycle);
}

@Override
public void setPulseWidth(int pulseWidthUs) throws ConnectionLostException {
public void setPulseWidth(int pulseWidthUs) throws ConnectionLostException, RuntimeException {
setPulseWidth((float) pulseWidthUs);
}

@Override
public void setPulseWidth(float pulseWidthUs)
throws ConnectionLostException {
assert (pulseWidthUs >= 0);
throws ConnectionLostException, RuntimeException {
if (pulseWidthUs < 0) {
throw new RuntimeException("pulseWidthUs must be larger than 0. A pulseWidthUs of " + pulseWidthUs + " was given.");
}
float p = pulseWidthUs / baseUs_;
setPulseWidthInClocks(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private boolean runTest(int inPin, int outPin)
}

private boolean runSingleTest(PwmOutput out, DigitalInput in, float dc)
throws InterruptedException, ConnectionLostException {
throws InterruptedException, ConnectionLostException, RuntimeException {
out.setDutyCycle(dc);
Thread.sleep(100);
int highCount = 0;
Expand Down

0 comments on commit 754e891

Please sign in to comment.