All files / client/src/hooks use-serial-io.ts

72.91% Statements 35/48
50% Branches 5/10
47.05% Functions 8/17
74.41% Lines 32/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124          33x 33x 33x 33x     33x 33x     33x 12x                   33x 12x     12x   12x 12x       33x 33x   33x 3x 3x 2x 1x       33x 1x 1x 1x       33x       33x       33x       33x 1x               33x                 33x                     33x                                                      
import { useState, useCallback, useRef, useEffect, useMemo } from "react";
import type { OutputLine } from "@shared/schema";
import { SerialCharacterRenderer } from "@/utils/serial-character-renderer";
 
export function useSerialIO() {
  const [serialOutput, setSerialOutput] = useState<OutputLine[]>([]);
  const [serialViewMode, setSerialViewMode] = useState<"monitor" | "plotter" | "both">("monitor");
  const [autoScrollEnabled, setAutoScrollEnabled] = useState<boolean>(true);
  const [serialInputValue, setSerialInputValue] = useState("");
  
  // Baudrate-simulated rendering
  const [renderedSerialText, setRenderedSerialText] = useState<string>("");
  const rendererRef = useRef<SerialCharacterRenderer | null>(null);
 
  // Convert renderedSerialText to OutputLine[] format for SerialMonitor
  const renderedSerialOutput = useMemo<OutputLine[]>(() => {
    Eif (!renderedSerialText) return [];
    
    const lines = renderedSerialText.split('\n');
    return lines.map((line, index) => ({
      text: line,
      complete: index < lines.length - 1, // All lines except last are complete
    }));
  }, [renderedSerialText]);
 
  // Initialize renderer once
  useEffect(() => {
    const renderer = new SerialCharacterRenderer((char: string) => {
      setRenderedSerialText((prev) => prev + char);
    });
    rendererRef.current = renderer;
 
    return () => {
      renderer.clear();
    };
  }, []);
 
  const showSerialMonitor = serialViewMode !== "plotter";
  const showSerialPlotter = serialViewMode !== "monitor";
 
  const cycleSerialViewMode = useCallback(() => {
    setSerialViewMode((prev) => {
      if (prev === "monitor") return "both";
      if (prev === "both") return "plotter";
      return "monitor";
    });
  }, []);
 
  const clearSerialOutput = useCallback(() => {
    setSerialOutput([]);
    rendererRef.current?.clear();
    setRenderedSerialText("");
  }, []);
 
  // Baudrate rendering methods
  const appendSerialOutput = useCallback((text: string) => {
    rendererRef.current?.enqueue(text);
  }, []);
 
  const setBaudrate = useCallback((baud: number | undefined) => {
    rendererRef.current?.setBaudrate(baud);
  }, []);
 
  const pauseRendering = useCallback(() => {
    rendererRef.current?.pause();
  }, []);
 
  const resumeRendering = useCallback(() => {
    rendererRef.current?.resume();
  }, []);
 
  /**
   * Stop rendering and clear the renderer queue.
   * Used on STOP to prevent old data from leaking into the next simulation.
   * Unlike pauseRendering(), this discards all pending characters.
   */
  const stopRendering = useCallback(() => {
    rendererRef.current?.clear();
  }, []);
 
  /**
   * Inject text directly into rendered output, bypassing the baudrate renderer.
   * Used for system messages ("--- Simulation paused ---" etc.) that must
   * appear instantly regardless of baudrate.
   */
  const appendRenderedText = useCallback((text: string) => {
    setRenderedSerialText((prev) => {
      // Ensure injected text starts on a new line if current output
      // doesn't end with one (e.g., partial serial line was rendering)
      if (prev.length > 0 && !prev.endsWith('\n')) {
        return prev + '\n' + text;
      }
      return prev + text;
    });
  }, []);
 
  return {
    // Existing API (unchanged - for backward compatibility and Plotter)
    serialOutput,
    setSerialOutput,
    serialViewMode,
    setSerialViewMode,
    autoScrollEnabled,
    setAutoScrollEnabled,
    serialInputValue,
    setSerialInputValue,
    showSerialMonitor,
    showSerialPlotter,
    cycleSerialViewMode,
    clearSerialOutput,
    
    // New baudrate rendering API
    renderedSerialText,
    renderedSerialOutput, // OutputLine[] format for SerialMonitor
    appendSerialOutput,
    setBaudrate,
    pauseRendering,
    resumeRendering,
    stopRendering,
    appendRenderedText,
  };
}