瀏覽代碼

megatron ctrl separation from quickstuff

LH 4 月之前
父節點
當前提交
f2d3acf6e3

+ 27 - 0
Megatron-Bridge/build.gradle

@@ -0,0 +1,27 @@
+defaultTasks 'distZip'
+
+apply plugin: 'java-library'
+apply plugin: 'application'
+
+
+sourceCompatibility = 1.17
+version = 'SNAPSHOT'
+mainClassName = 'de.nplusc.izc.MegatronBridge.C3'
+
+
+jar{
+	manifest{
+		attributes 'Implementation-Title': 'MegatronBridge',
+					'Implementation-Version': 'SNAPSHOT',
+					'Main-Class': 'de.nplusc.izc.MegatronBridge.C3'
+					
+	}
+}
+
+repositories{
+	mavenCentral()
+}
+
+dependencies{
+    implementation 'com.fazecast:jSerialComm:[2.0.0,3.0.0)'
+}

+ 9 - 0
Megatron-Bridge/src/main/java/de/nplusc/izc/MegatronBridge/C3.java

@@ -0,0 +1,9 @@
+package de.nplusc.izc.MegatronBridge;
+
+public class C3
+{
+    public static void main(String[] args)
+    {
+
+    }
+}

+ 8 - 0
Megatron-Bridge/src/main/java/de/nplusc/izc/MegatronBridge/CommandQueueItem.java

@@ -0,0 +1,8 @@
+package de.nplusc.izc.MegatronBridge;
+
+public class CommandQueueItem
+{
+    public CommandType type;
+    public int argument;
+    public String patharg;
+}

+ 11 - 0
Megatron-Bridge/src/main/java/de/nplusc/izc/MegatronBridge/CommandType.java

@@ -0,0 +1,11 @@
+package de.nplusc.izc.MegatronBridge;
+
+//internal action types, not directly exposed publicly
+public enum CommandType
+{
+    ServoAction,
+    OnboardAudio,
+    CustomAudio,
+    StreamAudio,
+    MundwerkToggle,
+}

+ 270 - 0
Megatron-Bridge/src/main/java/de/nplusc/izc/MegatronBridge/SerialSpammer.java

@@ -0,0 +1,270 @@
+package de.nplusc.izc.MegatronBridge;
+
+
+import com.fazecast.jSerialComm.SerialPort;
+
+import javax.sql.rowset.serial.SQLOutputImpl;
+import java.io.*;
+import java.util.LinkedList;
+import java.util.Queue;
+
+enum NextChunkAction
+{
+    WAIT,
+    REPEAT,
+    CONTINUE,
+    FINISHED
+}
+
+public class SerialSpammer
+{
+
+
+    private InputStream in = null;
+    private OutputStream o = null;
+    private NextChunkAction nextChunkReady = NextChunkAction.WAIT;
+    private int waitCtr = 0;
+    private Object workaround = new Object();
+
+    private Queue<CommandQueueItem> queue= new LinkedList<>();
+    private boolean startedStreamPlayback = false;
+
+    public Queue<CommandQueueItem> getQueue()
+    {
+        return queue;
+    }
+
+    private final char[] hex = "0123456789ABCDEF".toCharArray();
+    private RandomAccessFile streamfile = null;
+    public SerialSpammer(String comPortName)
+    {
+
+        System.out.println("Gobblygob");
+
+        int sendOffset = 0;
+        SerialPort comPort = SerialPort.getCommPort(comPortName);
+        comPort.openPort();
+        comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
+        in = comPort.getInputStream();
+        o = comPort.getOutputStream();
+        new Thread(()->
+
+        {
+            System.out.println("MunchMunchMunch");
+            try
+            {
+                while (true)
+                {
+                    if(in.available()<1)
+                    {
+                        continue;
+                    }
+                    int read = in.read();
+                    if (read == '~')
+                    {
+                        System.out.println("RSME");
+                        nextChunkReady = NextChunkAction.CONTINUE;
+                        synchronized (workaround)
+                        {
+                            System.out.println("POKE");
+                            workaround.notify();
+                        }
+
+                    }
+                    if (read == '<')
+                    {
+                        nextChunkReady = NextChunkAction.REPEAT;
+                        synchronized (workaround)
+                        {
+                            System.out.println("POKE");
+                            workaround.notify();
+                        }
+                    }
+
+                    System.out.write(read);
+                    System.out.flush();
+                }
+            }
+            catch (IOException e)
+            {
+                e.printStackTrace();
+            }
+        }).start();
+
+        new Thread(()->
+
+        {
+        while(true)
+        {
+            CommandQueueItem itm = queue.poll();
+            if(itm != null)
+            {
+                switch (itm.type)
+                {
+                    case ServoAction:
+                        if(itm.argument>15&&itm.argument<0)
+                        {
+                            break;
+                        }
+                        try
+                        {
+                            o.write('*');
+                            o.write('S');
+                            o.write(hex[itm.argument]);
+                            o.write('G');
+                        } catch (IOException e)
+                        {
+                            e.printStackTrace();
+                        }
+                        break;
+                    case OnboardAudio:
+                        if(itm.argument>15&&itm.argument<0)
+                        {
+                            break;
+                        }
+                        try
+                        {
+                            o.write('*');
+                            o.write('T');
+                            o.write(hex[itm.argument]);
+                            o.write('G');
+                        } catch (IOException e)
+                        {
+                            e.printStackTrace();
+                        }
+                        break;
+                    case CustomAudio:
+                        if(itm.argument<0)
+                        {
+                            break;
+                        }
+                        LinkedList<Character> reverser = new LinkedList<>();
+                        int arg = itm.argument;
+                        while(arg>15)
+                        {
+                            reverser.add(hex[arg%16]);
+                            arg>>=4;
+                        }
+                        try
+                        {
+                            o.write('*');
+                            while(true)
+                            {
+                                Character c = reverser.pollLast();
+                                if(c==null)
+                                {
+                                    break;
+                                }
+                                o.write(c);
+                            }
+                            o.write('v');
+                        } catch (IOException e)
+                        {
+                            e.printStackTrace();
+                        }
+
+                        break;
+
+                    case StreamAudio:
+                        String path = itm.patharg;
+                        System.out.println("Streaming: "+path);
+                        if(new File(path).exists())
+                        {
+                            try
+                            {
+                                streamfile = new RandomAccessFile(path,"r");
+                                nextChunkReady=NextChunkAction.CONTINUE;
+                                synchronized (workaround)
+                                {
+                                    System.out.println("POKE");
+                                    workaround.notify();
+                                }
+                            } catch (FileNotFoundException e)
+                            {
+                                e.printStackTrace();
+                            }
+
+                        }
+                        break;
+                    case MundwerkToggle:
+                        try
+                        {
+                            o.write('M');
+                            try
+                            {
+                                Thread.sleep(10);
+                            } catch (InterruptedException e)
+                            {
+                                e.printStackTrace();
+                            }
+                        } catch (IOException e)
+                        {
+                            e.printStackTrace();
+                        }
+                        break;
+                }
+            }
+            if(streamfile!=null)
+            {
+                try{
+                if(streamfile.getFilePointer()>=streamfile.length())
+                {
+                    System.out.println("FINI");
+                    System.err.println("FINI");
+                    streamfile.close();
+                    streamfile=null;
+                    startedStreamPlayback=false;
+                }
+                else
+                {
+                    if((!startedStreamPlayback)&&(streamfile.getFilePointer()==8192||streamfile.getFilePointer()==streamfile.length()))
+                    {
+                        System.out.println("PLAY");
+                        o.write('|');
+                        startedStreamPlayback=true;
+                    }
+                    switch(nextChunkReady)
+                    {
+                        case REPEAT:
+                            System.out.println("RTRY=("+streamfile.getFilePointer()+")");
+                            waitCtr=0;
+                            if(streamfile.getFilePointer()>=64)
+                            {
+                                streamfile.seek(streamfile.getFilePointer()-64);
+                            }
+                        case CONTINUE:
+                            waitCtr=0;
+                            System.out.println("STR=("+streamfile.getFilePointer()+")");
+                            byte[] readbfr = new byte[64];
+                            streamfile.read(readbfr);
+                            o.write('>');
+                            nextChunkReady=NextChunkAction.WAIT;
+                            o.write(readbfr);
+
+                            break;
+                    }
+                    synchronized (workaround)
+                    {
+                        if(nextChunkReady==NextChunkAction.WAIT)
+                        {
+                            System.out.println("GOTO WAIT");
+                            try
+                            {
+                                workaround.wait();
+                            } catch (InterruptedException e)
+                            {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                }
+                }
+                catch (IOException e)
+                {
+
+                }
+            }
+        }
+        }).start();
+    }
+}

+ 41 - 6
QuickStuff/src/main/java/QuickVerifyCrap/SerialSpammer.java

@@ -18,13 +18,15 @@ public class SerialSpammer
     static InputStream in = null;
     static OutputStream o = null;
     static NextChunkAction nextChunkReady = NextChunkAction.WAIT;
+    static int waitCtr = 0;
+    static Object workaround = new Object();
     public static void main(String[] args)
     {
         boolean startedStreamPlayback = false;
         System.out.println("Gobblygob");
         RandomAccessFile streamfile = null;
         int sendOffset = 0;
-        SerialPort comPort = SerialPort.getCommPort("ttyACM0");
+        SerialPort comPort = SerialPort.getCommPort("COM20");
         comPort.openPort();
         comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
         in = comPort.getInputStream();
@@ -46,12 +48,25 @@ public class SerialSpammer
                         int read = in.read();
                         if (read == '~')
                         {
+                            System.out.println("RSME");
                             nextChunkReady = NextChunkAction.CONTINUE;
+                            synchronized (workaround)
+                            {
+                                System.out.println("POKE");
+                                workaround.notify();
+                            }
+
                         }
                         if (read == '<')
                         {
                             nextChunkReady = NextChunkAction.REPEAT;
+                            synchronized (workaround)
+                            {
+                                System.out.println("POKE");
+                                workaround.notify();
+                            }
                         }
+
                         System.out.write(read);
                         System.out.flush();
                     }
@@ -96,6 +111,11 @@ public class SerialSpammer
                             {
                                 streamfile = new RandomAccessFile(path,"r");
                                 nextChunkReady=NextChunkAction.CONTINUE;
+                                synchronized (workaround)
+                                {
+                                    System.out.println("POKE");
+                                    workaround.notify();
+                                }
                             }
                         }
                     }
@@ -104,6 +124,10 @@ public class SerialSpammer
                         //System.out.println("Fuick");
                         o.write(chara);
                         o.flush();
+                        if(chara=='\n')
+                        {
+                            System.out.println("waitctr="+waitCtr);
+                        }
                         //System.out.println("ZilseZilse");
                     }
                 }
@@ -112,13 +136,15 @@ public class SerialSpammer
 
                     if(streamfile.getFilePointer()>=streamfile.length())
                     {
+                        System.out.println("FINI");
+                        System.err.println("FINI");
                         streamfile.close();
                         streamfile=null;
                         startedStreamPlayback=false;
                     }
                     else
                     {
-                        if((!startedStreamPlayback)&&(streamfile.getFilePointer()==192||streamfile.getFilePointer()==streamfile.length()))
+                        if((!startedStreamPlayback)&&(streamfile.getFilePointer()==8192||streamfile.getFilePointer()==streamfile.length()))
                         {
                             System.out.println("PLAY");
                             o.write('|');
@@ -127,21 +153,30 @@ public class SerialSpammer
                         switch(nextChunkReady)
                         {
                             case REPEAT:
+                                System.out.println("RTRY=("+streamfile.getFilePointer()+")");
+                                waitCtr=0;
                                 if(streamfile.getFilePointer()>=64)
                                 {
                                     streamfile.seek(streamfile.getFilePointer()-64);
                                 }
                             case CONTINUE:
+                                waitCtr=0;
                                 System.out.println("STR=("+streamfile.getFilePointer()+")");
                                 byte[] readbfr = new byte[64];
                                 streamfile.read(readbfr);
                                 o.write('>');
-                                o.write(readbfr);
                                 nextChunkReady=NextChunkAction.WAIT;
+                                o.write(readbfr);
+
                             break;
-                            case WAIT:
-                                //NOP;
-                                break;
+                        }
+                        synchronized (workaround)
+                        {
+                            if(nextChunkReady==NextChunkAction.WAIT)
+                            {
+                                System.out.println("GOTO WAIT");
+                                workaround.wait();
+                            }
                         }
                     }
                 }

+ 16 - 0
QuickStuff/src/main/java/QuickVerifyCrap/Zilsnerator.java

@@ -0,0 +1,16 @@
+package QuickVerifyCrap;
+
+public class Zilsnerator
+{
+    public static void main(String[] args)
+    {
+        char[] hex = "0123456789ABCDEF".toCharArray();
+        for(int i=0;i<16;i++)
+        {
+            for(int j=0;j<16;j++)
+            {
+                System.out.print(" 0x"+hex[i]+hex[j]);
+            }
+        }
+    }
+}

+ 1 - 1
settings.gradle

@@ -1,4 +1,4 @@
 include 'ToolKit','iZpl',  'IZSetup','WPCMGr','UpidTK', 'izstreamer', 'LogBlockHeatMapper','iZlaunch',
 'iZpaqSFX','MazeViewer','TWPUtil',"iZplPlugins:WMP","iZplPlugins:foobar2000_others","iZplPlugins:itunes","iZplPlugins:GameRadio",
 "iZplPlugins:Editor","izpl-shared","iZpl-server","iZplPlugins:Smartphonizer","QuickStuff","external:java-progressbar","iZplPlugins:rtsslink","iZplPlugins:JukeBox",
-"iZplPlugins:Extractor","spungit_deathtimer","iZplPlugins:discordjukebox","SenaBitWiggler","PlaceDECSVWiggler","SplitSecondARKiver","GPNBots"
+"iZplPlugins:Extractor","spungit_deathtimer","iZplPlugins:discordjukebox","SenaBitWiggler","PlaceDECSVWiggler","SplitSecondARKiver","GPNBots","Megatron-Bridge"