Class MainController

java.lang.Object
pl.polsl.rtsa.controller.MainController

public class MainController extends Object
Main JavaFX controller for the JSignalAnalysis UI.

Manages all user interactions (connect, start/stop, zoom, trigger, freeze), drives the 30 FPS render loop via AnimationTimer, and bridges the SignalAnalyzerApi backend with the canvas-based oscilloscope and FFT views.

Render Pipeline:

  1. Backend delivers SignalData via callback (background thread)
  2. Data is posted to the FX thread with Platform.runLater()
  3. AnimationTimer picks it up at ~30 FPS and redraws both canvases
  • Field Details

    • api

      private final SignalAnalyzerApi api
    • timeRenderer

      private final TimeDomainRen timeRenderer
    • fftRenderer

      private final FFTDomainRen fftRenderer
    • autoscaler

      private final Autoscaler autoscaler
    • voltageMin

      private double voltageMin
    • voltageMax

      private double voltageMax
    • magnitudeMax

      private double magnitudeMax
    • lastSignalData

      private volatile SignalData lastSignalData
    • newDataAvailable

      private volatile boolean newDataAvailable
    • isRunning

      private boolean isRunning
    • needsRedraw

      private boolean needsRedraw
    • cursorX

      private double cursorX
    • cursorY

      private double cursorY
    • cursorActive

      private boolean cursorActive
    • animationTimer

      private javafx.animation.AnimationTimer animationTimer
    • lastFrameTime

      private long lastFrameTime
    • FRAME_INTERVAL

      private static final long FRAME_INTERVAL
      See Also:
    • oscilloscopeCanvas

      private javafx.scene.canvas.Canvas oscilloscopeCanvas
    • fftCanvas

      private javafx.scene.canvas.Canvas fftCanvas
    • timeContainer

      private javafx.scene.layout.Pane timeContainer
    • fftContainer

      private javafx.scene.layout.Pane fftContainer
    • timeSection

      private javafx.scene.layout.VBox timeSection
    • fftSection

      private javafx.scene.layout.VBox fftSection
    • plotArea

      private javafx.scene.layout.VBox plotArea
    • timeZoomX

      private javafx.scene.control.Slider timeZoomX
    • timeZoomY

      private javafx.scene.control.Slider timeZoomY
    • fftZoomX

      private javafx.scene.control.Slider fftZoomX
    • fftZoomY

      private javafx.scene.control.Slider fftZoomY
    • timeZoomXLabel

      private javafx.scene.control.Label timeZoomXLabel
    • timeZoomYLabel

      private javafx.scene.control.Label timeZoomYLabel
    • fftZoomXLabel

      private javafx.scene.control.Label fftZoomXLabel
    • fftZoomYLabel

      private javafx.scene.control.Label fftZoomYLabel
    • portComboBox

      private javafx.scene.control.ComboBox<String> portComboBox
    • samplingFreq

      private javafx.scene.control.ComboBox<String> samplingFreq
    • connectButton

      private javafx.scene.control.Button connectButton
    • connectionStatus

      private javafx.scene.control.Label connectionStatus
    • toggleButtonStart

      private javafx.scene.control.ToggleButton toggleButtonStart
    • Vrms

      private javafx.scene.control.Label Vrms
    • Vmax

      private javafx.scene.control.Label Vmax
    • Vmin

      private javafx.scene.control.Label Vmin
    • dominantFreqLabel

      private javafx.scene.control.Label dominantFreqLabel
    • FFTCheck

      private javafx.scene.control.CheckBox FFTCheck
    • FreezeCheck

      private javafx.scene.control.CheckBox FreezeCheck
    • autoCheck

      private javafx.scene.control.CheckBox autoCheck
    • gridCheck

      private javafx.scene.control.CheckBox gridCheck
    • triggerCheck

      private javafx.scene.control.CheckBox triggerCheck
    • triggerLevel

      private javafx.scene.control.Slider triggerLevel
    • triggerLevelLabel

      private javafx.scene.control.Label triggerLevelLabel
    • triggerStatusLabel

      private javafx.scene.control.Label triggerStatusLabel
    • triggerRangeInitialized

      private boolean triggerRangeInitialized
      Whether the trigger slider range has been initialised from autoscale data.
  • Constructor Details

    • MainController

      public MainController()
  • Method Details

    • initialize

      public void initialize()
      FXML initialisation hook — called automatically after all @FXML fields have been injected.

      Wires canvas resize listeners, zoom/trigger slider listeners, populates the port and sample-rate combo boxes, registers API callbacks, and starts the render loop.

    • hookCanvasToPane

      private void hookCanvasToPane(javafx.scene.canvas.Canvas canvas, javafx.scene.layout.Pane container)
      Binds a Canvas size to its parent Pane so the canvas resizes automatically when the window layout changes.
      Parameters:
      canvas - the canvas to resize
      container - the parent pane whose dimensions drive the canvas size
    • handleConnect

      void handleConnect(javafx.event.ActionEvent event)
      Handles the “Connect” button press — attempts to connect to the serial port selected in portComboBox.
      Parameters:
      event - the originating action event
    • handleStart

      void handleStart(javafx.event.ActionEvent event)
      Handles the Start/Stop toggle button — starts or stops data acquisition and updates the button label accordingly.
      Parameters:
      event - the originating action event
    • changeSamplingFreq

      void changeSamplingFreq(javafx.event.ActionEvent event)
      Handles sample-rate combo box changes — sends the appropriate rate command to the hardware.
      Parameters:
      event - the originating action event
    • FFTVisualization

      void FFTVisualization(javafx.event.ActionEvent event)
      Toggles the FFT spectrum panel visibility based on the FFT checkbox state.
      Parameters:
      event - the originating action event
    • autoscaling

      void autoscaling()
      Recomputes voltage and magnitude ranges from the current signal data when the autoscale checkbox is selected.
    • onSignalData

      private void onSignalData(SignalData data)
      API data callback — stores the latest signal data for the next render frame unless the display is frozen.
      Parameters:
      data - the new signal data received from the backend
    • onError

      private void onError(String message)
      API error callback — displays the error message in the status label.
      Parameters:
      message - the error description
    • onConnectionChange

      private void onConnectionChange(ConnectionStatus status)
      API connection-state callback — updates the status label colour and text.
      Parameters:
      status - the new connection status
    • startRenderLoop

      private void startRenderLoop()
      Creates and starts the AnimationTimer that drives the render loop at approximately 30 FPS.
    • renderFrame

      private void renderFrame()
      Renders a single frame — invoked from the AnimationTimer.

      Handles empty-canvas drawing, frozen/stopped states, autoscaling, label updates, and delegates to drawAll(SignalData).

    • drawAll

      private void drawAll(SignalData data)
      Draws the time-domain and (optionally) FFT canvases for the given data.

      Applies zoom, trigger detection, window slicing, and delegates to TimeDomainRen and FFTDomainRen.

      Parameters:
      data - the signal data to render
    • updateTriggerSliderRange

      private void updateTriggerSliderRange(double vMin, double vMax)
      Updates the trigger-level slider's min/max to match the current autoscaled voltage range.
      Parameters:
      vMin - the current minimum voltage
      vMax - the current maximum voltage
    • updateLabels

      private void updateLabels(SignalData data)
      Refreshes the on-screen statistics labels (Vmax, Vmin, RMS, f0) from the given signal data.
      Parameters:
      data - the signal data whose statistics to display
    • shutdown

      public void shutdown()
      Shuts down the render loop and the backend API. Called from App.stop() during application exit.