I want to send a command in serial and it prints many lines of data in serial monitor.
but here auto scroll not working, and even I remove the lines (see comment // auto scroll)
still the scroll bar moving slowly to down even all data printed still scroll bar moving slowly to down which make the UI a bit lag, when I use Queue method instead of synchronized(buffer) it stop printing after few lines of data . below Is my code, I want to make it autoscroll to end / bottom like in arduino ide serial monitor
import g4p_controls.*;
import processing.serial.*;
Serial myPort;
GTextArea receivedDataArea;
GTextField commandInput;
GButton sendButton;
StringBuilder buffer = new StringBuilder();
int currentTextLength = 0;
void setup() {
size(800, 600);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
// Initialize serial port
String portName = Serial.list()[0]; // Adjust this if necessary
myPort = new Serial(this, portName, 115200);
// Create text area for received data
receivedDataArea = new GTextArea(this, 10, 10, 780, 500, G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE);
//receivedDataArea.setFont(new Font("Arial", Font.PLAIN, 16));
receivedDataArea.setTextEditEnabled(false);
// Create text field for command input
commandInput = new GTextField(this, 10, 520, 680, 30);
commandInput.setPromptText("Enter command here");
// Create send button
sendButton = new GButton(this, 700, 520, 90, 30, "Send");
sendButton.addEventHandler(this, "sendButtonEvent");
}
void draw() {
background(240);
updateReceivedDataArea();
}
void updateReceivedDataArea() {
synchronized (buffer) {
if (buffer.length() > currentTextLength) {
String newData = buffer.substring(currentTextLength);
receivedDataArea.appendText(newData);
currentTextLength = buffer.length();
// Auto-scroll to the bottom
int length = receivedDataArea.getText().length();
receivedDataArea.moveCaretTo(length, length);
}
}
}
void serialEvent(Serial myPort) {
String inData = myPort.readStringUntil('\n');
if (inData != null) {
synchronized (buffer) {
buffer.append(inData.trim()).append("\n");
}
}
}
void sendButtonEvent(GButton button, GEvent event) {
if (event == GEvent.CLICKED) {
String command = commandInput.getText();
if (command != null && !command.isEmpty()) {
myPort.write(command + "\n");
commandInput.setText(""); // Clear the input field after sending
}
}
}