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,2 @@
Manifest-Version: 1.0

View File

@@ -0,0 +1,6 @@
#Wed Jun 14 10:50:12 CDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip

430
RaspberryPi/HPOCV11/HPOCV11.ino Executable file
View File

@@ -0,0 +1,430 @@
#include <SPI.h>
#define ON 1
#define OFF 0
#define TIMREADA2D 0
#define TIMCLICKRELAY 1
#define TIMFLASHEDISONLED 2
#define TIMWRITED2A 3
#define TIMREADDIGS 4
void AnalogIn(void);
void AnalogOut(void);
void FlashEdisonLED(void);
void Relays(void);
void ReadDigitals(void);
void Timers(void);
unsigned int anout1; // Analog output value 1 - 0xFFFF (0 - 20mA)
unsigned int anout2; // Analog output value 2 - 0xFFFF (0 - 20mA)
unsigned int anin1; // Analog output value 1 - 0xFFFF (0-20mA, 0-10V, 0-5V)
unsigned int anin2; // Analog output value 2 - 0xFFFF (0-20mA, 0-10V, 0-5V)
unsigned int anin3; // Analog output value 3 - 0xFFFF (0-20mA, 0-10V, 0-5V)
unsigned int anin4; // Analog output value 4 - 0xFFFF (0-20mA, 0-10V, 0-5V)
// unsigned char edisonled; // flag for led on edison cannot use since messes with SPI
unsigned char redled; // flag for red status led
unsigned char greenled; // flag for green status led
unsigned char relay1, relay2, relay3, relay4, relay5, relay6; // Flags for control of relays
unsigned char digin1, digin2, digin3, digin4, digin5, digin6, digin7, digin8; // Flags show dig input state
unsigned long oldticker;
unsigned int ticker; // Millisecond counter
unsigned int timers[10];
void setup()
{
anout1 = 0;
anout2 = 0;
relay1 = 0;
relay2 = 0;
relay3 = 0;
relay4 = 0;
relay5 = 0;
relay6 = 0;
redled = 0;
greenled = 0;
pinMode(0, OUTPUT); // Relay 1
pinMode(1, OUTPUT); // Relay 2
pinMode(2, OUTPUT); // Relay 3
pinMode(3, OUTPUT); // Relay 4
pinMode(4, OUTPUT); // Relay 5
pinMode(5, OUTPUT); // Relay 6
pinMode(6, OUTPUT); // D2A CS
pinMode(8, OUTPUT); // Red board LED
pinMode(9, OUTPUT); // Green board LED
pinMode(11, OUTPUT); // SPI Out data
pinMode(12, INPUT); // SPI In data
pinMode(13, OUTPUT); // SPI CLK & Board LED)
pinMode(A5, INPUT);
pinMode(A0, OUTPUT); // Channel select LSB
pinMode(A1, OUTPUT); // Channel select 2nd bit
pinMode(A2, OUTPUT); // Channel select MSB
pinMode(A3, INPUT); // Result from reading digital inputs
// SPISettings settingsA(2000000, MSBFIRST, SPI_MODE1);
// SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
SPI.begin();
SPI.setBitOrder(MSBFIRST);
Serial.begin(9600);
pinMode(SS, OUTPUT);
}
void Relays(void)
{
if (relay1 == 1)
digitalWrite(0, HIGH);
else
digitalWrite(0, LOW);
if (relay2 == 1)
digitalWrite(1, HIGH);
else
digitalWrite(1, LOW);
if (relay3 == 1)
digitalWrite(2, HIGH);
else
digitalWrite(2, LOW);
if (relay4 == 1)
digitalWrite(3, HIGH);
else
digitalWrite(3, LOW);
if (relay5 == 1)
digitalWrite(4, HIGH);
else
digitalWrite(4, LOW);
if (relay6 == 1)
digitalWrite(5, HIGH);
else
digitalWrite(5, LOW);
}
void LEDs(void)
{
if (redled == 1)
digitalWrite(8, HIGH);
else
digitalWrite(8, LOW);
if (greenled == 1)
digitalWrite(9, HIGH);
else
digitalWrite(9, LOW);
}
void Timers(void)
{
unsigned char x;
unsigned long milli;
milli = millis();
if (milli == oldticker)
return;
oldticker = milli;
ticker++;
for (x=0; x<10;x++)
{
if(timers[x] > 0) timers[x]--;
}
// anout1++;
}
void AnalogOut(void)
{
static unsigned char x = 8;
static unsigned char diff;
unsigned char val;
unsigned int val16;
unsigned int incomingByte;
if (timers[TIMWRITED2A])
return;
anout1++;
// SPI.beginTransaction(2000000, MSBFIRST, SPI_MODE1);
digitalWrite(6, LOW);
val = 16; // Chan A
SPI.transfer(x);
if (diff == 1) x++;
if (diff == 2) x--;
diff = 0;
val16 = anout1;
val16 = val16 >> 8;
val = val16 & 0xFF;
SPI.transfer(0);
// SPI.transfer(val);
val = anout1 & 0xFF;
SPI.transfer(val);
// SPI.transfer(0);
/*
Serial.print(x);
Serial.print("\n");
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if (incomingByte == '.') diff = 1;
if (incomingByte == ',') diff = 2;
if (incomingByte == ' ') diff = 0;
}
*/
timers[TIMWRITED2A] = 100;
digitalWrite(6, HIGH);
}
void AnalogIn(void)
{
unsigned char a,b,c;
unsigned int x;
static unsigned char chan = 0;
if (timers[TIMREADA2D])
return;
switch(chan)
{
case 0:
{
digitalWrite(A0, LOW); // Need to set up mux for each A2D like the relays
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
a = SPI.transfer(0);
b = SPI.transfer(0);
c = SPI.transfer(0);
/* Serial.print(a);
Serial.print(",");
Serial.print(b);
Serial.print(",");
Serial.print(c);
Serial.print(",");
*/ if (c == 13) // 13 good reading, 45 overrange, anything else A2D not ready
{
x = (unsigned int) a * 256;
x = x + b;
if (x >= 32767) // wrong ref fix
{ // wrong ref fix
x = x - 32767; // wrong ref fix
x = x * 2; // wrong ref fix
} // wrong ref fix
}
else
x = 0;
anin1 = x;
break;
}
case 1:
{
digitalWrite(A0, HIGH); // Need to set up mux for each A2D like the relays
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
a = SPI.transfer(0);
b = SPI.transfer(0);
c = SPI.transfer(0);
if (c == 13) // 13 good reading, 45 overrange, anything else A2D not ready
{
x = (unsigned int) a * 256;
x = x + b;
if (x >= 32767) // wrong ref fix
{ // wrong ref fix
x = x - 32767; // wrong ref fix
x = x * 2; // wrong ref fix
} // wrong ref fix
else
x = 0;
anin2 = x;
break;
}
}
case 2:
{
digitalWrite(A0, LOW); // Need to set up mux for each A2D like the relays
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
a = SPI.transfer(0);
b = SPI.transfer(0);
c = SPI.transfer(0);
if (c == 13) // 13 good reading, 45 overrange, anything else A2D not ready
{
x = (unsigned int) a * 256;
x = x + b;
if (x >= 32767) // wrong ref fix
{ // wrong ref fix
x = x - 32767; // wrong ref fix
x = x * 2; // wrong ref fix
} // wrong ref fix
}
else
x = 0;
anin3 = x;
break;
}
case 3:
{
digitalWrite(A0, HIGH); // Need to set up mux for each A2D like the relays
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
a = SPI.transfer(0);
b = SPI.transfer(0);
c = SPI.transfer(0);
if (c == 13) // 13 good reading, 45 overrange, anything else A2D not ready
{
x = (unsigned int) a * 256;
x = x + b;
if (x >= 32767) // wrong ref fix
{ // wrong ref fix
x = x - 32767; // wrong ref fix
x = x * 2; // wrong ref fix
} // wrong ref fix
}
else
x = 0;
anin4 = x;
break;
}
default: ;
}
chan++;
if(chan > 3)
chan = 0;
Serial.print(anin1);
Serial.print(",");
Serial.print(anin2);
Serial.print(",");
Serial.print(anin3);
Serial.print(",");
Serial.print(anin4);
Serial.print("\n");
timers[TIMREADA2D] = 1000;
}
void ReadDigitals(void)
{
if (timers[TIMREADDIGS])
return;
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
if (digitalRead(A3))
digin1 = 0;
else
digin1 = 1;
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
if (digitalRead(A3))
digin2 = 0;
else
digin2 = 1;
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
if (digitalRead(A3))
digin3 = 0;
else
digin3 = 1;
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
if (digitalRead(A3))
digin4 = 0;
else
digin4 = 1;
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
if (digitalRead(A3))
digin5 = 0;
else
digin5 = 1;
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
if (digitalRead(A3))
digin6 = 0;
else
digin6 = 1;
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
if (digitalRead(A3))
digin7 = 0;
else
digin7 = 1;
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
if (digitalRead(A3))
digin8 = 0;
else
digin8 = 1;
timers[TIMREADDIGS] = 500;
Serial.print(digin1);
Serial.print(", ");
Serial.print(digin2);
Serial.print(", ");
Serial.print(digin3);
Serial.print(", ");
Serial.print(digin4);
Serial.print(", ");
Serial.print(digin5);
Serial.print(", ");
Serial.print(digin6);
Serial.print(", ");
Serial.print(digin7);
Serial.print(", ");
Serial.print(digin8);
Serial.print(", ");
Serial.print("\n");
}
void loop() // the loop function runs over and over again forever
{
AnalogIn();
ReadDigitals();
LEDs();
Timers();
Relays();
AnalogOut();
anout1++;
if (timers[TIMCLICKRELAY] == 0)
{
if (relay1)
{
relay1 = 0;
relay2 = 1;
relay3 = 0;
relay4 = 1;
relay5 = 0;
relay6 = 1;
redled = 1;
greenled = 0;
}
else
{
relay1 = 1;
relay2 = 0;
relay3 = 1;
relay4 = 0;
relay5 = 1;
relay6 = 0;
redled = 0;
greenled = 1;
}
timers[TIMCLICKRELAY] = 5000;
}
}

