Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Dropdown list made by Dumle29 (Mikkel Jeppesen) for processing,
- it uses the ControlP5 library and the processing.Serial library
- */
- import controlP5.*; // Import the Serial, and controlP5 libraries.
- import processing.serial.*;
- ControlP5 controlP5; // Define the variable controlP5 as a ControlP5 type.
- DropdownList ports; // Define the variable ports as a Dropdownlist.
- Serial myPort; // Define the variable port as a Serial object.
- int Ss; // The dropdown list will return a float value, which we will connvert into an int. we will use this int for that).
- String[] comList ; // A string to hold the ports in.
- boolean serialSet; // A value to test if we have setup the Serial port.
- boolean Comselected = false; // A value to test if you have chosen a port in the list.
- char[] val = new char[10]; // Data received from the serial port
- boolean packetStart = false;
- boolean hasPrinted = false;
- int reading;
- int count = 0;
- void setup() {
- // Set the size, for this example 120 by 120 px is fine.
- size(400,128);
- textSize(128);
- controlP5 = new ControlP5(this);
- ports = controlP5.addDropdownList("list-1",10,25,100,84); //Make a dropdown list called ports. ("name", left margin, top margin, width, height (84 here since the boxes have a height of 20, and theres 1 px between each item so 4 items (or scroll bar).
- // Setup the dropdownlist by using a function. This is more pratical if you have several list that needs the same settings.
- customize(ports);
- }
- // The dropdown list returns the data in a way, that i dont fully understand, again mokey see monkey do. However once inside the two loops, the value (a float) can be achive via the used line ;).
- void controlEvent(ControlEvent theEvent) {
- if (theEvent.isGroup())
- {
- // Store the value of which box was selected, we will use this to acces a string (char array).
- float S = theEvent.group().value();
- // Since the list returns a float, we need to convert it to an int. For that we us the int() function.
- Ss = int(S);
- // With this code, its a one time setup, so we state that the selection of port has been done. You could modify the code to stop the serial connection and establish a new one.
- Comselected = true;
- }
- }
- // Here we setup the dropdown list.
- void customize(DropdownList ddl)
- {
- ddl.setBackgroundColor(color(200)); // Set the background color of the list (you wont see this though).
- ddl.setItemHeight(20); // Set the height of each item when the list is opened.
- ddl.setBarHeight(15);// Set the height of the bar itself.
- ddl.captionLabel().set("Select serial port"); // Set the lable of the bar when nothing is selected.
- ddl.captionLabel().style().marginTop = 3; // Set the top margin of the lable.
- ddl.captionLabel().style().marginLeft = 3; // Set the left margin of the lable.
- ddl.valueLabel().style().marginTop = 3; // Set the top margin of the value selected.
- comList = myPort.list(); // Store the Serial ports in the array comList.
- int size = comList.length; // Get the amount of ports
- for(int i=0; i< size; i++) // Add the ports to the list
- {
- ddl.addItem(comList[i],i);
- }
- ddl.setColorBackground(color(60)); // Set the color of the background of the items and the bar.
- ddl.setColorActive(color(255,128)); // Set the color of the item your mouse is hovering over.
- }
- void startSerial(String[] theport)
- {
- myPort = new Serial(this, theport[Ss], 9600); // When this function is called, we setup the Serial connection with the accuried values. The int Ss is the Serial select int we use.
- serialSet = true; // Since this is a one time setup, we state that we now have set up the connection.
- controlP5.hide(ports); println("removed");
- //println(ports);
- }
- void draw() {
- background(0);
- while(Comselected == true && serialSet == false) // Setup the serial port once when we have chosen a port
- {
- startSerial(comList);
- }
- while(Comselected && serialSet) // When we are all set, and a serial link has been established, start monitoring the temp.
- {
- if ( myPort.available() > 0)
- {
- reading = myPort.read(); // Get a byte
- if(reading == 'S') // If it's S as in start
- {
- count = 0; // Reset the count of bytes recieved
- //print("Start");
- packetStart = true;
- }
- else if(reading == 'E')
- {
- myPort.read();
- //println("End");
- packetStart = false;
- hasPrinted = false;
- }
- if(packetStart && reading != 'S' && reading != 'E')
- {
- val[count] = char(reading); // read it and store it in val
- count++;
- }
- }
- if(!packetStart && !hasPrinted)
- {
- for(int i=0; i<count; i++)
- {
- if(i == count-1)
- {
- println(val[i]);
- }
- else
- {
- print(val[i]);
- }
- }
- hasPrinted = true;
- }
- text(val, 0, 5, 10, 108);
- fill(255);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement