﻿using CSmcCs;

namespace BankArc
{
    public partial class Form1 : Form
    {
        /////////////////////////////////////////////////
        // SMC access value
        /////////////////////////////////////////////////
        string szDeviceName = "";
        short ArcStartDir;
        short MotionType;
        bool bRet;
        short Id;
        short AxisNo;
        short BankNum;

        short InterAxis;
        short InterType;
        short AutoEndPointPull;
        short BankNo;
        short Reserved;
        double ArcSpeed;
        short Coordinates;
        int Center_X;
        int Center_Y;
        int End_X;
        int End_Y;

        CSmc Smc = new CSmc();

        public Form1()
        {
            InitializeComponent();
        }

        private void BankArc_Load(object sender, System.EventArgs e)
        {
            string szErrorString;
            int ret = 0;
            short i = 0;
            string tmp;

            lblComment.Text = "OK";

            //-------------
            // Initialize
            //-------------
            szDeviceName = "SMC000";
            txtDeviceName.Text = szDeviceName;
            AxisNo = 1;

            txtArcSpeed.Text = "10000";
            rdoOptionSingle.Checked = true;
            MotionType = (short)CSmcConst.CSMC_SINGLE;
            rdoDirCw.Checked = true;
            ArcStartDir = (short)CSmcConst.CSMC_CW;

            txtTargetAxis.Text = string.Format("{0:d}", AxisNo + 1);

            //AxisNo ComboBox
            cboTargetAxis.Items.Clear();
            for (i = 1; i <= 8; i++)
            {
                cboTargetAxis.Items.Add(i);
            }
            //ComboBox Initialise
            cboTargetAxis.SelectedIndex = AxisNo - 1;

            BankNum = 4;
            //BankNum ComboBox
            cboTargetBankNum.Items.Clear();
            for (i = 1; i <= 4; i++)
            {
                cboTargetBankNum.Items.Add(i);
            }
            //ComboBox Initialise
            cboTargetBankNum.SelectedIndex = BankNum - 1;

            ret = Smc.WInit(szDeviceName, out Id);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWInit = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
            }



            InterAxis = 0;
            InterType = 0;
            AutoEndPointPull = 0;
            BankNo = 0;
            Reserved = 0;
            ArcSpeed = 0;
            Coordinates = 0;
            Center_X = 0;
            Center_Y = 0;
            End_X = 0;
            End_Y = 0;
        }

        private void btnExit_Click(object sender, System.EventArgs e)
        {
            Smc.WExit(Id);
            this.Close();
        }