138
RaspberryPi/boardtest.py Normal file
View File

@@ -0,0 +1,138 @@
"""Test the IO Board."""
import mraa
import time
relay = []
for x in range(0, 6):
pin = mraa.Gpio(x)
pin.dir(mraa.DIR_OUT)
relay.append(pin)
pin.write(0)
pinRedLED = mraa.Gpio(8)
pinRedLED.dir(mraa.DIR_OUT)
pinGreenLED = mraa.Gpio(9)
pinGreenLED.dir(mraa.DIR_OUT)
input_mux = [
[],
[0, 0, 0], # Input 1
[1, 0, 0], # Input 2
[0, 1, 0], # Input 3
[1, 1, 0], # Input 4
[0, 0, 1], # Input 5
[1, 0, 1], # Input 6
[0, 1, 1], # Input 7
[1, 1, 1], # Input 8
]
pinA0 = mraa.Gpio(14)
pinA0.dir(mraa.DIR_OUT)
pinA1 = mraa.Gpio(15)
pinA1.dir(mraa.DIR_OUT)
pinA2 = mraa.Gpio(16)
pinA2.dir(mraa.DIR_OUT)
pinA3 = mraa.Gpio(17)
pinA3.dir(mraa.DIR_IN)
spi_bus = mraa.Spi(5)
print(spi_bus.lsbmode(False))
def red_LED(out):
"""Set the Red LED to the specified output."""
if out:
pinRedLED.write(1)
else:
pinRedLED.write(0)
def green_LED(out):
"""Set the Green LED to the specified output."""
if out:
pinGreenLED.write(1)
else:
pinGreenLED.write(0)
def relay_loop():
"""Loop through all relays."""
loops = 0
while loops < 10:
for p in relay:
p.write(1)
time.sleep(1)
p.write(0)
loops += 1
def read_dig_in(io_port):
"""Read IO Board digital input."""
if io_port == 0 or io_port > 8:
print("CANNOT READ A PORT THAT DOES NOT EXIST: {}".format(io_port))
return False
pin_values = input_mux[io_port]
pinA0.write(pin_values[0])
pinA1.write(pin_values[1])
pinA2.write(pin_values[2])
return pinA3.read() == 0
def dig_in_loop():
"""Read all digital inputs."""
for i in range(1, 9):
print("DIGIN {} = {}".format(i, read_dig_in(i)))
def led_flash(loops):
"""Flash the LED's from red to green."""
led_loops = 0
while led_loops < loops:
red_LED(True)
green_LED(False)
time.sleep(0.25)
red_LED(False)
green_LED(True)
time.sleep(0.25)
led_loops += 1
red_LED(False)
green_LED(False)
def read_analog_in(io_port):
"""Read IO Board digital input."""
global spi_bus
if io_port < 1 or io_port > 4:
print("CANNOT READ A PORT THAT DOES NOT EXIST: {}".format(io_port))
return False
pin_values = input_mux[io_port]
pinA0.write(pin_values[0])
pinA1.write(pin_values[1])
pinA2.write(pin_values[2])
ok_status = False
while ok_status is False:
pinA0.write(pin_values[0])
pinA1.write(pin_values[1])
pinA2.write(pin_values[2])
x = spi_bus.write(bytearray([0, 0, 0]))
print((x[0], x[1], x[2]))
c_bin = format(x[2], '#010b')[2:]
if c_bin[-3:] == "101":
ok_status = True
print("OK: {}, Status: {}".format(ok_status, c_bin))
print(x[0] * 256 + x[1])
time.sleep(10)
if __name__ == '__main__':
# led_flash(10)
print("Analog {}".format(1))
read_analog_in(1)

35
RaspberryPi/build.gradle Normal file
View File

@@ -0,0 +1,35 @@
/*
* This build file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* user guide available at https://docs.gradle.org/3.5/userguide/java_library_plugin.html
*/
// 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.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:21.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
compile group: 'io.mraa', name: 'mraa', version:'1.5.1'
compile group: 'com.pi4j', name: 'pi4j-core', version: '1.1'
}

View File

@@ -0,0 +1,2 @@
Manifest-Version: 1.0

View File

@@ -0,0 +1,6 @@
#Wed Jun 14 10:50:12 CDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip

127
RaspberryPi/pi_boardtest.py Normal file
View File

@@ -0,0 +1,127 @@
"""Test the IO Board using a Raspberry Pi."""
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
import time
import spidev
spi = spidev.SpiDev()
spi.open(0, 0)
GPIO.setmode(GPIO.BOARD)
if GPIO.getmode() == 11:
mux_channels = [5, 6, 13]
inp_channel = 19
relay_channels = [21, 20, 16, 12, 25, 24, 23, 18]
else:
mux_channels = [29, 31, 33]
inp_channel = 35
relay_channels = [40, 38, 36, 32, 22, 18, 16, 12]
GPIO.setup(mux_channels, GPIO.OUT)
GPIO.setup(relay_channels, GPIO.OUT)
GPIO.setup(inp_channel, GPIO.IN)
input_mux = [
[],
[0, 0, 0], # Input 1
[1, 0, 0], # Input 2
[0, 1, 0], # Input 3
[1, 1, 0], # Input 4
[0, 0, 1], # Input 5
[1, 0, 1], # Input 6
[0, 1, 1], # Input 7
[1, 1, 1], # Input 8
]
GPIO_mux = [
(),
(GPIO.LOW, GPIO.LOW, GPIO.LOW), # 1
(GPIO.HIGH, GPIO.LOW, GPIO.LOW), # 2
(GPIO.LOW, GPIO.HIGH, GPIO.LOW), # 3
(GPIO.HIGH, GPIO.HIGH, GPIO.LOW), # 4
(GPIO.LOW, GPIO.LOW, GPIO.HIGH), # 5
(GPIO.HIGH, GPIO.LOW, GPIO.HIGH), # 6
(GPIO.LOW, GPIO.HIGH, GPIO.HIGH), # 7
(GPIO.HIGH, GPIO.HIGH, GPIO.HIGH) # 8
]
def relay_write(relay_number, status):
"""Write the specified status to the relay at the relay_number."""
write_val = GPIO.HIGH if status >= 1 else GPIO.LOW
if relay_number <= 8 and relay_number >= 1:
GPIO.output(relay_channels[relay_number - 1], write_val)
return True
else:
print("CANNOT WRITE TO A RELAY THAT DOES NOT EXIST: {}".format(relay_number))
return False
def relay_loop():
"""Set all relays to 1 sequentially, then set all relays to 0 sequentially."""
for i in range(1, 9):
relay_write(i, 1)
time.sleep(1)
for i in range(1, 9):
relay_write(i, 0)
time.sleep(1)
def read_dig_in(io_port):
"""Read IO Board digital input."""
if io_port == 0 or io_port > 8:
print("CANNOT READ A PORT THAT DOES NOT EXIST: {}".format(io_port))
return False
GPIO.output(mux_channels, GPIO_mux[io_port])
return GPIO.input(inp_channel) == 0
def dig_in_loop():
"""Read all digital inputs."""
for i in range(1, 9):
print("DIGIN {} = {}".format(i, read_dig_in(i)))
def read_analog_in(io_port, verbose=False):
"""Read IO Board digital input."""
global spi_bus
if io_port < 1 or io_port > 4:
print("CANNOT READ A PORT THAT DOES NOT EXIST: {}".format(io_port))
return False
GPIO.output(mux_channels, GPIO_mux[io_port])
ok_status = False
tries = 0
while (ok_status is False) and (tries < 5):
GPIO.output(mux_channels, GPIO_mux[io_port])
x = spi.xfer2(bytearray([0, 0, 0]))
if verbose:
print((x[0], x[1], x[2]))
c_bin = format(x[2], '#010b')[2:]
if c_bin[-3:] == "101":
ok_status = True
if verbose:
print("OK: {}, Status: {}".format(ok_status, c_bin))
print(x[0] * 256 + x[1])
if ok_status:
return x[0] * 256 + x[1]
time.sleep(0.25)
tries += 1
if __name__ == '__main__':
# led_flash(10)
# relay_loop()
dig_in_loop()
for x in range(0, 10):
for i in range(1, 5):
print("Analog {}".format(i))
print(read_analog_in(i))
GPIO.cleanup()

View File

@@ -0,0 +1,18 @@
/*
* This settings file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
* In a single project build this file can be empty or even removed.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/3.5/userguide/multi_project_builds.html
*/
/*
// To declare projects as part of a multi-project build use the 'include' method
include 'shared'
include 'api'
include 'services:webservice'
*/
rootProject.name = 'IOBoard'

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;
}
}