Home > Mobile >  How to write a function that calls function from the library?
How to write a function that calls function from the library?

Time:10-24

I am trying to write some program to display telemetry data on charts using Processing IDE. I use ControlP5 library to create chart. There will be many of them so I wanted to write some function that generates them, but there is some problem, I am not sure how to send chart name to the function. I tried using this but IDE throws some error:

The function create_chart(String, int, int, int, int, int, String) does not exist.

Here is code that I use:

ControlP5 gui;
Chart dane;

int i = 0, minimum, maximum;
int zero_point;
void setup()
{

  size(1280, 720);
  smooth();
  gui= new ControlP5(this);
 
  PFont pfont = createFont("TIMES", 20, true); // use true/false for smooth/no-smooth
  ControlFont font = new ControlFont(pfont, 30);
  gui.setFont(font);
  dane.create_chart("Dane", 0, 450, 300, 415, 210, "world");
  
  delay(100);
}



void draw()
{
  textSize(15);
  i=i (int(random(-50, 50)));
  background(45);/*
  dane.push("zero", 0);
  dane.push("world", i);
  minimum = int(min(dane.getValuesFrom("world")));
  maximum = int(max(dane.getValuesFrom("world")));
  if (maximum<0) maximum = 0;
  if (minimum>0) minimum = 0;
  dane.setRange(minimum, maximum);
  text(maximum, 390, 220);
  text(minimum, 390, 510);
  if (minimum<=0 && minimum!=maximum) {
    zero_point = 220   290*maximum/(maximum-minimum);
    text(0, 390, zero_point);
  }
  dane.getValueLabel();*/
}


class Dane {

  Dane dane = new Dane();
}
public void create_chart(String name, int chart_number, int size_x, int size_y, int position_x, int position_y, String data)
  {

    gui.printPublicMethodsFor(Chart.class);
    dane = gui.addChart(name)
      .setPosition(position_x, position_y)
      .setSize(size_x, size_y)
      .setRange(-20, 20)
      .setView(Chart.LINE)
      ;
    this.getColor().setBackground(color(100, 100));
    this.addDataSet("zero");
    this.setData("zero", new float[500]);
    this.addDataSet("world");
    this.setColors("world", color(100, 100, 500));
    this.setStrokeWeight(3);
  }

CodePudding user response:

Seems like you are using a sample of code different from the version of the ControlP5 library you are using, probably the signature has changed in the version you are using. Have you checked the documentation for the exact version of ControlP5 you are using?

CodePudding user response:

Yes, I'm using controlP5-2.2.5 but I think the problem is somewhere in Java syntax, because everything works fine when I use it as a program without function:

    import controlP5.*;

ControlP5 gui;
Chart dane;
int i = 0, minimum, maximum;

void setup()
{
 size(1280, 720);
 smooth();
 gui= new ControlP5(this);
 
 PFont pfont = createFont("TIMES",20,true); // use true/false for smooth/no-smooth
 ControlFont font = new ControlFont(pfont,30);
 gui.setFont(font);
  
 gui.printPublicMethodsFor(Chart.class);
 dane = gui.addChart("Dane")
           .setPosition(415,210)
           .setSize(450,300)
           .setRange(-20,20)
           .setView(Chart.LINE)
           ;
 dane.getColor().setBackground(color(100,100));
 
 dane.addDataSet("world");
 dane.setColors("world", color(100, 100, 500));
 
 dane.setStrokeWeight(3);
 line(400, 225, 410, 225);
 
 delay(100);
}

void draw()
{
  i=i (int(random(-50, 50)));
 background(45);
  dane.addData("world", i);
  minimum = int(min(dane.getValuesFrom("world")));
  maximum = int(max(dane.getValuesFrom("world")));
 text(maximum, 390, 220);
 text(minimum, 390, 500);
 dane.setRange(minimum, maximum);
 dane.getValueLabel();
}

Thw problem happens when I try to define function

create_chart

CodePudding user response:

As pointed out by @Andy the create_chart method is not defined within the Dane class in the above code example. This should be fixed first.

If you didn’t intend to add create_chart to a separate class, you might want to change

dane.create_chart(…);

to simply

create_chart(…);

This however renders the Dane class (even more) useless.

Otherwise in your code dane in the setup() method is an instance of Chart from the ControlP5 library. The Chart class doesn’t seem to have any create_chart method and you probably intended to call the method from your Dane class.

So you probably want to change the type of dane from Chart to Dane.

The Dane class itself seems to have some issues as well (what’s the purpose of the dane member? And ControlP5.addChart returns Chart and not Dane) but this is out-of-scope for this question.

All in all your code has several issues that make it difficult to guess what your expectations are and how to fix it.

  • Related