-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSerialSelector.pde
131 lines (107 loc) · 3.11 KB
/
SerialSelector.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
public class SerialSelector {
public boolean m_chosen;
public String m_port;
PFont m_font;
int m_fontSize = 18;
float m_fontHeight;
float m_fontAscent;
//color m_bgcolor = color(80,120,230,150);
color m_bgcolor = color(30,30,50,140);
color m_color = color(255);
int m_borderInset = 20;
public SerialSelector() {
m_chosen = false;
// Set up the font
m_font = createFont("Arial", m_fontSize);
textFont(m_font);
textSize(m_fontSize);
m_fontHeight = textAscent() + textDescent();
m_fontAscent = textAscent();
// Connect the draw and mouse events, so this displays automagically
registerMethod("mouseEvent", this);
registerMethod("draw", this);
}
public void mouseEvent(MouseEvent e) {
// If we haven't selected a serial port, draw a menu to display the possible choices
if(m_chosen) {
return;
}
int x = e.getX();
int y = e.getY();
switch (e.getAction()) {
case MouseEvent.RELEASE:
// do something for mouse released
// println("SerialSelector: released!");
break;
}
}
// Filter out some ports we don't care about, eg /dev/tty. on OS/X
String[] listPorts() {
ArrayList<String> ports = new ArrayList<String>();
String OS = System.getProperty("os.name");
if(OS.startsWith("Mac OS X")) {
for(String s : Serial.list()) {
// Mask unlikely ports on OS/X
if(s.startsWith("/dev/tty")
| s.contains("Bluetooth-PDA-Sync")
| s.contains("Bluetooth-Modem")
| s.contains("Bluetooth-Incoming-Port")) {
continue;
}
ports.add(s);
}
}
else if(OS.startsWith("Linux")) {
for(String s : Serial.list()) {
// Mask unlikely ports on Linux
if(s.startsWith("/dev/ttyACM")) {
ports.add(s);
}
}
}
else {
// Other OS
for(String s : Serial.list()) {
ports.add(s);
}
}
return ports.toArray(new String[0]);
}
void draw() {
// If we haven't selected a serial port, draw a menu to display the possible choices
if(m_chosen) {
return;
}
//mouseY = mouseY + 29;
// Draw a rectangle to show we are presenting a meu
pushStyle();
fill(m_bgcolor);
stroke(m_bgcolor);
strokeWeight(1);
rect(m_borderInset,m_borderInset,width-m_borderInset*2, height-m_borderInset*2);
float y = m_borderInset*2 + m_fontAscent;
textFont(m_font);
textSize(m_fontSize);
fill(m_color);
textAlign(CENTER);
text("Connect to a BlinkyTape", width/2, y);
y+=m_fontHeight*3;
textAlign(LEFT);
for (String p : listPorts()) {
if (mousePressed && (mouseButton == LEFT) && (mouseY > y && mouseY < y + 20)) {
fill(255, 0, 255);
m_port = p;
m_chosen = true;
}
else if (mouseY > y && mouseY < y + 20) {
fill(0, 0, 255);
}
else {
fill(255);
}
text(p, m_borderInset*2, y);
y+=m_fontHeight;
}
popStyle();
}
}