-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessingTerminal.pde
150 lines (144 loc) · 4.1 KB
/
ProcessingTerminal.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Serial terminal
Johan terryn
*/
import processing.serial.*;
import java.text.*;
import java.util.*;
import controlP5.*;
// https://www.sojamo.de/libraries/controlP5/reference/index.html
ControlP5 cp5;
ScrollableList list;
int listX = 1250;
int listY = 50;
int listWidth = 200;
int listHeight = 200;
int barHeight = 25;
Boolean toFile = false;
Serial myPort; // The serial port
String fileName;
PrintWriter output;
DateFormat fnameFormat = new SimpleDateFormat("MMdd_HHmmss");
DateFormat timeFormat = new SimpleDateFormat("hh:mm:ss:SSS");
int cols = 1500;
int rows = 900;
int col = 0;
int row = 10;
PImage rec, start;
String command = "";
void setup() {
size(1500, 1000);
cp5 = new ControlP5(this);
List l = Arrays.asList("9600", "19200", "38400", "57600", "115200", "250000", "500000", "1000000");
list = cp5.addScrollableList("Baud rate")
.setPosition(listX, listY)
.setSize(listWidth, listHeight)
.setBarHeight(barHeight)
.setItemHeight(20)
.addItems(l)
.setType(ScrollableList.DROPDOWN);
//CColor c = new CColor();
//c.setBackground(color(125,0,0));
//list.getItem(4).put("color", c);
myPort = new Serial(this, Serial.list()[0], 115200); //set data speed required -> equal to sender!!!
rec = loadImage("record-icon.png");
start = loadImage("start-icon.png");
textFont(createFont("Go-Medium.ttf", 10));
textSize(10);
background(0);
while (myPort.available() > 0) { //clean up of com port data
myPort.readChar();
}
}
void draw() {
if (toFile) {
image(rec, 1280, 900);
} else {
image(start, 1280, 900);
}
push(); //command line area
fill(255);
rect(190, 940, 1000, 25, 25);
fill(0);
if (command.length() > 0) {
textSize(16);
text(command, 200, 957);
}
pop(); //command line area
while (myPort.available() > 0) {
char inByte = myPort.readChar();
//print(inByte); //to console
if (toFile) {
output.print(inByte);
}
// to screen
text(inByte, col+=11, row);
if ((inByte == '\n') || (col > cols)) { // end of line
col = 0;
row += 11;
}
if (row > rows) { //end of page
col = 0;
row = 10;
background(0);
}
}
}
void keyPressed() {
if (focused) {
switch (key) {
case ESC:
if (output != null) {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
}
exit(); // Stops the program
case BACKSPACE:
if (command.length() > 0) {
command = command.substring(0, command.length()-1); //remove last character
}
break;
case ENTER: //send command
if (command.length() > 0) {
myPort.write(command);
println(command);
command = ""; //empty command
break;
}
default: //add valid ascii characters to the command
if (key >= 32 && key <= 127) {
command += key;
}
}
}
}
void mousePressed() {
if (mouseButton == LEFT) {
int x = mouseX;
int y = mouseY;
if ( x > 1280 && x < 1352 && y > 900 && y < 972) { // inside record / stop button
toFile = !toFile;
if (toFile)
{
Date now = new Date();
fileName = fnameFormat.format(now);
output = createWriter(fileName + ".txt"); // save the file in the sketch folder}
} else {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
}
}
}
}
void controlEvent(ControlEvent theEvent) { //when something in the list is selected
int baudRates[] = {9600, 19200, 38400, 57600, 115200, 250000, 500000, 1000000};
myPort.clear(); //delete the port
myPort.stop(); //stop the port
if (theEvent.isController() &&list.isMouseOver()) {
myPort = new Serial(this, Serial.list()[0], baudRates[(int)theEvent.getController().getValue()]); //set Baudrate
println("Serial index set to: " + baudRates[(int)theEvent.getController().getValue()]);
push();
fill(0);
rect (listX, listY,listWidth,listHeight);
pop();
}
}