61 lines
1.4 KiB
Java
61 lines
1.4 KiB
Java
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();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|