java.lang.Object
pl.polsl.rtsa.hardware.RealDeviceClient
- All Implemented Interfaces:
DeviceClient
Implementation of
DeviceClient for the real hardware device via UART.
Handles serial communication, data buffering, and FFT processing.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AppConfigprivate doubleprivate final SignalProcessingServiceprivate intprivate final AtomicBooleanprivate final List<DataListener> private static final org.slf4j.Loggerprivate final ExecutorServiceprivate Threadprivate final int[]private final AtomicBooleanprivate intprivate intprivate com.fazecast.jSerialComm.SerialPort -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(DataListener listener) Registers a listener to receive data and error events.booleanEstablishes a connection to the specified port.voidCloses the active connection and releases resources.Retrieves a list of currently available serial ports on the system.private voidnotifyError(String msg) Dispatches an error message to all registered listeners.private voidnotifyListeners(SignalResult result) Dispatches aSignalResultto all registered listeners.private booleanPerforms a handshake with the device to verify protocol compatibility.private voidprocessBuffer(int[] rawData) Converts raw ADC data to voltage, computes the FFT, and notifies all registered listeners with aSignalResult.private voidreadLoop()Main read loop — runs on the reader thread.private voidRecalculatessamplesPerFrameso that UI updates happen at ~30 FPS for the current sample rate.voidsendCommand(DeviceCommand cmd) Sends a command to the connected device.private voidSpawns the daemon reader thread that decodes the serial byte stream.private voidupdateUI()Snapshots the most recent samples from the ring buffer and submits them to the DSP executor for voltage conversion, FFT, and listener dispatch.
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
config
-
serialPort
private com.fazecast.jSerialComm.SerialPort serialPort -
listeners
-
running
-
readerThread
-
currentSampleRate
private double currentSampleRate -
dspService
-
processingExecutor
-
isProcessing
-
ringBuffer
private final int[] ringBuffer -
headIndex
private int headIndex -
samplesPerFrame
private int samplesPerFrame -
samplesSinceLastUpdate
private int samplesSinceLastUpdate
-
-
Constructor Details
-
RealDeviceClient
public RealDeviceClient()
-
-
Method Details
-
recalculateSamplesPerFrame
private void recalculateSamplesPerFrame()RecalculatessamplesPerFrameso that UI updates happen at ~30 FPS for the current sample rate. -
connect
Description copied from interface:DeviceClientEstablishes a connection to the specified port.- Specified by:
connectin interfaceDeviceClient- Parameters:
portName- The system port name (e.g., "COM3", "/dev/ttyACM0").- Returns:
trueif the connection was successfully established,falseotherwise.
-
performHandshake
private boolean performHandshake()Performs a handshake with the device to verify protocol compatibility. Sends '?' and expects "OSC_V1".- Returns:
- true if handshake succeeds.
-
disconnect
public void disconnect()Description copied from interface:DeviceClientCloses the active connection and releases resources.- Specified by:
disconnectin interfaceDeviceClient
-
sendCommand
Description copied from interface:DeviceClientSends a command to the connected device.- Specified by:
sendCommandin interfaceDeviceClient- Parameters:
cmd- TheDeviceCommandto send.
-
addListener
Description copied from interface:DeviceClientRegisters a listener to receive data and error events.- Specified by:
addListenerin interfaceDeviceClient- Parameters:
listener- TheDataListenerto add.
-
getAvailablePorts
Description copied from interface:DeviceClientRetrieves a list of currently available serial ports on the system.- Specified by:
getAvailablePortsin interfaceDeviceClient- Returns:
- A list of port names.
-
startReading
private void startReading()Spawns the daemon reader thread that decodes the serial byte stream. -
readLoop
private void readLoop()Main read loop — runs on the reader thread.Decodes the 2-byte sync protocol (high byte bit 7 = 1, low byte bit 7 = 0) into 10-bit ADC values and writes them into the ring buffer. Every
samplesPerFramesamples, triggers a UI update. -
updateUI
private void updateUI()Snapshots the most recent samples from the ring buffer and submits them to the DSP executor for voltage conversion, FFT, and listener dispatch. Skipped if a previous DSP job is still running. -
processBuffer
private void processBuffer(int[] rawData) Converts raw ADC data to voltage, computes the FFT, and notifies all registered listeners with aSignalResult.- Parameters:
rawData- the raw 10-bit ADC values
-
notifyListeners
Dispatches aSignalResultto all registered listeners.- Parameters:
result- the processed signal result
-
notifyError
Dispatches an error message to all registered listeners.- Parameters:
msg- the error description
-