Class RealDeviceClient

java.lang.Object
pl.polsl.rtsa.hardware.RealDeviceClient
All Implemented Interfaces:
DeviceClient

public class RealDeviceClient extends Object implements DeviceClient
Implementation of DeviceClient for the real hardware device via UART.

Handles serial communication, data buffering, and FFT processing.

  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • config

      private final AppConfig config
    • serialPort

      private com.fazecast.jSerialComm.SerialPort serialPort
    • listeners

      private final List<DataListener> listeners
    • running

      private final AtomicBoolean running
    • readerThread

      private Thread readerThread
    • currentSampleRate

      private double currentSampleRate
    • dspService

      private final SignalProcessingService dspService
    • processingExecutor

      private final ExecutorService processingExecutor
    • isProcessing

      private final AtomicBoolean 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()
      Recalculates samplesPerFrame so that UI updates happen at ~30 FPS for the current sample rate.
    • connect

      public boolean connect(String portName)
      Description copied from interface: DeviceClient
      Establishes a connection to the specified port.
      Specified by:
      connect in interface DeviceClient
      Parameters:
      portName - The system port name (e.g., "COM3", "/dev/ttyACM0").
      Returns:
      true if the connection was successfully established, false otherwise.
    • 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: DeviceClient
      Closes the active connection and releases resources.
      Specified by:
      disconnect in interface DeviceClient
    • sendCommand

      public void sendCommand(DeviceCommand cmd)
      Description copied from interface: DeviceClient
      Sends a command to the connected device.
      Specified by:
      sendCommand in interface DeviceClient
      Parameters:
      cmd - The DeviceCommand to send.
    • addListener

      public void addListener(DataListener listener)
      Description copied from interface: DeviceClient
      Registers a listener to receive data and error events.
      Specified by:
      addListener in interface DeviceClient
      Parameters:
      listener - The DataListener to add.
    • getAvailablePorts

      public List<String> getAvailablePorts()
      Description copied from interface: DeviceClient
      Retrieves a list of currently available serial ports on the system.
      Specified by:
      getAvailablePorts in interface DeviceClient
      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 samplesPerFrame samples, 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 a SignalResult.
      Parameters:
      rawData - the raw 10-bit ADC values
    • notifyListeners

      private void notifyListeners(SignalResult result)
      Dispatches a SignalResult to all registered listeners.
      Parameters:
      result - the processed signal result
    • notifyError

      private void notifyError(String msg)
      Dispatches an error message to all registered listeners.
      Parameters:
      msg - the error description