private void btnConnect_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (port != null && port.IsOpen)
         {
             port.Close();
             btnConnect.Content = "Connect USB";
             currentTransition  = GUITransitions.H4usbClose;
             handleGUIstates();
         }
         else
         {
             port.PortName = comArray[comboBoxCOM.SelectedIndex];
             port.BaudRate = 9600;
             port.Parity   = Parity.None;
             port.DataBits = 8;
             port.StopBits = StopBits.One;
             port.Open();
             btnConnect.Content = "Disconnect";
             currentTransition  = GUITransitions.H3usbOpen;
             handleGUIstates();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Info", MessageBoxButton.OK, MessageBoxImage.Exclamation);
     }
 }
 private void btnClearImg_Click(object sender, RoutedEventArgs e)
 {
     myPlot = null;
     canvasPreview.Children.Clear();     //removes previous images/elements from the canvas
     canvasPreview.Background = System.Windows.Media.Brushes.White;
     currentTransition        = GUITransitions.H2imgClear;
     handleGUIstates();
 }
        private async void btnSliceImg_Click(object sender, RoutedEventArgs e)     //slices the image to individual lines that are either drawn or moved without drawing
        {
            countCmdSent = 0;

            //read start and end gcode from text boxes
            myPlot.StartGCODE = "G1 Z1\n";
            myPlot.EndGCODE   = txtEndGcode.Text + "\n";
            //myPlot.GeneratedGCODE.Clear();
            //canvasPreview.Children.Clear();

            myPlot.Img.Freeze();                          //bitmapimages needs to be frozen before they can be accessed by other threads
            await Task.Run(() => myPlot.GenerateGCODE()); //generates the GCODE to send to the robot

            Dispatcher.Invoke(() =>
            {
                canvasPreview.Children.Clear();
                canvasPreview.Background = System.Windows.Media.Brushes.White;
                //draws preview lines that the robot is going to move along:
                foreach (TraceLine lineCommand in myPlot.AllLines)
                {
                    Line myLine = new Line();
                    if (lineCommand.Draw)
                    {
                        myLine.Stroke = System.Windows.Media.Brushes.Black;
                    }
                    else
                    {
                        myLine.Stroke = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 230, 230, 230));
                    }
                    myLine.StrokeThickness = 1;
                    myLine.X1 = lineCommand.X0 * scaleToPreview;
                    myLine.Y1 = lineCommand.Y0 * scaleToPreview;
                    myLine.X2 = lineCommand.X1 * scaleToPreview;
                    myLine.Y2 = lineCommand.Y1 * scaleToPreview;
                    canvasPreview.Children.Add(myLine);
                }
            });

            sliderCmdCount.Maximum = myPlot.AllLines.Count - 1;

            txtOut.Text = "GCODE commands = " + myPlot.GeneratedGCODE.Count + "\nNumber of lines = " + myPlot.AllLines.Count + "\n";

            currentTransition = GUITransitions.H1imgSlice;
            handleGUIstates();

            //SystemSounds.Exclamation.Play();
        }
        private void btnSelectImg_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Image file (*.bmp) | *.bmp";
            //bool result = (bool)openFileDialog.ShowDialog();
            if ((bool)openFileDialog.ShowDialog())
            {
                myPlot = new Plottr(openFileDialog.FileName); //creates a plottr object with the selected image

                canvasPreview.Children.Clear();               //removes previous images/elements from the canvas
                myPlot.ImgMoveX = Convert.ToInt32((Plottr.RobotWidth - myPlot.GetImgWidth) / 2);
                myPlot.ImgMoveY = Convert.ToInt32((Plottr.RobotHeight - myPlot.GetImgHeight) / 2);
                placeImageAt(myPlot.ImgMoveX, myPlot.ImgMoveY);     //places the image in the center of preview canvas

                currentTransition = GUITransitions.H0imgOpen;
                handleGUIstates();
            }
        }
        private async void btnSendImg_Click(object sender, RoutedEventArgs e)       //send the whole sliced image to the robot over usb
        {
            txtOut.Text      += String.Format("Drawing image. Starting at command {0} of {1}\n", countCmdSent, myPlot.GeneratedGCODE.Count);
            currentTransition = GUITransitions.H5startDrawing;
            handleGUIstates();

            try
            {
                //countCmdSent = 0 is set when an image is sliced
                for (; countCmdSent < myPlot.GeneratedGCODE.Count; countCmdSent++)       //for loop instead of for each gives the possibility to start at a specific command
                {
                    if (btnPauseDrawing.Content.ToString().Contains("Continue"))
                    {
                        break;
                    }

                    string[] getLineNo = myPlot.GeneratedGCODE[countCmdSent].Split('L');
                    if (int.TryParse(getLineNo[getLineNo.Count() - 1], out int lineNo))     //shows on slider the current line (not command) being drawn
                    {
                        sliderCmdCount.Value = lineNo;
                    }

                    bool timedOut = await sendSerialStringAsync(myPlot.GeneratedGCODE[countCmdSent]);     //sends the gcode over usb to the robot

                    if (timedOut)
                    {
                        txtOut.Text += "Timed out\n";
                        //break;      //exits the for loop
                    }

                    //countCmdSent = i;        //increment number of commands sent
                }
                txtOut.Text += "Commands successfully sent = " + countCmdSent + "\n";
            }
            catch (Exception ex)
            {
                string msg = "Commands successfully sent = " + countCmdSent + "\n" + ex.Message;
                MessageBox.Show(msg, "Info", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }