Prechádzať zdrojové kódy

CLI arguments and GPIO shutdown

Tankernn 8 rokov pred
rodič
commit
2d2c4a813f

+ 9 - 3
pom.xml

@@ -12,9 +12,15 @@
 
 	<dependencies>
 		<dependency>
-		    <groupId>com.pi4j</groupId>
-		    <artifactId>pi4j-core</artifactId>
-		   <version>1.0</version>
+			<groupId>com.pi4j</groupId>
+			<artifactId>pi4j-core</artifactId>
+			<version>1.0</version>
+		</dependency>
+		<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
+		<dependency>
+			<groupId>commons-cli</groupId>
+			<artifactId>commons-cli</artifactId>
+			<version>1.3</version>
 		</dependency>
 	</dependencies>
 

+ 82 - 21
src/main/java/eu/tankernn/assembly/compiler/Assemble.java

@@ -9,6 +9,15 @@ import java.io.IOException;
 import java.io.PrintStream;
 import java.util.HashMap;
 
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PatternOptionBuilder;
+
 import eu.tankernn.assembly.output.GPIOHandler;
 import eu.tankernn.assembly.output.ParallelOutput;
 
@@ -19,22 +28,19 @@ public class Assemble {
 	static HashMap<String, Byte> labels = new HashMap<String, Byte>();
 	static HashMap<String, Byte> variables = new HashMap<String, Byte>();
 	static byte currentAddress = 0;
-	
+
 	static boolean outputToGPIO = false;
-	static boolean bigEndianData = true;
-	static boolean bigEndianAddress = true;
+	static boolean bigEndianData = false;
+	static boolean bigEndianAddress = false;
+
+	static File fileIn;
+	static File fileOut = null;
 
 	public static void main(String[] args) {
+		parseArguments(args);
+
 		if (outputToGPIO)
 			GPIOHandler.init(new ParallelOutput());
-		
-		File fileIn;
-
-		if (args.length > 0) {
-			fileIn = new File(args[0]);
-		} else {
-			fileIn = new File("data/in.ta");
-		}
 
 		try {
 			in = new BufferedReader(new FileReader(fileIn));
@@ -43,8 +49,7 @@ public class Assemble {
 			return;
 		}
 
-		if (args.length > 1) {
-			File fileOut = new File(args[1]);
+		if (fileOut != null) {
 			if (fileOut.exists()) {
 				log("File " + fileOut.getAbsolutePath() + " already exists, aborting.");
 				return;
@@ -61,12 +66,16 @@ public class Assemble {
 			out = System.out;
 		}
 
+		System.out.println("Compiling file: " + fileIn.getName());
+
 		try {
 			startCompile();
 		} catch (IOException ex) {
 			log("The compiler encountered an error: ");
 			ex.printStackTrace();
 		}
+		if (outputToGPIO)
+			GPIOHandler.cleanUp();
 	}
 
 	public static void startCompile() throws IOException {
@@ -91,17 +100,17 @@ public class Assemble {
 	}
 
 	public static void output(Byte[] bytes) throws IOException {
-		for (int i = 0; i < bytes.length; i++)
-			currentAddress++;
-
 		if (outputToGPIO)
 			GPIOHandler.writeData(bytes);
-		else
-			for (Byte b : bytes)
-				out.println(byteToBinaryString(currentAddress, 4, bigEndianAddress) + " : " + byteToBinaryString(b, 8, false));
+		
+		for (Byte b : bytes) {
+			out.println(
+					byteToBinaryString(currentAddress, 4, bigEndianAddress) + " : " + byteToBinaryString(b, 8, false));
+			currentAddress++;
+		}	
 	}
-	
-	private static String byteToBinaryString(int b, int wordLength, boolean reverse) {
+
+	public static String byteToBinaryString(int b, int wordLength, boolean reverse) {
 		int template = (int) Math.pow(2, wordLength) - 1;
 		StringBuilder result = new StringBuilder();
 		result.append(Integer.toBinaryString((b & template) + template + 0x1).substring(1));
@@ -109,4 +118,56 @@ public class Assemble {
 			result.reverse();
 		return result.toString();
 	}
+
+	private static void parseArguments(String[] args) {
+		Options options = new Options();
+
+		Option input = new Option("i", "input", true, "input file path");
+		input.setRequired(true);
+		input.setType(PatternOptionBuilder.EXISTING_FILE_VALUE);
+		options.addOption(input);
+
+		Option output = new Option("o", "output", true, "output file");
+		output.setRequired(false);
+		output.setType(PatternOptionBuilder.FILE_VALUE);
+		options.addOption(output);
+
+		Option gpio = new Option("g", "gpio", false, "output to GPIO");
+		gpio.setRequired(false);
+		options.addOption(gpio);
+
+		Option bigEndData = new Option("D", "big-endian-data", false, "Flip the data before outputting");
+		bigEndData.setRequired(false);
+		options.addOption(bigEndData);
+
+		Option bigEndAddress = new Option("A", "big-endian-address", false, "Flip the data before outputting");
+		bigEndAddress.setRequired(false);
+		options.addOption(bigEndAddress);
+
+		CommandLineParser parser = new DefaultParser();
+		HelpFormatter formatter = new HelpFormatter();
+		CommandLine cmd;
+
+		try {
+			cmd = parser.parse(options, args);
+		} catch (ParseException e) {
+			System.out.println(e.getMessage());
+			formatter.printHelp("utility-name", options);
+
+			System.exit(1);
+			return;
+		}
+
+		outputToGPIO = cmd.hasOption("gpio");
+		bigEndianData = cmd.hasOption("big-endian-data");
+		bigEndianAddress = cmd.hasOption("big-endian-address");
+
+		try {
+			fileIn = (File) cmd.getParsedOptionValue("input");
+			if (cmd.hasOption("output"))
+				fileOut = (File) cmd.getParsedOptionValue("output");
+		} catch (ParseException e) {
+			e.printStackTrace();
+		}
+	}
 }

+ 17 - 6
src/main/java/eu/tankernn/assembly/output/GPIOHandler.java

@@ -13,7 +13,7 @@ public class GPIOHandler {
 	static final Pin WRITE_PIN = RaspiPin.GPIO_26;
 	private static DataOutputMethod dom;
 
-	static GpioPinDigitalOutput[] addressOutputPins = new GpioPinDigitalOutput[8];
+	static GpioPinDigitalOutput[] addressOutputPins = new GpioPinDigitalOutput[ADDRESS_PINS.length];
 	static GpioPinDigitalOutput writePin;
 
 	// create GPIO controller instance
@@ -25,25 +25,36 @@ public class GPIOHandler {
 		dom = method;
 		dom.init(GPIO);
 
-		for (int i = 0; i < addressOutputPins.length; i++)
+		for (int i = 0; i < addressOutputPins.length; i++) {
 			addressOutputPins[i] = GPIO.provisionDigitalOutputPin(ADDRESS_PINS[i], "Address pin " + i, PinState.LOW);
-
+			addressOutputPins[i].setShutdownOptions(true, PinState.LOW);
+		}
 		writePin = GPIO.provisionDigitalOutputPin(WRITE_PIN, "Write pin", PinState.HIGH);
+		writePin.setShutdownOptions(true, PinState.HIGH);
 	}
 
 	public static void writeData(Byte[] bytes) {
 		for (byte b : bytes) {
 			// Output address
 			for (int i = 0; i < addressOutputPins.length; i++) {
-				addressOutputPins[i].setState((currentAddress & 1 << i) == 1);
+				addressOutputPins[i].setState((currentAddress++ & 1 << i) == 1);
 			}
-			currentAddress++;
 
 			// Output data
 			dom.output(b);
 
 			// Pulse write pin
-			writePin.pulse(10);
+			writePin.pulse(10, PinState.LOW, true);
+			
+			try {
+				Thread.sleep(50);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			}
 		}
 	}
+	
+	public static void cleanUp() {
+		GPIO.shutdown();
+	}
 }