How can I save an OSC message inside a variable?

Hi everyone!

I’m quite new to processing, so please forgive me if there are any obvious/dumb mistakes here.

I’m working on a sketch where I’m trying to have the webcam read a QR code via BoofCV and have oscP5 deliver the message to an external app (Arena).

The code is working as intended, the problem is that after the QR code is read by Processing, it keeps sending the message to Arena in a loop.

What I’m trying to achieve is store the value of “myMessage” inside a variable, so that in the next loop I can verify if the new message is the same as the old one and have the loop delay the next time the QR is being read.

I tried with a string, but Processing tells me that it can not convert from OscMessage to String.

Can anyone help me out on this?

I’ll attach the code below, but as I said I’m quite new so I beg your forgiveness if anything of the following doesn’t make sense to you :')

import oscP5.*;
import netP5.*;
import processing.video.*;
import boofcv.processing.*;
import java.util.*;
import georegression.struct.shapes.Polygon2D_F64;
import georegression.struct.point.Point2D_F64;
import boofcv.alg.fiducial.qrcode.QrCode;


OscP5 oscP5;
NetAddress myRemoteLocation;
Capture cam;
SimpleQrCode detector;

void setup() {
  initializeCamera(640, 480);
  surface.setSize(cam.width, cam.height);
  detector = Boof.detectQR();
  
  oscP5 = new OscP5(this,12000);
  
  myRemoteLocation = new NetAddress("127.0.0.1",7000);
}

void draw() {
  if (cam.available() == true) {
    cam.read();

    List<QrCode> found = detector.detect(cam);

    image(cam, 0, 0);
    noFill();
    strokeWeight(5);
    stroke(255, 0, 0);

                      for( QrCode qr : found ) {
                              OscMessage myMessage = new OscMessage(""+qr.message);
                              myMessage.add(1); 
                              oscP5.send(myMessage, myRemoteLocation);
                              println (qr.message);

      beginShape();
      for( int i = 0; i < qr.bounds.size(); i++ ) {
          Point2D_F64 p = qr.bounds.get(i);
          vertex( (int)p.x, (int)p.y );
      }
      // close the loop
      Point2D_F64 p = qr.bounds.get(0);
      vertex( (int)p.x, (int)p.y );
      endShape();
    }
  }
}

void initializeCamera( int desiredWidth, int desiredHeight ) {
  String[] cameras = Capture.list();

  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    cam = new Capture(this, desiredWidth, desiredHeight);
    cam.start();
  }
}

The issue of P5 keep sending the message, probably is about the draw() loop. Perhaps the way to deal with it is controlling how and when (even if) your program loops. See the reference for loop(), noLoop() and redraw() about this.

But

can not convert from OscMessage to String.

is clear. OscMessage is a type, as is String. They are not the same. You can’t compare an orange with a spaceship. You might wanna check the JavaDoc for the library, here the page for OscMessage. As you can see there, there is a toString() method that should convert your message to a string. Or you might just store and compare the OscMessages themself. Would look some thing like

//global 
OscMessage storedMessage;

// then later
OscMessage myMessage = new OscMessage(""+qr.message);
           myMessage.add(1); 
storedMessage = myMessage;

//and even later when you wanna check:

if (storedMessage == myMessage){ 
//do your thing 
}
// You need to research if OscMessages can be compared by == operator, 
// probably not. perhaps the inherited  method equals() should work.
// like myMessage.equals(storedMessage), again the javadoc is you friend

The library site is full of infos. Like this, for instance:

Automatic event detection
oscP5 locates functions inside your sketch and will link incoming OSC message to matching functions automatically oscP5Plug → example. Incoming OSC messages can easily be captured within your sketch by implementing the oscEvent function oscP5message → example.