Combines Raspi and Non-Raspi code

This commit is contained in:
Patrick McDonagh
2018-05-22 13:23:27 -05:00
parent 37bc6054c7
commit 85dd43d8e7
24 changed files with 1268 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
package com.henrypump.io;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
/**
* Created by patrickjmcd on 6/14/17.
*/
public class AnalogIn {
private int channel;
private int rawValue;
private double lastValue;
private Instant lastStored = Instant.EPOCH;
private double rawMax, rawMin, euMax, euMin;
private List<Integer> channelMux;
private double m, b;
private double[] history = new double[100];
private long badReads = 0;
public AnalogIn(int channel, double rawMin, double rawMax, double euMin, double euMax) {
this.channel = 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);
}
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 = 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<String, Long> getStatus(){
HashMap<String, Long> status;
status = new HashMap<String, Long>();
status.put("badReads", badReads);
status.put("measurementAge", Duration.between(lastStored, Instant.now()).toMillis());
return status;
}
public static void main(String[] args) {
MuxSetup mux = null;
Instant now = Instant.now();
mux = new MuxSetup();
HashMap<String, Long> aI1status;
// HashMap<String, Long> aI2status;
System.out.println("Testing Analog Inputs");
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);
for (int i = 0; i < 500; i++) {
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));
// }
}
}

View File

@@ -0,0 +1,60 @@
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;
public int rawValue;
public double lastValue;
public Instant lastStored = Instant.EPOCH;
public double rawMax, rawMin, euMax, euMin;
private double m, b;
public AnalogOut(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(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();
}
}
}

View File

@@ -0,0 +1,69 @@
package com.henrypump.io;
import mraa.Dir;
import mraa.Gpio;
import mraa.Result;
import java.util.List;
/**
* Created by patrickjmcd on 6/14/17.
*/
public class DigitalIn {
public int channel;
public int value;
public List<Integer> channelMux;
public DigitalIn(int channel){
this.channel = channel;
this.channelMux = MuxSetup.muxValues.get(channel);
}
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){
MuxSetup mux = new MuxSetup();
System.out.println("Testing Digital Inputs");
DigitalIn dI1 = new DigitalIn(1);
DigitalIn dI2 = new DigitalIn(2);
DigitalIn dI3 = new DigitalIn(3);
DigitalIn dI4 = new DigitalIn(4);
DigitalIn dI5 = new DigitalIn(5);
DigitalIn dI6 = new DigitalIn(6);
DigitalIn dI7 = new DigitalIn(7);
DigitalIn dI8 = new DigitalIn(8);
dI1.read(mux);
System.out.println("Input " + dI1.channel + ": " + dI1.value);
dI2.read(mux);
System.out.println("Input " + dI2.channel + ": " + dI2.value);
dI3.read(mux);
System.out.println("Input " + dI3.channel + ": " + dI3.value);
dI4.read(mux);
System.out.println("Input " + dI4.channel + ": " + dI4.value);
dI5.read(mux);
System.out.println("Input " + dI5.channel + ": " + dI5.value);
dI6.read(mux);
System.out.println("Input " + dI6.channel + ": " + dI6.value);
dI7.read(mux);
System.out.println("Input " + dI7.channel + ": " + dI7.value);
dI8.read(mux);
System.out.println("Input " + dI8.channel + ": " + dI8.value);
}
}

View File

@@ -0,0 +1,85 @@
package com.henrypump.io;
import mraa.Dir;
import mraa.Gpio;
import mraa.Result;
/**
* Created by patrickjmcd on 6/14/17.
*/
public class DigitalOut {
int channel;
Gpio gpioPin;
public DigitalOut(int channel){
// if ((channel >=0 && channel <=5) || (channel == 8) || (channel == 9)) {
this.channel = channel;
gpioPin = new Gpio(channel);
gpioPin.dir(Dir.DIR_OUT);
// } else {
// System.err.println("Error: This channel does not exist,.");
// System.exit(Result.ERROR_INVALID_PARAMETER.swigValue());
// }
}
public void write(int value){
gpioPin.write(value);
}
public static void main(String[] args){
DigitalOut dO0 = new DigitalOut(21);
DigitalOut dO1 = new DigitalOut(20);
DigitalOut dO2 = new DigitalOut(16);
DigitalOut dO3 = new DigitalOut(12);
DigitalOut dO4 = new DigitalOut(25);
DigitalOut dO5 = new DigitalOut(24);
DigitalOut redLED = new DigitalOut(23);
DigitalOut greenLED = new DigitalOut(18);
System.out.println("Testing Digital Outputs");
try {
dO0.write(1);
Thread.sleep(500);
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);
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);
dO0.write(0);
Thread.sleep(500);
System.out.println("Testing the status LEDs");
for (int i = 0; i < 10; i++) {
redLED.write(0);
greenLED.write(1);
Thread.sleep(250);
redLED.write(1);
greenLED.write(0);
Thread.sleep(250);
}
redLED.write(0);
greenLED.write(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,133 @@
package com.henrypump.io;
import mraa.Dir;
import mraa.Gpio;
import mraa.Spi;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* Created by patrickjmcd on 6/14/17.
*/
public class MuxSetup {
private int A0val = 0;
private int A1val = 0;
private int A2val = 0;
private Gpio gpioA0;
private Gpio gpioA1;
private Gpio gpioA2;
private Gpio gpioA3;
private Gpio gpioAnOutTrigger;
private Spi spi;
public static List<List<Integer>> 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)
);
public MuxSetup(){
gpioA0 = new Gpio(14);
gpioA0.dir(Dir.DIR_OUT);
gpioA1 = new Gpio(15);
gpioA1.dir(Dir.DIR_OUT);
gpioA2 = new Gpio(16);
gpioA2.dir(Dir.DIR_OUT);
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);
}
public int set(int a0val, int a1val, int a2val){
this.A0val = a0val;
this.A1val = a1val;
this.A2val = a2val;
return apply();
}
int apply(){
gpioA0.write(A0val);
gpioA1.write(A1val);
gpioA2.write(A2val);
return A0val + A1val * 2 + A2val * 4;
}
public int readDigital(){
if (gpioA3.read() == 0){
return 1;
} else {
return 0;
}
}
public int readAnalog() {
int a = spi.writeByte((short) 0);
int b = spi.writeByte((short) 0);
int c = spi.writeByte((short) 0);
// System.out.println("a= " + a + ", b= " + b + ", c= " + c);
// System.out.println("Raw= " + (a * 256 + b));
if (c == 13){
int x = a * 256;
x = x + b;
return x;
} else {
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;
}
}