Guest
Public paste!

Untitled

By: a guest | Mar 15th, 2010 | Syntax: Java | Size: 4.68 KB | Hits: 64 | Expires: Never
Copy text to clipboard
  1. import processing.serial.*;
  2.  
  3. // Globals
  4. int g_winW             = 800;   // Window Width
  5. int g_winH             = 600;   // Window Height
  6. boolean g_dumpToFile   = true;  // Dumps data to c:\\output.txt in a comma seperated format (easy to import into Excel)
  7. boolean g_enableFilter = true;  // Enables simple filter to help smooth out data.
  8.  
  9. cDataArray g_xAccel    = new cDataArray(200);
  10. cDataArray g_yAccel    = new cDataArray(200);
  11. cDataArray g_zAccel    = new cDataArray(200);
  12. cGraph g_graph         = new cGraph(10, 190, 780, 400);
  13. Serial g_serial;
  14. PFont  g_font;
  15.  
  16. void setup()
  17. {
  18.   size(g_winW, g_winH, P2D);
  19.  
  20.   println(Serial.list());
  21.   g_serial = new Serial(this, Serial.list()[1], 9600, 'N', 8, 1.0);
  22.   g_font = loadFont("ArialMT-20.vlw");
  23.   textFont(g_font, 14);
  24.  
  25.   strokeWeight(2);
  26.   stroke(255, 0, 0);     line(20, 420, 35, 420);
  27.   stroke(0, 255, 0);     line(20, 440, 35, 440);
  28.   stroke(0, 0, 255);     line(20, 460, 35, 460);
  29.   fill(0, 0, 0);
  30.   text("xAccel", 40, 430);
  31.   text("yAccel", 40, 450);
  32.   text("zAccel", 40, 470);
  33. }
  34.  
  35. void draw()
  36. {
  37.   while (g_serial.available() >= 2*6+2)
  38.   {
  39.     processSerialData();
  40.   }
  41.  
  42.   strokeWeight(1);
  43.   fill(255, 255, 255);
  44.   g_graph.drawGraphBox();
  45.  
  46.   strokeWeight(1);
  47.   stroke(255, 0, 0);
  48.   g_graph.drawLine(g_xAccel, 0, 1024);
  49.   stroke(0, 255, 0);
  50.   g_graph.drawLine(g_yAccel, 0, 1024);
  51.   stroke(0, 0, 255);
  52.   g_graph.drawLine(g_zAccel, 0, 1024);
  53. }
  54.  
  55. void processSerialData()
  56. {
  57.   int inByte = 0;
  58.   int curMatchPos = 0;
  59.   int[] intBuf = new int[2];
  60.  
  61.   intBuf[0] = 0xAD;
  62.   intBuf[1] = 0xDE;
  63.  
  64.   while (g_serial.available() < 2); // Loop until we have enough bytes
  65.   inByte = g_serial.read();
  66.  
  67.   while(curMatchPos < 2)
  68.   {
  69.     if (inByte == intBuf[curMatchPos])
  70.     {
  71.       ++curMatchPos;
  72.      
  73.       if (curMatchPos == 2)
  74.         break;
  75.    
  76.       while (g_serial.available() < 2); // Loop until we have enough bytes
  77.       inByte = g_serial.read();
  78.     }
  79.     else
  80.     {
  81.       if (curMatchPos == 0)
  82.       {
  83.         while (g_serial.available() < 2); // Loop until we have enough bytes
  84.         inByte = g_serial.read();
  85.       }
  86.       else
  87.       {
  88.         curMatchPos = 0;
  89.       }
  90.     }
  91.   }
  92.  
  93.   while (g_serial.available() < 2*6);
  94.   {
  95.     byte[] inBuf = new byte[2];
  96.     int xAccel, yAccel, zAccel;
  97.  
  98.     g_serial.readBytes(inBuf);
  99.     xAccel = ((int)(inBuf[1]&0xFF) << 8) + ((int)(inBuf[0]&0xFF) << 0);
  100.     g_serial.readBytes(inBuf);
  101.     yAccel = ((int)(inBuf[1]&0xFF) << 8) + ((int)(inBuf[0]&0xFF) << 0);
  102.     g_serial.readBytes(inBuf);
  103.     zAccel = ((int)(inBuf[1]&0xFF) << 8) + ((int)(inBuf[0]&0xFF) << 0);
  104.     g_serial.readBytes(inBuf);
  105.     g_serial.readBytes(inBuf);
  106.     g_serial.readBytes(inBuf);
  107.    
  108.     g_xAccel.addVal(xAccel);
  109.     g_yAccel.addVal(yAccel);
  110.     g_zAccel.addVal(zAccel);
  111.   }
  112. }
  113.  
  114. class cDataArray
  115. {
  116.   float[] m_data;
  117.   int m_maxSize;
  118.   int m_startIndex = 0;
  119.   int m_endIndex = 0;
  120.   int m_curSize;
  121.  
  122.   cDataArray(int maxSize)
  123.   {
  124.     m_maxSize = maxSize;
  125.     m_data = new float[maxSize];
  126.   }
  127.  
  128.   void addVal(float val)
  129.   {
  130.    
  131.     if (g_enableFilter && (m_curSize != 0))
  132.     {
  133.       int indx;
  134.      
  135.       if (m_endIndex == 0)
  136.         indx = m_maxSize-1;
  137.       else
  138.         indx = m_endIndex - 1;
  139.      
  140.       m_data[m_endIndex] = getVal(indx)*.5 + val*.5;
  141.     }
  142.     else
  143.     {
  144.       m_data[m_endIndex] = val;
  145.     }
  146.    
  147.     m_endIndex = (m_endIndex+1)%m_maxSize;
  148.     if (m_curSize == m_maxSize)
  149.     {
  150.       m_startIndex = (m_startIndex+1)%m_maxSize;
  151.     }
  152.     else
  153.     {
  154.       m_curSize++;
  155.     }
  156.   }
  157.  
  158.   float getVal(int index)
  159.   {
  160.     return m_data[(m_startIndex+index)%m_maxSize];
  161.   }
  162.  
  163.   int getCurSize()
  164.   {
  165.     return m_curSize;
  166.   }
  167.  
  168.   int getMaxSize()
  169.   {
  170.     return m_maxSize;
  171.   }
  172. }
  173.  
  174. class cGraph
  175. {
  176.   float m_gWidth, m_gHeight;
  177.   float m_gLeft, m_gBottom, m_gRight, m_gTop;
  178.  
  179.   cGraph(float x, float y, float w, float h)
  180.   {
  181.     m_gWidth     = w;
  182.     m_gHeight    = h;
  183.     m_gLeft      = x;
  184.     m_gBottom    = g_winH - y;
  185.     m_gRight     = x + w;
  186.     m_gTop       = g_winH - y - h;
  187.   }
  188.  
  189.   void drawGraphBox()
  190.   {
  191.     stroke(0, 0, 0);
  192.     rectMode(CORNERS);
  193.     rect(m_gLeft, m_gBottom, m_gRight, m_gTop);
  194.   }
  195.  
  196.   void drawLine(cDataArray data, float minRange, float maxRange)
  197.   {
  198.     float graphMultX = m_gWidth/data.getMaxSize();
  199.     float graphMultY = m_gHeight/(maxRange-minRange);
  200.    
  201.     for(int i=0; i<data.getCurSize()-1; ++i)
  202.     {
  203.       float x0 = i*graphMultX+m_gLeft;
  204.       float y0 = m_gBottom-((data.getVal(i)-minRange)*graphMultY);
  205.       float x1 = (i+1)*graphMultX+m_gLeft;
  206.       float y1 = m_gBottom-((data.getVal(i+1)-minRange)*graphMultY);
  207.       line(x0, y0, x1, y1);
  208.     }
  209.   }
  210. }