diff --git a/LaserGRBL/GrblEmulator/EmulatorUI.cs b/LaserGRBL/GrblEmulator/EmulatorUI.cs index a2622d53..b2e9a7df 100644 --- a/LaserGRBL/GrblEmulator/EmulatorUI.cs +++ b/LaserGRBL/GrblEmulator/EmulatorUI.cs @@ -19,7 +19,7 @@ public partial class EmulatorUI : Form { private static EmulatorUI istance; private bool canclose = false; - + RollingBuffer rb = new RollingBuffer(30); private const int CP_NOCLOSE_BUTTON = 0x200; protected override CreateParams CreateParams { @@ -55,63 +55,108 @@ public EmulatorUI(string initmessage) { InitializeComponent(); Grblv11Emulator.EmulatorMessage += Grblv11Emulator_EmulatorMessage; - Grblv11Emulator_EmulatorMessage(initmessage); } - StringBuilder sb = new StringBuilder(); - void Grblv11Emulator_EmulatorMessage(string message) { if (message == null) - { - if (InvokeRequired) - Invoke(new Grblv11Emulator.SendMessage(ManageClearMessage), new object [] {null}); - else - ManageClearMessage(null); - } + rb.Clear(); else - { - lock (sb) - { sb.AppendLine(message); } - } + rb.Add(message); } - void ManageClearMessage(string message) + private void RT_Tick(object sender, EventArgs e) { - lock (sb) - { sb.Length = 0; } - RTB.Text = ""; + string buff = ""; + + string[] arr = rb.ToArray(); + + foreach (string s in arr) + buff = buff + s + "\r\n"; + + RTB.Text = buff; + + + RTB.SelectionStart = RTB.TextLength; + RTB.ScrollToCaret(); } - private void RT_Tick(object sender, EventArgs e) + private void EmulatorUI_FormClosing(object sender, FormClosingEventArgs e) { - string buff = null; - lock (sb) - { - if (sb.Length > 0) + e.Cancel = !canclose; + } + } + + + public class RollingBuffer //one reader, one writer... no need to sync?! + { + private string[] buffer; + private int head; + private int tail; + private int count; + private int capacity; + + public RollingBuffer(int size) + { + lock(this) + { + buffer = new string[size]; + capacity = size; + head = 0; + tail = 0; + count = 0; + } + } + + public void Add(string item) + { + //lock (this) + //{ + buffer[head] = item; + head = (head + 1) % capacity; + + if (count == capacity) { - buff = sb.ToString(); - sb.Length = 0; + tail = (tail + 1) % capacity; // Overwrite oldest element } - } + else + { + count++; + } + //} + } - if (buff != null) - { - RTB.Text = RTB.Text + buff; + public string[] ToArray() + { + //lock(this) + //{ + string[] result = new string[count]; + for (int i = 0; i < count; i++) + result[i] = buffer[(tail + i) % capacity]; + return result; + //} + } - if (RTB.TextLength > 10000) - RTB.Text = RTB.Text.Substring(RTB.Text.Length - 10000); + internal void Clear() + { + //lock (this) + //{ + head = 0; + tail = 0; + count = 0; + //} + } - RTB.SelectionStart = RTB.TextLength; - RTB.ScrollToCaret(); - } + public int Count + { + get { return count; } } - private void EmulatorUI_FormClosing(object sender, FormClosingEventArgs e) + public int Capacity { - e.Cancel = !canclose; + get { return capacity; } } } }