Get Selected Item value from ScrollableList

edited January 2017 in Arduino

I am getting error in recovering the Item Value set using .setvalue(0). Also please guide how to use when users change the default value.

`//Imports
import processing.serial.*;
import controlP5.*;
import java.util.*;

//Variable Declarations
ControlP5 lstPortList;
ControlP5 lstBaud;
String tport;
int tbaud;
Serial myPort;      // The serial port
int whichKey = -1;  // Variable to hold keystoke values
int inByte = -1;    // Incoming serial data
int Indexport =0;
int Indexbaud =1;

///Setup/////
void setup() {
  size(400, 400);
  // create a font with the third font available to the system:
  PFont myFont = createFont(PFont.list()[2], 14);
  textFont(myFont);

  // List all the available serial ports:
  // printArray(Serial.list());

  lstPortList = new ControlP5(this);
  String[] portNames =Serial.list();
  lstPortList.addScrollableList("Port")
    .setPosition(10, 10)
      .setSize(100, 100)
        .setBarHeight(20)
          .setItemHeight(20)
            .addItems(portNames)
              .setFont(createFont("Arial", 13))
                .setColorForeground(color(40, 128))
                  .setValue(Indexport);

  lstBaud = new ControlP5(this);
  List BaudList = Arrays.asList("4800", "9600", "19200", "38400", "57600", "115200");
  lstBaud.addScrollableList("Baud")
    .setPosition(200, 10)
      .setSize(100, 100)
        .setBarHeight(20)
          .setItemHeight(20)
            .addItems(BaudList)
              .setFont(createFont("Arial", 13))
                .setColorForeground(color(40, 128))
                  .setValue(Indexbaud);
  tport= port(Indexport);//lstPortList.get(ScrollableList.class, "port").getItem(Indexport).get("text");
  tbaud= baud(Indexbaud); //lstBaud.get(ScrollableList.class, "port").getItem(Indexbaud).get("text");
  myPort = new Serial(this, tport, tbaud );
}

void draw() { 

  background(0);
  text("Last Received: " + inByte, 10, 130);
  text("Last Sent: " + whichKey, 10, 100);
}

void serialEvent(Serial myPort) {
  inByte = myPort.read();
}

void keyPressed() {
  // Send the keystroke out:
  myPort.write(key);
  whichKey = key;
}

String port(int n) {
  return lstPortList.get(ScrollableList.class, "Port").getItem(n).get("text");  
}
int baud(int n) {
  return lstBaud.get(ScrollableList.class, "Baud").getItem(n).get("text");  
}`

Thanks

Answers

  • edited January 2017

    I am using CP5 and am trying since morning to list all the COM ports and Baud Speeds and allow user to select and then should connect to that COM port at that Baud.

  • I am trying to list all the COM ports and Baud Speeds and allow user to select and then should connect to that COM port at that Baud.

  • edited January 2017

    Not totally sir. But now I am through with it. I still have a minor glitch. The Port or Baud I select from the List doesn't get immediately selected but gets selected on the next selection. Here is my code

        //Imports
        import processing.serial.*;
        import controlP5.*;
        import java.util.*;
    
        //Variable Declarations
        ControlP5 cp5;
        ScrollableList portList;
        ScrollableList baudList;
        Textlabel labelPort;
        Textlabel labelBaud;
        Textlabel selectedPort;
        Textlabel selectedBaud;
        Textlabel messages;
        Textlabel receivedFromSerial;
        Textlabel sendToSerial;
        Serial myPort;      // The serial port
        int whichKey = -1;  // Variable to hold keystoke values
        int inByte = -1;    // Incoming serial data
        int Indexport =0;
        int Indexbaud =1;
        String tport;
        int tbaud;
        boolean NewCOMSetting = false;
        String currentPortName;
        int currentBaud;
        int lf = 10;    // Linefeed in ASCII
        String tString = null;
        ///Setup/////
        void setup() {
          initialiser();
        }
    
        void initialiser()
        {
          size(800, 400);
          // Common Font
          PFont myFont = createFont("Arial", 13);
          textFont(myFont);
          frame.setTitle("CIOM Serial COM Interface");
    
          // List all the available serial ports:
          cp5 = new ControlP5(this); 
          String[] portNames = Serial.list();
          labelPort = cp5.addTextlabel("lblPort")
            .setText("COMM Port")
              .setPosition(10, 10)
                .setColorValue(0xffffff00)
                  .setFont(myFont)
                    ;
          labelBaud = cp5.addTextlabel("lblBaud")
            .setText("Baud Rate")
              .setPosition(200, 10)
                .setColorValue(0xffffff00)
                  .setFont(myFont)
                    ;
          portList = cp5.addScrollableList("Port") 
            .setPosition(10, 30) 
              .setSize(100, 100) 
                .setBarHeight(20) 
                  .setItemHeight(20) 
                    .addItems(portNames) 
                      .setFont(myFont)
                        .setColorForeground(color(40, 128))
                          .setValue(Indexport)
                            ;
          List BList = Arrays.asList("4800", "9600", "19200", "38400", "57600", "115200");
          baudList= cp5.addScrollableList("Baud")
            .setPosition(200, 30)
              .setSize(100, 100)
                .setBarHeight(20)
                  .setItemHeight(20)
                    .addItems(BList)
                      .setFont(myFont)
                        .setColorForeground(color(40, 128))
                          .setValue(Indexbaud)
                            ;
          //tport=  portList.get(ScrollableList.class, "port").getItem(Indexport).get("text");
          //  tbaud=  baudList.get(ScrollableList.class, "port").getItem(Indexbaud).get("text");
          currentPortName = portList.getCaptionLabel().getText();
          currentBaud = int (baudList.getCaptionLabel().getText());
    
          messages = cp5.addTextlabel("lblmessage")
            .setPosition(10, 375)
              .setColorValue(0xffffff00)
                .setFont(myFont)
                  ;
    
          setSerialPort(currentPortName, currentBaud);
          receivedFromSerial = cp5.addTextlabel("lblRxFromSerial")
            .setText("Received From Serial - ")
              .setPosition(10, 100)
                .setColorValue(0xffffff00)
                  .setFont(myFont)
                    ;
    
          sendToSerial = cp5.addTextlabel("lblTxToSerial")
            .setText("Transmitted To Serial - ")
              .setPosition(10, 200)
                .setColorValue(0xffffff00)
                  .setFont(myFont)
                    ;
    
          // Throw out the first reading, in case we started reading 
          // in the middle of a string from the sender.
          tString = myPort.readStringUntil(lf);
          tString = null;
        }
    
        void draw() { 
    
          background(0);
    
         if (tString != null) {text(tString, 200, 113);}
          text(" ", 75, 200);
        }
        void serialEvent(Serial myPort) {
    
          while (myPort.available () > 0) {
            tString = myPort.readStringUntil(lf);
           // if (tString != null) {
              // receivedFromSerial.setText(tString);
            //  text(tString, 75, 100);
            //}
          }
        }
        void keyPressed() {
          // Send the keystroke out:
          myPort.write(key);
          whichKey = key;
        }
        void controlEvent(ControlEvent theEvent) {
    
          if (theEvent.getController() == portList) {
            if (portList.getCaptionLabel().getText() != currentPortName) {
              NewCOMSetting = true;
            }
          }
          if (theEvent.getController() == baudList) {
            if (int(baudList.getCaptionLabel().getText()) != currentBaud) {
              NewCOMSetting = true;
            }
          }
          if (NewCOMSetting == true) {
            currentPortName =portList.getCaptionLabel().getText();
            currentBaud = int (baudList.getCaptionLabel().getText());
            setSerialPort(currentPortName, currentBaud);
            NewCOMSetting = false;
          }
        }
    
        // Set serial port to desired value.
        void setSerialPort(String portName, int baud) {
          // Close the port if it's currently open.
          if (myPort != null) {
            myPort.stop();
          }
          try {
            // Open port
            myPort = new Serial(this, portName, baud);
            //selectedPort.setText(portName);
            // selectedBaud.setText(str(baud));
            messages.setText("Current Port - " + portName + " at Baud - " + str(baud));
            myPort.bufferUntil('\n');
          }
          catch (RuntimeException ex) {
            // Swallow error if port can't be opened, keep port closed.
            myPort = null;
          }
        }
    
Sign In or Register to comment.