diff --git a/src/main/java/com/pi4j/devices/ads1256/ADS1256.java b/src/main/java/com/pi4j/devices/ads1256/ADS1256.java index fad9c68..020b608 100644 --- a/src/main/java/com/pi4j/devices/ads1256/ADS1256.java +++ b/src/main/java/com/pi4j/devices/ads1256/ADS1256.java @@ -41,565 +41,25 @@ import com.pi4j.io.spi.SpiChipSelect; import com.pi4j.util.Console; +public final class ADS1256 extends AbstractADS125x { -public class ADS1256 { - - - enum MuxValue { - AIN0, - AIN1, - AIN2, - AIN3, - AIN4, - AIN5, - AIN6, - AIN7, - AINCOM - } - - enum ADS1256_GAIN { - ADS1256_GAIN_1, //= 0, /* GAIN 1 */ - ADS1256_GAIN_2, // = 1, /*GAIN 2 */ - ADS1256_GAIN_4, // = 2, /*GAIN 4 */ - ADS1256_GAIN_8, // = 3, /*GAIN 8 */ - ADS1256_GAIN_16, // = 4, /* GAIN 16 */ - ADS1256_GAIN_32, // = 5, /*GAIN 32 */ - ADS1256_GAIN_64, // = 6, /*GAIN 64 */ - } - - enum ADS1256_DRATE { - ADS1256_30000SPS, - ADS1256_15000SPS, - ADS1256_7500SPS, - ADS1256_3750SPS, - ADS1256_2000SPS, - ADS1256_1000SPS, - ADS1256_500SPS, - ADS1256_100SPS, - ADS1256_60SPS, - ADS1256_50SPS, - ADS1256_30SPS, - ADS1256_25SPS, - ADS1256_15SPS, - ADS1256_10SPS, - ADS1256_5SPS, - ADS1256_2d5SPS, - ADS1256_DRATE_MAX + @SuppressWarnings("exports") + public ADS1256(Context pi4j, SpiBus spiBus, SpiChipSelect chipSelect, + boolean reset, int drdyPin, int csPin, int rstPin, + boolean crtRstGpio, int pdwnPin, boolean crtPdwnGpio, + Console console, String traceLevel, double vref, DataRate sampleRate, boolean useBuffer) + throws InterruptedException { + super(pi4j, spiBus, chipSelect, reset, drdyPin, csPin, rstPin, crtRstGpio, + pdwnPin, crtPdwnGpio, console, traceLevel, vref, sampleRate, useBuffer); } - - static final int[] ADS1256_DRATE_E = - { - 0xF0, /*reset the default values */ - 0xE0, - 0xD0, - 0xC0, - 0xB0, - 0xA1, - 0x92, - 0x82, - 0x72, - 0x63, - 0x53, - 0x43, - 0x33, - 0x23, - 0x13, - 0x03 - }; - - /** - * See ADS1256App.java help text to explain these parms - * - * @param pi4j - * @param spiBus - * @param chipSelect - * @param reset - * @param drdyPin - * @param csPin - * @param rstPin - * @param crtRstGpio - * @param pdwnPin - * @param crtPdwnGpio - * @param console - * @param traceLevel - * @param vref - * @throws InterruptedException - */ - public ADS1256(Context pi4j, SpiBus spiBus, SpiChipSelect chipSelect, boolean reset, int drdyPin, int csPin, int rstPin, boolean crtRstGpio, int pdwnPin, boolean crtPdwnGpio, Console console, String traceLevel, double vref) throws InterruptedException { - super(); - this.console = console; - this.pi4j = pi4j; - this.chipSelect = chipSelect; - this.resetChip = reset; - this.spiBus = spiBus; - this.csPinNum = csPin; - this.rstPinNum = rstPin; - this.crtRstGpio = crtRstGpio; - this.drdyPinNum = drdyPin; - this.pdwnPinNum = pdwnPin; - this.crtPdwnGpio = crtPdwnGpio; - this.traceLevel = traceLevel; - - this.vref = vref; - // "trace", "debug", "info", "warn", "error" or "off"). If not specified, defaults to "info" - // must fully qualify logger as others exist and the slf4 code will use the first it - // encounters if using the defaultLogLevel - System.setProperty("org.slf4j.simpleLogger.log." + ADS1256.class.getName(), this.traceLevel); - this.logger = LoggerFactory.getLogger(ADS1256.class); - this.init(); - + @Override + public int getNumGpioPins() { + return 4; } - - /** - * Creates the SPI and requested GPIOs. - * Do reset if so requested by app - * Configure chip Gain and speed. - * - * @throws InterruptedException - */ - private void init() throws InterruptedException { - var spiConfig = Spi.newConfigBuilder(pi4j) - .id("SPI" + this.spiBus + " " + this.chipSelect) - .name("A/D converter") - .bus(this.spiBus) - .chipSelect(this.chipSelect) - .baud(976563) //Spi.DEFAULT_BAUD) - .mode(SpiMode.MODE_1) - .provider("linuxfs-spi") - .build(); - this.spi = this.pi4j.create(spiConfig); - - // required all configs - var inputConfig1 = DigitalInput.newConfigBuilder(pi4j) - .id("DRDY_pin") - .name("DRDY") - .address(this.drdyPinNum) - .provider("gpiod-digital-input"); // .pull(PullResistance.PULL_UP) - try { - this.drdyGpio = pi4j.create(inputConfig1); - } catch (Exception e) { - e.printStackTrace(); - console.println("create DigIn DRDY failed"); - System.exit(201); - } - // required all configs - var outputConfig2 = DigitalOutput.newConfigBuilder(pi4j) - .id("CS_pin") - .name("CS") - .address(this.csPinNum) - .shutdown(DigitalState.HIGH) - .initial(DigitalState.HIGH) - .provider("gpiod-digital-output"); - try { - this.csGpio = pi4j.create(outputConfig2); - } catch (Exception e) { - e.printStackTrace(); - console.println("create DigOut DRDY failed"); - System.exit(202); - } - // not always required, see README - if (this.crtRstGpio) { - var outputConfig3 = DigitalOutput.newConfigBuilder(pi4j) - .id("RST_pin") - .name("RESET") - .address(this.rstPinNum) - .shutdown(DigitalState.HIGH) - .initial(DigitalState.HIGH) - .provider("gpiod-digital-output"); - try { - this.rstGpio = pi4j.create(outputConfig3); - } catch (Exception e) { - e.printStackTrace(); - console.println("create DigOut RESET failed"); - System.exit(203); - } - } else { - this.logger.trace("RESET Gpio not requested/created"); - } - - // not always required, see README - if (this.crtPdwnGpio) { - var outputConfig4 = DigitalOutput.newConfigBuilder(pi4j) - .id("PDWN_pin") - .name("PDWN") - .address(this.pdwnPinNum) - .shutdown(DigitalState.HIGH) - .initial(DigitalState.HIGH) - .provider("gpiod-digital-output"); - try { - this.pdwnGpio = pi4j.create(outputConfig4); - } catch (Exception e) { - e.printStackTrace(); - console.println("create DigOut PDWN failed"); - System.exit(203); - } - } else { - this.logger.trace("PDWN Gpio not requested/created"); - } - - - if (this.resetChip) { - this.doReset(); - } - - - this.ADS1256_ConfigADC("ADS1256_GAIN_1", "ADS1256_10SPS"); - - - } - - /** - * Validate chip ID = 3, if not program will exist - * - * @return chip ID - * @throws InterruptedException - */ - public int validateChipID() throws InterruptedException { - // print program title/header - int id = 0; - this.logger.trace(">>> Enter displayProgramID"); - console.title("<-- The Pi4J Project -->", "SPI test program using ADS1256 AtoD Chip"); - this.waitForDrdyLow(); - id = this.readRegData(ADS1256_Declares.REG_STATUS) >> 4; - if (id != ADS1256_Declares.CHIP_ID) { - console.println("Incorrect chip ID : " + id); - System.exit(301); - } - this.logger.trace("<<< Exit displayProgramID : " + id); - return (id); - } - - - /** - * Set chip Gain and speed - * - * @param gain - * @param drate - * @throws InterruptedException - */ - private void ADS1256_ConfigADC(String gain, String drate) throws InterruptedException { - this.waitForDrdyLow(); - byte status = this.readRegData(ADS1256_Declares.REG_STATUS); - int[] buf = {0, 0, 0, 0}; - // preserve DRDY bit state and set ACAL bit - buf[0] = (status & ADS1256_Declares.STATUS_DRDY_MASK) | ADS1256_Declares.STATUS_ACAL; //STATUS_REG - buf[1] = (byte) (0b00000000 | (((0 & 0xf) << 4)) | 8); // MUX_REG initial: AIN0/AINCOM - buf[2] = (0 << 5) | (0 << 3) | (this.mapGainString(gain) << 0); // ADCON_REG CLK/SENSOR off - buf[3] = this.mapDrateString(drate); // DRATE_REG - this.csGpio.low(); - busyWaitMS(2); - this.spi.write(ADS1256_Declares.WREG | 0); - busyWaitMS(2); - this.spi.write(0x03); // writing 4 bytes data - busyWaitMS(2); - this.spi.write(buf[0]); - busyWaitMS(2); - this.spi.write(buf[1]); - busyWaitMS(2); - this.spi.write(buf[2]); - busyWaitMS(2); - this.spi.write(buf[3]); - busyWaitMS(2); - this.csGpio.high(); - busyWaitMS(1); - } - - /** - * Debug usage, display state of DRDY InputGpio - */ - private void showDrdyState() { - this.logger.trace("showDrdyState "); - if (this.drdyGpio.state() == DigitalState.HIGH) { - this.logger.trace("DRDY state HIGH"); - } else if (this.drdyGpio.state() == DigitalState.LOW) { - this.logger.trace("DRDY state LOW"); - } else { - this.logger.trace("DRDY state = " + this.drdyGpio.state()); - } - } - - /** - * If the RESET Gpio was configured reset dhip via Gpio, else use commands - * - * @throws InterruptedException - */ - public void doReset() throws InterruptedException { - this.logger.trace(">>> Enter doReset"); - if (this.rstGpio != null) { - this.rstGpio.high(); - busyWaitMS(200); - this.rstGpio.low(); - busyWaitMS(200); - this.rstGpio.high(); - } else { - this.writeCmd(ADS1256_Declares.RESET); - busyWaitMS(200); - } - this.logger.trace("<<< Exit doReset"); - } - - private void writeCmd(int cmd) { - this.logger.trace(">>> Enter writeCmd cmd " + cmd); - this.csGpio.low(); - busyWaitMS(2); - this.spi.write(cmd); - busyWaitMS(2); - this.csGpio.high(); - this.logger.trace("<<< Exit writeCmd"); - } - - - private void writeReg(int reg, int data) { - this.logger.trace(">>> Enter writeReg reg : " + reg + " data " + String.format("0X%02x: ", data & 0xff)); - this.csGpio.low(); - busyWaitMS(2); - byte[] buf = {0, 0, 0}; - buf[0] = (byte) (ADS1256_Declares.WREG | reg); - buf[1] = 0x00; - buf[2] = (byte) data; - this.spi.write(buf); - - busyWaitMS(2); - this.csGpio.high(); - this.logger.trace("<<< Exit writeReg"); - } - - private byte readRegData(int reg) { - this.logger.trace(">>> Enter readReg register# : " + reg); - byte rval = 42; - this.csGpio.low(); - busyWaitMS(2); - byte[] buf = new byte[4097] ; //{0, 0, 0}; - buf[0] = (byte) (ADS1256_Declares.RREG | reg); - buf[1] = 0x00; - this.spi.write(buf); - busyWaitMS(2); - rval = this.spi.readByte(); - busyWaitMS(2); - this.csGpio.high(); - busyWaitMS(200); // let chip quiet - this.logger.trace("<<< Exit readReg : " + String.format("0X%02x: ", rval & 0xff)); - return (byte) (rval & 0xff); - } - - private static void busyWaitNano(long nanos) { - long waitUntil = System.nanoTime() + (nanos); - while (waitUntil > System.nanoTime()) { - } - } - - private static void busyWaitMicros(long micros) { - long waitUntil = System.nanoTime() + (micros * 1000); - while (waitUntil > System.nanoTime()) { - } - } - - private static void busyWaitMS(long ms) { - long waitUntil = System.nanoTime() + (ms * 1000000); - while (waitUntil > System.nanoTime()) { - } - } - - /** - * Name(s) supplied by app. These are mapped to integer values - * - * @param name - * @return - */ - private short mapMuxString(String name) { - MuxValue[] muxMap = MuxValue.values(); - int posPin = 0xff; - for (MuxValue col : muxMap) { - // Calling ordinal() to find index - // of pin name - if (col.toString().contentEquals(name)) { - posPin = col.ordinal(); - this.logger.trace(" pname : " + name + " No : " + posPin); - break; - } - } - return (short) (posPin & 0xff); - } - - /** - * Name pairs pChannel and nChannel written into chips MUX register - * - * @param pChannel - * @param nChannel - * @throws IOException - * @throws InterruptedException - */ - private void mapMux(short pChannel, short nChannel) throws IOException, InterruptedException { - this.logger.trace(">>> Enter mapMux channel : " + pChannel + "/" + nChannel); - - - // create a data buffer and initialize a conversion request payload - byte[] muxData = new byte[]{(byte) ADS1256_Declares.WREG | ADS1256_Declares.REG_MUX, // 5H/register offset - (byte) (0b00000000), // number regs to write -1 - (byte) (0b00000000 | (((pChannel & 0xf) << 4)) | nChannel), // channel +, channel AINCOM - }; - Thread.sleep(2000); - byte[] muxValue = new byte[4]; - this.writeReg(ADS1256_Declares.REG_MUX, (((pChannel & 0xf) << 4) | nChannel)); - this.logger.trace("<<< Exit mapMux "); - - } - - - /** - * One-shot get the RDATA for ppName and pnName, then return the value - * - * @param ppName - * @param pnName - * @return - * @throws InterruptedException - * @throws IOException - */ - public double getADS1256State(String ppName, String pnName) throws InterruptedException, IOException { - - short pChannel = this.mapMuxString(ppName); - short nChannel = this.mapMuxString(pnName); - // allow for user to exit program using CTRL-C - this.logger.trace(">>> Enter getADS1256State channel : " + pChannel + "/" + nChannel); - // continue running program until user exits using CTRL-C - double rval = read(pChannel, nChannel); - this.logger.trace("<<< Exit getADS1256State: channel :" + pChannel + "/" + nChannel + " value :" + rval); - return (rval); - } - - /** - * Will repeatedly get the RDATA for ppName and pnName, until user ctrl-C - * - * @param ppName - * @param pnName - * @throws InterruptedException - * @throws IOException - */ - public void displayADS1256State(String ppName, String pnName) throws InterruptedException, IOException { - - short pChannel = this.mapMuxString(ppName); - short nChannel = this.mapMuxString(pnName); - // allow for user to exit program using CTRL-C - this.console.promptForExit(); - this.logger.trace(">>> Enter displayADS1256State channel : " + pChannel + "/" + nChannel); - // continue running program until user exits using CTRL-C - while (this.console.isRunning()) { - read(pChannel, nChannel); - Thread.sleep(1000); - } - console.emptyLine(); - this.logger.trace("<<< Exit displayADS1256State"); - } - - /** - * Read data via SPI bus from ADS1256 chip. - * - * @throws IOException - */ - public double read(short pChannel, short nChannel) throws IOException, InterruptedException { - this.logger.trace(">>> Enter read "); - // see if chip ready for additional commands - this.waitForDrdyLow(); - double conversion_value = this.getConversionValue(pChannel, nChannel); - - this.logger.trace(" |\r"); - this.logger.trace("<<< Exit read"); - return (conversion_value); - } - - - /** - * Polling for controlled number of times testing for - * DRDY Gpio being DigitalState.LOW, return true, else false - * - * @return - * @throws InterruptedException - */ - private boolean waitForDrdyLow() throws InterruptedException { - boolean rval = false; - long i = 0; - for (i = 0; i < 4000000; i++) { - if (this.drdyGpio.state() == DigitalState.LOW) { - rval = true; - break; - } else { - this.logger.trace("Not desired State counter " + i); - } - } - if (i >= 4000000) { - this.logger.trace("waitForDrdyLow Time Out ...\r\n"); - } - //this.logger.trace(" waitForDrdyLow " + rval); - return (rval); - } - - - /** - * Communicate to the ADC chip via SPI to get single-ended conversion value - * for a specified channel. - * Issue SYNC and WAKEUP commands so chip will calculate the - * digital value for pChannel nChannel - * - * @param pChannel analog input channel on ADC chip - * @param nChannel analog input channel on ADC chip - * @return conversion value for specified analog input channel - * @throws IOException - */ - public int getConversionValue(short pChannel, short nChannel) throws IOException, InterruptedException { - this.logger.trace(">>> Enter getConversionValue channel : " + pChannel + "/" + nChannel); - - - this.mapMux(pChannel, nChannel); - busyWaitMS(2); - - this.writeCmd(ADS1256_Declares.SYNC); - busyWaitMS(2); - this.writeCmd(ADS1256_Declares.WAKEUP); - busyWaitMS(2); - int value = this.doRDATA(); - busyWaitMS(2); - - this.logger.info("Channel :" + pChannel + "/" + nChannel + " value :" + value); //String.format(" | %06f", value)); // print - if (this.vref > 0) { - this.logger.info("A/D read input voltage : " + ((value * this.vref) / 0x7fffff + " \n")); - } - - this.logger.trace("<<< Exit getConversionValue "); - return value; - } - - /** - * REtrieve RDATA from chip - * - * @return - * @throws InterruptedException - */ - private int doRDATA() throws InterruptedException { - this.logger.trace(">>> Enter doRDATA "); - - int read = 0; - byte[] buf = {0, 0, 0}; - - this.waitForDrdyLow(); - busyWaitMS(1); - - this.csGpio.low(); - busyWaitMS(2); - this.spi.write(ADS1256_Declares.RDATA); - busyWaitMS(4); - this.spi.read(buf); - this.csGpio.high(); - read = (buf[0] << 16) & 0x00FF0000; - read |= (buf[1] << 8) & 0x0000FF00; - read |= buf[2] & 0x000000FF; - read &= 0x00ffffff; - //printf("%d %d %d \r\n",buf[0],buf[1],buf[2]); - if (read >= 0x800000) { - read -= 0x1000000; // negative value - } - this.logger.trace("<<< Exit doRDATA "); - return read; - + @Override + public int getNumAnalogPins() { + return 8; } }//end ADS1256 + diff --git a/src/main/java/com/pi4j/devices/ads1256/ADS1256App.java b/src/main/java/com/pi4j/devices/ads1256/ADS1256App.java index d51d7c5..2e98329 100644 --- a/src/main/java/com/pi4j/devices/ads1256/ADS1256App.java +++ b/src/main/java/com/pi4j/devices/ads1256/ADS1256App.java @@ -47,28 +47,39 @@ import com.pi4j.util.Console; public class ADS1256App { + public static enum PinTrackingConflictHandler { + printAndExit, + printAndRecover, + silentAndRecover + } public static void main(String[] args) throws InterruptedException, IOException { - var console = new Console(); Context pi4j = Pi4J.newAutoContext(); - double vref = 0; + double vref = 2.5; String ppName = "AINCOM"; String pnName = "AINCOM"; boolean crtRestart = false; boolean crtPdwn = false; - int drdyPin = 42; - int csPin = 42; - int rsrtPin = 42; - int pdwnPin = 42; + int drdyPin = 17; + int csPin = 22; + int rsrtPin = 18; + int pdwnPin = 27; boolean resetChip = false; SpiChipSelect chipSelect = SpiChipSelect.CS_0; SpiBus spiBus = SpiBus.BUS_0; DigitalState newState = DigitalState.UNKNOWN; boolean setPinState = false; boolean readPin = false; + boolean dumpInputStates = false; + boolean dumpRepeatedly = false; + boolean runChaser = false; + int chaseIntervalMS = 2000; + boolean useBuffer = false; + DataRate sampleRate = DataRate.SPS_10; + boolean useVoting = false; int gpiopPin = 42; @@ -78,7 +89,11 @@ public static void main(String[] args) throws InterruptedException, IOException " -pp -pn AIN0 AIN1 AIN2 AIN3 AIN4 AIN5 AIN6 AIN7 AINCOM -x reset \n" + " -p gpio pin number -rp read \"-p\" -sp set state \"-p\" HIGH/LOW \n" + " -s HEX value SPI # -t trace values : \"trace\", \"debug\", \"info\", \"warn\", \"error\" \n " + - " or \"off\" Default \"info\""; + " or \"off\" Default \"info\"\n" + + " -di dump all input states as quickly as possible. -dr Dump repeatedly at 1/sec. \n" + + " -chase INTERVAL_MS turn one GPIO on at a time, from 0 upward and repeat\n" + + " -sps sample rate/sec: 2.5, 5, 10, 15, 25, 30, 50, 60, 100, 500 (default 10SPS)\n" + + " -buf Enable analog input buffer (default off) -vo Use 3-way voting/median"; String traceLevel = "info"; for (int i = 0; i < args.length; i++) { @@ -99,6 +114,7 @@ public static void main(String[] args) throws InterruptedException, IOException readPin = true; } else if (o.contentEquals("-sp")) { // set state of -p String a = args[i + 1]; + a = a.toUpperCase(); i++; if (a.contentEquals("HIGH")) { newState = DigitalState.HIGH; @@ -107,7 +123,7 @@ public static void main(String[] args) throws InterruptedException, IOException newState = DigitalState.LOW; setPinState = true; } else { - console.println("Seting pin state invalid : " + a); + console.println("Attempted to set invalid pin state: " + a); System.exit(44); } } else if (o.contentEquals("-rst")) { // device address @@ -138,7 +154,7 @@ public static void main(String[] args) throws InterruptedException, IOException String a = args[i + 1]; spiBus = SpiBus.getByNumber(Short.parseShort(a.substring(2), 16)); i++; - } else if (o.contentEquals("-t")) { // device address + } else if (o.contentEquals("-t")) { // trace level String a = args[i + 1]; i++; traceLevel = a; @@ -157,7 +173,7 @@ public static void main(String[] args) throws InterruptedException, IOException console.println("-pp invalid : " + ppName); System.exit(42); } - } else if (o.contentEquals("-pn")) { // pin positive + } else if (o.contentEquals("-pn")) { // pin negative String a = args[i + 1]; i++; pnName = a; @@ -166,236 +182,109 @@ public static void main(String[] args) throws InterruptedException, IOException console.println("-pn invalid : " + pnName); System.exit(43); } + } else if (o.contentEquals("-chase")) { + runChaser = true; + try { + chaseIntervalMS = Integer.parseInt(args[i + 1]); + i++; + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + }//Assume interval not supplied } else if (o.contentEquals("-h")) { console.println(helpString); System.exit(44); + } else if (o.contentEquals("-di")) { + dumpInputStates = true; + } else if (o.contentEquals("-buf")) { + useBuffer = true; + } else if (o.contentEquals("-dr")) { + dumpRepeatedly = true; + } else if (o.contentEquals("-vo")) { + useVoting = true; + } else if (o.contentEquals("-sps")) { // samples per second + String a = args[i + 1]; + i++; + a = ("SPS_" + a.replace("d", ".")).strip(); + try { + sampleRate = DataRate.valueOf(a); + } catch (IllegalArgumentException e) { + System.out.println("Error parsing SPS argument:"); + e.printStackTrace(); + System.exit(44); + } } else { console.println(" !!! Invalid Parm " + o); console.println(helpString); System.exit(45); - } - } - var console = new Console(); - Context pi4j = Pi4J.newAutoContext(); - - - double vref = 2.5; - String ppName = "AINCOM"; - String pnName = "AINCOM"; + }//end default conditional branch + }//end for(i) - boolean crtRestart = false; - boolean crtPdwn = false; - int drdyPin = 17; - int csPin = 22; - int rsrtPin = 18; - int pdwnPin = 27; - boolean resetChip = false; - SpiChipSelect chipSelect = SpiChipSelect.CS_0; - SpiBus spiBus = SpiBus.BUS_0; - DigitalState newState = DigitalState.UNKNOWN; - boolean setPinState = false; - boolean readPin = false; - boolean dumpInputStates = false; - boolean dumpRepeatedly = false; - boolean runChaser = false; - int chaseIntervalMS = 2000; - boolean useBuffer = false; - DataRate sampleRate = DataRate.SPS_10; - boolean useVoting = false; + console.println("----------------------------------------------------------"); + console.println("PI4J PROVIDERS"); + console.println("----------------------------------------------------------"); + pi4j.providers().describe().print(System.out); + System.out.println("----------------------------------------------------------"); - int gpiopPin = 42; + ADS125x ads = new ADS1256(pi4j, spiBus, chipSelect, resetChip, drdyPin, csPin, rsrtPin, crtRestart, pdwnPin, crtPdwn, console, traceLevel, vref, sampleRate, useBuffer); + if (useVoting) + ads = new VotingADS125x(ads, 3); - console.title("<-- The Pi4J V2 Project Extension -->", "ADS1256App"); - String helpString = " parms: -vref decimal reference voltage \n" + - "-rst resetPin -cs chipSelectPin -drdy drdyPin -pdwn syn/pwrdPin \n" + - " -pp -pn AIN0 AIN1 AIN2 AIN3 AIN4 AIN5 AIN6 AIN7 AINCOM -x reset \n" + - " -p gpio pin number -rp read \"-p\" -sp set state \"-p\" HIGH/LOW \n" + - " -s HEX value SPI # -t trace values : \"trace\", \"debug\", \"info\", \"warn\", \"error\" \n " + - " or \"off\" Default \"info\"\n"+ - " -di dump all input states as quickly as possible. -dr Dump repeatedly at 1/sec. \n" + - " -chase INTERVAL_MS turn one GPIO on at a time, from 0 upward and repeat\n" + - " -sps sample rate/sec: 2.5, 5, 10, 15, 25, 30, 50, 60, 100, 500 (default 10SPS)\n" + - " -buf Enable analog input buffer (default off) -vo Use 3-way voting/median"; - - String traceLevel = "info"; - for (int i = 0; i < args.length; i++) { - String o = args[i]; - if (o.contentEquals("-vref")) { // reference voltage - String a = args[i + 1]; - i++; - vref = Float.parseFloat(a); - } else if (o.contentEquals("-p")) { // pin number - String a = args[i + 1]; - i++; - gpiopPin = Integer.parseInt(a); - if (gpiopPin > 3) { - console.println("Invalid GPIO pin number : " + gpiopPin); - System.exit(45); - } - } else if (o.contentEquals("-rp")) { // read -p - readPin = true; - } else if (o.contentEquals("-sp")) { // set state of -p - String a = args[i + 1]; - a = a.toUpperCase(); - i++; - if (a.contentEquals("HIGH")) { - newState = DigitalState.HIGH; - setPinState = true; - } else if (a.contentEquals("LOW")) { - newState = DigitalState.LOW; - setPinState = true; - } else { - console.println("Attempted to set invalid pin state: " + a); - System.exit(44); - } - } else if (o.contentEquals("-rst")) { // device address - String a = args[i + 1]; - i++; - rsrtPin = Integer.parseInt(a); - crtRestart = true; - } else if (o.contentEquals("-cs")) { // device address - String a = args[i + 1]; - i++; - csPin = Integer.parseInt(a); - } else if (o.contentEquals("-drdy")) { // device address - String a = args[i + 1]; - i++; - drdyPin = Integer.parseInt(a); - } else if (o.contentEquals("-pdwn")) { // device address - String a = args[i + 1]; - i++; - pdwnPin = Integer.parseInt(a); - crtPdwn = true; - } else if (o.contentEquals("-cNotUsed")) { - String a = args[i + 1]; - chipSelect = SpiChipSelect.getByNumber(Short.parseShort(a.substring(2), 16)); - i++; - } else if (o.contentEquals("-x")) { - resetChip = true; - } else if (o.contentEquals("-s")) { - String a = args[i + 1]; - spiBus = SpiBus.getByNumber(Short.parseShort(a.substring(2), 16)); - i++; - } else if (o.contentEquals("-t")) { // trace level - String a = args[i + 1]; - i++; - traceLevel = a; - if (a.contentEquals("trace") | a.contentEquals("debug") | a.contentEquals("info") | a.contentEquals("warn") | a.contentEquals("error") | a.contentEquals("off")) { - console.println("Changing trace level to : " + traceLevel); - } else { - console.println("Changing trace level invalid : " + traceLevel); - System.exit(41); - } - } else if (o.contentEquals("-pp")) { // pin positive - String a = args[i + 1]; - i++; - ppName = a; - if (a.contentEquals("AIN0") | a.contentEquals("AIN1") | a.contentEquals("AIN2") | a.contentEquals("AIN3") | a.contentEquals("AIN4") | a.contentEquals("AIN5") | a.contentEquals("AIN6") | a.contentEquals("AIN7") | a.contentEquals("AINCOM")) { - } else { - console.println("-pp invalid : " + ppName); - System.exit(42); - } - } else if (o.contentEquals("-pn")) { // pin negative - String a = args[i + 1]; - i++; - pnName = a; - if (a.contentEquals("AIN0") | a.contentEquals("AIN1") | a.contentEquals("AIN2") | a.contentEquals("AIN3") | a.contentEquals("AIN4") | a.contentEquals("AIN5") | a.contentEquals("AIN6") | a.contentEquals("AIN7") | a.contentEquals("AINCOM")) { - } else { - console.println("-pn invalid : " + pnName); - System.exit(43); - } - } else if(o.contentEquals("-chase")) { - runChaser = true; - try {chaseIntervalMS = Integer.parseInt(args[i+1]);i++;} - catch(NumberFormatException | ArrayIndexOutOfBoundsException e) {}//Assume interval not supplied - } else if (o.contentEquals("-h")) { - console.println(helpString); - System.exit(44); - } else if (o.contentEquals("-di")) { - dumpInputStates = true; - } else if (o.contentEquals("-buf")) { - useBuffer = true; - } else if (o.contentEquals("-dr")) { - dumpRepeatedly = true; - } else if (o.contentEquals("-vo")) { - useVoting = true; - } else if (o.contentEquals("-sps")) { // samples per second - String a = args[i + 1]; - i++; - a = ("SPS_"+a.replace("d", ".")).strip(); - try {sampleRate = DataRate.valueOf(a);} - catch(IllegalArgumentException e) { - System.out.println("Error parsing SPS argument:"); - e.printStackTrace(); - System.exit(44); - } - } else { - console.println(" !!! Invalid Parm " + o); - console.println(helpString); - System.exit(45); - }//end default conditional branch - }//end for(i) - - console.println("----------------------------------------------------------"); - console.println("PI4J PROVIDERS"); - console.println("----------------------------------------------------------"); - pi4j.providers().describe().print(System.out); - System.out.println("----------------------------------------------------------"); - - ADS125x ads = new ADS1256(pi4j, spiBus, chipSelect, resetChip, drdyPin, csPin, rsrtPin, crtRestart, pdwnPin, crtPdwn, console, traceLevel,vref, sampleRate, useBuffer); - if(useVoting) - ads = new VotingADS125x(ads,3); - - Thread.sleep(100);//Let settle before talking - Integer chipID = ads.validateChipID(); - if(chipID != null) { - System.out.println("Invalid chip ID: "+chipID); - System.exit(301); - } + Thread.sleep(100);//Let settle before talking + Integer chipID = ads.validateChipID(); + if (chipID != null) { + System.out.println("Invalid chip ID: " + chipID); + System.exit(301); + } - if (readPin) { - ads.setGpioDirIn(gpiopPin); - console.println(" pin " + gpiopPin + " state : " + (ads.isGpioHigh(gpiopPin)?"HIGH":"LOW")); - System.exit(0); - } else if (setPinState) { - ads.setGpioDirOut(gpiopPin); - ads.setGpio(gpiopPin, newState.isHigh()); - System.exit(0); - } else if (dumpInputStates) { - final AllInputPrinter ip = new AllInputPrinter(ads); - ip.setPrintingUnits(PrintingUnits.VOLTS); - System.out.print("\n\n\n\n\n\n\n\n\n\n\n"); - System.out.println("Press ENTER to end"); - try { - while(System.in.available() == 0 || System.in.read() != '\n') { - System.out.print("\033[10A");//Go back up 10 lines to rewrite the output - ip.printADSXXXInputStates(); - } - } catch(java.io.IOException e) {e.printStackTrace();} - System.exit(0); - } else if(runChaser) { - final GPIOChaser chaser = new GPIOChaser(ads); - System.out.println("Press ENTER to end"); - try { - while(System.in.available() == 0 || System.in.read() != '\n') { - chaser.updatePrintAndIterate(); - Thread.sleep(chaseIntervalMS); - }//end while(!enter) - } catch(java.io.IOException e) {e.printStackTrace();} - System.exit(0); - } else { - System.out.println("Press ENTER to end"); - int rtn = ads.readAnalogDifferential(MuxValue.valueOf(ppName).ordinal(), MuxValue.valueOf(pnName).ordinal()); - System.out.println("readAnalogDifferential() returned : channel :" + ppName + "/" + pnName + " value :" + rtn); - try { - while(dumpRepeatedly & (System.in.available() == 0 || System.in.read() != '\n')) { - System.out.print("\033[1A");//Go back up 1 line to rewite the output - rtn = ads.readAnalogDifferential(MuxValue.valueOf(ppName).ordinal(), MuxValue.valueOf(pnName).ordinal()); - System.out.println("readAnalogDifferential() returned : channel :" + ppName + "/" + pnName + " value :" + rtn); - Thread.sleep(1000); - }//end while(!enter) - } catch(java.io.IOException e) {e.printStackTrace();} - System.exit(0); - }//end default conditional branch + if (readPin) { + ads.setGpioDirIn(gpiopPin); + console.println(" pin " + gpiopPin + " state : " + (ads.isGpioHigh(gpiopPin) ? "HIGH" : "LOW")); + System.exit(0); + } else if (setPinState) { + ads.setGpioDirOut(gpiopPin); + ads.setGpio(gpiopPin, newState.isHigh()); + System.exit(0); + } else if (dumpInputStates) { + final AllInputPrinter ip = new AllInputPrinter(ads); + ip.setPrintingUnits(PrintingUnits.VOLTS); + System.out.print("\n\n\n\n\n\n\n\n\n\n\n"); + System.out.println("Press ENTER to end"); + try { + while (System.in.available() == 0 || System.in.read() != '\n') { + System.out.print("\033[10A");//Go back up 10 lines to rewrite the output + ip.printADSXXXInputStates(); + } + } catch (java.io.IOException e) { + e.printStackTrace(); + } + System.exit(0); + } else if (runChaser) { + final GPIOChaser chaser = new GPIOChaser(ads); + System.out.println("Press ENTER to end"); + try { + while (System.in.available() == 0 || System.in.read() != '\n') { + chaser.updatePrintAndIterate(); + Thread.sleep(chaseIntervalMS); + }//end while(!enter) + } catch (java.io.IOException e) { + e.printStackTrace(); + } + System.exit(0); + } else { + System.out.println("Press ENTER to end"); + int rtn = ads.readAnalogDifferential(MuxValue.valueOf(ppName).ordinal(), MuxValue.valueOf(pnName).ordinal()); + System.out.println("readAnalogDifferential() returned : channel :" + ppName + "/" + pnName + " value :" + rtn); + try { + while (dumpRepeatedly & (System.in.available() == 0 || System.in.read() != '\n')) { + System.out.print("\033[1A");//Go back up 1 lines to rewite the output + System.out.print("\033[1A");//Go back up 1 line to rewite the output + rtn = ads.readAnalogDifferential(MuxValue.valueOf(ppName).ordinal(), MuxValue.valueOf(pnName).ordinal()); + System.out.println("readAnalogDifferential() returned : channel :" + ppName + "/" + pnName + " value :" + rtn); + Thread.sleep(1000); + }//end while(!enter) + } catch (java.io.IOException e) { + e.printStackTrace(); + } + System.exit(0); + }//end default conditional branch }//end main() -}//end ADS1256App \ No newline at end of file +} \ No newline at end of file