Prechádzať zdrojové kódy

Small bugfix and log4j logging.

Frans Bergman 8 rokov pred
rodič
commit
55e2c1c5d9

+ 10 - 0
pom.xml

@@ -22,6 +22,16 @@
 			<artifactId>commons-cli</artifactId>
 			<version>1.3</version>
 		</dependency>
+		<dependency>
+			<groupId>org.apache.logging.log4j</groupId>
+			<artifactId>log4j-api</artifactId>
+			<version>2.6.2</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.logging.log4j</groupId>
+			<artifactId>log4j-core</artifactId>
+			<version>2.6.2</version>
+		</dependency>
 	</dependencies>
 
 	<build>

+ 38 - 20
src/main/java/eu/tankernn/assembly/compiler/Assemble.java

@@ -1,5 +1,14 @@
 package eu.tankernn.assembly.compiler;
 
+import static com.pi4j.io.gpio.RaspiPin.GPIO_00;
+import static com.pi4j.io.gpio.RaspiPin.GPIO_01;
+import static com.pi4j.io.gpio.RaspiPin.GPIO_02;
+import static com.pi4j.io.gpio.RaspiPin.GPIO_03;
+import static com.pi4j.io.gpio.RaspiPin.GPIO_04;
+import static com.pi4j.io.gpio.RaspiPin.GPIO_05;
+import static com.pi4j.io.gpio.RaspiPin.GPIO_06;
+import static com.pi4j.io.gpio.RaspiPin.GPIO_07;
+
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -17,11 +26,22 @@ 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 org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import com.pi4j.io.gpio.Pin;
+import com.pi4j.io.gpio.RaspiPin;
 
 import eu.tankernn.assembly.output.GPIOHandler;
 import eu.tankernn.assembly.output.ParallelOutput;
 
 public class Assemble {
+	private static final Logger LOG = LogManager.getLogger();
+
+	private static final Pin[] DATA_PINS = { GPIO_07, GPIO_00, GPIO_01, GPIO_02, GPIO_03, GPIO_04, GPIO_05, GPIO_06 };
+	private static final Pin[] ADDRESS_PINS = { RaspiPin.GPIO_22, RaspiPin.GPIO_23, RaspiPin.GPIO_24,
+			RaspiPin.GPIO_25 };
+
 	static PrintStream out;
 	static BufferedReader in;
 
@@ -40,45 +60,44 @@ public class Assemble {
 		parseArguments(args);
 
 		if (outputToGPIO)
-			GPIOHandler.init(new ParallelOutput());
+			GPIOHandler.init(new ParallelOutput(DATA_PINS), new ParallelOutput(ADDRESS_PINS));
 
 		try {
 			in = new BufferedReader(new FileReader(fileIn));
 		} catch (FileNotFoundException e) {
-			log("Could not find file " + fileIn.getAbsolutePath());
+			LOG.error("Could not find file {}", fileIn.getAbsolutePath());
 			return;
 		}
 
 		if (fileOut != null) {
 			if (fileOut.exists()) {
-				log("File " + fileOut.getAbsolutePath() + " already exists, aborting.");
+				LOG.error("File {} already exists, aborting.", fileOut.getAbsolutePath());
 				return;
 			} else {
 				try {
 					fileOut.createNewFile();
 					out = new PrintStream(new FileOutputStream(fileOut));
 				} catch (IOException e) {
-					System.err.println("Error creating/opening file " + fileOut.getAbsolutePath());
-					e.printStackTrace();
+					LOG.error("Error creating/opening file " + fileOut.getAbsolutePath());
+					LOG.catching(e);
 				}
 			}
 		} else {
 			out = System.out;
 		}
 
-		System.out.println("Compiling file: " + fileIn.getName());
-
 		try {
 			startCompile();
 		} catch (IOException ex) {
-			log("The compiler encountered an error: ");
-			ex.printStackTrace();
+			LOG.error("The compiler encountered an error.");
+			LOG.catching(ex);
 		}
 		if (outputToGPIO)
 			GPIOHandler.cleanUp();
 	}
 
 	public static void startCompile() throws IOException {
+		LOG.info("Compiling file: {}", fileIn.getName());
 		String line;
 		int index = 0;
 		while ((line = in.readLine()) != null) {
@@ -95,17 +114,13 @@ public class Assemble {
 		labels.put(label, (byte) currentAddress);
 	}
 
-	public static void log(String str) {
-		System.out.println(str);
-	}
-
 	public static void output(Byte[] bytes) throws IOException {
 		if (outputToGPIO)
 			GPIOHandler.writeData(bytes);
 
 		for (Byte b : bytes) {
-			out.println(
-					byteToBinaryString(bigEndianAddress ? Util.reverseByte(currentAddress, 4) : currentAddress, 4) + " : " + byteToBinaryString(b, 8));
+			out.println(byteToBinaryString(bigEndianAddress ? Util.reverseByte(currentAddress, 4) : currentAddress, 4)
+					+ " : " + byteToBinaryString(b, 8));
 			currentAddress++;
 		}
 	}
@@ -132,11 +147,11 @@ public class Assemble {
 		gpio.setRequired(false);
 		options.addOption(gpio);
 
-		Option bigEndData = new Option("D", "big-endian-data", false, "Flip the data before outputting");
+		Option bigEndData = new Option("D", "big-endian-data", false, "reverse the data before outputting");
 		bigEndData.setRequired(false);
 		options.addOption(bigEndData);
 
-		Option bigEndAddress = new Option("A", "big-endian-address", false, "Flip the data before outputting");
+		Option bigEndAddress = new Option("A", "big-endian-address", false, "reverse the address before outputting");
 		bigEndAddress.setRequired(false);
 		options.addOption(bigEndAddress);
 
@@ -147,8 +162,7 @@ public class Assemble {
 		try {
 			cmd = parser.parse(options, args);
 		} catch (ParseException e) {
-			System.out.println(e.getMessage());
-			formatter.printHelp("utility-name", options);
+			formatter.printHelp("tankernn-assembly-compiler", options);
 
 			System.exit(1);
 			return;
@@ -163,7 +177,11 @@ public class Assemble {
 			if (cmd.hasOption("output"))
 				fileOut = (File) cmd.getParsedOptionValue("output");
 		} catch (ParseException e) {
-			e.printStackTrace();
+			LOG.catching(e);
 		}
 	}
+
+	public static boolean isBigEndianAddress() {
+		return bigEndianAddress;
+	}
 }

+ 2 - 2
src/main/java/eu/tankernn/assembly/compiler/LineParser.java

@@ -61,7 +61,7 @@ public class LineParser {
 			addWithAddress(OPCodes.NOT_B);
 			break;
 		case "STO":
-			addWithoutAddress(OPCodes.STO);
+			addWithAddress(OPCodes.STO);
 			break;
 		case "OUT":
 			addWithoutAddress(OPCodes.OUT);
@@ -70,7 +70,7 @@ public class LineParser {
 			pos++;
 			switch (instruction[pos]) {
 			case "":
-
+				//TODO Jump instruction
 			}
 			addWithoutAddress(OPCodes.JMP);
 			break;

+ 29 - 23
src/main/java/eu/tankernn/assembly/output/GPIOHandler.java

@@ -1,5 +1,8 @@
 package eu.tankernn.assembly.output;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
 import com.pi4j.io.gpio.GpioController;
 import com.pi4j.io.gpio.GpioFactory;
 import com.pi4j.io.gpio.GpioPinDigitalOutput;
@@ -7,28 +10,30 @@ import com.pi4j.io.gpio.Pin;
 import com.pi4j.io.gpio.PinState;
 import com.pi4j.io.gpio.RaspiPin;
 
-public class GPIOHandler {
+import eu.tankernn.assembly.compiler.Assemble;
+import eu.tankernn.assembly.compiler.Util;
 
-	static final Pin[] ADDRESS_PINS = { RaspiPin.GPIO_22, RaspiPin.GPIO_23, RaspiPin.GPIO_24, RaspiPin.GPIO_25 };
+public class GPIOHandler {
+	private static final Logger LOG = LogManager.getLogger();
+	
 	static final Pin WRITE_PIN = RaspiPin.GPIO_26;
-	private static DataOutputMethod dom;
-
-	static GpioPinDigitalOutput[] addressOutputPins = new GpioPinDigitalOutput[ADDRESS_PINS.length];
 	static GpioPinDigitalOutput writePin;
-
-	// create GPIO controller instance
-	static final GpioController GPIO = GpioFactory.getInstance();
+	
+	private static OutputMethod dataOutput, addressOutput;
+	static GpioController GPIO;
 
 	static byte currentAddress;
 
-	public static void init(DataOutputMethod method) {
-		dom = method;
-		dom.init(GPIO);
-
-		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);
-		}
+	public static void init(OutputMethod dataMethod, OutputMethod addressMethod) {
+		// create GPIO controller instance
+		GPIO = GpioFactory.getInstance();
+		
+		dataOutput = dataMethod;
+		dataOutput.init(GPIO);
+		
+		addressOutput = addressMethod;
+		addressOutput.init(GPIO);
+		
 		writePin = GPIO.provisionDigitalOutputPin(WRITE_PIN, "Write pin", PinState.HIGH);
 		writePin.setShutdownOptions(true, PinState.HIGH);
 	}
@@ -36,20 +41,21 @@ public class GPIOHandler {
 	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);
-			}
+			byte address = Assemble.isBigEndianAddress() ? Util.reverseByte(new Byte(currentAddress), 4) : currentAddress;
+			addressOutput.output(address);
+			currentAddress++;
 
 			// Output data
-			dom.output(b);
+			dataOutput.output(b);
 
 			// Pulse write pin
-			writePin.pulse(10, PinState.LOW, true);
+			writePin.pulse(100, PinState.LOW, true);
 
 			try {
-				Thread.sleep(50);
+				Thread.sleep(100);
 			} catch (InterruptedException e) {
-				e.printStackTrace();
+				LOG.catching(e);
+				return;
 			}
 		}
 	}

+ 1 - 1
src/main/java/eu/tankernn/assembly/output/DataOutputMethod.java → src/main/java/eu/tankernn/assembly/output/OutputMethod.java

@@ -2,7 +2,7 @@ package eu.tankernn.assembly.output;
 
 import com.pi4j.io.gpio.GpioController;
 
-public interface DataOutputMethod {
+public interface OutputMethod {
 	public void init(GpioController GPIO);
 
 	public void output(byte b);

+ 18 - 11
src/main/java/eu/tankernn/assembly/output/ParallelOutput.java

@@ -4,24 +4,31 @@ import com.pi4j.io.gpio.GpioController;
 import com.pi4j.io.gpio.GpioPinDigitalOutput;
 import com.pi4j.io.gpio.Pin;
 import com.pi4j.io.gpio.PinState;
-import static com.pi4j.io.gpio.RaspiPin.*;
-
-public class ParallelOutput implements DataOutputMethod {
-
-	private static final Pin[] DATA_PINS = { GPIO_07, GPIO_00, GPIO_01, GPIO_02, GPIO_03, GPIO_04, GPIO_05, GPIO_06 };
-
-	private GpioPinDigitalOutput[] dataOutputPins = new GpioPinDigitalOutput[8];
 
+public class ParallelOutput implements OutputMethod {
+	
+	private Pin[] rawPins;
+	
+	private GpioPinDigitalOutput[] dataOutputPins;
+	
+	public ParallelOutput(Pin[] pins) {
+		rawPins = pins;
+		dataOutputPins = new GpioPinDigitalOutput[rawPins.length];
+	}
+	
 	@Override
 	public void init(GpioController GPIO) {
-		for (int i = 0; i < dataOutputPins.length; i++)
-			dataOutputPins[i] = GPIO.provisionDigitalOutputPin(DATA_PINS[i], "Data pin " + i, PinState.LOW);
+		for (int i = 0; i < dataOutputPins.length; i++) {
+			dataOutputPins[i] = GPIO.provisionDigitalOutputPin(rawPins[i], "Data pin " + i, PinState.LOW);
+			dataOutputPins[i].setShutdownOptions(true, PinState.LOW);
+		}
 	}
 
 	@Override
 	public void output(byte b) {
-		for (int i = 0; i < 8; i++)
-			dataOutputPins[i].setState((b & 1 << i) == 1);
+		for (int i = 0; i < dataOutputPins.length; i++) {
+			dataOutputPins[i].setState(((b >> i) & 1) == 1);
+		}
 	}
 
 }

+ 13 - 8
src/main/java/eu/tankernn/assembly/output/SerialOutput.java

@@ -4,25 +4,30 @@ import com.pi4j.io.gpio.GpioController;
 import com.pi4j.io.gpio.GpioPinDigitalOutput;
 import com.pi4j.io.gpio.Pin;
 import com.pi4j.io.gpio.PinState;
-import com.pi4j.io.gpio.RaspiPin;
 
-public class SerialOutput implements DataOutputMethod {
-	private static final Pin DATA_PIN = RaspiPin.GPIO_01;
-	private static final Pin DATA_CLOCK_PIN = RaspiPin.GPIO_02;
+public class SerialOutput implements OutputMethod {
+	private Pin dataPin;
+	private Pin clockPin;
+	
 	private GpioPinDigitalOutput dataOutputPin;
-	private GpioPinDigitalOutput dataClockPin;
+	private GpioPinDigitalOutput clockOutputPin;
+	
+	public SerialOutput(Pin dataPin, Pin clockPin) {
+		this.dataPin = dataPin;
+		this.clockPin = clockPin;
+	}
 
 	@Override
 	public void init(GpioController GPIO) {
-		dataOutputPin = GPIO.provisionDigitalOutputPin(DATA_PIN, "Data pin", PinState.LOW);
-		dataClockPin = GPIO.provisionDigitalOutputPin(DATA_CLOCK_PIN, "Data clock pin", PinState.LOW);
+		dataOutputPin = GPIO.provisionDigitalOutputPin(dataPin, "Data pin", PinState.LOW);
+		clockOutputPin = GPIO.provisionDigitalOutputPin(clockPin, "Serial clock pin", PinState.LOW);
 	}
 
 	@Override
 	public void output(byte b) {
 		for (int i = 0; i < 8; i++) {
 			dataOutputPin.setState((b & 1 << i) == 1);
-			dataClockPin.pulse(10);
+			clockOutputPin.pulse(10);
 		}
 	}
 

+ 13 - 0
src/main/res/log4j2.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="WARN">
+	<Appenders>
+		<Console name="Console" target="SYSTEM_OUT">
+			<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
+		</Console>
+	</Appenders>
+	<Loggers>
+		<Root level="error">
+			<AppenderRef ref="Console" />
+		</Root>
+	</Loggers>
+</Configuration>