Interface SignalAnalyzerApi

All Known Implementing Classes:
SignalAnalyzerApiImpl

public interface SignalAnalyzerApi
Main API interface for the Signal Analyzer backend.

This interface defines the complete contract between the JavaFX frontend and the backend services. All methods are designed to be called from the UI thread and handle threading internally.

Usage Example:


 SignalAnalyzerApi api = SignalAnalyzerApi.create();
 
 // Subscribe to data updates
 api.setDataCallback(data -> Platform.runLater(() -> updateCharts(data)));
 
 // Connect and start
 api.connect("/dev/ttyACM0");
 api.startAcquisition();
 
 // Cleanup
 api.shutdown();
 

Thread Safety:

  • All public methods are thread-safe
  • Callbacks are invoked on a background thread
  • Use Platform.runLater() for UI updates in callbacks
  • Method Details

    • create

      static SignalAnalyzerApi create()
      Creates a new API instance with real hardware support.
      Returns:
      A new SignalAnalyzerApi instance.
    • createMock

      static SignalAnalyzerApi createMock()
      Creates a new API instance with mock hardware for testing.
      Returns:
      A new SignalAnalyzerApi instance using mock data.
    • getAvailablePorts

      AvailablePorts getAvailablePorts()
      Gets the list of available serial ports.
      Returns:
      AvailablePorts containing port names and metadata.
    • refreshPorts

      AvailablePorts refreshPorts()
      Refreshes the list of available serial ports. Useful after plugging/unplugging devices.
      Returns:
      Updated AvailablePorts.
    • connect

      void connect(String portName)
      Connects to a device on the specified port.

      This method performs handshake validation and will throw if the device does not respond correctly.

      Parameters:
      portName - The system port name (e.g., "COM3", "/dev/ttyACM0").
      Throws:
      ConnectionException - if connection fails.
    • disconnect

      void disconnect()
      Disconnects from the currently connected device. Safe to call even if not connected.
    • getConnectionStatus

      ConnectionStatus getConnectionStatus()
      Gets the current connection status.
      Returns:
      ConnectionStatus with connection details.
    • isConnected

      boolean isConnected()
      Checks if a device is currently connected.
      Returns:
      true if connected, false otherwise.
    • startAcquisition

      void startAcquisition()
      Starts data acquisition. Must be connected first.
      Throws:
      DeviceException - if not connected or command fails.
    • stopAcquisition

      void stopAcquisition()
      Stops data acquisition. Safe to call even if not acquiring.
    • isAcquiring

      boolean isAcquiring()
      Checks if acquisition is currently active.
      Returns:
      true if acquiring data, false otherwise.
    • getAcquisitionConfig

      AcquisitionConfig getAcquisitionConfig()
      Gets the current acquisition configuration.
      Returns:
      AcquisitionConfig with current settings.
    • setSampleRate1kHz

      void setSampleRate1kHz()
      Sets the sample rate to 1 kHz.
    • setSampleRate10kHz

      void setSampleRate10kHz()
      Sets the sample rate to 10 kHz.
    • setSampleRate20kHz

      void setSampleRate20kHz()
      Sets the sample rate to 20 kHz (Turbo Mode).

      Note: Turbo mode may have reduced impedance tolerance.

    • getCurrentSampleRate

      double getCurrentSampleRate()
      Gets the current sample rate.
      Returns:
      Sample rate in Hz.
    • setDataCallback

      void setDataCallback(Consumer<SignalData> callback)
      Sets the callback for receiving signal data updates.

      The callback is invoked on a background thread. Use Platform.runLater() for UI updates.

      Parameters:
      callback - Consumer that receives SignalData updates, or null to clear.
    • setErrorCallback

      void setErrorCallback(Consumer<String> callback)
      Sets the callback for receiving error notifications.

      The callback is invoked on a background thread.

      Parameters:
      callback - Consumer that receives error messages, or null to clear.
    • setConnectionCallback

      void setConnectionCallback(Consumer<ConnectionStatus> callback)
      Sets the callback for connection state changes.
      Parameters:
      callback - Consumer that receives ConnectionStatus updates, or null to clear.
    • getLastSignalData

      SignalData getLastSignalData()
      Gets the last received signal data. Returns null if no data has been received yet.
      Returns:
      The most recent SignalData, or null.
    • computeFFT

      double[] computeFFT(double[] voltageData, double sampleRate)
      Computes FFT for the given voltage data. Useful for processing external data.
      Parameters:
      voltageData - Time-domain voltage samples.
      sampleRate - Sample rate used to capture the data.
      Returns:
      FFT magnitude spectrum.
    • convertToVoltage

      double[] convertToVoltage(int[] rawData)
      Converts raw ADC values to voltage.
      Parameters:
      rawData - Raw 10-bit ADC values (0-1023).
      Returns:
      Voltage values (0.0-5.0V).
    • computeStatistics

      SignalStatistics computeStatistics(double[] voltageData, double[] freqData, double sampleRate)
      Computes signal statistics for the given data.
      Parameters:
      voltageData - Time-domain voltage samples.
      freqData - Frequency-domain magnitude data.
      sampleRate - Sample rate in Hz.
      Returns:
      Computed SignalStatistics.
    • shutdown

      void shutdown()
      Shuts down the API and releases all resources.

      This method should be called when the application is closing. It stops acquisition, disconnects, and shuts down all background threads.

    • isShutdown

      boolean isShutdown()
      Checks if the API has been shut down.
      Returns:
      true if shutdown() has been called.