Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
f4329a85eThis file is pinned to the selected release. Download the original to open it in your design tool.
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->
<!-- Copyright (C) 2025 Jibril Sharafi -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/css/button.css">
<link rel="stylesheet" type="text/css" href="/css/styles.css">
<link rel="stylesheet" type="text/css" href="/css/section.css">
<link rel="stylesheet" type="text/css" href="/css/typography.css">
<link rel="stylesheet" type="text/css" href="/css/forms.css">
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
<script src="/js/api-client.js"></script>
<script src="/js/issues.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0" integrity="sha384-e6nUZLBkQ86NJ6TVVKAeSaK8jWa3NhkYWZFomE39AvDbQWeie9PlQqM3pmYW5d1g" crossorigin="anonymous"></script>
<title>Waveform Capture</title>
<style>
.control-panel {
display: flex;
flex-direction: column;
gap: 15px;
padding: 20px;
background-color: #f8f9fa;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.control-row {
display: flex;
align-items: center;
gap: 15px;
flex-wrap: wrap;
}
.control-label {
font-weight: bold;
min-width: 120px;
}
.channel-select {
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
min-width: 200px;
}
.status-indicator {
display: inline-block;
padding: 6px 12px;
border-radius: 4px;
font-weight: bold;
font-size: 14px;
}
.status-idle {
background-color: #6c757d;
color: white;
}
.status-armed {
background-color: #ffc107;
color: black;
}
.status-capturing {
background-color: #17a2b8;
color: white;
animation: pulse 1s infinite;
}
.status-complete {
background-color: #28a745;
color: white;
}
.status-error {
background-color: #dc3545;
color: white;
}
@keyframes pulse {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.6;
}
}
.stats-panel {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
padding: 20px;
background-color: #f8f9fa;
border-radius: 8px;
}
.stat-item {
display: flex;
flex-direction: column;
gap: 5px;
}
.stat-label {
font-size: 12px;
color: #6c757d;
text-transform: uppercase;
}
.stat-value {
font-size: 20px;
font-weight: bold;
color: #2c3e50;
}
.error-message {
padding: 15px;
background-color: #f8d7da;
border: 1px solid #f5c6cb;
border-radius: 4px;
color: #721c24;
margin-top: 10px;
}
.info-message {
padding: 15px;
background-color: #d1ecf1;
border: 1px solid #bee5eb;
border-radius: 4px;
color: #0c5460;
margin-top: 10px;
}
.button-group {
display: flex;
gap: 10px;
}
.loading {
opacity: 0.6;
pointer-events: none;
}
/* Tab navigation styles */
.tab-container {
display: flex;
gap: 5px;
margin-bottom: 20px;
flex-wrap: wrap;
border-bottom: 2px solid #dee2e6;
}
.tab {
padding: 10px 20px;
background-color: #f8f9fa;
border: 1px solid #dee2e6;
border-bottom: none;
border-radius: 8px 8px 0 0;
cursor: pointer;
transition: all 0.2s;
font-weight: 500;
position: relative;
bottom: -2px;
user-select: none;
}
.tab:hover {
background-color: #e9ecef;
}
.tab.active {
background-color: white;
border-bottom: 2px solid white;
font-weight: bold;
color: #007bff;
}
.tab.has-data {
border-left: 4px solid #28a745;
}
.tab span {
pointer-events: none; /* Make entire tab clickable */
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
.tab-close {
margin-left: 10px;
color: #dc3545;
font-weight: bold;
cursor: pointer;
}
.tab-close:hover {
color: #c82333;
}
/* Sections inside the tab container lay out exactly like direct body sections */
#tabContentContainer {
display: contents;
}
/* Chart wrapper with fixed height to prevent runaway growth */
.chart-wrapper {
width: 100%;
max-width: 100%;
height: 360px; /* pick appropriate px height */
min-height: 240px;
}
/* Make canvas fill the wrapper (important) */
.chart-wrapper canvas {
width: 100% !important;
height: 100% !important;
display: block;
}
</style>
</head>
<body>
<div class="buttonNavigation-container">
<a class="buttonNavigation" type="outer" href="/calibration">⚖️ Calibration</a>
</div>
<!-- Tab Navigation -->
<div class="tab-container" id="tabContainer"></div>
<section class="section-box">
<h2>Capture Control</h2>
<div class="control-panel">
<div class="control-row">
<span class="control-label">Status:</span>
<span id="statusIndicator" class="status-indicator status-idle">IDLE</span>
</div>
<div class="control-row button-group">
<button id="armButton" class="buttonPrimary" onclick="armCapture()">
📡 Capture
</button>
</div>
</div>
</section>
<section class="section-box">
<h2>Grid Frequency</h2>
<div class="control-panel">
<div class="control-row">
<span class="control-label">Live (500 ms):</span>
<button id="gridFreqToggle" class="buttonSecondary" onclick="toggleGridFreqPolling()">⏸ Pause</button>
</div>
</div>
<div style="height: 15px;"></div>
<div class="chart-wrapper">
<canvas id="gridFrequencyChart"></canvas>
</div>
<div style="height: 15px;"></div>
<div class="stats-panel">
<div class="stat-item">
<span class="stat-label">Current</span>
<span id="gridFreqCurrent" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Min (window)</span>
<span id="gridFreqMin" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Max (window)</span>
<span id="gridFreqMax" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Window</span>
<span id="gridFreqWindow" class="stat-value">-</span>
</div>
</div>
</section>
<!-- Dynamic tab content will be inserted here -->
<div id="tabContentContainer"></div>
<script>
const api = new EnergyMeAPI();
let waveformChart = null;
let statusPollInterval = null;
let channelWaveforms = {}; // Store waveform data per channel: {channelIndex: data}
let activeChannelIndex = null;
let availableChannels = []; // List of channel objects with index and label
// Initialize on page load
document.addEventListener('DOMContentLoaded', async () => {
initGridFrequencyChart();
startGridFreqPolling();
await loadChannels();
const status = await refreshStatus();
// If status is complete on page load, auto-fetch the data
if (status === 'complete') {
await fetchWaveformData();
}
});
// === Grid frequency live chart ===
const GRID_FREQ_POLL_MS = 500;
const GRID_FREQ_MAX_POINTS = 240; // 2 minutes at 500 ms
let gridFrequencyChart = null;
let gridFreqTimer = null;
let gridFreqRunning = false;
let gridFreqGeneration = 0; // invalidates stale poll chains across pause/resume
const gridFreqLabels = [];
const gridFreqValues = [];
function initGridFrequencyChart() {
const ctx = document.getElementById('gridFrequencyChart').getContext('2d');
gridFrequencyChart = new Chart(ctx, {
type: 'line',
data: {
labels: gridFreqLabels,
datasets: [{
label: 'Grid Frequency (Hz)',
data: gridFreqValues,
borderColor: 'rgb(54, 162, 235)',
backgroundColor: 'rgba(54, 162, 235, 0.1)',
borderWidth: 2,
pointRadius: 0,
tension: 0.2,
fill: true
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
animation: false,
plugins: {
legend: { display: false },
tooltip: {
mode: 'index',
intersect: false,
callbacks: {
label: (context) => `${context.parsed.y.toFixed(4)} Hz`
}
}
},
scales: {
x: {
title: { display: true, text: 'Time' },
ticks: { maxTicksLimit: 8, maxRotation: 0, autoSkip: true }
},
y: {
title: { display: true, text: 'Frequency (Hz)' },
grace: '25%',
ticks: { callback: (value) => Number(value).toFixed(3) }
}
},
interaction: { mode: 'index', intersect: false }
}
});
}
// Self-rescheduling loop: the next request is only scheduled after the
// previous one completes, so at most one request is in flight.
async function pollGridFrequency() {
const generation = gridFreqGeneration;
const started = Date.now();
try {
const data = await api.get('ade7953/grid-frequency');
if (data && typeof data.gridFrequency === 'number' && data.gridFrequency > 0) {
addGridFrequencyPoint(started, data.gridFrequency);
}
} catch (error) {
console.error('Grid frequency poll failed:', error);
}
if (!gridFreqRunning || generation !== gridFreqGeneration) return;
const elapsed = Date.now() - started;
gridFreqTimer = setTimeout(pollGridFrequency, Math.max(0, GRID_FREQ_POLL_MS - elapsed));
}
function addGridFrequencyPoint(timestampMs, frequency) {
const date = new Date(timestampMs);
const label = date.toLocaleTimeString('en-GB') + '.' + Math.floor(date.getMilliseconds() / 100);
gridFreqLabels.push(label);
gridFreqValues.push(frequency);
while (gridFreqValues.length > GRID_FREQ_MAX_POINTS) {
gridFreqLabels.shift();
gridFreqValues.shift();
}
gridFrequencyChart.update('none');
const windowSeconds = (gridFreqValues.length * GRID_FREQ_POLL_MS) / 1000;
document.getElementById('gridFreqCurrent').textContent = `${frequency.toFixed(4)} Hz`;
document.getElementById('gridFreqMin').textContent = `${Math.min(...gridFreqValues).toFixed(4)} Hz`;
document.getElementById('gridFreqMax').textContent = `${Math.max(...gridFreqValues).toFixed(4)} Hz`;
document.getElementById('gridFreqWindow').textContent = `${windowSeconds.toFixed(0)} s`;
}
function startGridFreqPolling() {
if (gridFreqRunning) return;
gridFreqRunning = true;
gridFreqGeneration++;
document.getElementById('gridFreqToggle').textContent = '⏸ Pause';
pollGridFrequency();
}
function stopGridFreqPolling() {
gridFreqRunning = false;
if (gridFreqTimer) {
clearTimeout(gridFreqTimer);
gridFreqTimer = null;
}
document.getElementById('gridFreqToggle').textContent = '▶ Resume';
}
function toggleGridFreqPolling() {
if (gridFreqRunning) {
stopGridFreqPolling();
} else {
startGridFreqPolling();
}
}
/**
* Load available channels from API and create tabs
*/
async function loadChannels() {
try {
const channels = await api.getChannelConfig();
// API returns an array of channel objects
if (Array.isArray(channels)) {
availableChannels = channels
.filter(channel => channel && channel.active)
.map(channel => ({
index: channel.index,
label: channel.label || `Channel ${channel.index}`
}))
.sort((a, b) => a.index - b.index);
// Create tabs for all active channels
updateAvailableChannelTabs();
// Select channel 0 by default if available
const channel0 = availableChannels.find(ch => ch.index === 0);
if (channel0) {
activeChannelIndex = 0;
updateAvailableChannelTabs();
} else if (availableChannels.length > 0) {
activeChannelIndex = availableChannels[0].index;
updateAvailableChannelTabs();
}
}
} catch (error) {
showStatus('Failed to load channels: ' + error.message, 'error');
}
}
/**
* Update tabs to show all available channels
*/
function updateAvailableChannelTabs() {
const tabContainer = document.getElementById('tabContainer');
tabContainer.innerHTML = '';
availableChannels.forEach(channel => {
const tab = document.createElement('div');
tab.className = 'tab';
// Mark as active if this is the selected channel
if (channel.index === activeChannelIndex) {
tab.className += ' active';
}
// Mark as has-data if we've captured this channel
if (channelWaveforms[channel.index]) {
tab.className += ' has-data';
}
tab.innerHTML = `<span>${channel.label}</span>`;
tab.onclick = () => selectChannelForCapture(channel.index);
tabContainer.appendChild(tab);
});
}
/**
* Select a channel for capture (clicking on tab)
*/
function selectChannelForCapture(channelIndex) {
activeChannelIndex = parseInt(channelIndex);
// Refresh status when switching tabs
refreshStatus();
// If we have captured data for this channel, display it
if (channelWaveforms[channelIndex]) {
displayWaveform(channelWaveforms[channelIndex]);
calculateStats(channelWaveforms[channelIndex]);
} else {
// Otherwise just update the active tab and hide content
hideContent();
}
updateAvailableChannelTabs();
}
/**
* Arm waveform capture for active channel
*/
async function armCapture() {
if (activeChannelIndex === null) {
showStatus('Please select a channel first', 'error');
return;
}
setLoading(true);
try {
const response = await api.post('ade7953/waveform/arm', {
channelIndex: activeChannelIndex
});
if (response.success === true) {
showStatus(`Capture armed for channel ${activeChannelIndex}. Waiting for next line cycle...`, 'info');
startStatusPolling();
} else {
showStatus(response.message || 'Unexpected response from server', 'error');
}
} catch (error) {
if (error.message.includes('409')) {
showStatus('Capture already in progress. Wait for it to complete or refresh status.', 'error');
} else {
showStatus('Failed to arm capture: ' + error.message, 'error');
}
} finally {
setLoading(false);
}
}
/**
* Poll capture status and auto-fetch data when complete
*/
function startStatusPolling() {
stopStatusPolling();
statusPollInterval = setInterval(async () => {
const status = await refreshStatus();
if (status === 'complete') {
stopStatusPolling();
await fetchWaveformData();
} else if (status === 'error' || status === 'idle') {
stopStatusPolling();
}
}, 500);
}
function stopStatusPolling() {
if (statusPollInterval) {
clearInterval(statusPollInterval);
statusPollInterval = null;
}
}
/**
* Refresh capture status
*/
async function refreshStatus() {
try {
const response = await api.get('ade7953/waveform/status');
const status = response.state || 'unknown';
updateStatusIndicator(status);
return status;
} catch (error) {
console.error('Failed to get status:', error);
updateStatusIndicator('error');
return 'error';
}
}
/**
* Update status indicator UI
*/
function updateStatusIndicator(status) {
const indicator = document.getElementById('statusIndicator');
indicator.className = 'status-indicator status-' + status;
indicator.textContent = status.toUpperCase();
}
/**
* Fetch and display waveform data
*/
async function fetchWaveformData() {
setLoading(true);
try {
const data = await api.get('ade7953/waveform/data');
const channelIndex = data.channelIndex;
// Store data for this channel
channelWaveforms[channelIndex] = data;
// Update tabs to show green indicator
updateAvailableChannelTabs();
// Display the captured data
displayWaveform(data);
calculateStats(data);
showStatus(`Waveform captured successfully for channel ${channelIndex}!`, 'success');
} catch (error) {
if (error.message.includes('404')) {
showStatus('No waveform data available. Please arm capture first.', 'error');
} else {
showStatus('Failed to fetch waveform data: ' + error.message, 'error');
}
} finally {
setLoading(false);
}
}
/**
* Hide chart and stats content when no data
*/
function hideContent() {
const contentContainer = document.getElementById('tabContentContainer');
contentContainer.innerHTML = '';
if (waveformChart) {
waveformChart.destroy();
waveformChart = null;
}
}
/**
* Display waveform chart with voltage and current
*/
function displayWaveform(data) {
// Data already contains scaled values in V and A
const voltageValues = data.voltage;
const currentValues = data.current;
// microsDelta already contains cumulative microseconds from backend
// Convert to milliseconds
const timeMs = data.microsDelta.map(micros => micros / 1000.0);
// Format time labels to 1 decimal place
const timeLabels = timeMs.map(t => t.toFixed(1));
// Apply phase shift to voltage if needed (device provides the shift angle)
let shiftedVoltageValues = voltageValues;
const phaseShiftDegrees = data.phaseShiftDegrees || 0;
if (phaseShiftDegrees !== 0) {
// Calculate total duration and frequency
const totalDurationUs = data.microsDelta[data.microsDelta.length - 1];
const durationSeconds = totalDurationUs / 1000000.0;
const numSamples = voltageValues.length;
const samplingFreq = (numSamples - 1) / durationSeconds;
// Estimate grid frequency (50 Hz or 60 Hz)
const estimatedCycles = durationSeconds * 50; // Assume 50 Hz initially
const gridFreq = Math.abs(estimatedCycles - Math.round(estimatedCycles)) < 0.2 ? 50 : 60;
// Calculate phase shift in samples
const degreesPerSample = 360 * gridFreq / samplingFreq;
const shiftSamples = Math.round(phaseShiftDegrees / degreesPerSample);
// Shift voltage array
shiftedVoltageValues = new Array(voltageValues.length);
for (let i = 0; i < voltageValues.length; i++) {
const shiftedIndex = i + shiftSamples;
if (shiftedIndex >= 0 && shiftedIndex < voltageValues.length) {
shiftedVoltageValues[i] = voltageValues[shiftedIndex];
} else {
shiftedVoltageValues[i] = null; // Outside valid range
}
}
}
// Create content container if it doesn't exist
const contentContainer = document.getElementById('tabContentContainer');
// Add phase information to title if shift is applied
const phaseInfo = phaseShiftDegrees !== 0
? ` (Voltage Phase ${data.voltagePhase} → Current Phase ${data.currentPhase}, shifted ${phaseShiftDegrees.toFixed(1)}°)`
: '';
contentContainer.innerHTML = `
<section class="section-box">
<h2>Voltage & Current Waveform${phaseInfo}</h2>
<div class="chart-wrapper">
<canvas id="waveformChart"></canvas>
</div>
</section>
<section class="section-box">
<h2>Waveform Statistics</h2>
<div class="stats-panel">
<div class="stat-item">
<span class="stat-label">Capture Time</span>
<span id="statCaptureTime" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Samples</span>
<span id="statSamples" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Duration</span>
<span id="statDuration" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Sampling Freq</span>
<span id="statSamplingFreq" class="stat-value">-</span>
</div>
<!-- Force Voltage stats to a new full-width row -->
<div style="grid-column: 1 / -1;">
<div style="display:flex; gap:15px; flex-wrap:wrap; align-items:flex-start;">
<div class="stat-item">
<span class="stat-label">RMS Voltage (approx.)</span>
<span id="statRmsVoltage" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Min Voltage</span>
<span id="statMinVoltage" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Max Voltage</span>
<span id="statMaxVoltage" class="stat-value">-</span>
</div>
</div>
</div>
<!-- Force Current stats to a new full-width row -->
<div style="grid-column: 1 / -1;">
<div style="display:flex; gap:15px; flex-wrap:wrap; align-items:flex-start;">
<div class="stat-item">
<span class="stat-label">RMS Current (approx.)</span>
<span id="statRmsCurrent" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Min Current</span>
<span id="statMinCurrent" class="stat-value">-</span>
</div>
<div class="stat-item">
<span class="stat-label">Max Current</span>
<span id="statMaxCurrent" class="stat-value">-</span>
</div>
</div>
</div>
</div>
<div style="margin-top: 20px;">
<button class="buttonSecondary" onclick="downloadData(${data.channelIndex})">
💾 Download Data
</button>
</div>
</section>
`;
// Destroy existing chart if any
if (waveformChart) {
waveformChart.destroy();
}
// Create combined waveform chart with dual Y-axes
const ctx = document.getElementById('waveformChart').getContext('2d');
// Build datasets array - always show original voltage, add shifted if different
const datasets = [];
// Original voltage (lighter if shifted version exists)
if (phaseShiftDegrees !== 0) {
datasets.push({
label: `Voltage Original (Phase ${data.voltagePhase})`,
data: voltageValues,
borderColor: 'rgba(75, 192, 192, 0.3)',
backgroundColor: 'rgba(75, 192, 192, 0.05)',
borderWidth: 1,
borderDash: [5, 5],
pointRadius: 0,
yAxisID: 'y-voltage'
});
// Shifted voltage (main display)
datasets.push({
label: `Voltage Shifted to Phase ${data.currentPhase} (${phaseShiftDegrees.toFixed(1)}°)`,
data: shiftedVoltageValues,
borderColor: 'rgb(75, 192, 192)',
backgroundColor: 'rgba(75, 192, 192, 0.1)',
borderWidth: 2,
pointRadius: 0,
yAxisID: 'y-voltage',
spanGaps: false
});
} else {
// No shift - just show voltage normally
datasets.push({
label: 'Voltage (V)',
data: voltageValues,
borderColor: 'rgb(75, 192, 192)',
backgroundColor: 'rgba(75, 192, 192, 0.1)',
borderWidth: 2,
pointRadius: 0,
yAxisID: 'y-voltage'
});
}
// Current (always shown)
datasets.push({
label: 'Current (A)',
data: currentValues,
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132, 0.1)',
borderWidth: 2,
pointRadius: 0,
yAxisID: 'y-current'
});
waveformChart = new Chart(ctx, {
type: 'line',
data: {
labels: timeLabels,
datasets: datasets
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: true,
position: 'top'
},
tooltip: {
mode: 'index',
intersect: false
}
},
scales: {
x: {
title: {
display: true,
text: 'Time (ms)'
},
ticks: {
callback: function(value, index) {
// Show every Nth label to avoid crowding
return index % Math.ceil(timeLabels.length / 20) === 0 ? this.getLabelForValue(value) : '';
}
}
},
'y-voltage': {
type: 'linear',
position: 'left',
title: {
display: true,
text: 'Voltage (V)'
},
grid: {
color: 'rgba(75, 192, 192, 0.1)'
}
},
'y-current': {
type: 'linear',
position: 'right',
title: {
display: true,
text: 'Current (A)'
},
grid: {
drawOnChartArea: false
}
}
},
interaction: {
mode: 'index',
intersect: false
}
}
});
}
/**
* Calculate and display statistics
*/
function calculateStats(data) {
// Data already contains scaled values in V and A
const voltageValues = data.voltage;
const currentValues = data.current;
// microsDelta contains cumulative microseconds, last value is total duration
const totalDurationUs = data.microsDelta[data.microsDelta.length - 1];
const durationMs = (totalDurationUs / 1000.0).toFixed(1);
// Calculate sampling frequency
const samplingFreqHz = data.sampleCount > 1 ? ((data.sampleCount - 1) / (totalDurationUs / 1000000.0)).toFixed(0) : 0;
// Calculate RMS (Root Mean Square) values
const voltageRms = Math.sqrt(voltageValues.reduce((sum, v) => sum + v * v, 0) / voltageValues.length).toFixed(1);
const minVoltage = Math.min(...voltageValues).toFixed(1);
const maxVoltage = Math.max(...voltageValues).toFixed(1);
const currentRms = Math.sqrt(currentValues.reduce((sum, i) => sum + i * i, 0) / currentValues.length).toFixed(3);
const minCurrent = Math.min(...currentValues).toFixed(3);
const maxCurrent = Math.max(...currentValues).toFixed(3);
// Format capture timestamp (convert unix millis to local ISO time with milliseconds)
let captureTimeStr = '-';
if (data.captureStartUnixMillis) {
const captureDate = new Date(data.captureStartUnixMillis);
captureTimeStr = captureDate.toLocaleString('en-GB', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 3
}).replace(',', '');
}
// Update UI
document.getElementById('statCaptureTime').textContent = captureTimeStr;
document.getElementById('statSamples').textContent = data.sampleCount;
document.getElementById('statDuration').textContent = `${durationMs} ms`;
document.getElementById('statSamplingFreq').textContent = `${samplingFreqHz} Hz`;
document.getElementById('statRmsVoltage').textContent = `${voltageRms} V`;
document.getElementById('statMinVoltage').textContent = `${minVoltage} V`;
document.getElementById('statMaxVoltage').textContent = `${maxVoltage} V`;
document.getElementById('statRmsCurrent').textContent = `${currentRms} A`;
document.getElementById('statMinCurrent').textContent = `${minCurrent} A`;
document.getElementById('statMaxCurrent').textContent = `${maxCurrent} A`;
}
/**
* Download waveform data as JSON
*/
function downloadData(channelIndex) {
const data = channelWaveforms[channelIndex];
if (!data) {
showStatus('No waveform data to download', 'error');
return;
}
const dataStr = JSON.stringify(data, null, 2);
const dataBlob = new Blob([dataStr], { type: 'application/json' });
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = `waveform_ch${channelIndex}_${new Date().getTime()}.json`;
link.click();
URL.revokeObjectURL(url);
}
/**
* UI Helper Functions
*/
function setLoading(loading) {
const panel = document.querySelector('.control-panel');
if (panel) {
if (loading) {
panel.classList.add('loading');
} else {
panel.classList.remove('loading');
}
}
}
// Cleanup on page unload
window.addEventListener('beforeunload', () => {
stopStatusPolling();
stopGridFreqPolling();
});
</script>
</body>
</html>