From 8b51ff19a845c6378ced64ff4a915d5d886eeba0 Mon Sep 17 00:00:00 2001 From: Mute Date: Sat, 8 Aug 2026 11:38:05 +0000 Subject: [PATCH] Make UI event-driven, heat maps at 10Hz from tick loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Heat maps fed from tick loop via BeginInvoke (10Hz, one column per tick) - BLE label updates on ConnectionChanged event (was 4Hz poll) - Recv gauges update on StrengthChanged event (B1 notifications) - Button states refreshed via RefreshButtonStates on all state changes - Status timer reduced from 250ms to 1s — only music label + tray icon - Eliminates 4Hz choppy polling --- MainForm.cs | 68 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/MainForm.cs b/MainForm.cs index 4ac6575..026902a 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -260,7 +260,7 @@ public class MainForm : Form _tray.ContextMenuStrip.Items.Add("Exit", null, (_, _) => ExitFromTray()); _tray.DoubleClick += (_, _) => ShowFromTray(); - _statusTimer = new System.Windows.Forms.Timer { Interval = 250 }; + _statusTimer = new System.Windows.Forms.Timer { Interval = 1000 }; _statusTimer.Tick += OnStatusTick; _statusTimer.Start(); @@ -269,6 +269,8 @@ public class MainForm : Form Resize += OnResize; _server.ClientChanged += OnWsClientChanged; + _device.ConnectionChanged += OnDeviceConnectionChanged; + _device.StrengthChanged += OnDeviceStrengthChanged; } async void OnLoad(object? sender, EventArgs e) @@ -297,6 +299,14 @@ public class MainForm : Form var frame = B0.Build(0, modeA, modeB, valA, valB, freqA, intA, freqB, intB); await _device.SendB0(frame); } + if (!IsDisposed) + { + BeginInvoke(() => + { + _heatA.AddTick(freqA, intA, _state.LastActiveA); + _heatB.AddTick(freqB, intB, _state.LastActiveB); + }); + } } catch (Exception ex) { @@ -368,31 +378,44 @@ public class MainForm : Form BeginInvoke(() => { _lblWs.Text = connected ? "WS: client connected" : "WS: idle"; - _btnTest.Enabled = !connected && !IsTestRunning; - _btnMusic.Enabled = !connected && !_isMusicRunning; + RefreshButtonStates(); UpdateTrayIcon(); }); } + void OnDeviceConnectionChanged(bool connected) + { + if (IsDisposed) return; + BeginInvoke(() => + { + _lblBle.Text = connected + ? $"BLE: connected ({_device.DeviceName})" + : "BLE: disconnected"; + _btnConnect.Enabled = !connected; + RefreshButtonStates(); + }); + } + + void OnDeviceStrengthChanged(int a, int b) + { + if (IsDisposed) return; + BeginInvoke(() => + { + _gaugeRecvA.Value = Math.Clamp(a, 0, 200); + _gaugeRecvB.Value = Math.Clamp(b, 0, 200); + }); + } + + void RefreshButtonStates() + { + _btnTest.Enabled = !_server.HasClient && !IsTestRunning; + _btnMusic.Enabled = !_server.HasClient && !_isMusicRunning; + _btnStop.Enabled = true; + } + void OnStatusTick(object? sender, EventArgs e) { if (IsDisposed) return; - _lblBle.Text = _device.IsConnected - ? $"BLE: connected ({_device.DeviceName})" - : "BLE: disconnected"; - _heatA.AddTick( - _state.LastFreqA ?? Array.Empty(), - _state.LastIntA ?? Array.Empty(), - _state.LastActiveA); - _heatB.AddTick( - _state.LastFreqB ?? Array.Empty(), - _state.LastIntB ?? Array.Empty(), - _state.LastActiveB); - _gaugeRecvA.Value = Math.Clamp(_device.StrengthA, 0, 200); - _gaugeRecvB.Value = Math.Clamp(_device.StrengthB, 0, 200); - _btnTest.Enabled = !_server.HasClient && !IsTestRunning; - _btnMusic.Enabled = !_server.HasClient && !_isMusicRunning; - _btnStop.Enabled = true; if (_isMusicRunning && _musicStopwatch != null) { @@ -457,7 +480,7 @@ public class MainForm : Form BeginInvoke(() => { IsTestRunning = false; - _btnTest.Enabled = !_server.HasClient; + RefreshButtonStates(); }); } @@ -525,8 +548,7 @@ public class MainForm : Form { _lblTrack.Text = $"Analysis failed: {ex.Message}"; _isMusicRunning = false; - _btnMusic.Enabled = !_server.HasClient; - _btnTest.Enabled = !_server.HasClient && !IsTestRunning; + RefreshButtonStates(); }); return; } @@ -578,7 +600,7 @@ public class MainForm : Form if (!IsDisposed) { _lblTrack.Text = ""; - _btnMusic.Enabled = !_server.HasClient; + RefreshButtonStates(); } } }