mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-08 00:16:13 +00:00
improve control line example
This commit is contained in:
parent
8eaf3f5c5f
commit
a2f0097092
@ -289,7 +289,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
private ToggleButton rtsBtn, ctsBtn, dtrBtn, dsrBtn, cdBtn, riBtn;
|
private ToggleButton rtsBtn, ctsBtn, dtrBtn, dsrBtn, cdBtn, riBtn;
|
||||||
|
|
||||||
ControlLines(View view) {
|
ControlLines(View view) {
|
||||||
runnable = this::start; // w/o explicit Runnable, a new lambda would be created on each postDelayed, which would not be found again by removeCallbacks
|
runnable = this::run; // w/o explicit Runnable, a new lambda would be created on each postDelayed, which would not be found again by removeCallbacks
|
||||||
|
|
||||||
rtsBtn = view.findViewById(R.id.controlLineRts);
|
rtsBtn = view.findViewById(R.id.controlLineRts);
|
||||||
ctsBtn = view.findViewById(R.id.controlLineCts);
|
ctsBtn = view.findViewById(R.id.controlLineCts);
|
||||||
@ -317,7 +317,9 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean refresh() {
|
private void run() {
|
||||||
|
if (!connected)
|
||||||
|
return;
|
||||||
try {
|
try {
|
||||||
EnumSet<UsbSerialPort.ControlLine> controlLines = usbSerialPort.getControlLines();
|
EnumSet<UsbSerialPort.ControlLine> controlLines = usbSerialPort.getControlLines();
|
||||||
rtsBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.RTS));
|
rtsBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.RTS));
|
||||||
@ -326,33 +328,37 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
dsrBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.DSR));
|
dsrBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.DSR));
|
||||||
cdBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.CD));
|
cdBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.CD));
|
||||||
riBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.RI));
|
riBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.RI));
|
||||||
|
mainLooper.postDelayed(runnable, refreshInterval);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
status("getControlLines() failed: " + e.getMessage() + " -> stopped control line refresh");
|
status("getControlLines() failed: " + e.getMessage() + " -> stopped control line refresh");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void start() {
|
void start() {
|
||||||
if (connected) {
|
if (!connected)
|
||||||
try {
|
return;
|
||||||
EnumSet<UsbSerialPort.ControlLine> controlLines = usbSerialPort.getSupportedControlLines();
|
try {
|
||||||
if (!controlLines.contains(UsbSerialPort.ControlLine.RTS)) rtsBtn.setVisibility(View.INVISIBLE);
|
EnumSet<UsbSerialPort.ControlLine> controlLines = usbSerialPort.getSupportedControlLines();
|
||||||
if (!controlLines.contains(UsbSerialPort.ControlLine.CTS)) ctsBtn.setVisibility(View.INVISIBLE);
|
if (!controlLines.contains(UsbSerialPort.ControlLine.RTS)) rtsBtn.setVisibility(View.INVISIBLE);
|
||||||
if (!controlLines.contains(UsbSerialPort.ControlLine.DTR)) dtrBtn.setVisibility(View.INVISIBLE);
|
if (!controlLines.contains(UsbSerialPort.ControlLine.CTS)) ctsBtn.setVisibility(View.INVISIBLE);
|
||||||
if (!controlLines.contains(UsbSerialPort.ControlLine.DSR)) dsrBtn.setVisibility(View.INVISIBLE);
|
if (!controlLines.contains(UsbSerialPort.ControlLine.DTR)) dtrBtn.setVisibility(View.INVISIBLE);
|
||||||
if (!controlLines.contains(UsbSerialPort.ControlLine.CD)) cdBtn.setVisibility(View.INVISIBLE);
|
if (!controlLines.contains(UsbSerialPort.ControlLine.DSR)) dsrBtn.setVisibility(View.INVISIBLE);
|
||||||
if (!controlLines.contains(UsbSerialPort.ControlLine.RI)) riBtn.setVisibility(View.INVISIBLE);
|
if (!controlLines.contains(UsbSerialPort.ControlLine.CD)) cdBtn.setVisibility(View.INVISIBLE);
|
||||||
} catch (IOException e) {
|
if (!controlLines.contains(UsbSerialPort.ControlLine.RI)) riBtn.setVisibility(View.INVISIBLE);
|
||||||
Toast.makeText(getActivity(), "getSupportedControlLines() failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
run();
|
||||||
}
|
} catch (IOException e) {
|
||||||
if (refresh())
|
Toast.makeText(getActivity(), "getSupportedControlLines() failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
mainLooper.postDelayed(runnable, refreshInterval);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop() {
|
void stop() {
|
||||||
mainLooper.removeCallbacks(runnable);
|
mainLooper.removeCallbacks(runnable);
|
||||||
|
rtsBtn.setChecked(false);
|
||||||
|
ctsBtn.setChecked(false);
|
||||||
|
dtrBtn.setChecked(false);
|
||||||
|
dsrBtn.setChecked(false);
|
||||||
|
cdBtn.setChecked(false);
|
||||||
|
riBtn.setChecked(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user