From d4775b7a45ea004df91e8eb3c7e17cf91e98f6c9 Mon Sep 17 00:00:00 2001 From: Tom Aarts Date: Fri, 17 Jan 2025 18:40:40 -0600 Subject: [PATCH] fix bad merge ADS1256 --- .../com/pi4j/devices/ads1256/ADS1256.java | 171 +++++++++++- .../com/pi4j/devices/ads1256/ADS1256App.java | 245 ++---------------- 2 files changed, 190 insertions(+), 226 deletions(-) diff --git a/src/main/java/com/pi4j/devices/ads1256/ADS1256.java b/src/main/java/com/pi4j/devices/ads1256/ADS1256.java index fad9c68..a6850cb 100644 --- a/src/main/java/com/pi4j/devices/ads1256/ADS1256.java +++ b/src/main/java/com/pi4j/devices/ads1256/ADS1256.java @@ -13,7 +13,7 @@ * * this project can be found here: https://pi4j.com/ * * ********************************************************************** * * %% - * * * Copyright (C) 2012 - 2024 Pi4J + * * * Copyright (C) 2012 - 2022 Pi4J * * %% * * * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -37,14 +37,20 @@ package com.pi4j.devices.ads1256; import com.pi4j.context.Context; +import com.pi4j.io.exception.IOException; +import com.pi4j.io.gpio.digital.DigitalInput; +import com.pi4j.io.gpio.digital.DigitalOutput; +import com.pi4j.io.gpio.digital.DigitalState; +import com.pi4j.io.spi.Spi; import com.pi4j.io.spi.SpiBus; import com.pi4j.io.spi.SpiChipSelect; +import com.pi4j.io.spi.SpiMode; import com.pi4j.util.Console; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ADS1256 { - enum MuxValue { AIN0, AIN1, @@ -602,4 +608,161 @@ private int doRDATA() throws InterruptedException { return read; } -}//end ADS1256 + + + private short mapDrateString(String name) { + ADS1256_DRATE[] drateMap = ADS1256_DRATE.values(); + int posPin = 0xff; + for (ADS1256_DRATE col : drateMap) { + // Calling ordinal() to find index + // of drate. + if (col.toString().contentEquals(name)) { + posPin = col.ordinal(); + this.logger.trace(" drate : " + name + " No : " + ADS1256_DRATE_E[posPin]); + break; + } + } + return (short) (ADS1256_DRATE_E[posPin] & 0xff); + } + + private short mapGainString(String name) { + ADS1256_GAIN[] drateMap = ADS1256_GAIN.values(); + int posPin = 0xff; + for (ADS1256_GAIN col : drateMap) { + // Calling ordinal() to find index + // of Gain name. + if (col.toString().contentEquals(name)) { + posPin = col.ordinal(); + this.logger.trace(" gain : " + name + " No : " + posPin); + break; + } + } + return (short) (posPin & 0xff); + } + + public DigitalState readGpio(int pin) { + this.logger.trace(">>> Enter readGpio pin " + pin); + DigitalState rval = DigitalState.UNKNOWN; + byte regVal = this.readRegData(ADS1256_Declares.REG_IO); + if (this.isPinInput(pin, regVal)) { + rval = this.getPinState(pin, regVal); + } + this.logger.trace("<<< Exit readGpio State" + rval); + return (rval); + } + + public boolean setGpioDirOut(int pin) { + this.logger.trace(">>> Enter setGpioDirOut pin " + pin); + boolean rval = false; + // this.logger.trace("ADCON" + this.readRegData(ADS1256_Declares.REG_ADCON)); + byte regVal = this.readRegData(ADS1256_Declares.REG_IO); + regVal &= ~(0x10 << pin) & 0xff; + this.writeReg(ADS1256_Declares.REG_IO, regVal); + rval = true; + this.logger.trace("<<< Exit setGpioDirOut " + rval); + return (rval); + } + + public boolean setGpioDirIn(int pin) { + this.logger.trace(">>> Enter setGpioDirIn pin " + pin); + boolean rval = false; + byte regVal = this.readRegData(ADS1256_Declares.REG_IO); + regVal |= (0x10 << pin) & 0xff; + this.writeReg(ADS1256_Declares.REG_IO, regVal); + this.logger.trace("<<< Exit setGpioDirIn State" + rval); + rval = true; + return (rval); + } + + private DigitalState getPinState(int pin, byte registerVal) { + DigitalState rval = DigitalState.UNKNOWN; + this.logger.trace(">>> Enter isPinInput pin: " + pin); + if ((registerVal & (0x01 << pin)) == 0) { + rval = DigitalState.LOW; + } else { + rval = DigitalState.HIGH; + } + this.logger.trace(" Exit isPinInput "); + return (rval); + } + + public boolean setGpio(int pin, DigitalState newState) { + this.logger.trace(">>> Enter setGpio pin " + pin + " state : " + newState); + boolean rval = false; + byte regVal = this.readRegData(ADS1256_Declares.REG_IO); + if (this.isPinOutput(pin, regVal)) { + rval = this.setPinState(pin, newState, regVal); + } else { + this.logger.trace("Pin " + pin + " not configured for output"); + } + this.logger.trace("<<< Exit setGpio " + rval); + return (rval); + } + + private boolean setPinState(int pin, DigitalState newState, byte registerVal) { + boolean rval = false; + this.logger.trace(">>> Enter setPinState pin: " + pin + " state: " + newState); + if (newState == DigitalState.HIGH) { + registerVal |= (1 << pin); + rval = true; + } else { + registerVal &= ~(1 << pin); + rval = true; + } + this.writeReg(ADS1256_Declares.REG_IO, registerVal); + this.logger.trace(" Exit setPinState " + rval); + return (rval); + } + + private boolean isPinInput(int pin, byte registerVal) { + boolean rval = false; + this.logger.trace(">>> Enter isPinInput pin: " + pin); + if ((registerVal & (0x10 << pin)) > 0) { + rval = true; + } + this.logger.trace(" Exit isPinInput " + rval); + return (rval); + } + + private boolean isPinOutput(int pin, byte registerVal) { + boolean rval = false; + this.logger.trace(">>> Enter isPinOutput pin: " + pin); + if ((registerVal & (0x10 << pin)) == 0) { + rval = true; + } + this.logger.trace(" Exit isPinOutput " + rval); + return (rval); + } + + + // SPI device + // public SpiDevice spi; + + // ADC channel count + // file + private Spi spi; + private final Console console; + private final String traceLevel; + private final Logger logger; + + private double vref = 2.5; + private final SpiChipSelect chipSelect; + private final SpiBus spiBus; + + private final Context pi4j; + private boolean resetChip = false; + + private DigitalInput drdyGpio; + private final int drdyPinNum; // 17 + private DigitalOutput csGpio; + private final int csPinNum; // 22 + + private DigitalOutput rstGpio; + private final int rstPinNum; // 18 + private boolean crtRstGpio = false; + private DigitalOutput pdwnGpio; + + private final int pdwnPinNum; // 27 + private boolean crtPdwnGpio = false; +} + diff --git a/src/main/java/com/pi4j/devices/ads1256/ADS1256App.java b/src/main/java/com/pi4j/devices/ads1256/ADS1256App.java index d51d7c5..d00ed0e 100644 --- a/src/main/java/com/pi4j/devices/ads1256/ADS1256App.java +++ b/src/main/java/com/pi4j/devices/ads1256/ADS1256App.java @@ -14,7 +14,7 @@ * * this project can be found here: https://pi4j.com/ * * ********************************************************************** * * %% - * * * Copyright (C) 2012 - 2024 Pi4J + * * * Copyright (C) 2012 - 2022 Pi4J * * %% * * * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,11 +35,9 @@ * */ + import com.pi4j.Pi4J; import com.pi4j.context.Context; -import com.pi4j.devices.ads1256.ADS125x.DataRate; -import com.pi4j.devices.ads1256.ADS125x.MuxValue; -import com.pi4j.devices.ads1256.AllInputPrinter.PrintingUnits; import com.pi4j.io.exception.IOException; import com.pi4j.io.gpio.digital.DigitalState; import com.pi4j.io.spi.SpiBus; @@ -49,7 +47,6 @@ public class ADS1256App { public static void main(String[] args) throws InterruptedException, IOException { - var console = new Console(); Context pi4j = Pi4J.newAutoContext(); @@ -175,227 +172,31 @@ public static void main(String[] args) throws InterruptedException, IOException System.exit(45); } } - var console = new Console(); - Context pi4j = Pi4J.newAutoContext(); - - double vref = 2.5; - String ppName = "AINCOM"; - String pnName = "AINCOM"; + short pinCount = 8; + console.println("----------------------------------------------------------"); + console.println("PI4J PROVIDERS"); + console.println("----------------------------------------------------------"); + pi4j.providers().describe().print(System.out); + System.out.println("----------------------------------------------------------"); - 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; + ADS1256 spiCls = new ADS1256(pi4j, spiBus, chipSelect, resetChip, drdyPin, csPin, rsrtPin, crtRestart, pdwnPin, crtPdwn, console, traceLevel, vref); - int gpiopPin = 42; - 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"; + spiCls.validateChipID(); - 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) + if (readPin) { + spiCls.setGpioDirIn(gpiopPin); + console.println(" pin " + gpiopPin + " state : " + spiCls.readGpio(gpiopPin)); + } else if (setPinState) { + spiCls.setGpioDirOut(gpiopPin); + console.println(" pin " + gpiopPin + " rval " + spiCls.setGpio(gpiopPin, newState)); + } else { + double rtn = spiCls.getADS1256State(ppName, pnName); + System.out.println("getMCP3008State returned : channel :" + ppName + "/" + pnName + " value :" + rtn); - 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); - } + spiCls.displayADS1256State(ppName, pnName); + } + } - 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 - }//end main() -}//end ADS1256App \ No newline at end of file +} \ No newline at end of file