From 42dc1d36235292786322d28340a81c6cb3fd46c0 Mon Sep 17 00:00:00 2001 From: hurter Date: Wed, 31 Aug 2011 16:25:05 +0000 Subject: --- Anoto/Form1.cs | 390 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 390 insertions(+) create mode 100644 Anoto/Form1.cs (limited to 'Anoto/Form1.cs') diff --git a/Anoto/Form1.cs b/Anoto/Form1.cs new file mode 100644 index 0000000..9c63122 --- /dev/null +++ b/Anoto/Form1.cs @@ -0,0 +1,390 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using Anoto; +using System.Threading; +//using Ivy; +using IvyBus; + +namespace Anoto +{ + public struct Stroke + { + List Points; + } + + + public partial class Form1 : Form + { + + + Anoto.GenericStreamer.PenManagerClass PenManager; + + Dictionary PensStrocks; + + Dictionary> PensPoints; + Dictionary PensBrush; + Dictionary Pens; + + List AnotoPens; + + Mutex mutex; + Random Rnd; + + + Bitmap DrawingArea; + + + XMLStateMachine StateMAchine; + + public Form1() + { + InitializeComponent(); + + StateMAchine = new XMLStateMachine(); + StateMAchine.StateTable = "XMLStates.xml"; + StateMAchine.CurrentState = "Start"; + + + } + + void TheIvyDomain_DomainChanged(object sender, EventArgs e) + { + TheIvyBus.ivy.Stop(); + TheIvyBus.ivy.Start(TheIvyDomain.Domain); + } + + // delegate NewEvent + AnotoStrip[] Strips; + + private void Form1_Load(object sender, EventArgs e) + { + TranparentBlack = new SolidBrush(Color.FromArgb(100, 0, 0, 0)); + + TheIvyDomain.Location = new Point(0, 30); + + DrawingArea = new Bitmap(pictureBox1.Width, pictureBox1.Height); + pictureBox1.Image = DrawingArea; + + + //Start IVY + TheIvyBus.ivy.Start(TheIvyDomain.Domain); + + TheIvyDomain.DomainChanged += new EventHandler(TheIvyDomain_DomainChanged); + + this.Controls.Add(TheIvyDomain); + + + // Strips = AnotoStrip.CreateStrips(); + + Strips = AnotoStrip.LoadFileStrip("Strips.csv"); + + AnotoPens = new List(); + + PenManager = new Anoto.GenericStreamer.PenManagerClass(); + + PenManager.PenConnected += new Anoto.GenericStreamer._IPenManagerEvents_PenConnectedEventHandler(pm_PenConnected); + PenManager.PenDisconnected += new GenericStreamer._IPenManagerEvents_PenDisconnectedEventHandler(PenManager_PenDisconnected); + PenManager.NewCoordinate += new Anoto.GenericStreamer._IPenManagerEvents_NewCoordinateEventHandler(PenManager_NewCoordinate); + PenManager.PenDown += new Anoto.GenericStreamer._IPenManagerEvents_PenDownEventHandler(PenManager_PenDown); + PenManager.PenUp += new Anoto.GenericStreamer._IPenManagerEvents_PenUpEventHandler(PenManager_PenUp); + PenManager.Start(); + + mutex = new Mutex(); + + InitDictionary(); + + Rnd = new Random(); + + // this.PenUpdate += + } + + private void InitDictionary() + { + PensPoints = new Dictionary>(); + PensBrush = new Dictionary(); + Pens = new Dictionary(); + PensStrocks = new Dictionary(); + } + + void pm_PenConnected(string penSerial, Anoto.GenericStreamer.PenType PenType, ulong time, string productName, ushort pid) + { + + AnotoPen pen = new AnotoPen(penSerial, PenType, time, productName, pid); + AnotoPens.Add(pen); + + this.Invoke(new MethodInvoker(delegate() { listBoxAnotoPens.Items.Add(pen); })); + + Console.WriteLine("Pen connected " + penSerial + " " + PenType.ToString() + " " + productName); + PensPoints.Add(penSerial, new List()); + PensBrush.Add(penSerial, new SolidBrush(Color.FromArgb(255, Rnd.Next(255), Rnd.Next(255), Rnd.Next(255)))); + Pens.Add(penSerial, new Pen(Color.FromArgb(255, Rnd.Next(255), Rnd.Next(255), Rnd.Next(255)))); + + } + + void PenManager_PenDisconnected(string penSerial, GenericStreamer.PenType PenType, ulong time) + { + this.Invoke(new MethodInvoker(delegate() + { + mutex.WaitOne(); + AnotoPen p = null; + foreach (AnotoPen item in listBoxAnotoPens.Items) + { + if (item.PenSerial == penSerial) + p = item; + } + if (p != null) listBoxAnotoPens.Items.Remove(p); + mutex.ReleaseMutex(); + })); + } + + + void PenManager_PenUp(string penSerial, Anoto.GenericStreamer.PenType PenType, ulong time, byte penDownSeqNbr, int isSpcdGenerated) + { + Console.WriteLine("Pen Up " + penSerial + " time " + time); + } + + void PenManager_PenDown(string penSerial, Anoto.GenericStreamer.PenType PenType, ulong time, byte penDownSeqNbr, Anoto.GenericStreamer.PenTipType PenTipType, int isValidColor, byte r, byte g, byte b, int isSpcdGenerated) + { + Console.WriteLine("Pen Down " + penSerial + " time " + time); + + this.Invoke(new MethodInvoker(delegate() + { + mutex.WaitOne(); + PensPoints.Remove(penSerial); + + PensPoints.Add(penSerial, new List()); + mutex.ReleaseMutex(); + })); + } + + DateTime LastDataUpdate; + + void PenManager_NewCoordinate(string penSerial, Anoto.GenericStreamer.PenType PenType, ulong time, string page, int x, int y, byte imgSeqNbr, byte force) + { + this.Invoke(new MethodInvoker(delegate() + { + mutex.WaitOne(); + PensPoints[penSerial].Add(new Point(x, y)); + mutex.ReleaseMutex(); + + // Console.WriteLine("Pen NewCoordinate " + penSerial + " time " + time + " x " + x + " y " + y); + + if ((DateTime.Now - LastDataUpdate).TotalMilliseconds > 100) + { + PopulateGraphicalData(page, x, y, force); + LastDataUpdate = DateTime.Now; + } + + PopulateData(page, x, y, force); + })); + } + + private void PopulateGraphicalData(string page, int x, int y, byte force) + { + this.Invoke(new MethodInvoker(delegate() + { + SetProgressBarValue(progressBarX, x); + SetProgressBarValue(progressBarY, y); + SetProgressBarValue(progressBarForce, (int)force); + + labelX.Text = "X: " + x; + labelY.Text = "Y: " + y; + + labelPage.Text = page; + + pictureBox1.Invalidate(); + })); + } + + private void PopulateData(string page, int x, int y, byte force) + { + this.Invoke(new MethodInvoker(delegate() + { + //Find the Strip name + AnotoStrip strip = AnotoStrip.GetStrip(x, y, page, Strips); + + + if (strip != null) + { + Anoto.AnotoStrip.Cells cell = strip.GetStripArea(x, y); + labelStripInfo.Text = cell.ToString() + " : " + strip.GetTextForCell(cell) + + + Environment.NewLine + " " + strip.ToString(); + TheIvyBus.SendMsg("SelectionEvent acc=bordeaux wp=WP1 role=TC Flight=" + strip.SSR); + + if ((cell != StateMachineCurrentCell) || (StateMachineCurrentStrip != strip)) + { + Console.WriteLine("New Action"); + //Test if new message: + StateMachineInput(cell, strip); + + StateMachineCurrentStrip = strip; + StateMachineCurrentCell = cell; + } + } + + })); + } + + + private AnotoStrip StateMachineCurrentStrip; + private Anoto.AnotoStrip.Cells StateMachineCurrentCell; + + string LastAlidadeStart = ""; + string LastAlidadeStop = ""; + + public void DisplayAlidade(string start, string stop) + { + if (!string.IsNullOrEmpty(LastAlidadeStart)) + { + //hide the previous alidate + TheIvyBus.SendMsg("DistanceFeedbackOff acc=bordeaux wp=WP1 role=TC Start=" + LastAlidadeStart + " End=" + LastAlidadeStop); + } + + //If the same previous start and stop do nothing -> remove the alidade + if (!((start == LastAlidadeStart) && (stop == LastAlidadeStop))) + { + //Show the new alidade + TheIvyBus.SendMsg("DistanceFeedbackOn acc=bordeaux wp=WP1 role=TC Start=" + start + " End=" + stop); + + LastAlidadeStop = stop; + LastAlidadeStart = start; + + } + else + { + LastAlidadeStop = ""; + LastAlidadeStart = ""; + } + } + + + public void StateMachineInput(Anoto.AnotoStrip.Cells cell, AnotoStrip strip) + { + + if (StateMAchine.Next(cell.ToString()) != String.Empty) + { + Console.WriteLine(StateMAchine.Action); + //New state + switch (StateMAchine.Action) + { + case "Hilight": + //Send IVY Selection event + TheIvyBus.SendMsg("SelectionEvent acc=bordeaux wp=WP1 role=TC Flight=" + strip.SSR); + + break; + case "AlidadeInfoInfo": + //Send IVY Selection event + DisplayAlidade(StateMachineCurrentStrip.SSR , strip.SSR); + // TheIvyBus.SendMsg("DistanceFeedbackOn acc=bordeaux wp=WP1 role=TC Start=" + StateMachineCurrentStrip.SSR + " End=" + strip.SSR); + break; + case "AlidadeInfoBeacon": + //Send IVY Selection event + DisplayAlidade(StateMachineCurrentStrip.SSR, strip.GetTextForCell(cell)); + // TheIvyBus.SendMsg("DistanceFeedbackOn acc=bordeaux wp=WP1 role=TC Start=" + StateMachineCurrentStrip.SSR + " End=" + strip.GetTextForCell(cell)); + break; + + case "AlidadeBeaconInfo": + //Send IVY Selection event + DisplayAlidade(StateMachineCurrentStrip.GetTextForCell(StateMachineCurrentCell), strip.SSR); + // TheIvyBus.SendMsg("DistanceFeedbackOn acc=bordeaux wp=WP1 role=TC Start=" + StateMachineCurrentStrip.GetTextForCell(StateMachineCurrentCell) + " End=" + strip.SSR); + break; + + default: + break; + } + + + }; + } + + + + void SetProgressBarValue(ProgressBar pb, int val) + { + if (val < pb.Minimum) + pb.Minimum = val; + if (val > pb.Maximum) + pb.Maximum = val; + pb.Value = val; + } + + + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + PenManager.Stop(); + } + + private SolidBrush TranparentBlack; + + private void pictureBox1_Paint(object sender, PaintEventArgs e) + { + //draw the dots + Graphics g = e.Graphics; + + g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; + + g.FillRectangle(Brushes.White, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); + + mutex.WaitOne(); + + float scale = 10; + g.ScaleTransform(1 / scale, 1 / scale); + + foreach (var pen in PensPoints) + { + if (pen.Value.Count > 1) + g.DrawLines(Pens[pen.Key], pen.Value.ToArray()); + + foreach (var point in pen.Value) + { + + // g.FillEllipse(TranparentBlack, (float)(point.X) / 10.0f, (float)(point.Y) / 10.0f, 4, 4); + g.FillEllipse(TranparentBlack, (point.X) - scale * 2, (point.Y) - scale * 2, 4 * scale, 4 * scale); + } + + if (pen.Value.Count != 0) + PopulateData("", (int)pen.Value.Last().X, (int)pen.Value.Last().Y, 0); + } + mutex.ReleaseMutex(); + } + + + private void Form1_FormClosed(object sender, FormClosedEventArgs e) + { + TheIvyBus.ivy.Stop(); + } + + + } + + public class AnotoPen + { + + public string PenSerial; + public Anoto.GenericStreamer.PenType PenType; + public ulong Time; + public string ProductName; + public ushort Pid; + + public AnotoPen(string penSerial, Anoto.GenericStreamer.PenType penType, ulong time, string productName, ushort pid) + { + PenSerial = penSerial; + PenType = penType; + Time = time; + ProductName = productName; + Pid = pid; + } + + public override string ToString() + { + return PenType + "(" + PenSerial + ")"; + } + } +} -- cgit v1.1