Label text slow to update via network events

edited July 2017 in Library Questions

I'm working on a sketch that is receiving network events from an external program (specifically, an OpenFrameworks sketch), using the processing.net library.

Inside the draw method, I have the following code to parse the incoming data, and assign it appropriately to display a value of text in a text label:

void draw()
{
  // check for incoming data
  Client client = server.available();
  if (client != null) {
    // check for a full line of incoming data
    String line = client.readStringUntil('\n');

    if (line != null) {
      //println(line);
      int val = int(trim(line)); // extract the predicted class
      //println(val);
      if (val == 1) {
        messageText = "EVENT 1";
      } else if (val == 2) {
        messageText = "EVENT 2";
      } else if (val == 3) {
        messageText = "EVENT 3";
      }
    } 
  }

  // draw
  background(0);
  textFont(f,64);
  fill(255);
  textAlign(CENTER);
  text(messageText, width/2, height/2);

}

Through logging, I have verified that the data is being received properly

However, I'm experiencing a very annoying bug - the text of my messageText label is VERY slow to update...after a new event has occurred (and is shown as such through logging), the messageText will still display the value of the last event for several seconds.

Anyone have any pointers on how to speed up performance here?

Thanks!

Answers

  • On a side note:

      textFont(f,64);
      fill(255);
      textAlign(CENTER);
    

    can all go in setup.

    Related to receiving data... are you sending data to the same machine?

    Through logging, I have verified that the data is being received properly

    You are using a third party SW to do this? What SW is this? What rates are you receiving?

    Kf

  • @kkfrajer Thank you for the side note!

    I may have misspoke - I was using println to verify that the data was being received properly. The data is being sent from an OpenFrameworks sketch to this Processing sketch, on the same machine. I'm not sure of the rate at which I'm receiving - can that be configured with Server somehow?

  • I have no experience with OFW.... I have experience using the server-client examples (https://processing.org/reference/libraries/net/index.html) on the same machine without any problems. I can say that oscP5 works as well out of the box.

    Can you provide more details about your setup? Even sharing your second code or a minimum runnable version just to see your approach?

    This next would be my first attempt to understand what I am receiving:

    Kf

    int ctrRx1=0;
    int ctrRx2=0;
    
    void draw()
    {
      // check for incoming data
      Client client = server.available();
      if (client != null) {
        // check for a full line of incoming data
        String line = client.readStringUntil('\n');
        ctrRx1++;
        if (line != null) {
          ctrRx2++;
        }
      }
    
      // draw
      background(0);
      textFont(f, 64);
      fill(255);
      textAlign(CENTER);
      text("RATES: R1="+
        nfs(ctrRx1/frameCount*100, 3, 2)+
        "%\tR2="+
        nfs(ctrRx2/frameCount*100, 3, 2)+"%", 
        width/2, height/2);
    }
    
  • Hey @kfrajer, I almost figured out the problem - in an extremely hacky sort of way.

    Basically, after changing the framerate to 600, and the renderer to P3D, everything worked perfectly - the issue was that the network code inside of draw() was not being updated fast enough!

    As I said though, this is extremely hacky.

    I then realized that the Network library has clientEvent and serverEvent methods!

    So, I tried implementing the clientEvent method as such. However, I think I may be misunderstanding something...even though my original, hacky code seems to work OK now, my new code below using this delegate method doesn't seem to work at all. Basically, I have to run my sketch first, which creates a server, that my external program connects to. That program then sends out data that's received by my Processing sketch.

    Here's what my full sketch looks like - anyone know where my misunderstanding may be coming from?

    import processing.net.*; // include the networking library
    
    Server server; // will receive predictions 
    Client client;
    String messageText;
    int dataIn;
    PFont f;
    
    void setup() {
      fullScreen(P3D);
      frameRate(600);
      server = new Server(this, 5204); // listen on port 5204
      client = server.available();
    
      messageText = "NO HAND";
      textAlign(CENTER);
      fill(255);
      f = createFont("Arial",48,true); // Arial, 16 point, anti-aliasing on
      textFont(f, 120);
    }
    
    void draw() {
      // draw
      background(0);
      text(messageText, width/2, height/2);
    }
    
    // If there is information available to read
    // this event will be triggered
    void clientEvent(Client client) {
      String msg = client.readStringUntil('\n');
      // The value of msg will be null until the 
      // end of the String is reached
      if (msg != null) {    
          int val = int(trim(line)); // extract the predicted class
          println(val);
          if (val == 1) {
            messageText = "A";
          } else if (val == 2) {
            messageText = "B";
          } else if (val == 3) {
            messageText = "C";
          } else if (val == 4) {
            messageText = "D";
          }
        }
      }
    
    }
    
  • edited July 2017

    Method readStringUntil() is useless within clientEvent(), b/c the latter is triggered for each single byte received, so you don't get more than 1 byte anyways.

    Instead, use readChar() there, gathering each char returned from it, concatenating += them into 1 global String variable.

    Let's name that String as received.
    Also declare another global String called message.

    Now check whether the current char received is an ENTER, that is an \n.
    If true, assign received to message. And then clear received afterwards, assigning it to "".

Sign In or Register to comment.