        private void cboTargetAxis_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            AxisNo = (short)(cboTargetAxis.SelectedIndex + 1);
            txtTargetAxis.Text = string.Format("{0:d}", AxisNo + 1);
        }

        private bool SetBankParam()
        {
            int ret = 0;
            string szErrorString;
            string tmp;

            //----------------------------
            // Set Bank Number to Driver
            //----------------------------
            ret = Smc.WSetBankNumber(Id, AxisNo, BankNum);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWSetBankNumber = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return false;
            }

            //----------------------------
            // Set Interpolation to Driver
            //----------------------------
            int cal1, cal2;
            cal1 = (int)Math.Pow(2, (AxisNo - 1));
            cal2 = (int)Math.Pow(2, AxisNo);
            InterAxis = (short)(cal1 | cal2);

            //Circular arc interpolation
            InterType = 3;

            //Automatically enter an end point pull in operation at the end of arc interpolation operation.
            AutoEndPointPull = 1;

            for (BankNo = 1; BankNo <= BankNum; BankNo++)
            {
                ret = Smc.WSetBankInterpolation(Id, AxisNo, BankNo, InterType, InterAxis, Reserved);
                if (ret != 0)
                {
                    Smc.WGetErrorString(ret, out tmp);
                    szErrorString = string.Format("SmcWSetBankInterpolation = {0:d} : {1}", ret, tmp);
                    lblComment.Text = szErrorString;
                    return false;
                }
                ret = Smc.WSetBankArcParam(Id, AxisNo, BankNo, ArcStartDir, AutoEndPointPull);
                if (ret != 0)
                {
                    Smc.WGetErrorString(ret, out tmp);
                    szErrorString = string.Format("SmcWSetBankArcParam = {0:d} : {1}", ret, tmp);
                    lblComment.Text = szErrorString;
                    return false;
                }
            }

            //----------------------------
            // Set Arc Point to Driver
            //----------------------------
            try
            {
                ArcSpeed = double.Parse(txtArcSpeed.Text);
            }
            catch (FormatException)
            {
                ArcSpeed = 0;
            }

            Coordinates = 5000;

            Center_X = Coordinates;
            Center_Y = 0;
            if (ArcStartDir == (short)CSmcConst.CSMC_CW)
            {
                End_X = Coordinates;
                End_Y = Coordinates;
            }
            else
            {
                End_X = Coordinates;
                End_Y = (-1) * Coordinates;
            }
            ret = Smc.WSetBankArcPoint(Id, AxisNo, 1, ArcSpeed, Center_X, Center_Y, End_X, End_Y);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWSetBankArcPoint = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return false;
            }

            if (ArcStartDir == (short)CSmcConst.CSMC_CW)
            {
                Center_X = 0;
                Center_Y = (-1) * Coordinates;
                End_X = Coordinates;
                End_Y = (-1) * Coordinates;
            }
            else
            {
                Center_X = 0;
                Center_Y = Coordinates;
                End_X = Coordinates;
                End_Y = Coordinates;
            }
            ret = Smc.WSetBankArcPoint(Id, AxisNo, 2, ArcSpeed, Center_X, Center_Y, End_X, End_Y);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWSetBankArcPoint = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return false;
            }

            Center_X = (-1) * Coordinates;
            Center_Y = 0;
            if (ArcStartDir == (short)CSmcConst.CSMC_CW)
            {
                End_X = (-1) * Coordinates;
                End_Y = (-1) * Coordinates;
            }
            else
            {
                End_X = (-1) * Coordinates;
                End_Y = Coordinates;
            }

            ret = Smc.WSetBankArcPoint(Id, AxisNo, 3, ArcSpeed, Center_X, Center_Y, End_X, End_Y);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWSetBankArcPoint = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return false;
            }

            if (ArcStartDir == (short)CSmcConst.CSMC_CW)
            {
                Center_X = 0;
                Center_Y = Coordinates;
                End_X = (-1) * Coordinates;
                End_Y = Coordinates;
            }
            else
            {
                Center_X = 0;
                Center_Y = (-1) * Coordinates;
                End_X = (-1) * Coordinates;
                End_Y = (-1) * Coordinates;
            }

            ret = Smc.WSetBankArcPoint(Id, AxisNo, 4, ArcSpeed, Center_X, Center_Y, End_X, End_Y);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWSetBankArcPoint = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return false;
            }

            return true;
        }

        private void rdoDirCw_CheckedChanged(object sender, System.EventArgs e)
        {
            ArcStartDir = (short)CSmcConst.CSMC_CW;
        }

        private void rdoDirCcw_CheckedChanged(object sender, System.EventArgs e)
        {
            ArcStartDir = (short)CSmcConst.CSMC_CCW;
        }

        private void rdoOptionSingle_CheckedChanged(object sender, System.EventArgs e)
        {
            MotionType = (short)CSmcConst.CSMC_SINGLE;
        }

        private void rdoOptionLoop_CheckedChanged(object sender, System.EventArgs e)
        {
            MotionType = (short)CSmcConst.CSMC_LOOP;
        }

        private void btnComMove_Click(object sender, System.EventArgs e)
        {
            int ret;
            string tmp;
            string szErrorString;

            //----------------------------------
            // Set parameters to Driver
            //----------------------------------
            bRet = SetBankParam();
            if (bRet == false)
            {
                return;
            }

            //---------------
            // Ready motion
            //---------------
            ret = Smc.WSetBankReady(Id, AxisNo, MotionType);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWSetBankReady = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return;
            }

            ret = Smc.WGetBankArcPoint(Id, AxisNo, 1, out ArcSpeed, out Center_X, out Center_Y, out End_X, out End_Y);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWGetBankArcPoint = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return;
            }
            tmp = string.Format("{0:f1}", ArcSpeed);
            txtArcSpeed.Text = tmp;

            //---------------
            // Start Motion
            //---------------
            ret = Smc.WMotionStart(Id, AxisNo);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWMotionStart = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return;
            }
            lblComment.Text = "OK";
        }

        private void btnComStop_Click(object sender, System.EventArgs e)
        {
            int ret;
            string tmp;
            string szErrorString;

            ret = Smc.WMotionStop(Id, AxisNo);
            if (ret != 0)
            {
                Smc.WGetErrorString(ret, out tmp);
                szErrorString = string.Format("SmcWMotionStop = {0:d} : {1}", ret, tmp);
                lblComment.Text = szErrorString;
                return;
            }
            lblComment.Text = "OK";
        }

        private void cboTargetBankNum_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            BankNum = (short)(cboTargetBankNum.SelectedIndex + 1);
        }
    }
}