Matomat/Matomat/LCDDisplay.cs
2011-10-14 07:57:53 +00:00

206 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;
namespace Matomat
{
class LCDDisplay
{
private class instance
{
public LCDDisplay lCDDisplay;
public string signature;
public instance(LCDDisplay lCDDisplay, string signature)
{
this.lCDDisplay = lCDDisplay;
this.signature = signature;
}
}
private class Message
{
public string text;
public Status status;
public int time;
public Message(string text, Status status, int time)
{
this.text = text;
this.status = status;
this.time = time;
}
}
public class Comport
{
public string port;
public int bautrate;
public Parity parity;
public int databits;
public StopBits stopbits;
public Comport(string port, int bautrate, Parity parity, int databits, StopBits stopbits)
{
this.port = port;
this.bautrate = bautrate;
this.parity = parity;
this.databits = databits;
this.stopbits = stopbits;
}
}
static List<instance> instances;
private Queue<Message> messages;
private volatile bool _shouldStop;
private SerialPort serialPort;
private bool disable = false;
public LCDDisplay(Comport port)
{
this.serialPort = new SerialPort(port.port, port.bautrate, port.parity, port.databits, port.stopbits);
try
{
this.serialPort.Open();
}
catch (Exception)
{
this.disable = true;
}
messages = new Queue<Message>();
Thread workerThread = new Thread(this.DoWork);
workerThread.Start();
while (!workerThread.IsAlive);
}
public void DoWork()
{
Message m;
DateTime timeout = new DateTime();
while (!_shouldStop)
{
Thread.Sleep(1);
if (messages.Count != 0)
{
if (timeout.Ticks < DateTime.Now.Ticks)
{
m = messages.Dequeue();
timeout = DateTime.Now.AddSeconds(m.time);
display(m.text, m.status);
}
}
}
}
private void display(string text, Status status)
{
if (status == Status.Warn)
{
Console.Beep();
}
if (status == Status.Error)
{
Console.Beep();
Console.Beep();
}
Console.WriteLine(text);
this.displayClear();
byte[] btext = text.ToCharArray("ÄÖÜäöüß²€", new byte[] { 0xE1, 0xEF, 0xF5, 0xE1, 0xEF, 0xF5, 0xE2, 0xFF, 0xFF});
byte[] upper;
byte[] lower;
if (text.Length > 80)
{
upper = btext.Substring(0, 80);
lower = btext.Substring(80);
}
else
{
upper = btext;
lower = new byte[0];
}
this.displayEscCode("GTO0@");
this.displayCode(upper);
this.displayEscCode("GTO2@");
this.displayCode(lower);
}
private void displayClear()
{
this.displayEscCode("GTO0@");
this.displayCode(new byte[] { 0x0c });
this.displayEscCode("GTO2@");
this.displayCode(new byte[] { 0x0c });
this.displayEscCode("GTO0@");
}
private void displayCode(byte[] p)
{
if (!disable)
{
this.serialPort.Write(p, 0, p.Length);
}
}
private void displayEscCode(string code)
{
this.displayCode(new byte[] { 0x1b });
if (!disable)
{
this.serialPort.Write(code);
}
this.displayCode(new byte[] { 0x0d });
}
public enum Status
{
OK,
Warn,
Error
}
internal void print(string text)
{
this.anzeige(text, Status.OK, 0);
}
private void anzeige(string text, Status status, int time)
{
Message m = new Message(text, status, time);
messages.Enqueue(m);
}
internal void print(string text, int time)
{
this.anzeige(text, Status.OK, time);
}
internal void print(string text, Status status)
{
this.anzeige(text, status, 0);
}
internal void print(string text, Status status, int time)
{
this.anzeige(text, status, time);
}
internal static LCDDisplay getInstance(Comport port)
{
if (instances == null)
instances = new List<instance>();
string signature = Helper.Serialize(port);
foreach (instance i in instances)
{
if (i.signature == signature)
return i.lCDDisplay;
}
instance instance = new instance(new LCDDisplay(port),signature);
instances.Add(instance);
return instance.lCDDisplay;
}
public void RequestStop()
{
_shouldStop = true;
}
}
}