This repository has been archived on 2023-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
PainterlyUNO/Matrix App/adds/PortCommandQueue.cs

253 lines
7.1 KiB
C#
Raw Normal View History

2021-06-09 16:43:27 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Security.Permissions;
using System.Threading;
2021-06-09 17:53:36 +00:00
using static Matrix_App.Defaults;
2021-06-09 16:43:27 +00:00
namespace Matrix_App.adds
2021-06-09 16:43:27 +00:00
{
public class PortCommandQueue
{
private const string ThreadDeliverName = "Arduino Port Deliver Thread";
2021-06-09 16:43:27 +00:00
private readonly Queue<byte[]> byteWriteQueue = new();
private readonly Queue<byte> statusByteQueue = new();
2021-06-09 16:43:27 +00:00
private readonly Thread portDeliverThread;
2021-06-09 16:43:27 +00:00
private readonly SerialPort tx;
2021-06-09 16:43:27 +00:00
private bool running;
2021-06-09 16:43:27 +00:00
private volatile bool kill;
2021-06-09 16:43:27 +00:00
private volatile bool isPortValid;
2021-06-09 16:43:27 +00:00
private readonly byte[] received = new byte[ArduinoReceiveBufferSize];
2021-06-09 16:43:27 +00:00
private int mark;
private volatile bool synchronized;
private bool updateRealtime;
public PortCommandQueue(ref SerialPort tx)
2021-06-09 16:43:27 +00:00
{
this.tx = tx;
2021-06-09 16:43:27 +00:00
portDeliverThread = new Thread(ManageQueue) { Name = ThreadDeliverName };
2021-06-09 16:43:27 +00:00
}
private void ManageQueue()
{
try
{
while (!kill)
{
if (!isPortValid) continue;
if (byteWriteQueue.Count <= 0)
{
lock (byteWriteQueue)
{
Monitor.Wait(byteWriteQueue);
}
continue;
}
byte[] bytes;
int statusByte;
lock (byteWriteQueue)
2021-06-09 16:43:27 +00:00
{
bytes = byteWriteQueue.Dequeue();
statusByte = statusByteQueue.Dequeue();
}
2021-06-09 16:43:27 +00:00
lock (tx)
{
var success = false;
var tryCounter = 0;
do
2021-06-09 16:43:27 +00:00
{
try
{
tx.Write(bytes, 0, bytes.Length);
var b = ArduinoErrorByte;
var errorFlag = false;
mark = 0;
while (!errorFlag && (b = tx.ReadByte()) != statusByte)
{
received[mark++] = (byte) b;
errorFlag = b == ArduinoErrorByte;
}
synchronized = b == ArduinoSynchronizationByte;
success = !errorFlag;
Debug.WriteLine("===================> Com Success !");
}
catch (Exception e)
{
Debug.WriteLine("=============================> ERROR <=================================");
Debug.WriteLine(e.Message);
}
tryCounter++;
} while (!success && tryCounter < 3);
2021-06-09 16:43:27 +00:00
}
}
}
catch (ThreadInterruptedException)
2021-06-09 16:43:27 +00:00
{
Thread.CurrentThread.Interrupt();
}
2021-06-09 16:43:27 +00:00
}
[SecurityPermission(SecurityAction.Demand, ControlThread = true)]
2021-06-09 16:43:27 +00:00
public void Close()
{
try
{
if (!running) return;
kill = true;
portDeliverThread.Interrupt();
portDeliverThread.Join(1000);
2021-06-09 16:43:27 +00:00
}
catch (ThreadStartException)
{
// omit
}
catch (ThreadInterruptedException)
{
// omit
}
finally
{
if (tx.IsOpen)
{
tx.Close();
}
}
2021-06-09 16:43:27 +00:00
}
private void EnqueueArduinoCommand(params byte[] bytes)
2021-06-09 16:43:27 +00:00
{
if (!updateRealtime && bytes[0] != ArduinoInstruction.OpcodeScale &&
bytes[0] != ArduinoInstruction.OpcodePush)
return;
2021-06-09 16:43:27 +00:00
if (!running)
{
running = true;
portDeliverThread.Start();
}
lock (byteWriteQueue)
2021-06-09 16:43:27 +00:00
{
Monitor.Pulse(byteWriteQueue);
if (byteWriteQueue.Count >= ArduinoCommandQueueSize) return;
lock (byteWriteQueue)
2021-06-09 16:43:27 +00:00
{
byteWriteQueue.Enqueue(bytes);
2021-06-09 16:43:27 +00:00
}
}
}
public void EnqueueArduinoCommand(byte opcode, params byte[] data)
{
byte[] wrapper = new byte[data.Length + 1];
Buffer.BlockCopy(data, 0, wrapper, 1, data.Length);
2021-06-09 16:43:27 +00:00
wrapper[0] = opcode;
statusByteQueue.Enqueue(ArduinoSuccessByte);
2021-06-09 16:43:27 +00:00
EnqueueArduinoCommand(wrapper);
}
public void DequeueAll()
{
lock (byteWriteQueue)
{
byteWriteQueue.Clear();
}
}
public static void WaitForLastDequeue()
2021-06-09 16:43:27 +00:00
{
var timeCount = 0;
var wait = true;
2021-06-09 16:43:27 +00:00
while(wait)
{
timeCount++;
Thread.Sleep(500);
2021-06-09 17:53:36 +00:00
wait = timeCount == DequeueWaitTimeoutCounter;
2021-06-09 16:43:27 +00:00
}
}
public byte[] GetLastData()
{
return received;
2021-06-09 16:43:27 +00:00
}
public void ValidatePort()
{
try
{
if (!tx.IsOpen)
{
tx.Open();
isPortValid = true;
}
}
catch (Exception e)
{
isPortValid = false;
Debug.WriteLine("Failed opening port: " + e.Message);
}
2021-06-09 16:43:27 +00:00
}
public void InvalidatePort()
{
isPortValid = false;
tx.Close();
2021-06-09 16:43:27 +00:00
}
/// <summary>
/// Returns last location of written byte in the received buffer.
/// Call clears the mark. Any other call will return 0, unless the mark has been
/// altered by reading new data from the serial port.
/// </summary>
/// <returns></returns>
2021-06-09 16:43:27 +00:00
public int GetMark()
{
var tmp = mark;
2021-06-09 16:43:27 +00:00
mark = 0;
return tmp;
}
public void EnqueueArduinoCommandSynchronized(byte[] bytes)
{
statusByteQueue.Enqueue(ArduinoSynchronizationByte);
EnqueueArduinoCommand(bytes);
while (!synchronized)
{
}
Debug.WriteLine("======================> Synchronized!");
synchronized = false;
}
public void SetRealtimeUpdates(bool @checked)
{
updateRealtime = @checked;
}
public bool GetRealtimeUpdates()
{
return updateRealtime;
}
2021-06-09 16:43:27 +00:00
}
}