在Processing中的类中使用controlp5小部件

时间:2016-12-01 08:13:18

标签: java processing control-p5

我正在使用Processing和controlp5构建一个接口,用于Arduino,并附带一些传感器。我想要有很多传感器,所以我试图以模块化方式构建接口,这样我就可以实例化与传感器一样多的控制面板,并在硬件发送遥测数据包时立即更新所有面板。 / p>

我做了一些有用的东西并绘制了小部件,但看起来我的回调函数没有被触发 - 例如,当按下下面代码中的一个按钮时,相应的串行打印输出不会发生。此外,无论增益滑块正在做什么,主草图中的println(channelOne.currentGain);始终打印0。

我没有做太多的Java,所以我确信我遗漏了一些关于如何声明类及其变量的基本内容,任何关于如何正确设置它的指针都会非常感激!

这是我的主要计划:

import processing.core.PApplet;

SensorChannel channelOne;

void setup()
{
  size(800, 450);
  smooth();

  channelOne = new SensorChannel(this, 30, 30);
}

void draw()
{
  background(0);

  println(channelOne.currentGain);
  delay(20);
}

以下是SensorChannel的类定义:

import processing.core.PApplet;
import controlP5.*;

class SensorChannel {
  PApplet app;
  ControlP5 cp5;

  //Moving line graph
  Chart chart;

  //CheckBox buttons;
  boolean triggered;
  boolean manualOverride;
  boolean calibrateBaseline;
  boolean calibrateGain;
  boolean calibrateTrigger;
  boolean selfControl;

  //Sliders
  int currentGain;
  int currentBalance;
  int currentReading;
  int currentTrigger;

  SensorChannel(PApplet papp, int x, int y)
  {

    app = papp;
    cp5 = new ControlP5(papp);

    println("hello!");

    //Line graph
    chart = cp5.addChart("sensorChart")
    .setPosition(x+0, y+0)
    .setSize(306, 220)
    .setRange(0, 1024)
    .setView(Chart.LINE) // use Chart.LINE, Chart.PIE, Chart.AREA, Chart.BAR_CENTERED
    .setStrokeWeight(1.5)
    .setColorCaptionLabel(color(40))
    ;

    chart.addDataSet("sensorReading");
    chart.setData("sensorReading", new float[100]);
    chart.setColors("sensorReading", color(240));

    chart.addDataSet("sensorReading");
    chart.setData("sensorReading", new float[100]);
    chart.setColors("sensorReading", color(140));

    //Reading (10bits)
    //Trigger point (10bits)

    //Balancing digipot setting (1 byte)
    cp5.addSlider("currentBalance")
    .setLabel("Balance Pot")
    .setPosition(x+0,y+284)
    .setSize(306,20)
    .setRange(0,255)
    ;
    //Gain setting (1 byte)
    cp5.addSlider("currentGain")
    .setLabel("Gain")
    .setPosition(x+0,y+263)
    .setSize(306,20)
    .setRange(0,255)
    ;

    //Current Sensor Reading (1 byte)
    cp5.addSlider("currentReading")
    .setLabel("Sensor Reading")
    .setPosition(x+0,y+221)
    .setSize(306,20)
    .setRange(0,255)
    ;

    //Trigger Point (1 byte)
    cp5.addSlider("currentTrigger")
    .setLabel("Trigger Point")
    .setPosition(x+0,y+242)
    .setSize(306,20)
    .setRange(0,255)
    ;

    //Misc. Settings Toggles
    cp5.addToggle("triggered")
    .setLabel("Trigger")
    .setPosition(x+0,y+305)
    .setSize(50,20)
    ;

    cp5.addToggle("manualOverride")
    .setLabel("Override")
    .setPosition(x+51,y+305)
    .setSize(50,20)
    ;

    cp5.addToggle("calibrateBaseline")
    .setLabel("C. Baseline")
    .setPosition(x+102,y+305)
    .setSize(50,20)
    ;

    cp5.addToggle("calibrateGain")
    .setLabel("C. Gain")
    .setPosition(x+153,y+305)
    .setSize(50,20)
    ;

    cp5.addToggle("calibrateTrigger")
    .setLabel("C. Trig")
    .setPosition(x+204,y+305)
    .setSize(50,20)
    ;

    cp5.addToggle("selfControl")
    .setLabel("S. Control")
    .setPosition(x+255,y+305)
    .setSize(50,20)
    ;

  }

  void triggered(boolean theFlag)
  {
    println("Trigger status: " + theFlag);
  }

  void manualOverride(boolean theFlag)
  {
    println("Manual Override status: " + theFlag);
  }

  void calibrateBaseline(boolean theFlag)
  {
    println("Calibrate Baseline status: " + theFlag);
  }

  void calibrateGain(boolean theFlag)
  {
    println("Calibrate Gain status: " + theFlag);
  }

