Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2016 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/0190c2c5694863f15fe652ff766fc618 to your computer and use it in GitHub Desktop.
Save anonymous/0190c2c5694863f15fe652ff766fc618 to your computer and use it in GitHub Desktop.
Processing sketch to visualize and analize data from ADC's channels.
import controlP5.*;
import processing.serial.*;
boolean dataReady = false;
float offset = -1;
float voltsPerDiv = 0.5;
int ch1Color = color(255);
int ch2Color = #FF6464;
int ch3Color = color(0,255,0);
int ch4Color = #528DFF;
Serial myPort;
ControlP5 cp5;
Chart myChart;
Textfield outputCmd;
Textlabel ch1MeasureText, ch2MeasureText, ch3MeasureText, ch4MeasureText;
Textarea consoleText, outConsoleText;
Icon voltsPerDivUp, voltsPerDivDw, offsetUp, offsetDw;
Button resetDisplay, ch1Auto, ch1OnOff, ch2Auto,
ch2OnOff, ch3Auto, ch3OnOff, ch4Auto, ch4OnOff;
void setup() {
size(1100,650);
cp5 = new ControlP5(this);
myChart = cp5.addChart("PLOT")
.setPosition(60, 30)
.setSize(800, 400)
.setView(Chart.LINE)
;
// create text and info stuff
consoleText = cp5.addTextarea("txt")
.setPosition(
int(myChart.getPosition()[0]) + myChart.getWidth() + 10 ,
int(myChart.getPosition()[1]))
.setSize(200, myChart.getHeight())
.setFont(createFont("", 15))
.setLineHeight(14)
.setColor(color(200))
.setColorBackground(color(0, 100))
.setColorForeground(color(255, 100))
;
outConsoleText = cp5.addTextarea("outConsole")
.setPosition(
int(consoleText.getPosition()[0]),
int(consoleText.getPosition()[1]) + consoleText.getHeight() + 30)
.setSize(consoleText.getWidth(), 138)
.setFont(createFont("", 15))
.setLineHeight(14)
.setColor(color(200))
.setColorBackground(color(0, 100))
.setColorForeground(color(255, 100))
;
outputCmd = cp5.addTextfield("outputCmd")
.setPosition(
int(outConsoleText.getPosition()[0]),
int(outConsoleText.getPosition()[1]) + outConsoleText.getHeight() + 5)
.setSize(outConsoleText.getWidth(),25)
.setFont(createFont("", 20))
.setLabel("")
.setFocus(true)
;
// buttons
voltsPerDivUp = cp5.addIcon("voltsPerDivUp",1)
.setPosition(70,
int(myChart.getPosition()[1]) +
int(myChart.getHeight())+ 30)
.setSize(50,50)
.setFont(createFont("fontawesome-webfont.ttf", 60))
.setFontIcon(#00f106)
.setColorBackground(color(0,45,90))
.setRoundedCorners(10)
.showBackground()
;
voltsPerDivDw = cp5.addIcon("voltsPerDivDw",2)
.setPosition(
int(voltsPerDivUp.getPosition()[0] + int(voltsPerDivUp.getWidth()) + 10),
int(voltsPerDivUp.getPosition()[1]))
.setSize(50,50)
.setFont(createFont("fontawesome-webfont.ttf", 60))
.setFontIcon(#00f107)
.setColorBackground(color(0,45,90))
.setRoundedCorners(10)
.showBackground()
;
offsetUp = cp5.addIcon("offsetUp",3)
.setPosition(
int(voltsPerDivUp.getPosition()[0]),
int(voltsPerDivUp.getPosition()[1]) + int(voltsPerDivUp.getHeight()) + 10)
.setSize(50,50)
.setFont(createFont("fontawesome-webfont.ttf", 60))
.setFontIcon(#00f106)
.setColorBackground(color(0,45,90))
.setRoundedCorners(10)
.showBackground()
;
offsetDw = cp5.addIcon("offsetDw",4)
.setPosition(
int(offsetUp.getPosition()[0] + int(offsetUp.getWidth()) + 10),
int(offsetUp.getPosition()[1]))
.setSize(50,50)
.setFont(createFont("fontawesome-webfont.ttf", 60))
.setFontIcon(#00f107)
.setColorBackground(color(0,45,90))
.setRoundedCorners(10)
.showBackground()
;
resetDisplay = cp5.addButton("resetDisplay")
.setValue(0)
.setLabel("Reset Display")
.setPosition(
int(offsetUp.getPosition()[0]),
int(offsetUp.getPosition()[1]) + int(offsetUp.getHeight()) + 10)
.setSize(2*int(offsetUp.getWidth()) + 10, int(offsetUp.getHeight()))
;
// channel's control and measurement
Group ch1Group = cp5.addGroup("ch1Group")
.setPosition(190,470)
.setWidth(160)
.setBackgroundColor(color(80))
.setBackgroundHeight(160)
.setColorForeground(color(0,45,90))
.setColorLabel(ch1Color)
.setLabel("Channel 1")
.disableCollapse()
;
ch1OnOff = cp5.addButton("ch1OnOff")
.setLabel("ON/OFF")
.setPosition(10,10)
.setSize(140,20)
.setGroup("ch1Group")
.setSwitch(true)
.setOn()
;
ch1MeasureText = cp5.addTextlabel("ch1MeasureText")
.setPosition(10,40)
.setFont(createFont("",18))
.setGroup("ch1Group")
;
ch1Auto = cp5.addButton("ch1Auto")
.setLabel("Auto")
.setPosition(10,130)
.setSize(140,20)
.setGroup("ch1Group")
;
Group ch2Group = cp5.addGroup("ch2Group")
.setPosition(190+170,470)
.setWidth(160)
.setBackgroundColor(color(80))
.setBackgroundHeight(160)
.setColorForeground(color(0,45,90))
.setColorLabel(ch2Color)
.setLabel("Channel 2")
.disableCollapse()
;
ch2OnOff = cp5.addButton("ch2OnOff")
.setLabel("ON/OFF")
.setPosition(10,10)
.setSize(140,20)
.setGroup("ch2Group")
.setSwitch(true)
.setOn()
;
ch2MeasureText = cp5.addTextlabel("ch2MeasureText")
.setPosition(10,40)
.setFont(createFont("",18))
.setGroup("ch2Group")
;
ch2Auto = cp5.addButton("ch2Auto")
.setLabel("Auto")
.setPosition(10,130)
.setSize(140,20)
.setGroup("ch2Group")
;
Group ch3Group = cp5.addGroup("ch3Group")
.setPosition(190+2*170,470)
.setWidth(160)
.setBackgroundColor(color(80))
.setBackgroundHeight(160)
.setColorForeground(color(0,45,90))
.setColorLabel(ch3Color)
.setLabel("Channel 3")
.disableCollapse()
;
ch3OnOff = cp5.addButton("ch3OnOff")
.setLabel("ON/OFF")
.setPosition(10,10)
.setSize(140,20)
.setGroup("ch3Group")
.setSwitch(true)
.setOn()
;
ch3MeasureText = cp5.addTextlabel("ch3MeasureText")
.setPosition(10,40)
.setFont(createFont("",18))
.setGroup("ch3Group")
;
ch3Auto = cp5.addButton("ch3Auto")
.setLabel("Auto")
.setPosition(10,130)
.setSize(140,20)
.setGroup("ch3Group")
;
Group ch4Group = cp5.addGroup("ch4Group")
.setPosition(190+3*170,470)
.setWidth(160)
.setBackgroundColor(color(80))
.setBackgroundHeight(160)
.setColorForeground(color(0,45,90))
.setColorLabel(ch4Color)
.setLabel("Channel 4")
.disableCollapse()
;
ch4OnOff = cp5.addButton("ch4OnOff")
.setLabel("ON/OFF")
.setPosition(10,10)
.setSize(140,20)
.setGroup("ch4Group")
.setSwitch(true)
.setOn()
;
ch4MeasureText = cp5.addTextlabel("ch4MeasureText")
.setPosition(10,40)
.setFont(createFont("",18))
.setGroup("ch4Group")
;
ch4Auto = cp5.addButton("ch4Auto")
.setLabel("Auto")
.setPosition(10,130)
.setSize(140,20)
.setGroup("ch4Group")
;
// setup serial
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 115200);
myPort.bufferUntil('\n');
cp5.addFrameRate().setInterval(10).setPosition(0,height - 10);
}
void draw() {
background(color(100));
//draw y Axis
int axisDiv = 10;
float axisMax = voltsPerDiv*axisDiv + offset;
float axisMin = offset;
myChart.setRange(axisMin, axisMax);
textAlign(CENTER);
if (voltsPerDiv < 1)
text("[mV/Div]\n" + nf(voltsPerDiv*1000, 0, 2),
int(voltsPerDivUp.getPosition()[0] - 30),
int(voltsPerDivUp.getPosition()[1]) + 20);
else
text("[V/Div]\n" + nf(voltsPerDiv, 0, 2),
int(voltsPerDivUp.getPosition()[0] - 30),
int(voltsPerDivUp.getPosition()[1]) + 20);
if (abs(offset) < 1)
text("offset\n" + nf(offset*1000, 0, 2) + "\n[mV]",
int(offsetUp.getPosition()[0] - 30),
int(offsetUp.getPosition()[1]) + 10);
else
text("offset\n" + nf(offset, 0, 2) + "\n[V]",
int(offsetUp.getPosition()[0] - 30),
int(offsetUp.getPosition()[1]) + 10);
float axisRange = axisMax - axisMin;
int axisOffX = int(myChart.getPosition()[0]);
int axisOffY = int(myChart.getPosition()[1]);
int axisPos = myChart.getHeight() + axisOffY;
stroke(255);
textAlign(LEFT);
for (int i = 0; i <= axisDiv; i++)
{
int axisY = axisPos - i*(myChart.getHeight()/axisDiv);
text(nf((axisMin + (axisRange)/(axisDiv)*i), 0, 3), axisOffX - 48, axisY);
line(axisOffX, axisY, axisOffX - 10, axisY);
}
for (int i = 0; i <= axisDiv*5; i++)
{
int axisY = axisPos - i*(myChart.getHeight()/(axisDiv*5));
line(axisOffX, axisY, axisOffX - 5, axisY);
}
// calculate and display data
if (dataReady)
{
dataReady = false;
String inString, data, header;
inString = myPort.readString();
if (inString.charAt(2) == ':')
{
try
{
float raw_to_V = 2.495f/4095.0f;
int ch1Value, ch2Value, ch3Value, ch4Value;
header = inString.substring(0,2);
data = inString.substring(3);
String list [] = split(data, ',');
consoleText.append(str(millis()) + " < " + header + ":" + str(inString.length()) + "," + str(list.length),100);
consoleText.scroll(1);
if(header.equals("C1") && ch1OnOff.getBooleanValue())
{
for (int i = 0; i < list.length; i++)
{
ch1Value = Integer.parseInt(list[i].trim());
myChart.push("inCh1", (ch1Value*raw_to_V));
}
}
if(header.equals("C2") && ch2OnOff.getBooleanValue())
{
for (int i = 0; i < list.length; i++)
{
ch2Value = Integer.parseInt(list[i].trim());
myChart.push("inCh2", (ch2Value*raw_to_V));
}
}
if(header.equals("C3") && ch3OnOff.getBooleanValue())
{
for (int i = 0; i < list.length; i++)
{
ch3Value = Integer.parseInt(list[i].trim());
myChart.push("inCh3", (ch3Value*raw_to_V));
}
}
if(header.equals("C4") && ch4OnOff.getBooleanValue())
{
for (int i = 0; i < list.length; i++)
{
ch4Value = Integer.parseInt(list[i].trim());
myChart.push("inCh4", (ch4Value*raw_to_V));
}
}
}
catch (NumberFormatException error)
{
print(month() + "/" + day(),hour() + ":" + minute() + ":" + second());
println(" " + error);
}
if (ch1OnOff.getBooleanValue())
{
float average = 0;
float tempArray[] = myChart.getValuesFrom("inCh1");
for (int i = 0; i < tempArray.length; ++i)
average += tempArray[i];
average /= (float)(tempArray.length);
ch1MeasureText.setText(
"Avg: " + nf(average, 0, 3) + " [V]\n" +
"Max: " + nf(max(tempArray), 0, 3) + " [V]\n" +
"Min: " + nf(min(tempArray), 0, 3) + " [V]\n" +
"PkPk: " + nf((max(tempArray)-min(tempArray))*1000, 0, 1) + " [mV]");
}
else ch1MeasureText.setText("");
if (ch2OnOff.getBooleanValue())
{
float average = 0;
float tempArray[] = myChart.getValuesFrom("inCh2");
for (int i = 0; i < tempArray.length; ++i)
average += tempArray[i];
average /= (float)(tempArray.length);
ch2MeasureText.setText(
"Avg: " + nf(average, 0, 3) + " [V]\n" +
"Max: " + nf(max(tempArray), 0, 3) + " [V]\n" +
"Min: " + nf(min(tempArray), 0, 3) + " [V]\n" +
"PkPk: " + nf((max(tempArray)-min(tempArray))*1000, 0, 1) + " [mV]");
}
else ch2MeasureText.setText("");
if (ch3OnOff.getBooleanValue())
{
float average = 0;
float tempArray[] = myChart.getValuesFrom("inCh3");
for (int i = 0; i < tempArray.length; ++i)
average += tempArray[i];
average /= (float)(tempArray.length);
ch3MeasureText.setText(
"Avg: " + nf(average, 0, 3) + " [V]\n" +
"Max: " + nf(max(tempArray), 0, 3) + " [V]\n" +
"Min: " + nf(min(tempArray), 0, 3) + " [V]\n" +
"PkPk: " + nf((max(tempArray)-min(tempArray))*1000, 0, 1) + " [mV]");
}
else ch3MeasureText.setText("");
if (ch4OnOff.getBooleanValue())
{
float average = 0;
float tempArray[] = myChart.getValuesFrom("inCh4");
for (int i = 0; i < tempArray.length; ++i)
average += tempArray[i];
average /= (float)(tempArray.length);
ch4MeasureText.setText(
"Avg: " + nf(average, 0, 3) + " [V]\n" +
"Max: " + nf(max(tempArray), 0, 3) + " [V]\n" +
"Min: " + nf(min(tempArray), 0, 3) + " [V]\n" +
"PkPk: " + nf((max(tempArray)-min(tempArray))*1000, 0, 1) + " [mV]");
}
else ch4MeasureText.setText("");
}
}
}
void serialEvent(Serial p) {
dataReady = true;
}
void voltsPerDivUp(boolean theValue) {
voltsPerDiv /= 2;
}
void voltsPerDivDw(boolean theValue) {
voltsPerDiv *= 2;
}
void offsetUp(boolean theValue) {
offset -= voltsPerDiv;
}
void offsetDw(boolean theValue) {
offset += voltsPerDiv;
}
void resetDisplay(boolean theValue) {
voltsPerDiv = 0.5;
offset = -1;
}
void ch1Auto(boolean theValue) {
if (ch2OnOff.getBooleanValue())
{
float tempArray[] = myChart.getValuesFrom("inCh1");
float min, max;
min = min(tempArray);
max = max(tempArray);
voltsPerDiv = (max - min)/8;
offset = min;
offset -= voltsPerDiv;
}
}
void ch1OnOff(boolean theValue) {
if (theValue)
{
myChart.addDataSet("inCh1");
myChart.setData("inCh1", new float[1000]);
myChart.setColors("inCh1", ch1Color);
}
else myChart.removeDataSet("inCh1");
}
void ch2Auto(boolean theValue) {
if (ch2OnOff.getBooleanValue())
{
float tempArray[] = myChart.getValuesFrom("inCh2");
float min, max;
min = min(tempArray);
max = max(tempArray);
voltsPerDiv = (max - min)/8;
offset = min;
offset -= voltsPerDiv;
}
}
void ch2OnOff(boolean theValue) {
if (theValue)
{
myChart.addDataSet("inCh2");
myChart.setData("inCh2", new float[1000]);
myChart.setColors("inCh2", ch2Color);
}
else myChart.removeDataSet("inCh2");
}
void ch3Auto(boolean theValue) {
if (ch3OnOff.getBooleanValue())
{
float tempArray[] = myChart.getValuesFrom("inCh3");
float min, max;
min = min(tempArray);
max = max(tempArray);
voltsPerDiv = (max - min)/8;
offset = min;
offset -= voltsPerDiv;
}
}
void ch3OnOff(boolean theValue) {
if (theValue)
{
myChart.addDataSet("inCh3");
myChart.setData("inCh3", new float[1000]);
myChart.setColors("inCh3", ch3Color);
}
else myChart.removeDataSet("inCh3");
}
void ch4Auto(boolean theValue) {
if (ch4OnOff.getBooleanValue())
{
float tempArray[] = myChart.getValuesFrom("inCh4");
float min, max;
min = min(tempArray);
max = max(tempArray);
voltsPerDiv = (max - min)/8;
offset = min;
offset -= voltsPerDiv;
}
}
void ch4OnOff(boolean theValue) {
if (theValue)
{
myChart.addDataSet("inCh4");
myChart.setData("inCh4", new float[1000]);
myChart.setColors("inCh4", ch4Color);
}
else myChart.removeDataSet("inCh4");
}
public void outputCmd(String theText) {
outConsoleText.append(str(millis()) + " > " + theText,20);
outConsoleText.scroll(1);
myPort.write(theText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment