Advertisement
dumle29

Untitled

Apr 6th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. /*Dropdown list made by Dumle29 (Mikkel Jeppesen) for processing,
  2. it uses the ControlP5 library and the processing.Serial library
  3. */
  4.  
  5. import controlP5.*;              // Import the Serial, and controlP5 libraries.
  6. import processing.serial.*;
  7.  
  8. ControlP5 controlP5;             // Define the variable controlP5 as a ControlP5 type.
  9. DropdownList ports;              // Define the variable ports as a Dropdownlist.
  10. Serial myPort;                     // Define the variable port as a Serial object.
  11. int Ss;                          // The dropdown list will return a float value, which we will connvert into an int. we will use this int for that).
  12. String[] comList ;               // A string to hold the ports in.
  13. boolean serialSet;               // A value to test if we have setup the Serial port.
  14. boolean Comselected = false;     // A value to test if you have chosen a port in the list.
  15.  
  16. char[] val = new char[10];      // Data received from the serial port
  17. boolean packetStart = false;
  18. boolean hasPrinted = false;
  19. int reading;
  20. int count = 0;
  21.  
  22. void setup() {
  23.   // Set the size, for this example 120 by 120 px is fine.
  24.   size(400,128);
  25.   textSize(128);
  26.   controlP5 = new ControlP5(this);
  27.   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).
  28.   // Setup the dropdownlist by using a function. This is more pratical if you have several list that needs the same settings.
  29.   customize(ports);
  30. }
  31.  
  32. // 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 ;).
  33. void controlEvent(ControlEvent theEvent) {
  34.   if (theEvent.isGroup())
  35.   {
  36.     // Store the value of which box was selected, we will use this to acces a string (char array).
  37.     float S = theEvent.group().value();
  38.     // Since the list returns a float, we need to convert it to an int. For that we us the int() function.
  39.     Ss = int(S);
  40.     // 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.
  41.     Comselected = true;
  42.   }
  43. }
  44.  
  45. // Here we setup the dropdown list.
  46. void customize(DropdownList ddl)
  47. {
  48.   ddl.setBackgroundColor(color(200)); // Set the background color of the list (you wont see this though).
  49.   ddl.setItemHeight(20); // Set the height of each item when the list is opened.
  50.   ddl.setBarHeight(15);// Set the height of the bar itself.
  51.   ddl.captionLabel().set("Select serial port"); // Set the lable of the bar when nothing is selected.
  52.   ddl.captionLabel().style().marginTop = 3; // Set the top margin of the lable.
  53.   ddl.captionLabel().style().marginLeft = 3; // Set the left margin of the lable.
  54.   ddl.valueLabel().style().marginTop = 3; // Set the top margin of the value selected.
  55.  
  56.   comList = myPort.list(); // Store the Serial ports in the array comList.
  57.   int size = comList.length; // Get the amount of ports
  58.   for(int i=0; i< size; i++) // Add the ports to the list
  59.   {
  60.     ddl.addItem(comList[i],i);
  61.   }
  62.   ddl.setColorBackground(color(60)); // Set the color of the background of the items and the bar.
  63.   ddl.setColorActive(color(255,128)); // Set the color of the item your mouse is hovering over.
  64. }
  65.  
  66. void startSerial(String[] theport)
  67. {
  68.   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.
  69.   serialSet = true; // Since this is a one time setup, we state that we now have set up the connection.
  70.   controlP5.hide(ports);  println("removed");
  71.   //println(ports);
  72. }
  73.  
  74.  
  75. void draw() {
  76.   background(0);
  77.  
  78.   while(Comselected == true && serialSet == false) // Setup the serial port once when we have chosen a port
  79.   {
  80.     startSerial(comList);
  81.   }
  82.  
  83.   while(Comselected && serialSet) // When we are all set, and a serial link has been established, start monitoring the temp.
  84.   {
  85.     if ( myPort.available() > 0)
  86.     {
  87.       reading = myPort.read(); // Get a byte
  88.  
  89.       if(reading == 'S') // If it's S as in start
  90.       {
  91.         count = 0; // Reset the count of bytes recieved
  92.         //print("Start");
  93.         packetStart = true;
  94.       }
  95.       else if(reading == 'E')
  96.       {
  97.         myPort.read();
  98.         //println("End");
  99.         packetStart = false;
  100.         hasPrinted = false;
  101.       }
  102.  
  103.       if(packetStart && reading != 'S' && reading != 'E')
  104.       {
  105.         val[count] = char(reading);         // read it and store it in val
  106.         count++;
  107.       }
  108.     }
  109.  
  110.     if(!packetStart && !hasPrinted)
  111.     {
  112.       for(int i=0; i<count; i++)
  113.       {
  114.         if(i == count-1)
  115.         {
  116.           println(val[i]);
  117.         }
  118.         else
  119.         {
  120.           print(val[i]);
  121.         }
  122.       }
  123.       hasPrinted = true;
  124.     }
  125.     text(val, 0, 5, 10, 108);
  126.     fill(255);
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement