ofxOsc ofxGui | how to ofxToggle and ofxButton

#1

hi everyone!

i tried for some time but couldnt being able to receive a message by osc to start a ofxToggle or ofxButton (inside ofxPanel). With ofParameters it is working fine, but just for the toggles / buttons it doesnt work.

does anyone has any hint on how to do it?

below is the code that i am using to control ofParameter with osc messages, and what i thought would work with ofxToggle:

OFAPP::SETUP

sender.setup("192.168.0.100", 8000);
receiver.setup(9000);

OFAPP::UPDATE

ofxOscMessage r;
r.setAddress("/red1");
r.addFloatArg(red); // ofParameter<float> red;
sender.sendMessage(r, false);

//that is what i thought would work with ofxToggle
ofxOscMessage p;
p.setAddress("/pause");    
p.addBoolArg(pause); // ofxToggle pause OR ofxButton pause;
sender.sendMessage(p, false);

thank you!

#2

Hi @novas_fronteiras,

I’m not too sure why it doesn’t work for you, but I personally don’t use ofxToggle.

For some reasons you can’t add those to an ofGroupParameter, so it’s less neat when using a lot of gui parameters…
I personally use an ofxParameter < bool> then in the update function, I usually add something like :

if(paramBool)paramBool = false;

This way it has the behaviour of a ofxToggle, and you can use it with with OSC.

Hope this helps,

++

P

#3

Hi,
It isn’t clear to me what you want to do, but in case you want to set the ofxToggle state, based upon the value of an incoming ofxOscMessage, in ofApp::update():

ofxOscMessage m;
receiver.getNextMessage(m);
toggle = m.getArgAsBool;

And in case you want to send the value of the ofxToggle: you’re code works fine here.
The state of the ofxToggle is send in the message as a boolean argument.

#4

hi @Jildert and @pierre_tardif00

thank you for the asnwer!
now it is running with ofxToggle, but still no change with ofxButton.

I created an ofxButton stop; and am trying to send its command by osc. but none of the below is working:

ofxOscMessage stopOsc;
stopOsc.setAddress("/stop");
stopOsc.addBoolArg(stop);
sender.sendMessage(stopOsc, false);
ofxOscMessage stopOsc;
stopOsc.setAddress("/stop");
stopOsc.addFloatArg(stop);
sender.sendMessage(stopOsc, false);

etc…

any ideas?

#5

Ah, I got it :wink:
I think the best way do to this is to add a listener function to the ofxButton, as demonstrated in examples/gui/guiExample (in your OF folder) with the ringButton.
And then do the OSC sending in the listener function.
Is this suitable? Or is it necessary that you send the status of the ofxButton continuously in update()? Then @pierre_tardif00 his approach would work best, I think.
Good luck!

#6

ah, ok! got it!
i will try that way and also @pierre_tardif00 idea.

the only problem is that I have around 50 buttons haha
but, if that works, that will be the way!

thank you!

#7

In case you’re going to use ofParameter, you could check out the example @arturo gives in this thread:

#8

Hi,
as @pierre_tardif00 suggested, do not create instances of ofxToggle or ofxButton, instead use ofParameter<bool> or ofParameter<void> respectively. then use event listeners for each of these parameters to send the osc messages.
Sending the value of the ofxButton is not working because it does not hold a boolean, instead it is intended to trigger an event whenever pressed.

all this can sound a bit complicated but it is not.

decaler in ofApp.h

ofParameter<bool> boolParam;
ofParameter<void> voidParam;
ofxPanel gui;

void boolParamChanged(bool& b);
void voidParamTrigered();

ofApp.cpp


void ofApp::setup(){
gui.setup();
gui.add(boolParam.set("Bool Param", true)); // name as you wish and set the initial state as needed
gui.add(voidParam.set("Void param")); // you only can give it a name

boolParam.addListener(this, &ofApp::boolParamChanged);
voidParam.addListener(this, &ofApp::voidParamTrigered);

}
void ofApp::boolParamChanged(bool& b){
// put here what ever you want to happen when the toogle changes.
// the bool b (the function argument passed) holds the current state of the button

ofxOscMessage p;
p.setAddress("/pause");    
p.addBoolArg(b); 
sender.sendMessage(p, false);

}
void ofApp::voidParamTrigered(){
//this function gets triggered wheever the button is pressed.

ofxOscMessage p;
p.setAddress("/pause");  
// the following will not work as the void param holds no boolean.  
p.addBoolArg(voidParam); 
// instead put
p.addBoolArg(true); // or false depending on what you want 
sender.sendMessage(p, false);
}

I wrote out this from the top of my head here in teh forum so it has not been tested.

@pierre_tardif00 you can not add an ofxButton or ofxToogle to an ofParameterGroup because the ofParameterGroup only accepts ofParameters while these two are not ofParameters, instead these are ofxGui elements which hold internaly an of parameter.

hope this helps/

cheers

2 Likes
#9

Did you tried to send / receive the osc message as int besides bool? I remember some time ago having problems with bools in OSC too. Also the udp packet size of an osc maybe do not change in both cases. There’s a helper addon too ofxPubSubOsc that can do it simpler. If yo need to send several bool’s maybe you can encode/decode all too into a int, that it’s like 4 bytes/32bits in just one message… (Sorry to drop fast this replay but not readed in detail. Just some ideas)