summaryrefslogtreecommitdiff
path: root/Anoto/Form1.cs
blob: 9c63122db1cf46265e4b50553d64472c42b03bcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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<Point> Points;
    }


    public partial class Form1 : Form
    {


        Anoto.GenericStreamer.PenManagerClass PenManager;

        Dictionary<string, Stroke> PensStrocks;

        Dictionary<string, List<PointF>> PensPoints;
        Dictionary<string, Brush> PensBrush;
        Dictionary<string, Pen> Pens;

        List<AnotoPen> 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<AnotoPen>();

            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<string, List<PointF>>();
            PensBrush = new Dictionary<string, Brush>();
            Pens = new Dictionary<string, Pen>();
            PensStrocks = new Dictionary<string, Stroke>();
        }

        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<PointF>());
            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<PointF>());
               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 + ")";
        }
    }
}