  void calibrateTrigger(boolean theFlag)
  {
    println("Calibrate Trigger status: " + theFlag);
  }

  void selfControl(boolean theFlag)
  {
    println("Self Control status: " + theFlag);
  }

}

1 个答案:

答案 0 :(得分:2)

有些事情可以改进:

  • 无需致电delay()draw()每秒被调用大约60次,但您可以使用frameRate()轻松(并且干净地)控制(例如framerate(50);
  • 您正在SensorChannel中实例化cp5。也许您可以在主草图中创建一个ControlP5实例并传递对SensorChannel的引用(为多个未来通道重用实例)

有多种方法可以利用价值观。你应该看一下例子>贡献的图书馆> 使用

应该直截了当的一种方法是使用Controller的plugTo()方法。传递一个对象来插入控制器,只要该对象具有与控制器同名的属性,它就会设置它的值:

import controlP5.*;

SensorChannel channelOne;
ControlP5 cp5;

void setup()
{
  size(800, 450);
  smooth();
  cp5 = new ControlP5(this);
  channelOne = new SensorChannel(this,cp5, 30, 30);
}

void draw()
{
  background(0);

  text("currentGain:"+channelOne.currentGain,10,15);
}

class SensorChannel{
  PApplet app;
  ControlP5 cp5;

  //Moving line graph
  Chart chart;

  //CheckBox buttons;
  boolean triggered;
  boolean manualOverride;
  boolean calibrateBaseline;
  boolean calibrateGain;
  boolean calibrateTrigger;
  boolean selfControl;

  //Sliders
  int currentGain;
  int currentBalance;
  int currentReading;
  int currentTrigger;

  SensorChannel(PApplet papp, ControlP5 cp5,int x, int y)
  {

    app = papp;
    this.cp5 = cp5;

    println("hello!");

    //Line graph
    chart = cp5.addChart("sensorChart")
    .setPosition(x+0, y+0)
    .setSize(306, 220)
    .setRange(0, 1024)
    .setView(Chart.LINE) // use Chart.LINE, Chart.PIE, Chart.AREA, Chart.BAR_CENTERED
    .setStrokeWeight(1.5)
    .setColorCaptionLabel(color(40));

    chart.addDataSet("sensorReading");
    chart.setData("sensorReading", new float[100]);
    chart.setColors("sensorReading", color(240));

    chart.addDataSet("sensorReading");
    chart.setData("sensorReading", new float[100]);
    chart.setColors("sensorReading", color(140));

    //Reading (10bits)
    //Trigger point (10bits)

    //Balancing digipot setting (1 byte)
    cp5.addSlider("currentBalance")
    .setLabel("Balance Pot")
    .setPosition(x+0,y+284)
    .setSize(306,20)
    .setRange(0,255)
    .plugTo(this)
    ;
    //Gain setting (1 byte)
    cp5.addSlider("currentGain")
    .setLabel("Gain")
    .setPosition(x+0,y+263)
    .setSize(306,20)
    .setRange(0,255)
    .plugTo(this);
    ;

    //Current Sensor Reading (1 byte)
    cp5.addSlider("currentReading")
    .setLabel("Sensor Reading")
    .setPosition(x+0,y+221)
    .setSize(306,20)
    .setRange(0,255)
    .plugTo(this)
    ;

    //Trigger Point (1 byte)
    cp5.addSlider("currentTrigger")
    .setLabel("Trigger Point")
    .setPosition(x+0,y+242)
    .setSize(306,20)
    .setRange(0,255)
    .plugTo(this)
    ;

    //Misc. Settings Toggles
    cp5.addToggle("triggered")
    .setLabel("Trigger")
    .setPosition(x+0,y+305)
    .setSize(50,20)
    .plugTo(this)
    ;

    cp5.addToggle("manualOverride")
    .setLabel("Override")
    .setPosition(x+51,y+305)
    .setSize(50,20)
    .plugTo(this)
    ;

    cp5.addToggle("calibrateBaseline")
    .setLabel("C. Baseline")
    .setPosition(x+102,y+305)
    .setSize(50,20)
    .plugTo(this)
    ;

    cp5.addToggle("calibrateGain")
    .setLabel("C. Gain")
    .setPosition(x+153,y+305)
    .setSize(50,20)
    .plugTo(this)
    ;

    cp5.addToggle("calibrateTrigger")
    .setLabel("C. Trig")
    .setPosition(x+204,y+305)
    .setSize(50,20)
    .plugTo(this)
    ;

    cp5.addToggle("selfControl")
    .setLabel("S. Control")
    .setPosition(x+255,y+305)
    .setSize(50,20)
    .plugTo(this)
    ;

  }

}

这是一个建议。您可以随意浏览控制器事件和值的其他ControlP5示例。