-
Notifications
You must be signed in to change notification settings - Fork 14
/
port_detector.pde
115 lines (105 loc) · 3.76 KB
/
port_detector.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import processing.serial.*;
class PortDetector {
int detectionInterval = 2 * 1000; // N seconds
Timer timer = new Timer(detectionInterval);
String[] ports = new String[0];
String portNameDevice;
String portNameShort;
Serial[] serials = new Serial[ports.length];
boolean deviceConnected = false;
boolean portInitializationInProgress = false;
boolean spectruinoDetectionInProgress = false;
boolean detectionTimedOut = false;
boolean mockPort = false; // is simulation mode running?
boolean portReady() {
// is the physical port connected to spectruino?
return (deviceConnected && !spectruinoDetectionInProgress && !portInitializationInProgress); // meaning application just started, or application has detected spectruino
}
void init() {
ports = Serial.list();
serials = new Serial[ports.length];
mockPort = false;
deviceConnected = false;
}
void startPortDetection(PApplet parent) {
String[] portFound; // null if spectruino serial port not found (true if port was found)
String[] portIsSpecial; // we do not want special serial ports, otherwise errors when opening
init();
detectionTimedOut = false;
spectruinoDetectionInProgress = true;
println("Available ports:");
println(ports);
portInitializationInProgress = true;
for (int i=0; i<ports.length; i++) {
println("Probing: " + ports[i]);
portFound = match(ports[i].toLowerCase(), "usb|com\\d*$"); //[uUcC][sSoO][bBmM]
portIsSpecial = match(ports[i].toLowerCase(), "cu.");
//portFound = match(portFound, "!!!!!!!!!!!not cu.Bluetooth alebo cu."); //[uUcC][sSoO][bBmM]
if (portFound!=null && portIsSpecial==null) {
try {
serials[i] = new Serial(parent, ports[i], bitrate);
} catch (Exception e) {
serials[i] = null;
println("Problem probing port " + ports[i] + ".");
e.printStackTrace();
continue;
}
serials[i].bufferUntil(_c);
}// End if portFound
}
timer.start();
portInitializationInProgress = false;
}
Serial checkPortDetection(Serial p) {
println("Checking ports...");
if (portInitializationInProgress || detectionTimedOut()) {
return null;
}
if (spectruinoDetectionInProgress) {
byte[] portBytes = p.readBytes();
//// if (!spectruino05.this.isHeaderPresent(portBytes, portBytes.length)) {
if (!isHeaderPresent(portBytes, portBytes.length)) {
return null;
}
// close other ports
for (int i=0; i<serials.length; i++) {
if (serials[i]!=p) {
if (serials[i]!=null) {
serials[i].stop();
}
} else {
// found spectruino on port
//if (_DBG) {
println("Spectruino on port Nr. ["+i+"] "+ ports[i] );
printMainText("\nSpectruino connected on port "+ports[i]);
portNameDevice = new String(ports[i]);
String tmp[] = match(ports[i], "(?<=-).*$|com"); // |com
if (tmp!=null) {
portNameShort = tmp[0];
} else {
portNameShort = "unknown";
}
//}
spectruinoDetectionInProgress = false;
}
}
deviceConnected = true;
return p;
} else {
deviceConnected = false;
return null;
}
}
boolean detectionTimedOut() {
if (!detectionTimedOut && spectruinoDetectionInProgress && timer.isFinished()) {
for (int i=0; i<serials.length; i++) {
if (serials[i]!=null) {
serials[i].stop();
}
}
detectionTimedOut = true;
println("timer timed out");
}
return detectionTimedOut;
}
}