checkpoint
authoradam <adam@megacz.com>
Fri, 7 Apr 2006 13:34:05 +0000 (14:34 +0100)
committeradam <adam@megacz.com>
Fri, 7 Apr 2006 13:34:05 +0000 (14:34 +0100)
src/edu/berkeley/obits/AtmelSerial.java
src/edu/berkeley/obits/device/atmel/AvrDrone.c
src/edu/berkeley/obits/device/atmel/AvrDrone.java

index e30697a..fc95431 100644 (file)
@@ -320,7 +320,7 @@ public class AtmelSerial {
             //scan(at40k, cell, YLUT, true);
             //scan(at40k, cell, YLUT, false);
 
-            device.scanFPGA();
+            device.scanFPGA(true);
             Visualizer v = new Visualizer(at40k, device);
             v.show();
             v.setSize(1380, 1080);
@@ -556,6 +556,7 @@ public class AtmelSerial {
                 }
                 case ' ': {
                     enabled = !enabled;
+                    break;
                 }
                 case 'C': {
                     if (selx==-1 || sely==-1) break;
@@ -573,48 +574,57 @@ public class AtmelSerial {
                     break;
                 }
             } 
-            for(int x=5; x<=PIPELEN; x++) {
-                At40k.Cell cell = dev.cell(x, 22);
+            for(int xx=5; xx<=PIPELEN; xx++) {
+                final int x = xx;
+                final At40k.Cell cell = dev.cell(x, 22);
+                AvrDrone.ByteCallback bc = new AvrDrone.ByteCallback() {
+                        public void call(byte b) throws Exception {
+                            boolean y = (b & 0x80) != 0;
+                            
+                            Graphics g = getGraphics();
+                            g.setFont(new Font("sansserif", Font.BOLD, 24));
+                            g.setColor(Color.white);
+                            g.drawString("0", left(cell) + 12, top(cell) + 30);
+                            g.drawString("1", left(cell) + 12, top(cell) + 30);
+                            //g.setColor(RED);
+                            //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
+                            
+                            //g.drawString((y?"1":"0"), left(cell) + 12, top(cell) + 30);
+                            drawCell(g, x, 22, y?new Color(0xff, 0x99, 0x99):new Color(0x99, 0xff, 0x99));
+                        }
+                    };
+
                 scan(dev, cell, YLUT, true);
-                boolean y = (drone.readBus() & 0x80) != 0;
+                drone.readBus(bc);
                 scan(dev, cell, YLUT, false);
-                
-                Graphics g = getGraphics();
-                g.setFont(new Font("sansserif", Font.BOLD, 24));
-                g.setColor(Color.white);
-                g.drawString("0", left(cell) + 12, top(cell) + 30);
-                g.drawString("1", left(cell) + 12, top(cell) + 30);
-                //g.setColor(RED);
-                //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
-                
-                //g.drawString((y?"1":"0"), left(cell) + 12, top(cell) + 30);
-                drawCell(g, x, 22, y?new Color(0xff, 0x99, 0x99):new Color(0x99, 0xff, 0x99));
             }
         }
         public void mousePressed(MouseEvent e) {
-                            At40k.Cell cell = dev.cell(selx, sely);
-                            if (cell==null) return;
-                            int old = cell.c();
-                            scan(dev, cell, YLUT, true);
-                            boolean y = (drone.readBus() & 0x80) != 0;
-                            //scan(dev, cell, XLUT, true);
-                            //boolean x = (drone.readBus() & 0x80) != 0;
-                            scan(dev, cell, YLUT, false);
-                            cell.c(old);
-                            Graphics g = getGraphics();
-                            g.setFont(new Font("sansserif", Font.BOLD, 14));
-                            g.setColor(Color.white);
-                            //g.drawString("X=0", left(cell) + 10, top(cell) + 20);
-                            //g.drawString("X=1", left(cell) + 10, top(cell) + 20);
-                            /*
-                            g.setColor(Color.white);
-                            g.drawString("Y=0", left(cell) + 8, top(cell) + 35);
-                            g.drawString("Y=1", left(cell) + 8, top(cell) + 35);
-                            */
-                            //g.setColor(RED);
-                            //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
-                            g.setColor(BLUE);
-                            g.drawString("Y="+(y?"1":"0"), left(cell) + 8, top(cell) + 35);
+            /*
+            At40k.Cell cell = dev.cell(selx, sely);
+            if (cell==null) return;
+            int old = cell.c();
+            scan(dev, cell, YLUT, true);
+            boolean y = (drone.readBus() & 0x80) != 0;
+            //scan(dev, cell, XLUT, true);
+            //boolean x = (drone.readBus() & 0x80) != 0;
+            scan(dev, cell, YLUT, false);
+            cell.c(old);
+            Graphics g = getGraphics();
+            g.setFont(new Font("sansserif", Font.BOLD, 14));
+            g.setColor(Color.white);
+            //g.drawString("X=0", left(cell) + 10, top(cell) + 20);
+            //g.drawString("X=1", left(cell) + 10, top(cell) + 20);
+            
+            //g.setColor(Color.white);
+            //g.drawString("Y=0", left(cell) + 8, top(cell) + 35);
+            //g.drawString("Y=1", left(cell) + 8, top(cell) + 35);
+            
+            //g.setColor(RED);
+            //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
+            g.setColor(BLUE);
+            g.drawString("Y="+(y?"1":"0"), left(cell) + 8, top(cell) + 35);
+            */
         }
 
         public void mouseMoved(MouseEvent e) {
@@ -656,11 +666,13 @@ public class AtmelSerial {
         }
         public void refresh() {
             Graphics g = getGraphics();
+            /*
             int data = drone.readBus() & 0xff;
             for(int i=0; i<8; i++) {
                 g.setColor((data & (1<<i))==0 ? Color.black : Color.green);
                 g.drawString("D"+i,  (24*(WIDTH+2))+20, ((23-(i+7))*(HEIGHT+2))+60-HEIGHT/2);
             }
+            */
         }
         public static int left(At40k.Cell cell) { return (cell.col)   *(WIDTH+2)+20; }
         public static int top(At40k.Cell cell)  { return (23-cell.row)*(HEIGHT+2)+60; }
@@ -827,6 +839,7 @@ public class AtmelSerial {
     }
 
     public static void selfTest(AvrDrone device, At40k at40k, Visualizer v) {
+        /*
             int fail = 0;
             long now = System.currentTimeMillis();
             for(int x=0; x<24; x++)
@@ -856,6 +869,7 @@ public class AtmelSerial {
 
             System.out.println("failures: " + fail);
             System.out.println("scan time: " + (System.currentTimeMillis()-now) + "ms");
+        */
     }
 
 }
index d46dd49..b50dd07 100644 (file)
@@ -82,26 +82,34 @@ inline char recv() {
 }\r
 \r
 ISR(SIG_UART1_DATA) {\r
-  //if (write_empty()) return;\r
+\r
+  if (write_empty()) {\r
+    UCSR1B &= ~(1 << UDRIE1);\r
+    return;\r
+  }\r
+  /*\r
   portd(1, 0);\r
   _delay_ms(10);\r
   portd(1, 1);\r
   _delay_ms(10);\r
-  UCSR1B &= ~(1 << UDRIE1);\r
+  */\r
+  char ret = write_buf[write_buf_head];\r
+  write_buf_head = inc(write_buf_head);\r
+  UDR1 = (int)ret;\r
+\r
   sei();\r
 }\r
 \r
 void send(char c) {\r
 \r
+  while (write_full());\r
+\r
   write_buf[write_buf_tail] = c;\r
   write_buf_tail = inc(write_buf_tail);\r
 \r
   UCSR1B |= (1 << UDRIE1);\r
 \r
-  while(!(UCSR1A & (1 << UDRE1)));           /* Wait for data Regiester to be empty */\r
-  char ret = write_buf[write_buf_head];\r
-  write_buf_head = inc(write_buf_head);\r
-  UDR1 = (int)ret;\r
+  //while(!(UCSR1A & (1 << UDRE1)));           /* Wait for data Regiester to be empty */\r
 }\r
 \r
 \r
@@ -145,10 +153,12 @@ void doreset() {
 }\r
 \r
 #define TIMERVAL 100\r
+static volatile int sending = 0;\r
 ISR(SIG_OVERFLOW0) { \r
   PORTD = ~FISUA;\r
   TCNT0 = TIMERVAL;           // load the nearest-to-one-second value  into the timer0\r
-  TIMSK |= (1<<TOIE0);        //enable the compare match1 interrupt and the timer/counter0 overflow interrupt\r
+  TIMSK |= (1<<TOIE0);        // enable the compare match1 interrupt and the timer/counter0 overflow interrupt\r
+  if (sending) UDR1 = FISUA;\r
   sei();\r
 } \r
 void init_timer()  { \r
@@ -211,6 +221,12 @@ int main() {
       case 3:\r
         init_timer();\r
         break;\r
+      case 4:\r
+        sending = 1;\r
+        break;\r
+      case 5:\r
+        sending = 0;\r
+        break;\r
       default: die();\r
     }\r
   }\r
index 9e7da7e..35581da 100644 (file)
@@ -19,6 +19,7 @@ public class AvrDrone extends AtmelDevice {
         this.sp = sp;
         sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
         sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
+        sp.setInputBufferSize(1024);
         //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
         this.out = new DataOutputStream(sp.getOutputStream());
         this.in = new DataInputStream(sp.getInputStream());
@@ -35,18 +36,47 @@ public class AvrDrone extends AtmelDevice {
         Log.info(this, "device correctly identified itself; ready for operation");
     }
 
-    public synchronized void scanFPGA() throws DeviceException {
+    public synchronized void scanFPGA(boolean on) throws DeviceException {
         try {
-            out.writeByte(3);
-            out.flush();
+            if (on) {
+                out.writeByte(3);
+                out.flush();
+            } else {
+                // FIXME
+            }
         } catch (IOException e) { throw new DeviceException(e); }
     }
 
-    public synchronized byte readBus() throws DeviceException {
+    public static interface ByteCallback {
+        public void call(byte b) throws Exception;
+    }
+
+    private Vector callbacks = new Vector();
+
+    private Thread reader = new Thread() {
+            public void run() {
+                while(true) {
+                    try {
+                        byte b = in.readByte();
+                        ByteCallback bc = (ByteCallback)callbacks.remove(0);
+                        bc.call(b);
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        };
+
+    public synchronized void readBus(ByteCallback bc) throws DeviceException {
         try {
+            System.out.println("capacity: " + callbacks.size());
+            callbacks.add(bc);
             out.writeByte(2);
             out.flush();
-            return in.readByte();
+            if (reader != null) {
+                reader.start();
+                reader = null;
+            }
         } catch (IOException e) { throw new DeviceException(e); }
     }