using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using i8196F;

namespace AxisMotion
{
    public partial class Form1 : Form
    {
        byte bSlotNo;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            short ret = 0;
            byte i = 0;

            bool flgExistCard = false;

            for (i = 0; i < 9; i++)
            {
                ret = C96F.i8196f_check_card_in_slot(i);
                if (ret == 0)
                {
                    InitialI8196F(i);
                    cmbSlotNo.Items.Add(i.ToString());
                    flgExistCard = true;
                }
            }

            if (flgExistCard)
            {
                cmbSlotNo.SelectedIndex = 0;
                bSlotNo = System.Byte.Parse(cmbSlotNo.Text);
                timer1.Enabled = true;
            }
            else
            {
                MessageBox.Show("No I-8196F!");
                timer1.Enabled = false;
            }          
               
        }

        private void InitialI8196F(byte SlotNo)
        {
            short ret;

            int i = 0;

            byte AxisNo;

            ret = C96F.i8196f_registration(SlotNo);

            ret = C96F.i8196f_load_power_on_cfg(SlotNo, ePOWER_ON_SETTING.DEFAULT_POWER_ON_SETTING);

            for (i = 0; i < 6; i++)
            {
                AxisNo = (byte)(0x01 << i);

                ret = C96F.i8196f_set_pls_cfg(SlotNo, (eSINGLE_AXIS)AxisNo, ePULSE_MODE.PULSE_MODE_CW_CCW, 
                                        ePULSE_LOGIC.PULSE_LOGIC_ACTIVE_HIGH, eFORWARD_ACTIVE.PULSE_FORWARD_ACTIVE_HIGH);
                ret = C96F.i8196f_set_enc_cfg(SlotNo, (eSINGLE_AXIS)AxisNo, eENCODER_MODE.ENCODER_MODE_CW_CCW);
                ret = C96F.i8196f_set_limit(SlotNo, (eSINGLE_AXIS)AxisNo, eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_LOW, eSTOP_MODE.STOP_SUDDEN);
                ret = C96F.i8196f_set_alarm(SlotNo, (eSINGLE_AXIS)AxisNo, eENABLE_DISABLE.DISABLE, eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_LOW);
                ret = C96F.i8196f_set_inp(SlotNo, (eSINGLE_AXIS)AxisNo, eENABLE_DISABLE.DISABLE, eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_LOW);
                ret = C96F.i8196f_set_ready(SlotNo, (eSINGLE_AXIS)AxisNo, eENABLE_DISABLE.DISABLE, eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_LOW);
            }
        }

        private void cmbSlotNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            bSlotNo = System.Byte.Parse(cmbSlotNo.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TextBox[] txtGetIOSts = { txtGetIOSts0, txtGetIOSts1, txtGetIOSts2, txtGetIOSts3, txtGetIOSts4, txtGetIOSts5 };
            TextBox[] txtGetCMD = { txtGetCMD0, txtGetCMD1, txtGetCMD2, txtGetCMD3, txtGetCMD4, txtGetCMD5 };
            TextBox[] txtGetENC = { txtGetENC0, txtGetENC1, txtGetENC2, txtGetENC3, txtGetENC4, txtGetENC5 };
            TextBox[] txtGetSpd = { txtGetSpd0, txtGetSpd1, txtGetSpd2, txtGetSpd3, txtGetSpd4, txtGetSpd5 };
            TextBox[] txtGetMCsts = { txtGetMCsts0, txtGetMCsts1, txtGetMCsts2, txtGetMCsts3, txtGetMCsts4, txtGetMCsts5 };

            short ret = 0;
	        int i = 0;
	        byte AxisNo = 0;
            ushort wDIStatus = 0;
            int LP = 0, EP = 0, Spd = 0;
	        byte bDone = 0;
	        ushort wStopStatus = 0;

	        for (i = 0; i < 6; i++)
	        {

		        AxisNo = (byte)(0x01<<i);

                ret = C96F.i8196f_get_mdi_status(bSlotNo, (eSINGLE_AXIS)AxisNo, ref wDIStatus);
                txtGetIOSts[i].Text = "0x" + wDIStatus.ToString("X4");

		        ret = C96F.i8196f_get_cmdcounter( bSlotNo, (eSINGLE_AXIS)AxisNo, ref LP);
		        txtGetCMD[i].Text = LP.ToString();

		        ret = C96F.i8196f_get_enccounter( bSlotNo, (eSINGLE_AXIS)AxisNo, ref EP);
		        txtGetENC[i].Text = EP.ToString();

		        ret = C96F.i8196f_get_speed(bSlotNo, (eSINGLE_AXIS)AxisNo, ref Spd);
		        txtGetSpd[i].Text = Spd.ToString();

		        ret = C96F.i8196f_get_motion_done(bSlotNo, (eSINGLE_AXIS)AxisNo, ref bDone, ref wStopStatus);
		        txtGetMCsts[i].Text = "(" + bDone.ToString() + ")" + wStopStatus.ToString("X4");
        		
	        }
        }


        private void ResetCnt(object sender, EventArgs e)
        {
            int index = int.Parse((sender as Button).Tag.ToString());

            int i = 0;
            short ret = 0;
            byte AxisNo = 0;
            int LogicPos = 0;
            int EncPos = 0;

            if (index < 6)
            {
                AxisNo = (byte)(0x01 << index);
                ret = C96F.i8196f_set_cmdcounter(bSlotNo, (eSINGLE_AXIS)AxisNo, LogicPos);
                ret = C96F.i8196f_set_enccounter(bSlotNo, (eSINGLE_AXIS)AxisNo, EncPos);
            }
            else if (index == 6)
            {
                for (i = 0; i < 6; i++)
                {
                    AxisNo = (byte)(0x01 << i);
                    ret = C96F.i8196f_set_cmdcounter(bSlotNo, (eSINGLE_AXIS)AxisNo, LogicPos);
                    ret = C96F.i8196f_set_enccounter(bSlotNo, (eSINGLE_AXIS)AxisNo, EncPos);
                }
            }
        }

        private void ToggleSVON(object sender, EventArgs e)
        {
            int index = int.Parse((sender as Control).Tag.ToString());

            byte AxisNo = (byte)(0x01 << index);
            short ret = 0;
            eCHANNEL_STATE OnOff = 0;

            ret = C96F.i8196f_get_servo_on_status(bSlotNo, (eSINGLE_AXIS)AxisNo, ref OnOff);
            if (OnOff == eCHANNEL_STATE.OFF)
            { ret = C96F.i8196f_set_servo_on(bSlotNo, (eSINGLE_AXIS)AxisNo, eCHANNEL_STATE.ON); }
            else
            { ret = C96F.i8196f_set_servo_on(bSlotNo, (eSINGLE_AXIS)AxisNo, eCHANNEL_STATE.OFF); }
        }

        private void ToggleRALM(object sender, EventArgs e)
        {
            int index = int.Parse((sender as Control).Tag.ToString());

            byte AxisNo = (byte)(0x01 << index);
            short ret = 0;
            eCHANNEL_STATE OnOff = 0;

            ret = C96F.i8196f_get_servo_almrst_status(bSlotNo, (eSINGLE_AXIS)AxisNo, ref OnOff);
            if (OnOff == eCHANNEL_STATE.OFF)
            { ret = C96F.i8196f_set_alarm_reset(bSlotNo, (eSINGLE_AXIS)AxisNo, eCHANNEL_STATE.ON); }
            else
            { ret = C96F.i8196f_set_alarm_reset(bSlotNo, (eSINGLE_AXIS)AxisNo, eCHANNEL_STATE.OFF); }
        }

        private void MoveAxis(object sender, EventArgs e)
        {
            int index = int.Parse((sender as Control).Tag.ToString());

            TextBox[] txtSpeed = { txtSpeed0, txtSpeed1, txtSpeed2, txtSpeed3, txtSpeed4, txtSpeed5 };
            TextBox[] txtAcc = { txtAcc0, txtAcc1, txtAcc2, txtAcc3, txtAcc4, txtAcc5 };
            TextBox[] txtDist = { txtDist0, txtDist1, txtDist2, txtDist3, txtDist4, txtDist5 };

            byte AxisNo = (byte)(0x01 << index);
            short ret = 0;

            uint Speed = System.UInt32.Parse(txtSpeed[index].Text);
            uint Acc = System.UInt32.Parse(txtAcc[index].Text);
            int Dist = System.Int32.Parse(txtDist[index].Text);

            eMOVE_DIRECTION eDirection;

            if (rdoR.Checked)
            {
                if (chkSCurve.Checked)
                { ret = C96F.i8196f_s_move(bSlotNo, (eSINGLE_AXIS)AxisNo, Speed / 10, Speed, Speed / 10, Acc, Acc, Dist); }
                else
                { ret = C96F.i8196f_t_move(bSlotNo, (eSINGLE_AXIS)AxisNo, Speed / 10, Speed, Speed / 10, Acc, Acc, Dist); }
            }
            else if (rdoA.Checked)
            {
                if (chkSCurve.Checked)
                { ret = C96F.i8196f_abs_s_move(bSlotNo, (eSINGLE_AXIS)AxisNo, Speed / 10, Speed, Speed / 10, Acc, Acc, Dist); }
                else
                { ret = C96F.i8196f_abs_t_move(bSlotNo, (eSINGLE_AXIS)AxisNo, Speed / 10, Speed, Speed / 10, Acc, Acc, Dist); }
            }
            else if (rdoV.Checked)
            {
                if (Dist > 0)
                {eDirection = eMOVE_DIRECTION.MOVE_DIRECTION_FORWARD;}
                else
                {eDirection = eMOVE_DIRECTION.MOVE_DIRECTION_REVERSE;}
                ret = C96F.i8196f_velocity_move(bSlotNo, (eSINGLE_AXIS)AxisNo, Speed / 10, Speed, Acc, eDirection);
            }
        }

        private void StopAxis(object sender, EventArgs e)
        {
            int index = int.Parse((sender as Control).Tag.ToString());

            int i = 0;
            byte AxisNo = 0;
            short ret = 0;

            if (index < 6)
            {
                AxisNo = (byte)(0x01 << index);
                // StopMode = 1: slow down then stop, 2: sudden stop
                ret = C96F.i8196f_stop_move(bSlotNo, (eSINGLE_AXIS)AxisNo, eSTOP_MODE.STOP_SLOWDOWN);
            }
            else if (index == 6)
            {
                for (i = 0; i < 6; i++)
                {
                    AxisNo = (byte)(0x01 << i);
                    // StopMode = 1: slow down then stop, 2: sudden stop
                    ret = C96F.i8196f_stop_move(bSlotNo, (eSINGLE_AXIS)AxisNo, eSTOP_MODE.STOP_SLOWDOWN);
                }
            }
        }
    }
}