BlinkenSimJava v.0.1 (2004-06-29)
Christian Heimke

Christian Heimke commited on 2011-07-15 09:12:42
Showing 10 changed files, with 373 additions and 0 deletions.

... ...
@@ -0,0 +1,80 @@
1
+import java.lang.*;
2
+import java.applet.*;
3
+import java.awt.*;
4
+
5
+public class BlinkenSim extends Applet
6
+{ 
7
+  private DynMcufClient mcuf;
8
+  private FrameProcessor frameProc;
9
+
10
+  public void init( )
11
+  {
12
+    String host = "";
13
+    int port = 0;
14
+
15
+    host = getParameter( "host" );
16
+    if( host.length( ) <= 0 )
17
+      host = "proxy.blinkenlights.de";
18
+    
19
+    port = Integer.parseInt( getParameter( "port" ) );
20
+    if( port <= 0 || port > 65535 )
21
+      port = 4242;
22
+
23
+    mcuf = new DynMcufClient( );
24
+    frameProc = new FrameProcessor( this );
25
+    mcuf.start( host, port, frameProc );
26
+  }
27
+
28
+  public void stop( )
29
+  {
30
+    mcuf.stop( );
31
+    mcuf = null;
32
+    frameProc = null;
33
+  }
34
+
35
+  public void paint( Graphics g )
36
+  {
37
+    int height, width, channels;
38
+    byte[][][] data;
39
+    Image img;
40
+    Graphics gBuf;
41
+    int y, x, c, val;
42
+
43
+    Frame frame = frameProc.getFrame( );
44
+    height = frame.getHeight( );
45
+    width = frame.getWidth( );
46
+    channels = frame.getChannels( );
47
+    data = frame.getData( );
48
+    
49
+    img = createImage( width * 10 + 20, height * 20 + 50 );
50
+    gBuf = img.getGraphics( );
51
+
52
+    gBuf.setColor( Color.black );
53
+    gBuf.fillRect( 0, 0, width * 10 + 20, height * 20 + 50 );
54
+    
55
+    gBuf.setColor( Color.white );
56
+    gBuf.drawString( "BlinkenSimJava: " + height + "x" + width + "-" + channels, 20, 20 );
57
+
58
+    for( y = 0; y < height; y++ )
59
+    {
60
+      for( x = 0; x < width; x++ )
61
+      {
62
+        val = 0;
63
+        for( c = 0; c < channels; c++ )
64
+          val += data[y][x][c] & 0xFF;
65
+        val /= channels;
66
+        gBuf.setColor( new Color( val, val, val ) );
67
+        gBuf.fillRect( x * 10 + 10, y * 20 + 30, 10, 20 );
68
+      }
69
+    }
70
+
71
+    g.drawImage( img, 0, 0, this );
72
+
73
+    //System.out.println( frame.toString( ) );
74
+  }
75
+
76
+  public void update( Graphics g )
77
+  {
78
+    paint( g );
79
+  }
80
+}
... ...
@@ -0,0 +1,58 @@
1
+import java.lang.*;
2
+import java.applet.*;
3
+import java.awt.*;
4
+import java.io.*;
5
+import java.net.*;
6
+
7
+public class DynMcufClient
8
+{
9
+  private DatagramSocket sock = null;
10
+  private InetAddress host;
11
+  private int port;
12
+  private DynMcufClientSend send;
13
+  private DynMcufClientRecv recv;
14
+
15
+  public void start( String hostname, int port, FrameReceiver receiver )
16
+  {
17
+    stop( );
18
+    try
19
+    {
20
+      sock = new DatagramSocket( );
21
+      host = InetAddress.getByName( hostname );
22
+      this.port = port;
23
+      send = new DynMcufClientSend( sock, host, port );
24
+      recv = new DynMcufClientRecv( sock, host, port, receiver );
25
+      recv.start( );
26
+      send.start( );
27
+    }
28
+    catch( SocketException e )
29
+    {
30
+      sock = null;
31
+      host = null;
32
+      port = 0;
33
+    }
34
+    catch( UnknownHostException e )
35
+    {
36
+      sock = null;
37
+      host = null;
38
+      port = 0;
39
+    }
40
+  }
41
+
42
+  public void stop( )
43
+  {
44
+    sock = null;
45
+    host = null;
46
+    port = 0;
47
+    if( send != null )
48
+    {
49
+      send.terminate( );
50
+      send = null;
51
+    }
52
+    if( recv != null )
53
+    {
54
+      recv.terminate( );
55
+      recv = null;
56
+    }
57
+  }
58
+}
... ...
@@ -0,0 +1,63 @@
1
+import java.lang.*;
2
+import java.applet.*;
3
+import java.awt.*;
4
+import java.io.*;
5
+import java.net.*;
6
+
7
+public class DynMcufClientRecv extends Thread
8
+{
9
+  private DatagramSocket sock;
10
+  private InetAddress host;
11
+  private int port;
12
+  private FrameReceiver receiver;
13
+  private boolean termReq = false;
14
+
15
+  DynMcufClientRecv( DatagramSocket sock, InetAddress host, int port, FrameReceiver receiver )
16
+  {
17
+    this.sock = sock;
18
+    this.host = host;
19
+    this.port = port;
20
+    this.receiver = receiver;
21
+  }
22
+
23
+  public void run( )
24
+  {
25
+    while( ! termReq )
26
+    {
27
+      try
28
+      {
29
+        byte[] data = new byte[8192];
30
+        DatagramPacket framePacket = new DatagramPacket( data, 8192 );
31
+        sock.receive( framePacket );
32
+        if( framePacket.getAddress( ).equals( host ) && framePacket.getPort( ) == port )
33
+        {
34
+          int length = framePacket.getLength( );
35
+          data = framePacket.getData( );
36
+          if( length >= 12 && data[0] == 0x23 && data[1] == 0x54
37
+                           && data[2] == 0x26 && data[3] == 0x66 )
38
+          {
39
+            int height = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
40
+            int width = (data[6] & 0xFF) << 8 | (data[7] & 0xFF);
41
+            int channels = (data[8] & 0xFF) << 8 | (data[9] & 0xFF);
42
+            int maxval = (data[10] & 0xFF) << 8 | (data[11] & 0xFF);
43
+            if( height >= 1 && height <= 100 && width >= 1 && width <= 100
44
+             && channels >= 1 && channels <= 3 && maxval >= 1 && maxval <= 255
45
+             && length >= 12 + height * width * channels )
46
+            {
47
+              Frame frame = new Frame( height, width, channels );
48
+              frame.setData( maxval, data, 12 );
49
+              receiver.newFrame( frame );
50
+            }
51
+          }
52
+        }
53
+      }
54
+      catch( IOException e ) { }
55
+    }
56
+  }
57
+
58
+  public void terminate( )
59
+  {
60
+    termReq = true;
61
+    this.interrupt( );
62
+  }
63
+}
... ...
@@ -0,0 +1,42 @@
1
+import java.lang.*;
2
+import java.applet.*;
3
+import java.awt.*;
4
+import java.io.*;
5
+import java.net.*;
6
+
7
+public class DynMcufClientSend extends Thread
8
+{
9
+  private DatagramSocket sock;
10
+  private DatagramPacket request;
11
+  private boolean termReq = false;
12
+
13
+  DynMcufClientSend( DatagramSocket sock, InetAddress host, int port )
14
+  {
15
+    this.sock = sock;
16
+    byte[] data = { 0x42, 0x42, 0x42, 0x42, 0, 0, 0, 0, 0, 0, 0, 0 };
17
+    request = new DatagramPacket( data, 12, host, port );
18
+  }
19
+
20
+  public void run( )
21
+  {
22
+    while( ! termReq )
23
+    {
24
+      try
25
+      {
26
+        sock.send( request );
27
+      }
28
+      catch( IOException e ) { }
29
+      try
30
+      {
31
+        sleep( 10000 );
32
+      }
33
+      catch( InterruptedException e ) { }
34
+    }
35
+  }
36
+
37
+  public void terminate( )
38
+  {
39
+    termReq = true;
40
+    this.interrupt( );
41
+  }
42
+}
... ...
@@ -0,0 +1,74 @@
1
+public class Frame
2
+{
3
+  private int height;
4
+  private int width;
5
+  private int channels;
6
+  private byte[][][] data;
7
+
8
+  Frame( int height, int width, int channels )
9
+  {
10
+    this.height = height;
11
+    this.width = width;
12
+    this.channels = channels;
13
+    data = new byte[height][width][channels];
14
+  }
15
+
16
+  Frame( Frame frame )
17
+  {
18
+    height = frame.height;
19
+    width = frame.width;
20
+    channels = frame.channels;
21
+    data = (byte[][][])frame.data.clone( );
22
+  }
23
+
24
+  public void setData( int maxval, byte[] rawData, int startIndex )
25
+  {
26
+    int i, h, w, c;
27
+    for( i = startIndex, h = 0; h < height; h++ )
28
+      for( w = 0; w < width; w++ )
29
+        for( c = 0; c < channels; c++, i++ )
30
+          if( rawData[i] > maxval )
31
+            data[h][w][c] = (byte)255;
32
+          else
33
+            data[h][w][c] = (byte)((rawData[i] & 0xFF) * 255 / maxval);
34
+  }
35
+
36
+  public int getHeight( )
37
+  {
38
+    return height;
39
+  }
40
+
41
+  public int getWidth( )
42
+  {
43
+    return width;
44
+  }
45
+
46
+  public int getChannels( )
47
+  {
48
+    return channels;
49
+  }
50
+
51
+  public byte[][][] getData( )
52
+  {
53
+    return data;
54
+  }
55
+
56
+  public String toString( )
57
+  {
58
+    String str = "";
59
+    int h, w, c, val;
60
+    for( h = 0; h < height; h++ )
61
+    {
62
+      for( w = 0; w < width; w++ )
63
+      {
64
+        val = 0;
65
+        for( val = 0, c = 0; c < channels; c++ )
66
+          val += (data[h][w][c] & 0xFF);
67
+        val /= channels * 32;
68
+        str = str + " -+*%#&@".substring( val, val + 1); 
69
+      }
70
+      str = str + "\n";
71
+    }
72
+    return str;
73
+  }
74
+}
... ...
@@ -0,0 +1,24 @@
1
+import java.lang.*;
2
+
3
+public class FrameProcessor extends FrameReceiver
4
+{ 
5
+  private BlinkenSim blinkenSim;
6
+  private Frame frame;
7
+
8
+  FrameProcessor( BlinkenSim blinkenSim )
9
+  {
10
+    this.blinkenSim = blinkenSim;
11
+    frame = new Frame( 1, 1, 1 );
12
+  }
13
+
14
+  public void newFrame( Frame frame )
15
+  {
16
+    this.frame = frame;
17
+    blinkenSim.repaint( );
18
+  }
19
+  
20
+  public Frame getFrame( )
21
+  {
22
+    return new Frame( frame );
23
+  }
24
+}
... ...
@@ -0,0 +1,6 @@
1
+public class FrameReceiver
2
+{ 
3
+  public void newFrame( Frame frame )
4
+  {
5
+  }
6
+}
... ...
@@ -0,0 +1,4 @@
1
+BlinkenLights stream viever in Java
2
+done by 1stein@schuermans.info
3
+very incomplete
4
+no comments yet ;)
... ...
@@ -0,0 +1,11 @@
1
+<html>
2
+  <head>
3
+    <title>test for BlinkenSimJava</title>
4
+  </head>
5
+  <body>
6
+    <applet code="BlinkenSim.class" width="300" height="500">
7
+      <param name="host" value="bl_proxy.1stein.no-ip.com">
8
+      <param name="port" value="23231">
9
+    </applet>
10
+  </body>
11
+</html>
... ...
@@ -0,0 +1,11 @@
1
+<html>
2
+  <head>
3
+    <title>test for BlinkenSimJava</title>
4
+  </head>
5
+  <body>
6
+    <applet code="BlinkenSim.class" width="300" height="500">
7
+      <param name="host" value="192.168.0.33">
8
+      <param name="port" value="23231">
9
+    </applet>
10
+  </body>
11
+</html>
0 12