Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- void setup()
- {
- frame.setResizable(true);
- size(displayWidth -100, displayHeight - 100);
- port = new Serial(this, Serial.list()[Serial.list().length - 1], 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 ;
- }
- }
- void Sample()
- {
- int rbl = 4000;
- int rs = 0;
- byte[] ring = new byte[rbl];
- int rbp = 0;
- while (true)
- {
- while (!capture)
- {
- while (port.available () < 1) {
- }
- lastsample = (byte)port.read();
- }
- if (allsamples >= 20000)
- {
- allsamples = 0;
- time = millis() - last5ks;
- last5ks = millis();
- }
- allsamples ++;
- while (port.available () < 1) {
- }
- int lastsamplei = port.read();
- if (lastsamplei >= 0)
- {
- lastsample = (byte)lastsamplei;
- if (lastsamplei > threshold)
- {
- rs = 4000;
- }
- if ( rs-- > 0)
- {
- 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(20000000/time, 170, height - 25); //s/s
- text(( Float.toString( (float)(100 * zoom) / (20000000/time) * 1000)), 170, height - 70); //msdiv
- text(100 * zoom, 350, height - 25); //sdiv
- if (oforbox)
- {
- text(abs((boxstart - boxend) * zoom), 550, height - 25); //sbox
- text(abs(boxstart - boxend) * zoom / (20000.0/time), 550, height-70);
- text(1000/(abs(boxstart - boxend) * zoom / (20000.0/time)), 700, height-70);
- }
- 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;
- }
- }
- }
- /*
- 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()
- {
- Serial.begin(1000000);
- pinMode(A0, INPUT);
- ADCSRA &= ~PS_128;
- ADCSRA |= PS_16;
- }
- void loop()
- {
- Serial.write(analogRead(A0) >> 2);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement