serialVisualization/hoverbrettvisualizer/hoverbrettvisualizer.pde

103 lines
2.3 KiB
Plaintext

import processing.serial.*;
Serial serialport;
Visualization visVoltage;
Visualization visCurrent;
float voltage = 50;
float current = 0.0;
long lastReceive=0; //last time serial received
long lastDelay=0;
void setup() {
size(640, 360);
printArray(Serial.list());
serialport = new Serial(this, Serial.list()[2], 9600);
//vis = new BarV(150,150,10,100,0,100);
//vis = new BarH(150,150,100,10,0,100);
visVoltage = new Tacho(150,150,100,0,100);
//vis = new Direction(150,150,100,0,100,0,1);
visVoltage.setShowMinMax(true);
visVoltage.setTitle("Voltage [V]");
visCurrent= new Tacho(150+250,150,100,0,100);
visCurrent.setShowMinMax(true);
visCurrent.setTitle("Current [A]");
}
void draw() {
receive();
background(255);
visVoltage.setValue(voltage);
visCurrent.setValue(current);
visVoltage.drawVis();
visCurrent.drawVis();
fill(color(0,0,0));
textSize(12);
long _delay=lastDelay;
if (millis()-lastReceive>1000){ //show counting up if update too long ago
_delay=millis()-lastReceive;
}
text("Delay="+(_delay), 5,12);
}
public void receive()
{
byte[] inBuffer = new byte[8];
while(serialport.available()>0) {
//inBuffer = serialport.readBytes();
serialport.readBytes(inBuffer);
//if (inBuffer != null) {
voltage = extract_float(inBuffer,0);
current = extract_float(inBuffer,4);
//}
}
if (received){
lastDelay=millis()-lastReceive;
lastReceive=millis();
}
}
public int extract_uint16_t(byte array[], int startbyte) {
return ((int)array[startbyte] & 0xff) | ((int)array[startbyte+1] & 0xff)<<8;
}
public int extract_int16_t(byte array[], int startbyte) {
if ( ((int)array[startbyte+1] & 0x80) == 0x00 ) { //2's complement, not negative
return ( ((int)array[startbyte] & 0xff) | ((int)array[startbyte+1] & 0x7f)<<8 );
}else{ //value is negative
return -32768 + ( ((int)array[startbyte] & 0xff) | ((int)array[startbyte+1] & 0x7f)<<8 );
}
}
public float extract_float(byte array[], int startbyte) {
int MASK = 0xff;
int bits = 0;
int i = startbyte+3; //+3 because goes backwards and has 4 bytes
for (int shifter = 3; shifter >= 0; shifter--) {
bits |= ((int) array[i] & MASK) << (shifter * 8);
i--;
}
return Float.intBitsToFloat(bits);
}