Advertisement
martin2250

Arduino + Processing oscilloscope

Nov 17th, 2013
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1. import processing.serial.*;
  2. import java.awt.event.*;
  3.  
  4. Serial port;
  5. byte[] values;
  6.  
  7.  
  8.  
  9. int threshold = 60;
  10. boolean save = false;
  11. int zoom = 4;
  12. int offsetx = 0;
  13. int lastxPos = -1;
  14. boolean isgoing = false;
  15. int lasts = 0;
  16. int lastxOffset = 0;
  17. byte lastsample = 0;
  18. long last5ks = 0;
  19. long allsamples = 0;
  20. long time = 1000;
  21. boolean capture = true;
  22. boolean zorth = false;
  23. boolean oforbox = false;
  24. int boxstart = 0;
  25. int boxend = 0;
  26.  
  27.  
  28. void setup()
  29. {
  30.   frame.setResizable(true);
  31.   size(displayWidth -100, displayHeight - 100);
  32.   port = new Serial(this, Serial.list()[Serial.list().length - 1], 1000000);    //adjust to your COM-Port
  33.   values = new byte[400000000];
  34.   addMouseWheelListener(new MouseWheelListener() {
  35.     public void mouseWheelMoved(MouseWheelEvent mwe) {
  36.       mouseWheel(mwe.getWheelRotation());
  37.     }
  38.   }
  39.   );
  40.  
  41.   smooth();
  42.   thread("Sample");
  43. }
  44.  
  45.  
  46. void mouseDragged()
  47. {
  48.   if (oforbox)
  49.   {
  50.     boxend = mouseX;
  51.   }
  52.   else
  53.   {
  54.     offsetx = lastxOffset + ((mouseX - lastxPos) * zoom);
  55.     if (offsetx < 0)
  56.       offsetx = 0;
  57.     if ((offsetx) > lasts)
  58.       offsetx = lasts ;
  59.   }
  60. }
  61.  
  62. void mousePressed()
  63. {
  64.   if (oforbox)
  65.   {
  66.     boxstart = mouseX;
  67.     boxend = mouseX;
  68.   }
  69.   else
  70.   {
  71.     lastxPos = mouseX;
  72.     lastxOffset = offsetx;
  73.   }
  74. }
  75.  
  76. void keyPressed() //+++++++++++++++++++++++++++++++
  77. {
  78.   if (key == 's'||key == 'S')
  79.   {
  80.     saveFrame();
  81.   }
  82.  
  83.   if (key == 'c'||key == 'C')
  84.   {
  85.     capture = !capture;
  86.   }
  87.  
  88.   if (key == CODED && keyCode == CONTROL)
  89.   {
  90.     zorth = true;
  91.   }
  92.  
  93.   if (key == 'b'||key == 'B')
  94.   {
  95.     oforbox = !oforbox;
  96.   }
  97.   if (key == 'r'||key == 'R')
  98.   {
  99.     lasts = 0;
  100.     offsetx = 0;
  101.   }
  102. }
  103. void keyReleased()
  104. {
  105.   if (key == CODED && keyCode == CONTROL)
  106.   {
  107.     zorth = false;
  108.   }
  109. }
  110.  
  111. void mouseWheel(int delta) {
  112.   if (zorth)
  113.   {
  114.     threshold -= delta * 4;
  115.     if (threshold > 255)
  116.       threshold = 255;
  117.     if (threshold < 1)
  118.       threshold = 1;
  119.   }
  120.   else
  121.   {
  122.     int oldzoom = zoom;
  123.     zoom += delta;
  124.     if (zoom <= 0)
  125.       zoom = 1;
  126.     if (zoom != oldzoom)
  127.     {  
  128.       if (zoom < oldzoom)
  129.       {
  130.         offsetx += (width - mouseX);
  131.       }
  132.       else
  133.       {
  134.         offsetx -= (width - mouseX);
  135.       }
  136.     }
  137.     if (offsetx < 0)
  138.       offsetx = 0;
  139.     if ((offsetx) > lasts)
  140.       offsetx = lasts ;
  141.   }
  142. }
  143.  
  144. void Sample()
  145. {
  146.   int rbl = 4000;
  147.   int rs = 0;
  148.   byte[] ring = new byte[rbl];
  149.   int rbp = 0;
  150.  
  151.   while (true)
  152.   {
  153.     while (!capture)
  154.     {
  155.       while (port.available () < 1) {
  156.       }
  157.       lastsample = (byte)port.read();
  158.     }
  159.     if (allsamples >= 20000)
  160.     {
  161.       allsamples = 0;
  162.       time = millis() - last5ks;
  163.       last5ks = millis();
  164.     }
  165.     allsamples ++;
  166.  
  167.     while (port.available () < 1) {
  168.     }
  169.     int lastsamplei = port.read();
  170.     if (lastsamplei >= 0)
  171.     {
  172.       lastsample = (byte)lastsamplei;
  173.  
  174.       if (lastsamplei > threshold)
  175.       {
  176.         rs = 4000;
  177.       }
  178.  
  179.       if ( rs-- > 0)
  180.       {
  181.  
  182.         if (!isgoing)
  183.         {
  184.           for (int i = rbp; i < (rbp + rbl); i++)
  185.           {
  186.             if (i >= rbl)            
  187.               values[lasts++] = ring[i - rbl];
  188.             else              
  189.               values[lasts++] = ring[i];
  190.           }
  191.           isgoing = true;
  192.         }
  193.         values[lasts++] = lastsample;
  194.       }
  195.       else
  196.       {
  197.         ring[rbp++] = lastsample;
  198.         if (rbp >= rbl)
  199.         {
  200.           rbp = 0;
  201.         }
  202.         isgoing = false;
  203.       }
  204.     }
  205.   }
  206. }
  207.  
  208. int Yval(int vale) {
  209.   vale *= (height - 140) / 255.0 ;
  210.   vale += 120;
  211.   return Y(vale);
  212. }
  213.  
  214. int Y(int valu)
  215. {
  216.   return height - valu;
  217. }
  218.  
  219. void draw()
  220. {
  221.  
  222.   background(0);
  223.  
  224.  
  225.   stroke(#000060);
  226.  
  227.   int y1 = Yval(threshold) - 15;
  228.   if (y1 < 1)
  229.     y1 = 1;
  230.   fill(#000040);
  231.  
  232.   triangle(1, y1, 1, Yval(threshold) + 15, 15, Yval(threshold)); //THRESHOLD
  233.  
  234.   fill(30);
  235.   stroke(30);
  236.   rect(0, Y(120), width, 120);// BOTTOM
  237.  
  238.  
  239.   fill(#005000);
  240.   stroke(#005000);
  241.   int l = lastsample;
  242.   if (l < 0) l += 256;
  243.   rect(0, Yval(l), 20, (int)(l / 255.0f * (height - 120)) - 1); //CURRENT LEVEL
  244.  
  245.   stroke(#002000);
  246.  
  247.  
  248.  
  249.  
  250.   for (int i = width - 100; i > 0; i -= 100)
  251.   {
  252.     for (int y = 1; y + 120 < height; y+= 40)
  253.     {  
  254.       line(i, Y(y + 120), i, Y(y + 130));
  255.     }
  256.   }
  257.  
  258.  
  259.   fill(#AAAAFF);
  260.  
  261.   textSize(14);
  262.   text("samples in memory", 20, height - 45);
  263.   text("bytes available", 20, height - 90);
  264.  
  265.  
  266.  
  267.   text("samples/s", 170, height - 45);
  268.   text("ms/div", 170, height - 90);
  269.  
  270.  
  271.   text("samples/div", 350, height - 45);
  272.  
  273.  
  274.   if (oforbox)
  275.   {
  276.     text("selected samples", 550, height - 45);
  277.     text("selected ms", 550, height - 90);
  278.     text("Period  > Hz", 700, height - 90);
  279.   }
  280.  
  281.  
  282.   textSize(20);
  283.  
  284.   text(lasts, 20, height - 25);
  285.   text(port.available(), 20, height - 70);
  286.  
  287.  
  288.  
  289.   text(20000000/time, 170, height - 25);          //s/s
  290.   text((    Float.toString( (float)(100 * zoom) /   (20000000/time)   * 1000)), 170, height - 70); //msdiv
  291.  
  292.  
  293.   text(100 * zoom, 350, height - 25);            //sdiv
  294.  
  295.  
  296.   if (oforbox)
  297.   {
  298.     text(abs((boxstart - boxend) * zoom), 550, height - 25); //sbox
  299.     text(abs(boxstart - boxend) * zoom /   (20000.0/time), 550, height-70);
  300.     text(1000/(abs(boxstart - boxend) * zoom /   (20000.0/time)), 700, height-70);
  301.   }
  302.  
  303.  
  304.  
  305.   if (capture)
  306.   {
  307.     text("capture: on", width - 200, height - 25);
  308.   }
  309.   else
  310.   {
  311.     text("capture: off", width - 200, height - 25);
  312.   }
  313.   fill(#808000);
  314.   if (oforbox)
  315.   {
  316.     {
  317.       int x1 = boxstart - 15;
  318.       if (x1 < 1)
  319.         x1 = 1;
  320.       if (x1 > width)
  321.         x1 = width;
  322.       int x2 = boxstart + 15;
  323.       if (x2 < 1)
  324.         x2 = 1;
  325.       if (x2 > width)
  326.         x2 = width;
  327.  
  328.  
  329.       triangle(x1, height - 120, x2, height - 120, boxstart, height - 135); //BOXSTART
  330.       line(boxstart, height - 120, boxstart, 1);
  331.     }
  332.     {
  333.       int x1 = boxend - 15;
  334.       if (x1 < 1)
  335.         x1 = 1;
  336.       if (x1 > width)
  337.         x1 = width;
  338.       int x2 = boxend + 15;
  339.       if (x2 < 1)
  340.         x2 = 1;
  341.       if (x2 > width)
  342.         x2 = width;
  343.  
  344.  
  345.       triangle(x1, height - 120, x2, height - 120, boxend, height - 135); //BOXEND
  346.       line(boxend, height - 120, boxend, 1);
  347.     }
  348.   }
  349.  
  350.  
  351.   stroke(#00FFFF);
  352.  
  353.  
  354.  
  355.   if (lasts > 0)
  356.   {
  357.     int valpos = lasts - offsetx;
  358.     int lastval = values[valpos];
  359.  
  360.     int zoomstep = zoom / 4;
  361.     if (zoomstep < 1)
  362.       zoomstep = 1;
  363.  
  364.  
  365.     for (int i = width - 1; i > 0; i--)
  366.     {
  367.       for (int c = 0; c < zoom; c+= zoomstep)
  368.       {
  369.         if (valpos - c < 0)
  370.         {
  371.           return;
  372.         }
  373.         int val = values[valpos - c];
  374.         if (val < 0)
  375.           val += 256;
  376.         line(i + 1, Yval(lastval), i, Yval(val));
  377.         lastval = val;
  378.       }
  379.       valpos -= zoom;
  380.     }
  381.   }
  382. }
  383.  
  384. /*
  385.  
  386.  
  387.  const unsigned char PS_16 = (1 << ADPS2);
  388.  const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
  389.  const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
  390.  const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  391.  
  392.  
  393.  void setup()
  394.  {
  395.  Serial.begin(1000000);
  396.  pinMode(A0, INPUT);
  397.  
  398.  ADCSRA &= ~PS_128;
  399.  ADCSRA |= PS_16;
  400.  }
  401.  
  402.  void loop()
  403.  {
  404.  Serial.write(analogRead(A0) >> 2);
  405.  }
  406.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement