Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import controlP5.*;
- import processing.serial.*;
- //rough template for serial port selection
- //based on controlP5 examples by andreas schlegel
- //changes by devicer.net
- Serial port;
- ControlP5 cp5;
- DropdownList d1;
- String[] seriallist;
- int serialSpeed = 9600;
- void setup() {
- size(600, 600);
- println(Serial.list());
- cp5 = new ControlP5(this);
- String[] seriallist = Serial.list();
- println(seriallist.length);
- if (seriallist.length == 0) {
- println("Empty serial list, no ports available/active");
- }
- if (seriallist.length == 1) {
- println("only one port found, using that one!");
- }
- else {
- if (seriallist.length > 1) {
- //do stuf fto create drop down choice...
- d1 = cp5.addDropdownList("serial ports select d1")
- .setPosition(100, 100)
- ;
- customize(d1); // customize the first list
- d1.captionLabel().set("select serial port");
- for (int i=0;i<seriallist.length;i++) {
- d1.addItem(seriallist[i], i);
- }
- }
- }
- }
- void draw() {
- background(0);
- // do stuff!
- }
- void customize(DropdownList ddl) {
- // a convenience function to customize a DropdownList
- ddl.setBackgroundColor(color(190));
- ddl.setItemHeight(20);
- ddl.setBarHeight(15);
- ddl.captionLabel().style().marginTop = 3;
- ddl.captionLabel().style().marginLeft = 3;
- ddl.valueLabel().style().marginTop = 3;
- //ddl.scroll(0);
- ddl.setColorBackground(color(60));
- ddl.setColorActive(color(255, 128));
- }
- void controlEvent(ControlEvent theEvent) {
- // DropdownList is of type ControlGroup.
- // A controlEvent will be triggered from inside the ControlGroup class.
- // therefore you need to check the originator of the Event with
- // if (theEvent.isGroup())
- // to avoid an error message thrown by controlP5.
- if (theEvent.isGroup()) {
- // check if the Event was triggered from a ControlGroup
- println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
- // println("getgroup: ", theEvent.getGroup().toString());
- if (theEvent.getGroup().toString().equals("serial ports select d1 [DropdownList]")) {
- println("got a serial ports select event: ");
- println(theEvent.getGroup().getCaptionLabel().getText());//this should be the portname, as a string
- if (port != null) {
- port.clear();
- port.stop();
- println("closed old port, opening new one...");
- }
- port = new Serial(this, theEvent.getGroup().getCaptionLabel().getText(), serialSpeed);
- }
- }
- else if (theEvent.isController()) {
- println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment