Skip to content

Commit

Permalink
Improve performance of sending button inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Leapward-Koex committed Apr 16, 2024
1 parent fabead4 commit 3835123
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
60 changes: 43 additions & 17 deletions MaiTouchComConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ internal class MaiTouchComConnector(MaiTouchSensorButtonStateManager buttonState
private static SerialPort? serialPort;
private bool isActiveMode;
private bool _connected;
private CancellationTokenSource? _tokenSource;
private Thread? _pollThread;
private bool _shouldReconnect = true;
private readonly MaiTouchSensorButtonStateManager _buttonState = buttonState;

Expand Down Expand Up @@ -50,18 +52,10 @@ public async Task StartTouchSensorPolling()
OnConnectStatusChange?.Invoke("Connected to port");
_connected = true;

while (true)
{
if (isActiveMode)
{
SendTouchscreenState();
await Task.Delay(1);
}
else
{
await Task.Delay(100);
}
}
_tokenSource = new CancellationTokenSource(); // Create a token source.
_pollThread = new Thread(() => PollingThread(_tokenSource.Token)); // Pass the token to the thread you want to stop.
_pollThread.Priority = ThreadPriority.Highest;
_pollThread.Start();

}
catch (TimeoutException) { }
Expand All @@ -73,9 +67,6 @@ public async Task StartTouchSensorPolling()
MessageBox.Show(ex.Message, "Error connecting to COM port", MessageBoxButton.OK, MessageBoxImage.Error);
});

}
finally
{
Logger.Info("Disconnecting from COM port");
_connected = false;
OnConnectStatusChange?.Invoke("Not Connected");
Expand All @@ -85,6 +76,23 @@ public async Task StartTouchSensorPolling()
serialPort.DiscardOutBuffer();
serialPort.Close();
}

}
}
}

private void PollingThread(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
if (isActiveMode)
{
SendTouchscreenState();
Thread.Sleep(1);
}
else
{
Thread.Sleep(100);
}
}
}
Expand All @@ -96,13 +104,22 @@ public async Task Disconnect()
_connected = false;
try
{
if (_tokenSource != null && !_tokenSource.IsCancellationRequested)
{
_tokenSource.Cancel();
_pollThread?.Join();
_tokenSource.Dispose();
_tokenSource = null;
}


if (serialPort != null)
{
serialPort.DtrEnable = false;
serialPort.RtsEnable = false;
serialPort.DataReceived -= SerialPort_DataReceived;
await Task.Delay(200);
if (serialPort.IsOpen == true)
if (serialPort.IsOpen)
{
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
Expand Down Expand Up @@ -165,7 +182,16 @@ void SendTouchscreenState()
if (_connected)
{
var currentState = _buttonState.GetCurrentState();
serialPort?.Write(currentState, 0, currentState.Length);
try
{
serialPort?.Write(currentState, 0, currentState.Length);
}
catch (Exception ex) {
if (Properties.Settings.Default.IsDebugEnabled)
{
Logger.Error("Error when writing to serial port on button update", ex);
}
}
}
}
}
20 changes: 9 additions & 11 deletions MaiTouchSensorButtonStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,21 @@ public MaiTouchSensorButtonStateManager(Label buttonStateValue)
{
this.buttonStateValue = buttonStateValue;
SetupUpdateLoop();

Check warning on line 53 in MaiTouchSensorButtonStateManager.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

}

private async void SetupUpdateLoop()
private async Task SetupUpdateLoop()
{
string? lastButtonState = null;
while (true)
{
Application.Current.Dispatcher.Invoke(() =>
if (lastButtonState != buttonState.ToString())
{
buttonStateValue.Content = buttonState.ToString();
});
lastButtonState = buttonState.ToString();
Application.Current.Dispatcher.Invoke(() =>
{
buttonStateValue.Content = lastButtonState;
});
}
await Task.Delay(16);
}
}
Expand All @@ -83,12 +87,6 @@ public void ReleaseButton(TouchValue button)

public byte[] GetCurrentState()
{
Application.Current.Dispatcher.Invoke(() =>
{
buttonStateValue.Content = buttonState.ToString();
});


return
[
0x28,
Expand Down

0 comments on commit 3835123

Please sign in to comment.