Help: Processing, ControlP5 --Using multiple contols

Alright, I am building a new virtual contol panel for Walter using the controlP5 system of buttons and sliders etc. I have taken the example code for each of the contols I would like to use, cleaned them up and converted their final output to an int so it is all happy-slappy and ready to be sent via serial UART. Each of my controls work stand-alone and work great. Now I have to start putting them all together...

These controls do not have to be "watched" within void draw, instead when they are used, they get caught and read by "controlEvent". Each of the example codes use basically the same controlEvent routine so when you start putting these controls together, a way to distinguish which control what moved or adjusted is needed. There is a controlEvent example, which I have poured over and thought that I had followed exactly but I still come up with errors. Not to mention, I am still learning/figuring out what is what in terms of variable names, functions, classes etc. I mean, c'mon when all variables can be named whatever you want, it is sometimes hard to figure out what is a keyword and what is a name someone made up (yes, I know a lot of stuff is color coded but it's still tough).

Currently, I am working with the control event example and also the radiobutton example (I just picked the radiobutton code at random for this testing and experimentation). The radiobutton code looks like this:

/**
 * ControlP5 RadioButton
 * by andreas schlegel, 2009
 */

 

import controlP5.*;

ControlP5 controlP5;

int myColorBackground = color(0,0,0);

public RadioButton r;
void setup() {
  size(400,400);
  smooth();
  controlP5 = new ControlP5(this);
  r = controlP5.addRadioButton("radioButton",20,160);
  r.setColorForeground(color(120));
  r.setColorActive(color(255));
  r.setColorLabel(color(255));
  r.setItemsPerRow(5);
  r.setSpacingColumn(50);

  addToRadioButton(r,"Auto",1);
  addToRadioButton(r,"RC",2);
  addToRadioButton(r,"TBD",3);
  addToRadioButton(r,"TBD",4);
  addToRadioButton(r,"TBD",5);
}


void addToRadioButton(RadioButton theRadioButton, String theName, int theValue ) {
  Toggle t = theRadioButton.addItem(theName,theValue);
  t.captionLabel().setColorBackground(color(80));
  t.captionLabel().style().movePadding(2,0,-1,2);
  t.captionLabel().style().moveMargin(-2,0,0,-3);
  t.captionLabel().style().backgroundWidth = 46;
}


void draw() {
  background(myColorBackground);
}



void controlEvent(ControlEvent theEvent) {
  print("got an event from "+theEvent.group().name()+"\t");
  for(int i=0;i<theEvent.group().arrayValue().length;i++) {
    print(int(theEvent.group().arrayValue()[i]));
  }
  println("\t "+theEvent.group().value());
  myColorBackground = color(int(theEvent.group().value()*50),0,0);
}

Now, it seems that the first thing to do is to set an id for a new button when it is created. In addition, it also seems that it must be declared as "public" although I am a little confused here as well. In terms of the identification, it seemed pretty straight-forward according to the example. .setId() is simply added to the "add button" command. If I were to do this to the above code, it would look like this:

  r = controlP5.addRadioButton("radioButton",20,160).setId(1);

However, when I do this, I get this error: cannot convert from void to RadioButton. This leads me to think I am missing something very simple --or-- I am missing the big picture of the whole system and I should just slit my wrists and end it now. Either way, I am open to your thoughts.

 

type

Processing is one of those languages that needs to know what is what. If you create something new, you must tell Processing what type of something it is gonna be. A language is often called typefull, because of this property. The activity is often called type casting.

The example code is type casting r as a RadioButton in the line
public RadioButton r;

My guess is (I am a Processing newby myself) that a new thingey is by default of type void. And that Processing is complaining about your attempts to recast it as something else.

One issue you might run into

One issue you might run into is the use of TBD multiple times for the names of the radio buttons. will look at it in a bit to see if i can help.

edit: confirmed the tbd giving warnings, but didn’t run into any other issues. What version of processing are you using?

I currently still have 1.0.9 though I have 1.2.1 ready to install.

** r =**

  r = controlP5.addRadioButton(“radioButton”,20,160).setId(1);

use instead

r = controlP5.addRadioButton(“radioButton”,20,160)

r.setId(1);

I believe you are getting the void error due to the return value of setId being…well…void. So once you create the object r, set it’s id field by using the r.setId() command.

One thing I also did was use

int myid = 1;

r.setId(myid);

let me know if this helps…

last note, in some of their examples they show different methods of doing the same thing and don’t necessarily document it well…in fact he documention appears to be lacking in general… This is why I’ve been doing al ot of looking at the library vs actually trying to use it…it can be very confusing at first…  :confused:

Well…

Update

I think you are right, rik – I tried this instead, r.setId(1); instead and it took (not with the radiobuttons however, but with the other controls). The ID was also read correctly by a switch (theEvent.controller().id()) { as well. I guess the radio buttons are just their own animal, I dunno. At any rate, I am pressing forward and might just simply have to abandon the radio buttons for a different control that wants to play nice. In the meantime, I must say that all these issues are indeed forcing me to learn a lot more about structure which is good. I also must admit that while I am not on step 3 trying to do step 487, I am on step 25 and trying to skip to about step 50 or so. --I am breaking my own rules but seem to be keeping most information from going way over my head and at least learning the major steps along the way. Thanks for your help as always guys.

That’s funny, voodoo

As soon as I hit submit on the above comment, I only then saw your post --great minds think alike and ours do too!

 

One thing I wanted to

One thing I wanted to mention about the event is that instead of using .id I believe you can also use the name, so “auto”, “rc”, “tbd”, etc… it is case sensetive though…so be aware…

Update(2);

Alright boys, 2 outta 2 working. Adding third… Hold on to your butts.