Просмотр исходного кода

Add a bit of command line parsing

Baglio Tabifata 4 лет назад
Родитель
Сommit
35cf7bc7ad
2 измененных файлов с 83 добавлено и 42 удалено
  1. 39 33
      src/net/hkzlab/dupal/App.java
  2. 44 9
      src/net/hkzlab/dupal/boardio/DuPALAnalyzer.java

+ 39 - 33
src/net/hkzlab/dupal/App.java

@@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
 
 import net.hkzlab.devices.PAL16R4Specs;
 import net.hkzlab.devices.PAL16R6Specs;
+import net.hkzlab.devices.PAL16R8Specs;
 import net.hkzlab.devices.PALSpecs;
 import net.hkzlab.dupal.boardio.DuPALAnalyzer;
 import net.hkzlab.dupal.boardio.DuPALManager;
@@ -12,13 +13,24 @@ import net.hkzlab.dupal.boardio.DuPALManager;
 public class App {
     private final static Logger logger = LoggerFactory.getLogger(DuPALManager.class);
 
+    private static String serialDevice = null;
+    private static PALSpecs pspecs = null;
+    private static int outMask = -1;
+    private static String outDir = null;
+
     public static void main(String[] args) throws Exception {
-        DuPALManager dpm = new DuPALManager("/dev/ttyUSB0");
-         PALSpecs pspecs = new PAL16R6Specs();
-        //PALSpecs pspecs = new PAL16R4Specs();
-        // DuPALAnalyzer dpan = new DuPALAnalyzer(dpm, pspecs);
-        DuPALAnalyzer dpan = new DuPALAnalyzer(dpm, pspecs, 0xC0, "/tmp/dupal.dmp");
-        //DuPALAnalyzer dpan = new DuPALAnalyzer(dpm, pspecs, 0x80, "/tmp/dupal.dmp");
+        if(args.length < 3) {
+            logger.error("Wrong number of arguments passed.\n"+
+                        "dupal_analyzer <serial_port> <pal_type> <output_dir> [hex_outmask]\n" +
+                        "Where <pal_type> can be: 16R8, 16R6, 16R4\n");
+
+            return;
+        }
+
+        parseArgs(args);
+
+        DuPALManager dpm = new DuPALManager(serialDevice);
+        DuPALAnalyzer dpan = new DuPALAnalyzer(dpm, pspecs, outMask, outDir);
 
         if(!dpm.enterRemoteMode()) {
             System.out.println("Unable to enter remote mode!");
@@ -26,36 +38,30 @@ public class App {
         }
 
         dpan.startAnalisys();
+    }
 
-        /*
-        try {
-            System.out.println(dpm.enterRemoteMode());
-
-            for(int idx = 0; idx <= 0xFFFF; idx++) {
-                dpm.writeCommand(DuPALProto.buildWRITECommand(idx));
-                int addr = DuPALProto.handleWRITEResponse(dpm.readResponse());
-
-                if(addr >= 0) {
-                    System.out.println("idx -> " + idx + " written -> " + addr);
-                } else {
-                    System.out.println("Write error!");
-                    break;
-                }
-
-                dpm.writeCommand(DuPALProto.buildREADCommand());
-                System.out.println("Read ... " + DuPALProto.handleREADResponse(dpm.readResponse()));
-            }
-        } finally {
-            dpm.cleanup();
+    private static void parseArgs(String[] args) {
+        serialDevice = args[0];
+        
+        switch(args[1].toUpperCase()) {
+            case "16R8":
+                pspecs = new PAL16R8Specs();
+                break;
+            case "16R6":
+                pspecs = new PAL16R6Specs();
+                break;
+            case "16R4":
+                pspecs = new PAL16R4Specs();
+                break;
+            default:
+                logger.error("Bad PAL type selected.");
+                System.exit(-1);
         }
-        */
-        //SubState ss = new SubState("TEST", new byte[] {-1, 0, -1, 0, -1, -1});
-        //System.out.println(ss.toString());
-        //System.out.println(ss.hashCode());
 
-        //MacroState ms = new MacroState("TEST", new boolean[] {true, true, false, true}, 3);
-        //System.out.println(ms.toString());
-        //System.out.println(ms.hashCode());
+        outDir = args[2];
 
+        if(args.length >= 4) {
+            outMask = Integer.parseInt(args[3], 16);
+        }
     }
 }

