Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: /u/martin2250
- Special Thanks to /u/OverVolt for improving the code to run ~2.5 times faster!
- */
- import processing.serial.*;
- import java.awt.event.*;
- Serial port;
- byte[] values;
- int threshold = 60;
- boolean save = false;
- int zoom = 4;
- int offsetx = 0;
- int lastxPos = -1;
- boolean isgoing = false;
- int lasts = 0;
- int lastxOffset = 0;
- byte lastsample = 0;
- long last5ks = 0;
- long allsamples = 0;
- long time = 1000;
- boolean capture = true;
- boolean zorth = false;
- boolean oforbox = false;
- int boxstart = 0;
- int boxend = 0;
- long rate = 0;
- long lastRate = 0;
- long lastCount = 0;
- void setup()
- {
- frame.setResizable(true);
- size(displayWidth -100, displayHeight - 100);
- port = new Serial(this, "COM4", 1000000); //adjust to your COM-Port
- values = new byte[400000000];
- addMouseWheelListener(new MouseWheelListener() {
- public void mouseWheelMoved(MouseWheelEvent mwe) {
- mouseWheel(mwe.getWheelRotation());
- }
- }
- );
- smooth();
- thread("Sample");
- }
- void mouseDragged()
- {
- if (oforbox)
- {
- boxend = mouseX;
- }
- else
- {
- offsetx = lastxOffset + ((mouseX - lastxPos) * zoom);
- if (offsetx < 0)
- offsetx = 0;
- if ((offsetx) > lasts)
- offsetx = lasts ;
- }
- }
- void mousePressed()
- {
- if (oforbox)
- {
- boxstart = mouseX;
- boxend = mouseX;
- }
- else
- {
- lastxPos = mouseX;
- lastxOffset = offsetx;
- }
- }
- void keyPressed() //+++++++++++++++++++++++++++++++
- {
- if (key == 's'||key == 'S')
- {
- saveFrame();
- }
- if (key == 'c'||key == 'C')
- {
- capture = !capture;
- }
- if (key == CODED && keyCode == CONTROL)
- {
- zorth = true;
- }
- if (key == 'b'||key == 'B')
- {
- oforbox = !oforbox;
- }
- if (key == 'r'||key == 'R')
- {
- lasts = 0;
- offsetx = 0;
- }
- }
- void keyReleased()
- {
- if (key == CODED && keyCode == CONTROL)
- {
- zorth = false;
- }
- }
- void mouseWheel(int delta) {
- if (zorth)
- {
- threshold -= delta * 4;
- if (threshold > 255)
- threshold = 255;
- if (threshold < 1)
- threshold = 1;
- }
- else
- {
- int oldzoom = zoom;
- zoom += delta;
- if (zoom <= 0)
- zoom = 1;
- if (zoom != oldzoom)
- {
- if (zoom < oldzoom)
- {
- offsetx += (width - mouseX);
- }
- else
- {
- offsetx -= (width - mouseX);
- }
- }
- if (offsetx < 0)
- offsetx = 0;
- if ((offsetx) > lasts)
- offsetx = lasts ;
- }
- }
- long allsamples2;
- void Sample()
- {
- int rbl = 4000;
- int rs = 0;
- byte[] ring = new byte[rbl];
- int rbp = 0;
- while (true)
- {
- if (millis() - lastRate > 2000)
- {
- lastRate = millis();
- rate = (allsamples2 - lastCount) / 2;
- lastCount = allsamples2;
- }
- int lastsamplei = port.read();
- if (lastsamplei >= 0)
- {
- allsamples2++;
- lastsample = (byte)lastsamplei;
- if (lastsamplei > threshold)
- {
- rs = 4000;
- }
- if ( rs-- > 0 && capture)
- {
- if (!isgoing)
- {
- for (int i = rbp; i < (rbp + rbl); i++)
- {
- if (i >= rbl)
- values[lasts++] = ring[i - rbl];
- else
- values[lasts++] = ring[i];
- }
- isgoing = true;
- }
- values[lasts++] = lastsample;
- }
- else
- {
- ring[rbp++] = lastsample;
- if (rbp >= rbl)
- {
- rbp = 0;
- }
- isgoing = false;
- }
- }
- }
- }
- int Yval(int vale) {
- vale *= (height - 140) / 255.0 ;
- vale += 120;
- return Y(vale);
- }
- int Y(int valu)
- {
- return height - valu;
- }
- void draw()
- {
- background(0);
- stroke(#000060);
- int y1 = Yval(threshold) - 15;
- if (y1 < 1)
- y1 = 1;
- fill(#000040);
- triangle(1, y1, 1, Yval(threshold) + 15, 15, Yval(threshold)); //THRESHOLD
- fill(30);
- stroke(30);
- rect(0, Y(120), width, 120);// BOTTOM
- fill(#005000);
- stroke(#005000);
- int l = lastsample;
- if (l < 0) l += 256;
- rect(0, Yval(l), 20, (int)(l / 255.0f * (height - 120)) - 1); //CURRENT LEVEL
- stroke(#002000);
- for (int i = width - 100; i > 0; i -= 100)
- {
- for (int y = 1; y + 120 < height; y+= 40)
- {
- line(i, Y(y + 120), i, Y(y + 130));
- }
- }
- fill(#AAAAFF);
- textSize(14);
- text("samples in memory", 20, height - 45);
- text("bytes available", 20, height - 90);
- text("samples/s", 170, height - 45);
- text("ms/div", 170, height - 90);
- text("samples/div", 350, height - 45);
- if (oforbox)
- {
- text("selected samples", 550, height - 45);
- text("selected ms", 550, height - 90);
- text("Period > Hz", 700, height - 90);
- }
- textSize(20);
- text(lasts, 20, height - 25);
- text(port.available(), 20, height - 70);
- text(rate, 170, height - 25); //s/s SAMPLES PER SECOND
- if (rate != 0)
- text(Float.toString((float)(100000*zoom)/(float)rate), 170, height - 70); //msdiv MILIS PER DIV
- text(100 * zoom, 350, height - 25); //sdiv SAMPLES PER DIV
- if (oforbox)
- {
- int sizeof = abs((boxstart - boxend) * zoom);
- text(sizeof, 550, height - 25); // SIZE OF THE SELECTION
- float selectedtime = sizeof*1000/ (float)rate;
- text( Float.toString(selectedtime) , 550, height-70); //SELECTED TIME
- text( Float.toString(1000/selectedtime ) , 700, height-70); //FREQUENCY
- }
- if (capture)
- {
- text("capture: on", width - 200, height - 25);
- }
- else
- {
- text("capture: off", width - 200, height - 25);
- }
- fill(#808000);
- if (oforbox)
- {
- {
- int x1 = boxstart - 15;
- if (x1 < 1)
- x1 = 1;
- if (x1 > width)
- x1 = width;
- int x2 = boxstart + 15;
- if (x2 < 1)
- x2 = 1;
- if (x2 > width)
- x2 = width;
- triangle(x1, height - 120, x2, height - 120, boxstart, height - 135); //BOXSTART
- line(boxstart, height - 120, boxstart, 1);
- }
- {
- int x1 = boxend - 15;
- if (x1 < 1)
- x1 = 1;
- if (x1 > width)
- x1 = width;
- int x2 = boxend + 15;
- if (x2 < 1)
- x2 = 1;
- if (x2 > width)
- x2 = width;
- triangle(x1, height - 120, x2, height - 120, boxend, height - 135); //BOXEND
- line(boxend, height - 120, boxend, 1);
- }
- }
- stroke(#00FFFF);
- if (lasts > 0)
- {
- int valpos = lasts - offsetx;
- int lastval = values[valpos];
- int zoomstep = zoom / 4;
- if (zoomstep < 1)
- zoomstep = 1;
- for (int i = width - 1; i > 0; i--)
- {
- for (int c = 0; c < zoom; c+= zoomstep)
- {
- if (valpos - c < 0)
- {
- return;
- }
- int val = values[valpos - c];
- if (val < 0)
- val += 256;
- line(i + 1, Yval(lastval), i, Yval(val));
- lastval = val;
- }
- valpos -= zoom;
- }
- }
- }
- /*
- #include <avr/io.h>;
- #include <avr/interrupt.h>
- const unsigned char PS_16 = (1 << ADPS2);
- const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
- const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
- const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
- void setup()
- {
- UCSR0B |= _BV(3);//TXEN
- UCSR0C = (1<<7)|(1 << 2)|(1 << 1);
- ADCSRA &= ~PS_128;
- ADCSRA |= PS_16;
- ADMUX |= (1 << REFS0);
- ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
- // No MUX values needed to be changed to use ADC0
- ADCSRA |= (1 << ADIE); // Enable ADC Interrupt
- TCCR0A = 0;
- TCCR0B = 0;
- ADCSRA |= (1 << 5); // Set ADC to Free-Running Mode
- ADCSRA |= (1 << ADEN); // Enable ADC
- ADCSRA |= (1 << ADSC); // Start A2D Conversions
- while(true);
- }
- ISR(ADC_vect)
- {
- UDR0 = ADCH;
- }
- void loop()
- {
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement