import React, { useEffect, useRef, useState } from 'react'; import { num } from '../api/useXplane.js'; // G1000 TMR/REF window (PFD). The real unit shows a generic timer plus the // reference V-speeds and barometric minimums. This implements the timer // (count-up or count-down with START/STOP/RESET) and the V-speed / minimums // references with simple on/off bugs. Self-contained — no sim dependency. const VSPEEDS = [ { key: 'vr', label: 'Vr', def: 55 }, { key: 'vx', label: 'Vx', def: 62 }, { key: 'vy', label: 'Vy', def: 74 }, { key: 'vg', label: 'Vg', def: 68 }, // best glide ]; function fmt(sec) { const s = Math.max(0, Math.floor(sec)); const h = Math.floor(s / 3600), m = Math.floor((s % 3600) / 60), ss = s % 60; const pad = (n) => String(n).padStart(2, '0'); return h > 0 ? `${pad(h)}:${pad(m)}:${pad(ss)}` : `${pad(m)}:${pad(ss)}`; } export default function TimerRef({ values, onClose, minimums = { on: false, ft: 500 }, onMinimums }) { const [dir, setDir] = useState('up'); // 'up' | 'dn' const [running, setRunning] = useState(false); const [elapsed, setElapsed] = useState(0); // seconds const [target, setTarget] = useState(300); // count-down start (s) const [vbugs, setVbugs] = useState({}); // key -> bool (shown on tape, future) // Minimums are lifted to App so the PFD altimeter can show the BARO MIN bug. const minsOn = minimums.on, mins = minimums.ft; const setMins = (fn) => onMinimums && onMinimums((m) => ({ ...m, ft: Math.max(0, typeof fn === 'function' ? fn(m.ft) : fn) })); const setMinsOn = (fn) => onMinimums && onMinimums((m) => ({ ...m, on: typeof fn === 'function' ? fn(m.on) : fn })); const tickRef = useRef(null); useEffect(() => { if (!running) return; const t0 = Date.now() - elapsed * 1000; tickRef.current = setInterval(() => setElapsed((Date.now() - t0) / 1000), 250); return () => clearInterval(tickRef.current); }, [running]); // eslint-disable-line const shown = dir === 'dn' ? Math.max(0, target - elapsed) : elapsed; const alt = num(values.altitude); const belowMins = minsOn && alt > 0 && alt <= mins; const reset = () => { setRunning(false); setElapsed(0); }; return (
TIMER / REFERENCES
{fmt(shown)}
{dir === 'dn' && (
{fmt(target)}
)}
REFERENCES — V-SPEEDS
{VSPEEDS.map((v) => ( ))}
{mins} FT
{belowMins &&
MINIMUMS
}
); }