Understanding Callback with respect to selectInput()

edited January 2018 in Questions about Code

Hello all. I'm new to this, but not so new to basic coding, but it has been a long time since I've done any serious coding...

Here's my issue -- I have a Processing script that loads in a text file, parses it out and sends some info to an Arduino board. It works great, but I'd like to present the operator with the option of selecting a file, but I'm having trouble getting my head around the concept of "callback" stuff and in specific how to use it with the selectInput() function.

For the purposes of this, I have stripped out all the other stuff and just have the file load bits...

String[] protocol;
String[] fname;
int flag=0;

void setup() {
  selectInput("Select a file to process:", "fileSelected");
  while(flag==0){
    if(protocol!=null){
      flag=1;
    }
  }
  println(protocol[0]);  
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    protocol = loadStrings(selection.getAbsolutePath());
  }
}

My idea is that by putting the while loop it should hold the code until the variable protocol has something other than null. I have a feeling that I'm barking up the wrong tree with this and that null is the wrong thing to be checking for?

Any ideas or links to some tutorials or whatever would be greatly appreciated!!

Greg

Answers

  • Well it’s not the optimum but it looks ok

    Does it work?

    try also printArray(protocol); in line 12

    The dialogue for open a file is done in another thread and when it’s done, then the function fileselected gets called.

    You could set protocol to „-1“ after line 17 to start handling this situation

  • Thanks for the response, Chrisir!

    Unfortunately, it doesn't work. It seems to me to get hung up in the while() command. It prints the "User selected.. yada yada yada" message but get's hung up there.

    I'll give your suggestions a try and report back, though.

  • I had a look at that thread you shared, GoToLoop and thanks!! It works great.

    As I expected, the problem was in my while loop -- I thought it was what I thought was null, but I think it was more to do with the flag variable and checking the null... whatever...

    At anyrate, if anybody's interested, here's the working code as I got it. Now all I have to do is figure out how to hide the code window completely:)

    String[] protocol;
    String[] fname;
    
    void setup() {
      selectInput("Select a file to process:", "fileSelected");
      // selectInput("Please select canvas picture:", "selectImage");
      while (protocol == null)  delay(100);
      printArray(protocol); 
    }
    
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        //println("User selected " + selection.getAbsolutePath());
        protocol=loadStrings(selection.getAbsolutePath());
        //printArray(protocol);
      }
    }
    
Sign In or Register to comment.