Skip to content

Commit 754e891

Browse files
authored
Merge pull request #259 from topherbuckley/silentAssertionErrors
Replaced assert statment with error check and AssertionError throw
2 parents cc38d9f + ac982f6 commit 754e891

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,27 @@ public interface PwmOutput extends Closeable {
9898
*
9999
* @param dutyCycle The duty cycle, as a real value from 0.0 to 1.0.
100100
* @throws ConnectionLostException The connection to the IOIO has been lost.
101+
* @throws RuntimeException dutyCycle must take values from 0 to 1 (inclusive).
101102
* @see #setPulseWidth(int)
102103
*/
103-
void setDutyCycle(float dutyCycle) throws ConnectionLostException;
104+
void setDutyCycle(float dutyCycle) throws ConnectionLostException, RuntimeException;
104105

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

116118
/**
117119
* The same as {@link #setPulseWidth(int)}, but with sub-microsecond
118120
* precision.
119121
*/
120122
void setPulseWidth(float pulseWidthUs)
121-
throws ConnectionLostException;
123+
throws ConnectionLostException, RuntimeException;
122124
}

IOIOLibCore/src/main/java/ioio/lib/impl/AnalogInputImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public float getReference() {
6666
@Override
6767
synchronized public void setValue(int value) {
6868
// Log.v("AnalogInputImpl", "Pin " + pinNum_ + " value is " + value);
69-
assert (value >= 0 && value < 1024);
69+
if (value < 0 || value >= 1024) {
70+
throw new RuntimeException("value must be between 0 (inclusive) and 1024 (exclusive). A value of " + value + " was given.");
71+
}
7072
value_ = value;
7173
++sampleCount_;
7274
bufferPush((short) value);

IOIOLibCore/src/main/java/ioio/lib/impl/DigitalInputImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class DigitalInputImpl extends AbstractPin implements DigitalInput,
4747
@Override
4848
synchronized public void setValue(int value) {
4949
// Log.v("DigitalInputImpl", "Pin " + pinNum_ + " value is " + value);
50-
assert (value == 0 || value == 1);
50+
if (value != 0 && value != 1) {
51+
throw new RuntimeException("value must be 0 or 1. A value of " + value + " was given.");
52+
}
5153
value_ = (value == 1);
5254
if (!valid_) {
5355
valid_ = true;

IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,24 @@ public synchronized void close() {
5959
}
6060

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

6769
@Override
68-
public void setPulseWidth(int pulseWidthUs) throws ConnectionLostException {
70+
public void setPulseWidth(int pulseWidthUs) throws ConnectionLostException, RuntimeException {
6971
setPulseWidth((float) pulseWidthUs);
7072
}
7173

7274
@Override
7375
public void setPulseWidth(float pulseWidthUs)
74-
throws ConnectionLostException {
75-
assert (pulseWidthUs >= 0);
76+
throws ConnectionLostException, RuntimeException {
77+
if (pulseWidthUs < 0) {
78+
throw new RuntimeException("pulseWidthUs must be larger than 0. A pulseWidthUs of " + pulseWidthUs + " was given.");
79+
}
7680
float p = pulseWidthUs / baseUs_;
7781
setPulseWidthInClocks(p);
7882
}

applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private boolean runTest(int inPin, int outPin)
7373
}
7474

7575
private boolean runSingleTest(PwmOutput out, DigitalInput in, float dc)
76-
throws InterruptedException, ConnectionLostException {
76+
throws InterruptedException, ConnectionLostException, RuntimeException {
7777
out.setDutyCycle(dc);
7878
Thread.sleep(100);
7979
int highCount = 0;

0 commit comments

Comments
 (0)