polargraphcontroller/SerialPortWindow.pde

121 lines
3.3 KiB
Plaintext
Raw Normal View History

2015-04-26 21:58:07 +02:00
/*------------------------------------------------------------------------
2015-09-05 17:00:22 +02:00
Class and controllers on the "serial port" subwindow
------------------------------------------------------------------------*/
2015-09-05 17:00:22 +02:00
ControlFrameSimple addSerialPortControlFrame(String theName, int theWidth, int theHeight, int theX, int theY, int theColor ) {
final Frame f = new Frame( theName );
final ControlFrameSimple p = new ControlFrameSimple( this, theWidth, theHeight, theColor );
2015-04-26 21:58:07 +02:00
2015-09-05 17:00:22 +02:00
f.add( p );
p.init();
f.setTitle(theName);
f.setSize( p.w, p.h );
f.setLocation( theX, theY );
f.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
p.dispose();
f.dispose();
}
}
);
f.setResizable( true );
f.setVisible( true );
// sleep a little bit to allow p to call setup.
// otherwise a nullpointerexception might be caused.
try {
Thread.sleep( 100 );
}
catch(Exception e) {
}
2015-04-26 21:58:07 +02:00
ScrollableList sl = p.cp5().addScrollableList("dropdown_serialPort")
.setPosition(10, 10)
2017-06-25 22:50:44 +02:00
.setSize(150, 450)
.setBarHeight(20)
.setItemHeight(16)
.plugTo(this, "dropdown_serialPort");
2015-04-26 21:58:07 +02:00
sl.addItem("No serial connection", -1);
2015-09-05 17:00:22 +02:00
2017-04-21 00:30:31 +02:00
String[] ports = Serial.list();
2015-09-05 17:00:22 +02:00
for (int i = 0; i < ports.length; i++) {
println("Adding " + ports[i]);
sl.addItem(ports[i], i);
2015-09-05 17:00:22 +02:00
}
2015-09-05 17:00:22 +02:00
int portNo = getSerialPortNumber();
println("portNo: " + portNo);
if (portNo < 0 || portNo >= ports.length)
portNo = -1;
// set the value of the actual control
sl.setValue(portNo);
2015-04-26 21:58:07 +02:00
2017-06-25 22:50:44 +02:00
sl.setOpen(true);
2015-09-05 17:00:22 +02:00
return p;
2015-04-26 21:58:07 +02:00
}
2015-09-05 17:00:22 +02:00
void dropdown_serialPort(int newSerialPort)
2015-04-26 21:58:07 +02:00
{
2017-04-21 00:30:31 +02:00
println("In dropdown_serialPort, newSerialPort: " + newSerialPort);
// No serial in list is slot 0 in code because of list index
// So shift port index by one
2017-04-21 00:30:31 +02:00
newSerialPort -= 1;
2015-04-26 21:58:07 +02:00
if (newSerialPort == -2)
{
2015-09-05 17:00:22 +02:00
}
else if (newSerialPort == -1) {
println("Disconnecting serial port.");
useSerialPortConnection = false;
if (myPort != null)
{
myPort.stop();
myPort = null;
}
drawbotReady = false;
drawbotConnected = false;
serialPortNumber = newSerialPort;
}
else if (newSerialPort != getSerialPortNumber()) {
println("About to connect to serial port in slot " + newSerialPort);
// Print a list of the serial ports, for debugging purposes:
if (newSerialPort < Serial.list().length) {
try {
drawbotReady = false;
drawbotConnected = false;
if (myPort != null) {
myPort.stop();
myPort = null;
}
if (getSerialPortNumber() >= 0)
println("closing " + Serial.list()[getSerialPortNumber()]);
serialPortNumber = newSerialPort;
String portName = Serial.list()[serialPortNumber];
myPort = new Serial(this, portName, getBaudRate());
//read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
useSerialPortConnection = true;
println("Successfully connected to port " + portName);
}
catch (Exception e) {
println("Attempting to connect to serial port in slot " + getSerialPortNumber()
+ " caused an exception: " + e.getMessage());
}
} else {
println("No serial ports found.");
useSerialPortConnection = false;
}
} else {
println("no serial port change.");
2015-04-26 21:58:07 +02:00
}
}
2015-09-05 17:00:22 +02:00