Guest User

processing serial port select template

a guest
Oct 7th, 2014
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import controlP5.*;
  2. import processing.serial.*;
  3.  
  4. //rough template for serial port selection
  5. //based on controlP5 examples by andreas schlegel
  6. //changes by devicer.net
  7.  
  8. Serial port;
  9.  
  10. ControlP5 cp5;
  11. DropdownList d1;
  12.  
  13. String[] seriallist;
  14. int serialSpeed = 9600;
  15.  
  16. void setup() {
  17.   size(600, 600);
  18.   println(Serial.list());
  19.   cp5 = new ControlP5(this);
  20.  
  21.   String[] seriallist = Serial.list();
  22.   println(seriallist.length);
  23.   if (seriallist.length == 0) {
  24.     println("Empty serial list, no ports available/active");
  25.   }
  26.   if (seriallist.length == 1) {
  27.     println("only one port found, using that one!");
  28.   }
  29.   else {
  30.     if (seriallist.length > 1) {
  31.       //do stuf fto create drop down choice...
  32.       d1 = cp5.addDropdownList("serial ports select d1")
  33.         .setPosition(100, 100)
  34.           ;
  35.       customize(d1); // customize the first list
  36.       d1.captionLabel().set("select serial port");
  37.       for (int i=0;i<seriallist.length;i++) {
  38.         d1.addItem(seriallist[i], i);
  39.       }
  40.     }
  41.   }
  42. }
  43.  
  44.  
  45. void draw() {
  46.   background(0);
  47.   // do stuff!
  48. }
  49.  
  50.  
  51. void customize(DropdownList ddl) {
  52.   // a convenience function to customize a DropdownList
  53.   ddl.setBackgroundColor(color(190));
  54.   ddl.setItemHeight(20);
  55.   ddl.setBarHeight(15);
  56.   ddl.captionLabel().style().marginTop = 3;
  57.   ddl.captionLabel().style().marginLeft = 3;
  58.   ddl.valueLabel().style().marginTop = 3;
  59.   //ddl.scroll(0);
  60.   ddl.setColorBackground(color(60));
  61.   ddl.setColorActive(color(255, 128));
  62. }
  63.  
  64. void controlEvent(ControlEvent theEvent) {
  65.   // DropdownList is of type ControlGroup.
  66.   // A controlEvent will be triggered from inside the ControlGroup class.
  67.   // therefore you need to check the originator of the Event with
  68.   // if (theEvent.isGroup())
  69.   // to avoid an error message thrown by controlP5.
  70.  
  71.   if (theEvent.isGroup()) {
  72.     // check if the Event was triggered from a ControlGroup
  73.     println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
  74.     //    println("getgroup: ", theEvent.getGroup().toString());
  75.     if (theEvent.getGroup().toString().equals("serial ports select d1 [DropdownList]")) {
  76.       println("got a serial ports select event: ");
  77.       println(theEvent.getGroup().getCaptionLabel().getText());//this should be the portname, as a string
  78.       if (port != null) {
  79.         port.clear();
  80.         port.stop();
  81.         println("closed old port, opening new one...");
  82.       }
  83.       port = new Serial(this, theEvent.getGroup().getCaptionLabel().getText(), serialSpeed);
  84.     }
  85.   }
  86.   else if (theEvent.isController()) {
  87.     println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment