123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- package de.nplusc.izc.MegatronBridge;
- import com.fazecast.jSerialComm.SerialPort;
- import java.io.*;
- import java.util.Arrays;
- 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 int streampointer = 0;
- private Queue<CommandQueueItem> queue= new LinkedList<>();
- private boolean startedStreamPlayback = false;
- public Queue<CommandQueueItem> getQueue()
- {
- return queue;
- }
- private final char[] hex = "0123456789ABCDEF".toCharArray();
- private byte[] 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;
- }
- boolean wiggleMouth = false;
- if(itm.argument>0xFFFF)
- {
- wiggleMouth=true;
- itm.argument&=0xFFFF;
- }
- System.out.println(itm.argument);
- LinkedList<Character> reverser = new LinkedList<>();
- int arg = itm.argument;
- while(arg>0)
- {
- reverser.add(hex[arg%16]);
- arg>>=4;
- }
- try
- {
- o.write('*');
- while(true)
- {
- Character c = reverser.pollLast();
- if(c==null)
- {
- break;
- }
- o.write(c);
- System.out.println("CSEND="+c);
- }
- o.write(wiggleMouth?'V':'v');
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- break;
- case StreamAudio:
- if(streamfile==null)
- {
- streamfile = itm.streamData;
- nextChunkReady=NextChunkAction.CONTINUE;
- synchronized (workaround)
- {
- System.out.println("POKE");
- workaround.notify();
- }
- }
- break;
- case MundwerkToggle:
- try
- {
- o.write('M');
- try
- {
- Thread.sleep(10);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- break;
- case RawCommand:
- try
- {
- for(char c:itm.extendedArg.toCharArray())
- {
- o.write(c);
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- break;
- case VolumeSetter:
- try
- {
- for(int i=0;i<8;i++)
- {
- o.write('-');
- }
- for(int i=0;i<itm.argument;i++)
- {
- o.write('+');
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- break;
- }
- }
- if(streamfile!=null)
- {
- try{
- if(streampointer>=streamfile.length)
- {
- System.out.println("FINI");
- System.err.println("FINI");
- streamfile=null;
- startedStreamPlayback=false;
- }
- else
- {
- if((!startedStreamPlayback)&&(streampointer==8192||streampointer==streamfile.length))
- {
- System.out.println("PLAY");
- o.write('|');
- startedStreamPlayback=true;
- }
- switch(nextChunkReady)
- {
- case REPEAT:
- System.out.println("RTRY=("+streampointer+")");
- waitCtr=0;
- if(streampointer>=64)
- {
- streampointer-=64;
- }
- case CONTINUE:
- waitCtr=0;
- System.out.println("STR=("+streampointer+")");
- byte[] readbfr = Arrays.copyOfRange(streamfile,streampointer,streampointer+64);
- streampointer+=64;
- 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)
- {
- System.out.println("Zöinks");
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- }
|