diff --git a/src/main/java/com/pi4j/devices/mcp4725/MCP4725App.java b/src/main/java/com/pi4j/devices/mcp4725/MCP4725App.java index f2bbf82..59a2a94 100644 --- a/src/main/java/com/pi4j/devices/mcp4725/MCP4725App.java +++ b/src/main/java/com/pi4j/devices/mcp4725/MCP4725App.java @@ -6,13 +6,19 @@ import com.pi4j.context.Context; import com.pi4j.drivers.io.da.mcp472x.Mcp4725Driver; import com.pi4j.io.i2c.I2C; +import com.pi4j.io.i2c.I2CImplementation; import com.pi4j.util.Console; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class MCP4725App { static void main(String[] args) { - System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "INFO"); + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "OFF"); + + Logger logger = LoggerFactory.getLogger(MCP4725App.class); + logger.trace(">>> Enter: init"); Context pi4j = Pi4J.newAutoContext(); @@ -29,18 +35,17 @@ static void main(String[] args) { int address = MCP4725_Declares._MCP4725_DEFAULT_ADDRESS; boolean doReset = false; int registerData = 0; - boolean setOutputEEPROM = false; - boolean setOutputFast = false; double vref = 0; float eepromVolt = 0; - float fastVolt = 0; int onlyOne = 0; // numerous parms a mutually exclusive + boolean setOutputDigital = false; + boolean setOutputVoltage = false; + boolean persistValues = false; String helpString = " parms: -b 0x? hex value bus -a 0x?? hex value address \n " + - " -r reset chip -rde update DAC and EEPROM \n" + - " -ev eeprom voltage -fv fast voltage \n" + - " -rdf DAC value update fast -vref decimal reference voltage\n " + - "-rde -ev -fv -rdf mutually exclusive" ; + " -r reset chip -pv true/false persist values \n" + + " -v voltage -d DAC digital value -vref float reference voltage\n " + + "-d -v mutually exclusive" ; for (int i = 0; i < args.length; i++) { String o = args[i]; @@ -58,40 +63,30 @@ static void main(String[] args) { vref = Float.parseFloat(a); } else if (o.contentEquals("-r")) { doReset = true; - } else if (o.contentEquals("-rde")) { + } else if (o.contentEquals("-d")) { String a = args[i + 1]; i++; registerData = Integer.parseInt(a); - setOutputEEPROM = true; + setOutputDigital = true; if (registerData < 0 || registerData > 4095) { - console.println("-rde must be in range 0..4095"); + console.println("-d must be in range 0..4095"); System.exit(36); } onlyOne ++; - } else if (o.contentEquals("-rdf")) { - String a = args[i + 1]; - i++; - setOutputFast = true; - registerData = Integer.parseInt(a); - if (registerData < 0 || registerData > 4095) { - console.println("-rdf must be in range 0..4095"); - System.exit(37); - } - onlyOne ++; } else if (o.contentEquals("-h")) { console.println(helpString); System.exit(39); - } else if (o.contentEquals("-ev")) { // eeprom volts + } else if (o.contentEquals("-v")) { // eeprom volts String a = args[i + 1]; i++; + setOutputVoltage = true; eepromVolt = Float.parseFloat(a); onlyOne ++; - } else if (o.contentEquals("-fv")) { // fast volts - String a = args[i + 1]; - i++; - fastVolt = Float.parseFloat(a); - onlyOne ++; - } else { + }else if (o.contentEquals("-pv")) { + String a = args[i + 1]; + persistValues = Boolean.parseBoolean(a); + i++; + } else { console.println(" !!! Invalid Parm " + args); console.println(helpString); System.exit(42); @@ -110,54 +105,35 @@ static void main(String[] args) { } if ( (eepromVolt > vref) || (eepromVolt < 0) ) { - console.println("-ev greater than -vref, or less than zero"); + console.println("-v greater than -vref, or less than zero"); System.exit(51); - } - if ( (fastVolt > vref) || (fastVolt < 0) ) { - console.println("-ef greater than -vref, or less than zero"); - System.exit(51); - - } Mcp4725Driver dacChip ; I2C genCallDevice = null ; - I2C i2cDev = createI2cDevice("MCP4725", busNum, address, pi4j) ; + I2C i2cDev = createI2cDevice("MCP4725", busNum, address, I2CImplementation.DIRECT, pi4j) ; dacChip = new Mcp4725Driver( i2cDev, vref); if (doReset) { - genCallDevice = createI2cDevice("GenCallReset", busNum,0x00, pi4j); + genCallDevice = createI2cDevice("GenCallReset", busNum,0x00, I2CImplementation.SMBUS, pi4j); dacChip.resetChip(genCallDevice); } - if (setOutputEEPROM) { - dacChip.setEepromEnabled(true); - try { - dacChip.setDigitalValue(registerData); - } catch (Exception e) { - console.println("Error occurred setting DAC output via register value failed. \n Exception " + e.getMessage()); - } - dacChip.setEepromEnabled(false); - } - if (setOutputFast) { + logger.info("\nBefore state : \n" + dacChip); + + dacChip.setEepromEnabled(persistValues); + + if (setOutputDigital) { dacChip.setDigitalValue(registerData); } - if (eepromVolt > 0) { - dacChip.setEepromEnabled(true); - try { - dacChip.setVoltage(0, eepromVolt); - } catch (Exception e) { - console.println("Error occurred setting DAC output via target voltage failed. \n Exception " + e.getMessage()); - } - dacChip.setEepromEnabled(false); + if (setOutputVoltage) { + dacChip.setVoltage(0, eepromVolt); } + logger.info("\nCompletion state : \n" + dacChip); - if (fastVolt > 0) { - dacChip.setVoltage(fastVolt); - } if (genCallDevice != null) { @@ -170,14 +146,25 @@ static void main(String[] args) { } - static I2C createI2cDevice( String chipType, int bus, int address, Context pi4j) { + + + /** + * + * @param chipType String Part numer + * @param bus INT + * @param address INT + * @param pi4j Context + * @return I2C device + */ + static I2C createI2cDevice(String chipType, int bus, int address, I2CImplementation impType, Context pi4j) { String id = String.format("0X%02x: ", bus); String name = String.format("0X%02x: ", address); var i2cDeviceConfig = I2C.newConfigBuilder(pi4j) .bus(bus) .device(address) - .id("" + chipType + id + " " + name) + .id(chipType + id + " " + name) .name(name) + .i2cImplementation(impType) .build(); return pi4j.create(i2cDeviceConfig); @@ -185,5 +172,6 @@ static I2C createI2cDevice( String chipType, int bus, int address, Context pi4j) } + } diff --git a/src/main/java/com/pi4j/devices/mcp4725/README.md b/src/main/java/com/pi4j/devices/mcp4725/README.md index a71ed90..7c7a699 100644 --- a/src/main/java/com/pi4j/devices/mcp4725/README.md +++ b/src/main/java/com/pi4j/devices/mcp4725/README.md @@ -21,34 +21,43 @@ Pi Grnd A0 https://datasheetspdf.com/pdf-file/634126/MicrochipTechnology/MCP4725/1 --rde anf -rdf both have max input value 0x0FFF --ef and -ev cannot exceed -vref --vref option: the -d feature will display calculated output voltage. +-b 0x? hex value bus +-a 0x?? hex value address +-vref float reference voltage +-pv true/false. Persist values, default false. +-r reset chip +-d digital input 0 - 4095 +-v voltage 0 - vref + +Note -v -d mutually exclusive + +-v cannot exceed -vref + Updates using raw binary value -To Update DAC and EEPROM, set to 50 % of reference voltage Vin of 3.3 volts +To Update DAC and EEPROM, set to 50 % of reference voltage 1.6v Vin of 3.3 volts +sudo ./runMcp4725.sh -b 0x01 -a 0x62 -d 2047 -vref 3.3 -pv true -sudo ./runMcp4725.sh -b 0x01 -a 0x62 -rde 2047 -vref 3.3 +To Update DAC Fast, set to 100 % of reference voltage +sudo ./runMcp4725.sh -b 0x01 -a 0x62 -d 4095 -r -vref 3.3 -pv false To Update DAC Fast, set to 50 % of reference voltage -sudo ./runMcp4725.sh -b 0x01 -a 0x62 -rdf 2047 -vref 3.3 +sudo ./runMcp4725.sh -b 0x01 -a 0x62 -d 2047 -vref 3.3 -pv false -To Update DAC Fast, set to 100 % of reference voltage -sudo ./runMcp4725.sh -b 0x01 -a 0x62 -rdf 4095 -r -vref 3.3 Updates using voltage value To Update DAC Fast, set to 1.8v of reference voltage -sudo ./runMcp4725.sh -b 0x01 -a 0x62 -fv 1.8 -vref 3.3 +sudo ./runMcp4725.sh -b 0x01 -a 0x62 -v 1.8 -vref 3.3 -pv false To Update DAC and EEPROM, set to 3.3v -sudo ./runMcp4725.sh -b 0x01 -a 0x62 -ev 3.3 -vref 3.3 +sudo ./runMcp4725.sh -b 0x01 -a 0x62 -v 3.3 -vref 3.3 -pv true -To Update DAC Fast, reset chip, set to 3.1 -sudo ./runMcp4725.sh -b 0x01 -a 0x62 -ev 3.1 -r -vref 3.3 +To Update DAC Fast, reset chip, set to 1.8 +sudo ./runMcp4725.sh -b 0x01 -a 0x62 -v 1.8 -r -vref 3.3 -pv false diff --git a/src/main/java/com/pi4j/devices/mcp4728/MCP4728App.java b/src/main/java/com/pi4j/devices/mcp4728/MCP4728App.java index 34bfdc3..dc97257 100644 --- a/src/main/java/com/pi4j/devices/mcp4728/MCP4728App.java +++ b/src/main/java/com/pi4j/devices/mcp4728/MCP4728App.java @@ -41,7 +41,7 @@ public class MCP4728App { static void main(String[] args) { - System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "INFO"); + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "OFF"); Logger logger = LoggerFactory.getLogger(MCP4728App.class); logger.trace(">>> Enter: init"); diff --git a/src/main/java/com/pi4j/devices/mcp4728/README.md b/src/main/java/com/pi4j/devices/mcp4728/README.md index 7222386..bf7e6a3 100644 --- a/src/main/java/com/pi4j/devices/mcp4728/README.md +++ b/src/main/java/com/pi4j/devices/mcp4728/README.md @@ -44,9 +44,9 @@ String helpString = " parms: -b 0x? hex value bus -a 0x?? hex value address https://datasheetspdf.com/pdf-file/634126/MicrochipTechnology/MCP4728/1 --rde anf -rdf both have max input value 0x0FFF +-d haa max input value 0x0FFF --ef and -ev cannot exceed -vdd +-v cannot exceed -vdd