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OBoard.iml b/.idea/modules/IOBoard.iml
index 7313704..305231c 100644
--- a/.idea/modules/IOBoard.iml
+++ b/.idea/modules/IOBoard.iml
@@ -1,10 +1,11 @@
-
+
+
diff --git a/.idea/modules/IOBoard_main.iml b/.idea/modules/IOBoard_main.iml
index 09e89ab..d049e41 100644
--- a/.idea/modules/IOBoard_main.iml
+++ b/.idea/modules/IOBoard_main.iml
@@ -1,7 +1,7 @@
-
+
-
+
diff --git a/.idea/modules/IOBoard_test.iml b/.idea/modules/IOBoard_test.iml
index 5ea1652..f34c865 100644
--- a/.idea/modules/IOBoard_test.iml
+++ b/.idea/modules/IOBoard_test.iml
@@ -1,7 +1,7 @@
-
+
-
+
@@ -13,8 +13,8 @@
-
+
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 833ea7d..53f171f 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -2,19 +2,25 @@
-
-
-
+
+
+
+
+
+
-
+
+
+
+
@@ -22,7 +28,6 @@
-
@@ -37,11 +42,31 @@
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -50,28 +75,18 @@
-
-
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -79,54 +94,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -327,8 +294,8 @@
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
-
+
@@ -547,62 +515,46 @@
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
@@ -621,12 +573,98 @@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@@
-
@@ -1002,9 +1079,7 @@
-
-
-
+
@@ -1032,16 +1107,6 @@
-
-
-
-
-
-
-
-
-
-
@@ -1053,41 +1118,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -1098,58 +1128,54 @@
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
diff --git a/build.gradle b/build.gradle
index 03ecb14..90224ea 100644
--- a/build.gradle
+++ b/build.gradle
@@ -9,6 +9,9 @@
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
+group = "com.henrypump"
+version = '0.1'
+
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
diff --git a/src/main/java/com/henrypump/io/AnalogIn.java b/src/main/java/com/henrypump/io/AnalogIn.java
index 830f944..ac24fda 100644
--- a/src/main/java/com/henrypump/io/AnalogIn.java
+++ b/src/main/java/com/henrypump/io/AnalogIn.java
@@ -1,5 +1,8 @@
package com.henrypump.io;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.HashMap;
import java.util.List;
/**
@@ -7,102 +10,148 @@ import java.util.List;
*/
public class AnalogIn {
- public int channel;
- public int rawValue;
- public double lastValue;
- public double rawMax, rawMin, euMax, euMin;
- public List channelMux;
+ private int channel;
+ private int rawValue;
+ private double lastValue;
+ private Instant lastStored = Instant.EPOCH;
+ private double rawMax, rawMin, euMax, euMin;
+ private List channelMux;
+ private double m, b;
private double[] history = new double[100];
+ private long badReads = 0;
- AnalogIn(int channel, double rawMin, double rawMax, double euMin, double euMax) {
+ public AnalogIn(int channel, double rawMin, double rawMax, double euMin, double euMax) {
this.channel = channel;
- this.channelMux = MuxSetup.muxValues.get(channel);
+ if (channel != 99) {
+ this.channelMux = MuxSetup.muxValues.get(channel);
+ }
this.rawMax = rawMax;
this.rawMin = rawMin;
this.euMax = euMax;
this.euMin = euMin;
+
+ m = (euMax - euMin) / (rawMax - rawMin);
+ b = euMax - m * (rawMax);
}
- double getHistory(int pointIndex) {
+ public int getChannel() {
+ return channel;
+ }
+
+ public double getRawMax() {
+ return rawMax;
+ }
+
+ public double getRawMin() {
+ return rawMin;
+ }
+
+ public double getEuMax() {
+ return euMax;
+ }
+
+ public double getEuMin() {
+ return euMin;
+ }
+
+ public long getBadReads() {
+ return badReads;
+ }
+
+ public int getRawValue() {
+ return rawValue;
+ }
+
+ public double getLastValue() {
+ return lastValue;
+ }
+
+ public Instant getLastStored() {
+ return lastStored;
+ }
+
+ public double getHistory(int pointIndex) {
return history[pointIndex];
}
double setValue(int value) {
+// System.out.println(value);
this.rawValue = value;
- double pv = ((euMax - euMin)/(rawMax - rawMin)) * rawValue + (euMax - ((euMax - euMin)/(rawMax - rawMin)) * rawMax);
+ double pv = m * rawValue + b;
+ lastValue = pv;
+ lastStored = Instant.now();
+ System.arraycopy(history, 0, history,1, history.length - 1);
+ history[0] = lastValue;
+// System.out.println(value + " --> " + pv);
+ return pv;
+ }
+
+ public double read(MuxSetup mux){
+ mux.set(channelMux.get(0), channelMux.get(1), channelMux.get(2));
+ int rawIn = mux.readAnalog();
+ if (rawIn != -1){
+ badReads = 0;
+ return setValue(rawIn);
+ } else {
+ badReads++;
+ try {
+ Thread.sleep(5);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ return lastValue;
+ }
+
+ public double readSim(double simRaw){
+ double pv = ((euMax - euMin)/(rawMax - rawMin)) * simRaw + (euMax - ((euMax - euMin)/(rawMax - rawMin)) * rawMax);
lastValue = pv;
System.arraycopy(history, 0, history,1, history.length - 1);
history[0] = lastValue;
return pv;
}
+ public HashMap getStatus(){
+ HashMap status;
+ status = new HashMap();
+
+ status.put("badReads", badReads);
+ status.put("measurementAge", Duration.between(lastStored, Instant.now()).toMillis());
+ return status;
+ }
+
+
public static void main(String[] args) {
- MuxSetup mux = new MuxSetup();
+ MuxSetup mux = null;
+ Instant now = Instant.now();
+ mux = new MuxSetup();
+ HashMap aI1status;
+// HashMap aI2status;
+
System.out.println("Testing Analog Inputs");
- int rawIn;
+ AnalogIn aI1 = new AnalogIn(1, 32560, 65535, 0, 100);
+// AnalogIn aI2 = new AnalogIn(2, 0, 65535, 0, 50000);
+// AnalogIn aI3 = new AnalogIn(3, 0, 65535, 0, 100);
+// AnalogIn aI4 = new AnalogIn(4, 0, 65535, 0, 100);
- AnalogIn aI1 = new AnalogIn(1, 0, 65535, 0, 10);
- AnalogIn aI2 = new AnalogIn(2, 0, 65535, 0, 10);
- AnalogIn aI3 = new AnalogIn(3, 0, 65535, 0, 10);
- AnalogIn aI4 = new AnalogIn(4, 0, 65535, 0, 10);
+ for (int i = 0; i < 500; i++) {
- for (int i = 0; i < 100; i++) {
- mux.set(aI1.channelMux.get(0), aI1.channelMux.get(1), aI1.channelMux.get(2));
- rawIn = mux.readAnalog();
- if (rawIn != -1) {
- aI1.setValue(mux.readAnalog());
- System.out.println("Input " + aI1.channel + ": " + aI1.lastValue);
- } else {
- try {
- Thread.sleep(25);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- mux.set(aI2.channelMux.get(0), aI2.channelMux.get(1), aI2.channelMux.get(2));
-
- rawIn = mux.readAnalog();
- if (rawIn != -1) {
- aI2.setValue(mux.readAnalog());
- System.out.println("Input " + aI2.channel + ": " + aI2.lastValue);
- } else {
- try {
- Thread.sleep(25);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- mux.set(aI3.channelMux.get(0), aI3.channelMux.get(1), aI3.channelMux.get(2));
- rawIn = mux.readAnalog();
- if (rawIn != -1) {
- aI3.setValue(mux.readAnalog());
- System.out.println("Input " + aI3.channel + ": " + aI3.lastValue);
- } else {
- try {
- Thread.sleep(25);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- mux.set(aI4.channelMux.get(0), aI4.channelMux.get(1), aI4.channelMux.get(2));
- rawIn = mux.readAnalog();
- if (rawIn != -1) {
- aI4.setValue(mux.readAnalog());
- System.out.println("Input " + aI4.channel + ": " + aI4.lastValue);
- } else {
- try {
- Thread.sleep(25);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
+ double a1Val = aI1.read(mux);
+// double a2Val = aI2.read(mux);
+ aI1status = aI1.getStatus();
+// aI2status = aI2.getStatus();
+ System.out.println("Input " + aI1.channel + ": " + aI1.lastValue + ", age= " + aI1status.get("measurementAge") + "ms" + ", badReads= " + aI1status.get("badReads"));
+// System.out.println("Input " + aI2.channel + ": " + aI2.lastValue + ", age= " + aI2status.get("measurementAge") + "ms" + ", badReads= " + aI2status.get("badReads"));
+// System.out.println("Input " + aI3.channel + ": " + aI3.lastValue + ", age= " + Duration.between(aI3.lastStored, now).toMillis() + "ms");
+// System.out.println("Input " + aI4.channel + ": " + aI4.lastValue + ", age= " + Duration.between(aI4.lastStored, now).toMillis() + "ms");
+// System.out.println("--");
+// System.out.printf("%s,%s%n", aI1.lastValue, aI2.lastValue);
}
-
+// for (int j = 0; j < 100; j++){
+// System.out.println("History[" + j + "]= " + aI3.getHistory(j));
+// }
}
}
diff --git a/src/main/java/com/henrypump/io/AnalogInPi.java b/src/main/java/com/henrypump/io/AnalogInPi.java
deleted file mode 100644
index a5f0a21..0000000
--- a/src/main/java/com/henrypump/io/AnalogInPi.java
+++ /dev/null
@@ -1,94 +0,0 @@
-package com.henrypump.io;
-
-import java.io.IOException;
-import java.time.Duration;
-import java.time.Instant;
-import java.util.List;
-
-/**
- * Created by patrickjmcd on 6/21/17.
- */
-public class AnalogInPi {
- public int channel;
- public int rawValue;
- public double lastValue;
- public Instant lastStored = Instant.EPOCH;
- public double rawMax, rawMin, euMax, euMin;
- public List channelMux;
- private double[] history = new double[100];
-
- AnalogInPi(int channel, double rawMin, double rawMax, double euMin, double euMax) {
- this.channel = channel;
- this.channelMux = MuxSetupPi.muxValues.get(channel);
- this.rawMax = rawMax;
- this.rawMin = rawMin;
- this.euMax = euMax;
- this.euMin = euMin;
- }
-
- double getHistory(int pointIndex) {
- return history[pointIndex];
- }
-
- double setValue(int value) {
-// System.out.println(value);
- this.rawValue = value;
- double pv = ((euMax - euMin)/(rawMax - rawMin)) * rawValue + (euMax - ((euMax - euMin)/(rawMax - rawMin)) * rawMax);
- lastValue = pv;
- lastStored = Instant.now();
- System.arraycopy(history, 0, history,1, history.length - 1);
- history[0] = lastValue;
-// System.out.println(value + " --> " + pv);
- return pv;
- }
-
- double read(MuxSetupPi mux) throws IOException {
- mux.set(channelMux.get(0), channelMux.get(1), channelMux.get(2));
- int rawIn = mux.readAnalog();
- if (rawIn != -1){
- return setValue(rawIn);
- } else {
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- return lastValue;
- }
-
- public static void main(String[] args) {
- MuxSetupPi mux = null;
- Instant now = Instant.now();
- try {
- mux = new MuxSetupPi();
-
- System.out.println("Testing Analog Inputs");
-
- AnalogInPi aI1 = new AnalogInPi(1, 0, 65535, 0, 100);
- AnalogInPi aI2 = new AnalogInPi(2, 0, 65535, 0, 100);
- AnalogInPi aI3 = new AnalogInPi(3, 0, 65535, 0, 100);
-// AnalogInPi aI4 = new AnalogInPi(4, 0, 65535, 0, 100);
-
- for (int i = 0; i < 50; i++) {
-
- double a1Val = aI1.read(mux);
- double a2Val = aI2.read(mux);
- double a3Val = aI3.read(mux);
-// double a4Val = aI4.read(mux);
- now = Instant.now();
- System.out.println("Input " + aI1.channel + ": " + aI1.lastValue + ", age= " + Duration.between(aI1.lastStored, now).toMillis() + "ms");
- System.out.println("Input " + aI2.channel + ": " + aI2.lastValue + ", age= " + Duration.between(aI2.lastStored, now).toMillis() + "ms");
- System.out.println("Input " + aI3.channel + ": " + aI3.lastValue + ", age= " + Duration.between(aI3.lastStored, now).toMillis() + "ms");
-// System.out.println("Input " + aI4.channel + ": " + aI4.lastValue + ", age= " + Duration.between(aI4.lastStored, now).toMillis() + "ms");
- System.out.println("--");
-
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- }
-}
-
diff --git a/src/main/java/com/henrypump/io/AnalogOut.java b/src/main/java/com/henrypump/io/AnalogOut.java
index 57a5ceb..d0d6dd8 100644
--- a/src/main/java/com/henrypump/io/AnalogOut.java
+++ b/src/main/java/com/henrypump/io/AnalogOut.java
@@ -3,27 +3,58 @@ package com.henrypump.io;
import mraa.Dir;
import mraa.Gpio;
+import java.io.IOException;
+import java.time.Instant;
+
/**
* Created by patrickjmcd on 6/19/17.
*/
public class AnalogOut {
public int channel;
- Gpio triggerPin;
+ public int rawValue;
+ public double lastValue;
+ public Instant lastStored = Instant.EPOCH;
+ public double rawMax, rawMin, euMax, euMin;
+ private double m, b;
- AnalogOut(int channel){
+
+ public AnalogOut(int channel, double rawMin, double rawMax, double euMin, double euMax){
this.channel = channel;
- triggerPin = new Gpio(6);
- triggerPin.dir(Dir.DIR_OUT);
+ this.rawMin = rawMin;
+ this.rawMax = rawMax;
+ this.euMin = euMin;
+ this.euMax = euMax;
+
+ m = (rawMax - rawMin) / (euMax - euMin);
+ b = rawMax - m * euMax;
+
}
- public int trigger(int triggerVal){
- triggerPin.write(triggerVal);
- return triggerVal;
- }
-
- public int write(float writeVal){
-
+ public int write(MuxSetup mux, double writeVal){
+ rawValue = (int) (writeVal * m + b);
+ try {
+ mux.writeAnalog(channel, rawValue);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
return 0;
}
+
+ public static void main(String[] args){
+ try {
+ MuxSetup mux = new MuxSetup();
+ AnalogOut anOut1 = new AnalogOut(2, 0, 65535, 0, 100);
+
+ for (int i = 0; i < 10; i++) {
+ anOut1.write(mux, (double) (i + 1) * 10.0);
+ Thread.sleep(1000);
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+
}
diff --git a/src/main/java/com/henrypump/io/AnalogOutPi.java b/src/main/java/com/henrypump/io/AnalogOutPi.java
deleted file mode 100644
index 718fbdf..0000000
--- a/src/main/java/com/henrypump/io/AnalogOutPi.java
+++ /dev/null
@@ -1,60 +0,0 @@
-package com.henrypump.io;
-
-
-import java.io.IOException;
-import java.time.Instant;
-
-/**
- * Created by patrickjmcd on 6/19/17.
- */
-public class AnalogOutPi {
- public int channel;
- public int rawValue;
- public double lastValue;
- public Instant lastStored = Instant.EPOCH;
- public double rawMax, rawMin, euMax, euMin;
- private double m, b;
-
-
- AnalogOutPi(int channel, double rawMin, double rawMax, double euMin, double euMax){
- this.channel = channel;
- this.rawMin = rawMin;
- this.rawMax = rawMax;
- this.euMin = euMin;
- this.euMax = euMax;
-
- m = (rawMax - rawMin) / (euMax - euMin);
- b = rawMax - m * euMax;
-
- }
-
-
- public int write(MuxSetupPi mux, double writeVal){
- rawValue = (int) (writeVal * m + b);
- try {
- mux.writeAnalog(channel, rawValue);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return 0;
- }
-
- public static void main(String[] args){
- try {
- MuxSetupPi mux = new MuxSetupPi();
- AnalogOutPi anOut1 = new AnalogOutPi(1, 0, 65535, 0, 100);
-
- for (int i = 0; i < 10; i++) {
- anOut1.write(mux, (double) (i + 1) * 10.0);
- Thread.sleep(1000);
- }
- } catch (IOException e){
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- }
-
-
-}
diff --git a/src/main/java/com/henrypump/io/DigitalIn.java b/src/main/java/com/henrypump/io/DigitalIn.java
index 71356a2..04f445a 100644
--- a/src/main/java/com/henrypump/io/DigitalIn.java
+++ b/src/main/java/com/henrypump/io/DigitalIn.java
@@ -14,13 +14,15 @@ public class DigitalIn {
public int channel;
public int value;
public List channelMux;
- DigitalIn(int channel){
+ public DigitalIn(int channel){
this.channel = channel;
this.channelMux = MuxSetup.muxValues.get(channel);
}
- void setValue(int value){
- this.value = value;
+ public int read(MuxSetup mux){
+ mux.set(channelMux.get(0), channelMux.get(1), channelMux.get(2));
+ value = mux.readDigital();
+ return value;
}
public static void main(String[] args){
@@ -37,36 +39,28 @@ public class DigitalIn {
DigitalIn dI7 = new DigitalIn(7);
DigitalIn dI8 = new DigitalIn(8);
- mux.set(dI1.channelMux.get(0), dI1.channelMux.get(1), dI1.channelMux.get(2));
- dI1.setValue(mux.readDigital());
+ dI1.read(mux);
System.out.println("Input " + dI1.channel + ": " + dI1.value);
- mux.set(dI2.channelMux.get(0), dI2.channelMux.get(1), dI2.channelMux.get(2));
- dI2.setValue(mux.readDigital());
+ dI2.read(mux);
System.out.println("Input " + dI2.channel + ": " + dI2.value);
- mux.set(dI3.channelMux.get(0), dI3.channelMux.get(1), dI3.channelMux.get(2));
- dI3.setValue(mux.readDigital());
+ dI3.read(mux);
System.out.println("Input " + dI3.channel + ": " + dI3.value);
- mux.set(dI4.channelMux.get(0), dI4.channelMux.get(1), dI4.channelMux.get(2));
- dI4.setValue(mux.readDigital());
+ dI4.read(mux);
System.out.println("Input " + dI4.channel + ": " + dI4.value);
- mux.set(dI5.channelMux.get(0), dI5.channelMux.get(1), dI5.channelMux.get(2));
- dI5.setValue(mux.readDigital());
+ dI5.read(mux);
System.out.println("Input " + dI5.channel + ": " + dI5.value);
- mux.set(dI6.channelMux.get(0), dI6.channelMux.get(1), dI6.channelMux.get(2));
- dI6.setValue(mux.readDigital());
+ dI6.read(mux);
System.out.println("Input " + dI6.channel + ": " + dI6.value);
- mux.set(dI7.channelMux.get(0), dI7.channelMux.get(1), dI7.channelMux.get(2));
- dI7.setValue(mux.readDigital());
+ dI7.read(mux);
System.out.println("Input " + dI7.channel + ": " + dI7.value);
- mux.set(dI8.channelMux.get(0), dI8.channelMux.get(1), dI8.channelMux.get(2));
- dI8.setValue(mux.readDigital());
+ dI8.read(mux);
System.out.println("Input " + dI8.channel + ": " + dI8.value);
diff --git a/src/main/java/com/henrypump/io/DigitalInPi.java b/src/main/java/com/henrypump/io/DigitalInPi.java
deleted file mode 100644
index 5ef1fd5..0000000
--- a/src/main/java/com/henrypump/io/DigitalInPi.java
+++ /dev/null
@@ -1,81 +0,0 @@
-package com.henrypump.io;
-
-import mraa.Dir;
-import mraa.Gpio;
-import mraa.Result;
-
-import java.io.IOException;
-import java.util.List;
-
-/**
-* Created by patrickjmcd on 6/14/17.
-*/
-
-public class DigitalInPi {
- public int channel;
- public int value;
- public List channelMux;
- DigitalInPi(int channel){
- this.channel = channel;
- this.channelMux = MuxSetupPi.muxValues.get(channel);
- }
-
- void setValue(int value){
- this.value = value;
- }
-
- public static void main(String[] args){
- try {
- MuxSetupPi mux = new MuxSetupPi();
-
- System.out.println("Testing Digital Inputs");
-
-
- DigitalInPi dI1 = new DigitalInPi(1);
- DigitalInPi dI2 = new DigitalInPi(2);
- DigitalInPi dI3 = new DigitalInPi(3);
- DigitalInPi dI4 = new DigitalInPi(4);
- DigitalInPi dI5 = new DigitalInPi(5);
- DigitalInPi dI6 = new DigitalInPi(6);
- DigitalInPi dI7 = new DigitalInPi(7);
- DigitalInPi dI8 = new DigitalInPi(8);
-
- mux.set(dI1.channelMux.get(0), dI1.channelMux.get(1), dI1.channelMux.get(2));
- dI1.setValue(mux.readDigital());
- System.out.println("Input " + dI1.channel + ": " + dI1.value);
-
- mux.set(dI2.channelMux.get(0), dI2.channelMux.get(1), dI2.channelMux.get(2));
- dI2.setValue(mux.readDigital());
- System.out.println("Input " + dI2.channel + ": " + dI2.value);
-
- mux.set(dI3.channelMux.get(0), dI3.channelMux.get(1), dI3.channelMux.get(2));
- dI3.setValue(mux.readDigital());
- System.out.println("Input " + dI3.channel + ": " + dI3.value);
-
- mux.set(dI4.channelMux.get(0), dI4.channelMux.get(1), dI4.channelMux.get(2));
- dI4.setValue(mux.readDigital());
- System.out.println("Input " + dI4.channel + ": " + dI4.value);
-
- mux.set(dI5.channelMux.get(0), dI5.channelMux.get(1), dI5.channelMux.get(2));
- dI5.setValue(mux.readDigital());
- System.out.println("Input " + dI5.channel + ": " + dI5.value);
-
- mux.set(dI6.channelMux.get(0), dI6.channelMux.get(1), dI6.channelMux.get(2));
- dI6.setValue(mux.readDigital());
- System.out.println("Input " + dI6.channel + ": " + dI6.value);
-
- mux.set(dI7.channelMux.get(0), dI7.channelMux.get(1), dI7.channelMux.get(2));
- dI7.setValue(mux.readDigital());
- System.out.println("Input " + dI7.channel + ": " + dI7.value);
-
- mux.set(dI8.channelMux.get(0), dI8.channelMux.get(1), dI8.channelMux.get(2));
- dI8.setValue(mux.readDigital());
- System.out.println("Input " + dI8.channel + ": " + dI8.value);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
-
- }
-}
diff --git a/src/main/java/com/henrypump/io/DigitalOut.java b/src/main/java/com/henrypump/io/DigitalOut.java
index ec7dbfb..8ce9daf 100644
--- a/src/main/java/com/henrypump/io/DigitalOut.java
+++ b/src/main/java/com/henrypump/io/DigitalOut.java
@@ -11,7 +11,7 @@ public class DigitalOut {
int channel;
Gpio gpioPin;
- DigitalOut(int channel){
+ public DigitalOut(int channel){
// if ((channel >=0 && channel <=5) || (channel == 8) || (channel == 9)) {
this.channel = channel;
gpioPin = new Gpio(channel);
@@ -23,7 +23,7 @@ public class DigitalOut {
}
- void write(int value){
+ public void write(int value){
gpioPin.write(value);
}
diff --git a/src/main/java/com/henrypump/io/DigitalOutPi.java b/src/main/java/com/henrypump/io/DigitalOutPi.java
deleted file mode 100644
index 6e9b073..0000000
--- a/src/main/java/com/henrypump/io/DigitalOutPi.java
+++ /dev/null
@@ -1,142 +0,0 @@
-package com.henrypump.io;
-
-import com.pi4j.io.gpio.*;
-
-/**
- * Created by patrickjmcd on 6/20/17.
- */
-public class DigitalOutPi {
- int channel;
- final GpioController gpio = GpioFactory.getInstance();
- Pin gpioPin;
- GpioPinDigitalOutput pin;
- String pinNumbering = "wPi";
-
-
-
-
- DigitalOutPi(int channel){
- if ((channel >=1 && channel <=6) || (channel == 8) || (channel == 9)) {
- this.channel = channel;
-
- switch (channel) {
- case 1:
- if (pinNumbering == "wPi") {
- gpioPin = RaspiPin.GPIO_29;
- } else {
- gpioPin = RaspiPin.GPIO_21;
- }
- break;
- case 2:
- if (pinNumbering == "wPi") {
- gpioPin = RaspiPin.GPIO_28;
- } else {
- gpioPin = RaspiPin.GPIO_20;
- }
- break;
- case 3:
- if (pinNumbering == "wPi") {
- gpioPin = RaspiPin.GPIO_27;
- } else {
- gpioPin = RaspiPin.GPIO_16;
- }
- break;
- case 4:
- if (pinNumbering == "wPi") {
- gpioPin = RaspiPin.GPIO_26;
- } else {
- gpioPin = RaspiPin.GPIO_12;
- }
- break;
- case 5:
- if (pinNumbering == "wPi") {
- gpioPin = RaspiPin.GPIO_06;
- } else {
- gpioPin = RaspiPin.GPIO_25;
- }
- break;
- case 6:
- if (pinNumbering == "wPi") {
- gpioPin = RaspiPin.GPIO_05;
- } else {
- gpioPin = RaspiPin.GPIO_24;
- }
- break;
- case 8:
- if (pinNumbering == "wPi") {
- gpioPin = RaspiPin.GPIO_04;
- } else {
- gpioPin = RaspiPin.GPIO_23;
- }
- break;
- case 9:
- if (pinNumbering == "wPi") {
- gpioPin = RaspiPin.GPIO_01;
- } else {
- gpioPin = RaspiPin.GPIO_18;
- }
- break;
- }
-
-
- pin = gpio.provisionDigitalOutputPin(gpioPin, "Output" + channel, PinState.LOW);
- pin.setShutdownOptions(true, PinState.LOW);
- } else {
- System.err.println("Error: This channel does not exist,.");
- }
-
- }
-
- void write(int value){
- if (value >= 1) {
- pin.high();
- } else {
- pin.low();
- }
- }
-
- public static void main(String[] args){
- DigitalOutPi dO1 = new DigitalOutPi(1);
- DigitalOutPi dO2 = new DigitalOutPi(2);
- DigitalOutPi dO3 = new DigitalOutPi(3);
- DigitalOutPi dO4 = new DigitalOutPi(4);
- DigitalOutPi dO5 = new DigitalOutPi(5);
- DigitalOutPi dO6 = new DigitalOutPi(6);
-// DigitalOutPi redLED = new DigitalOut(8);
-// DigitalOutPi greenLED = new DigitalOut(9);
- System.out.println("Testing Digital Outputs");
-
-
- try {
- dO1.write(1);
- Thread.sleep(500);
- dO2.write(1);
- Thread.sleep(500);
- dO3.write(1);
- Thread.sleep(500);
- dO4.write(1);
- Thread.sleep(500);
- dO5.write(1);
- Thread.sleep(500);
- dO6.write(1);
- Thread.sleep(500);
- dO6.write(0);
- Thread.sleep(500);
- dO5.write(0);
- Thread.sleep(500);
- dO4.write(0);
- Thread.sleep(500);
- dO3.write(0);
- Thread.sleep(500);
- dO2.write(0);
- Thread.sleep(500);
- dO1.write(0);
- Thread.sleep(500);
-
-
-
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-}
diff --git a/src/main/java/com/henrypump/io/MuxSetup.java b/src/main/java/com/henrypump/io/MuxSetup.java
index 8a48c2b..73f2939 100644
--- a/src/main/java/com/henrypump/io/MuxSetup.java
+++ b/src/main/java/com/henrypump/io/MuxSetup.java
@@ -4,6 +4,7 @@ import mraa.Dir;
import mraa.Gpio;
import mraa.Spi;
+import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@@ -20,6 +21,7 @@ public class MuxSetup {
private Gpio gpioA1;
private Gpio gpioA2;
private Gpio gpioA3;
+ private Gpio gpioAnOutTrigger;
private Spi spi;
public static List> muxValues = Arrays.asList(
@@ -34,7 +36,7 @@ public class MuxSetup {
Arrays.asList(1,1,1)
);
- MuxSetup(){
+ public MuxSetup(){
gpioA0 = new Gpio(14);
gpioA0.dir(Dir.DIR_OUT);
@@ -47,6 +49,10 @@ public class MuxSetup {
gpioA3 = new Gpio(17);
gpioA3.dir(Dir.DIR_IN);
+ gpioAnOutTrigger = new Gpio(6);
+ gpioAnOutTrigger.dir(Dir.DIR_OUT);
+ gpioAnOutTrigger.write(1);
+
spi = new Spi(5);
}
@@ -57,7 +63,7 @@ public class MuxSetup {
return apply();
}
- public int apply(){
+ int apply(){
gpioA0.write(A0val);
gpioA1.write(A1val);
gpioA2.write(A2val);
@@ -89,4 +95,39 @@ public class MuxSetup {
return -1;
}
};
+
+ void preAnalogWrite(){
+ gpioAnOutTrigger.write(1);
+ }
+
+ void postAnalogWrite(){
+ gpioAnOutTrigger.write(0);
+ }
+
+ static String toBinary( byte b )
+ {
+ StringBuilder sb = new StringBuilder(8);
+ for( int i = 0; i < 8; i++ )
+ sb.append((b << i % 8 & 0x80) == 0 ? '0' : '1');
+ return sb.toString();
+ }
+
+ public int writeAnalog(int channel, int rawValue) throws IOException {
+ preAnalogWrite();
+ byte controlBits = (byte) 0b00010000;
+
+ if (channel == 2){
+ controlBits = (byte) 0b00100100;
+ }
+ byte anWrite[] = new byte[]{
+ controlBits,
+ (byte) (rawValue & 0xFF),
+ (byte) ((rawValue >> 8) & 0xFF)
+ };
+ byte[] result = spi.write(anWrite);
+ System.out.printf("writing %s, %s, %s%n", toBinary(anWrite[0]), toBinary(anWrite[1]), toBinary(anWrite[2]));
+ postAnalogWrite();
+ System.out.printf("0= %s, 1= %s, 2= %s%n", result[0] & 0xFF, result[1] & 0xFF, result[2] & 0xFF);
+ return 0;
+ }
}
diff --git a/src/main/java/com/henrypump/io/MuxSetupPi.java b/src/main/java/com/henrypump/io/MuxSetupPi.java
deleted file mode 100644
index 0632fa1..0000000
--- a/src/main/java/com/henrypump/io/MuxSetupPi.java
+++ /dev/null
@@ -1,142 +0,0 @@
-package com.henrypump.io;
-
-import com.pi4j.io.gpio.*;
-import com.pi4j.io.spi.SpiChannel;
-import com.pi4j.io.spi.SpiDevice;
-import com.pi4j.io.spi.SpiFactory;
-
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * Created by patrickjmcd on 6/21/17.
- */
-public class MuxSetupPi {
-
- final GpioController gpio = GpioFactory.getInstance();
- GpioPinDigitalOutput pinMux1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_21);
- GpioPinDigitalOutput pinMux2 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_22);
- GpioPinDigitalOutput pinMux3 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_23);
- GpioPinDigitalInput pinDigitalRead = gpio.provisionDigitalInputPin(RaspiPin.GPIO_24);
-
- GpioPinDigitalOutput pinAnOutTrigger = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "AnalogOutTrigger", PinState.HIGH);
-
- private int mux1value = 0;
- private int mux2value = 0;
- private int mux3value = 0;
-
- private SpiDevice spi;
-
- public static List> muxValues = Arrays.asList(
- Arrays.asList(),
- Arrays.asList(0,0,0),
- Arrays.asList(1,0,0),
- Arrays.asList(0,1,0),
- Arrays.asList(1,1,0),
- Arrays.asList(0,0,1),
- Arrays.asList(1,0,1),
- Arrays.asList(0,1,1),
- Arrays.asList(1,1,1)
- );
-
- MuxSetupPi() throws IOException{
- spi = SpiFactory.getInstance(SpiChannel.CS0, SpiDevice.DEFAULT_SPI_SPEED, SpiDevice.DEFAULT_SPI_MODE);
- }
-
- public int set(int a0val, int a1val, int a2val){
- this.mux1value = a0val;
- this.mux2value = a1val;
- this.mux3value = a2val;
- return apply();
- }
-
- public int apply(){
- if (mux1value >= 1) {
- pinMux1.high();
- } else {
- pinMux1.low();
- }
-
- if (mux2value >= 1) {
- pinMux2.high();
- } else {
- pinMux2.low();
- }
-
- if (mux3value >= 1) {
- pinMux3.high();
- } else {
- pinMux3.low();
- }
- return mux1value + mux2value * 2 + mux3value * 4;
- }
-
- public int readDigital(){
- if (pinDigitalRead.isHigh()){
- return 0;
- } else {
- return 1;
- }
- }
-
- public void preAnalogWrite(){
- pinAnOutTrigger.low();
- }
-
- public void postAnalogWrite(){
- pinAnOutTrigger.high();
- }
-
- static String toBinary( byte b )
- {
- StringBuilder sb = new StringBuilder(8);
- for( int i = 0; i < 8; i++ )
- sb.append((b << i % 8 & 0x80) == 0 ? '0' : '1');
- return sb.toString();
- }
-
- public int writeAnalog(int channel, int rawValue) throws IOException{
- preAnalogWrite();
- byte controlBits = (byte) 0b00010000;
-
- if (channel == 2){
- controlBits = (byte) 0b00100100;
- }
- byte anWrite[] = new byte[]{
- controlBits,
- (byte) (rawValue & 0xFF),
- (byte) ((rawValue >> 8) & 0xFF)
- };
- byte[] result = spi.write(anWrite);
- System.out.printf("writing %s, %s, %s%n", toBinary(anWrite[0]), toBinary(anWrite[1]), toBinary(anWrite[2]));
- postAnalogWrite();
- System.out.printf("0= %s, 1= %s, 2= %s%n", result[0] & 0xFF, result[1] & 0xFF, result[2] & 0xFF);
- return 0;
- }
-
- public int readAnalog() throws IOException {
- byte zeroes[] = new byte[] {
- (byte) 0b00000000,
- (byte) 0b00000000,
- (byte) 0b00000000
- };
-
- byte[] result = spi.write(zeroes);
-
-
-// System.out.println("Raw= " + (a * 256 + b));
- int a = result[0] & 0xFF;
- int b = result[1] & 0xFF;
- int c = result[2] & 0xFF;
-// System.out.println("a= " + a + ", b= " + b + ", c= " + c);
-
- if (c == 13){
- int x = a * 256;
- x = x + b;
- return x;
- } else {
- return -1;
- }
- }
-}