Advertisement
martin2250

Arduino Oscilloscope 2.1

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