From b7cfdbe8c2961ca430a6b02a3d315248477424c2 Mon Sep 17 00:00:00 2001 From: Christopher Buckley <15166572+topherbuckley@users.noreply.github.com> Date: Mon, 31 May 2021 12:56:56 +0900 Subject: [PATCH 1/5] Replaced assert statment with error check and AssertionError throw for setDutyCycle method. --- IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java | 3 ++- IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java | 4 ++-- .../src/main/java/ioio/tests/torture/PwmTest.java | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java b/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java index 08fde04ab..f16e70369 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java +++ b/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java @@ -100,7 +100,7 @@ public interface PwmOutput extends Closeable { * @throws ConnectionLostException The connection to the IOIO has been lost. * @see #setPulseWidth(int) */ - void setDutyCycle(float dutyCycle) throws ConnectionLostException; + void setDutyCycle(float dutyCycle) throws ConnectionLostException, AssertionError; /** * Sets the pulse width of the PWM output. The pulse width is duration of @@ -109,6 +109,7 @@ public interface PwmOutput extends Closeable { * * @param pulseWidthUs The pulse width, in microsecond units. * @throws ConnectionLostException The connection to the IOIO has been lost. + * @throws AssertionError dutyCycle must take values from 0 to 1 (inclusive). * @see #setDutyCycle(float) */ void setPulseWidth(int pulseWidthUs) throws ConnectionLostException; diff --git a/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java b/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java index 31efa632e..87b7ea10e 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java +++ b/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java @@ -59,8 +59,8 @@ public synchronized void close() { } @Override - public void setDutyCycle(float dutyCycle) throws ConnectionLostException { - assert (dutyCycle <= 1 && dutyCycle >= 0); + public void setDutyCycle(float dutyCycle) throws ConnectionLostException, AssertionError { + if (dutyCycle > 1 || dutyCycle < 0) throw new AssertionError("dutyCycle must be between 0 and 1"); setPulseWidthInClocks(period_ * dutyCycle); } diff --git a/applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java b/applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java index ac12e6253..0ce12df96 100644 --- a/applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java +++ b/applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java @@ -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, AssertionError { out.setDutyCycle(dc); Thread.sleep(100); int highCount = 0; From 65ad2383d5ad4a983d2c03ce22f177480ec9233b Mon Sep 17 00:00:00 2001 From: Christopher Buckley <15166572+topherbuckley@users.noreply.github.com> Date: Tue, 1 Jun 2021 11:32:34 +0900 Subject: [PATCH 2/5] Changed AssertionError to RuntimeException and updated if statement format --- IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java | 4 ++-- IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java | 6 ++++-- .../src/main/java/ioio/tests/torture/PwmTest.java | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java b/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java index f16e70369..c08b625cd 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java +++ b/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java @@ -100,7 +100,7 @@ public interface PwmOutput extends Closeable { * @throws ConnectionLostException The connection to the IOIO has been lost. * @see #setPulseWidth(int) */ - void setDutyCycle(float dutyCycle) throws ConnectionLostException, AssertionError; + void setDutyCycle(float dutyCycle) throws ConnectionLostException, RuntimeException; /** * Sets the pulse width of the PWM output. The pulse width is duration of @@ -109,7 +109,7 @@ public interface PwmOutput extends Closeable { * * @param pulseWidthUs The pulse width, in microsecond units. * @throws ConnectionLostException The connection to the IOIO has been lost. - * @throws AssertionError dutyCycle must take values from 0 to 1 (inclusive). + * @throws RuntimeException dutyCycle must take values from 0 to 1 (inclusive). * @see #setDutyCycle(float) */ void setPulseWidth(int pulseWidthUs) throws ConnectionLostException; diff --git a/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java b/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java index 87b7ea10e..2168dca6a 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java +++ b/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java @@ -59,8 +59,10 @@ public synchronized void close() { } @Override - public void setDutyCycle(float dutyCycle) throws ConnectionLostException, AssertionError { - if (dutyCycle > 1 || dutyCycle < 0) throw new AssertionError("dutyCycle must be between 0 and 1"); + 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); } diff --git a/applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java b/applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java index 0ce12df96..600d689d9 100644 --- a/applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java +++ b/applications/IOIOTortureTest/src/main/java/ioio/tests/torture/PwmTest.java @@ -73,7 +73,7 @@ private boolean runTest(int inPin, int outPin) } private boolean runSingleTest(PwmOutput out, DigitalInput in, float dc) - throws InterruptedException, ConnectionLostException, AssertionError { + throws InterruptedException, ConnectionLostException, RuntimeException { out.setDutyCycle(dc); Thread.sleep(100); int highCount = 0; From cfc92cbb729d2ceef651c1ad22da9475c8f5e3b0 Mon Sep 17 00:00:00 2001 From: Christopher Buckley <15166572+topherbuckley@users.noreply.github.com> Date: Tue, 1 Jun 2021 11:47:10 +0900 Subject: [PATCH 3/5] Added RuntimeException to javadoc for setDutyCycle. and fixed mistaken javadoc previously set on setPulseWidth --- IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java b/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java index c08b625cd..aaa687dcf 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java +++ b/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java @@ -98,6 +98,7 @@ 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, RuntimeException; @@ -109,7 +110,7 @@ public interface PwmOutput extends Closeable { * * @param pulseWidthUs The pulse width, in microsecond units. * @throws ConnectionLostException The connection to the IOIO has been lost. - * @throws RuntimeException dutyCycle must take values from 0 to 1 (inclusive). + * @throws RuntimeException pulseWidthUs must take values larger than zero. * @see #setDutyCycle(float) */ void setPulseWidth(int pulseWidthUs) throws ConnectionLostException; From ce3c1d18d6c6e2f7b8f93d02135c535230c32e13 Mon Sep 17 00:00:00 2001 From: Christopher Buckley <15166572+topherbuckley@users.noreply.github.com> Date: Tue, 1 Jun 2021 11:49:03 +0900 Subject: [PATCH 4/5] Replaced assert keyword with RunTimeException on setPulseWidth method and associated interface and javadoc --- IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java | 6 +++--- IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java b/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java index aaa687dcf..0d98aa149 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java +++ b/IOIOLibCore/src/main/java/ioio/lib/api/PwmOutput.java @@ -108,17 +108,17 @@ public interface PwmOutput extends Closeable { * 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; } diff --git a/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java b/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java index 2168dca6a..1c26bf7b3 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java +++ b/IOIOLibCore/src/main/java/ioio/lib/impl/PwmImpl.java @@ -67,14 +67,16 @@ public void setDutyCycle(float dutyCycle) throws ConnectionLostException, Runtim } @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); } From ac982f6d8d444bebb27d2c30024f4db2e19fdab6 Mon Sep 17 00:00:00 2001 From: Christopher Buckley <15166572+topherbuckley@users.noreply.github.com> Date: Tue, 1 Jun 2021 12:13:01 +0900 Subject: [PATCH 5/5] replaced assert keyword with RuntimeExceptions for analog and digital input setValue methods --- IOIOLibCore/src/main/java/ioio/lib/impl/AnalogInputImpl.java | 4 +++- IOIOLibCore/src/main/java/ioio/lib/impl/DigitalInputImpl.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/IOIOLibCore/src/main/java/ioio/lib/impl/AnalogInputImpl.java b/IOIOLibCore/src/main/java/ioio/lib/impl/AnalogInputImpl.java index dedc6e0f4..1c501ad65 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/impl/AnalogInputImpl.java +++ b/IOIOLibCore/src/main/java/ioio/lib/impl/AnalogInputImpl.java @@ -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); diff --git a/IOIOLibCore/src/main/java/ioio/lib/impl/DigitalInputImpl.java b/IOIOLibCore/src/main/java/ioio/lib/impl/DigitalInputImpl.java index 5f498acb6..e84448b15 100644 --- a/IOIOLibCore/src/main/java/ioio/lib/impl/DigitalInputImpl.java +++ b/IOIOLibCore/src/main/java/ioio/lib/impl/DigitalInputImpl.java @@ -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;