+ 44 - 9
src/net/hkzlab/dupal/boardio/DuPALAnalyzer.java

@@ -1,5 +1,6 @@
 package net.hkzlab.dupal.boardio;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -26,22 +27,35 @@ import net.hkzlab.palanalisys.SubState;
 public class DuPALAnalyzer {
     private final Logger logger = LoggerFactory.getLogger(DuPALAnalyzer.class);
 
+    private static final String SERIALIZED_DUMP = "dupalstat.dmp";
+    private static final String OUT_TABLE = "dupal_outputs.tbl";
+    private static final String REGOUT_TABLE = "dupal_regoutputs.tbl";
+
     private static final long SAVE_INTERVAL = 5 * 60 * 1000; // 5 minutes
 
     private MacroState[] mStates;
 
     private final DuPALManager dpm;
     private final PALSpecs pspecs;
-    private final String serObjPath;
+    private final String outPath;
     private final HashMap<Integer, StateLink[]> pathMap;
+    
+    private final String serdump_path;
+    private final String tblPath_out;
+    private final String tblPath_regout;
+    
     private int IOasOUT_Mask = -1;
     private int additionalOUTs = 0;
 
-    public DuPALAnalyzer(final DuPALManager dpm, final PALSpecs pspecs, final int IOasOUT_Mask, String serObjPath) {
+    public DuPALAnalyzer(final DuPALManager dpm, final PALSpecs pspecs, final int IOasOUT_Mask, final String outPath) {
         this.dpm = dpm;
         this.pspecs = pspecs;
         this.IOasOUT_Mask = IOasOUT_Mask;
-        this.serObjPath = serObjPath;
+        this.outPath = outPath;
+
+        serdump_path = outPath + File.pathSeparator + SERIALIZED_DUMP;
+        tblPath_out = outPath + File.pathSeparator + OUT_TABLE;
+        tblPath_regout = outPath + File.pathSeparator + REGOUT_TABLE;
 
         this.pathMap = new HashMap<>();
         this.mStates = new MacroState[1 << pspecs.getNumROUTPins()];
@@ -97,13 +111,34 @@ public class DuPALAnalyzer {
 
         additionalOUTs = calculateAdditionalOutsFromMask(IOasOUT_Mask);
 
-        if(serObjPath != null) restoreStatus(serObjPath);
+        if(outPath != null) restoreStatus(serdump_path);
         internal_analisys();
-        if(serObjPath != null) saveStatus(serObjPath);
+        if(serdump_path != null) saveStatus(serdump_path);
 
         //try { printStateStructure(System.out, pspecs, mStates); } catch(IOException e){};
-        //try { printLogicTableOUTPUTS(System.out, pspecs, additionalOUTs, IOasOUT_Mask, mStates); } catch(IOException e){};
-        try { printLogicTableREGOUTPUTS(System.out, pspecs, additionalOUTs, IOasOUT_Mask, mStates); } catch(IOException e){};
+        printTables();
+    }
+
+    private void printTables() {
+        FileOutputStream fout = null;
+        
+        try {
+            fout = new FileOutputStream(tblPath_out);
+            printLogicTableOUTPUTS(fout, pspecs, additionalOUTs, IOasOUT_Mask, mStates);
+            fout.close();
+        } catch(IOException e) {
+            logger.error("Error printing out the outputs table.");
+            e.printStackTrace();
+        }
+        
+        try {
+            fout = new FileOutputStream(tblPath_regout);
+            printLogicTableREGOUTPUTS(fout, pspecs, additionalOUTs, IOasOUT_Mask, mStates);
+            fout.close();
+        } catch(IOException e) {
+            logger.error("Error printing out the registered outputs table.");
+            e.printStackTrace();
+        }
     }
 
     private int guessIOs() {
@@ -186,8 +221,8 @@ public class DuPALAnalyzer {
         long last_save = 0;
         while(true) {
             long now = System.currentTimeMillis();
-            if(((now - last_save) >= SAVE_INTERVAL) && (serObjPath != null)) {
-                saveStatus(serObjPath);
+            if(((now - last_save) >= SAVE_INTERVAL) && (serdump_path != null)) {
+                saveStatus(serdump_path);
                 last_save = now;
             }