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 Homing
{
    public partial class Form1 : Form
    {
        byte bSlotNo;
        eSINGLE_AXIS eAxisNo;

        public Form1()
        {
            InitializeComponent();
        }

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

            bool flgExistCard = false;

            eAxisNo = eSINGLE_AXIS.AXIS_0;
            cmbAxisNo.SelectedIndex = 0;

            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 cmbAxisNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            eAxisNo = (eSINGLE_AXIS)(0x01 << cmbAxisNo.SelectedIndex);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            short ret = 0;
            ushort wDIStatus = 0;
            int LP = 0, EP = 0, Spd = 0;
            byte bDone = 0;
            ushort wStopStatus = 0;

            ret = C96F.i8196f_get_cmdcounter(bSlotNo, eAxisNo, ref LP);
            txtCmdCnt.Text = LP.ToString();

            ret = C96F.i8196f_get_enccounter(bSlotNo, eAxisNo, ref EP);
            txtEncCnt.Text = EP.ToString();

            ret = C96F.i8196f_get_speed(bSlotNo, eAxisNo, ref Spd);
            txtCurSpeed.Text = Spd.ToString();

            ret = C96F.i8196f_get_motion_done(bSlotNo, eAxisNo, ref bDone, ref wStopStatus);
            txtMotionSts.Text = "(" + bDone.ToString() + ")" + "0x" + wStopStatus.ToString("X4");

            ret = C96F.i8196f_get_mdi_status(bSlotNo, eAxisNo, ref wDIStatus);
            txtIOSts.Text = "0x" + wDIStatus.ToString("X4");

            if ((wDIStatus & 0x0008) != 0)
            { txtEMG.BackColor = Color.FromArgb(255, 0, 0); }//red 
            else
            { txtEMG.BackColor = Color.FromArgb(212, 212, 212); }//light gray

            if ((wDIStatus & 0x0020) != 0)
            { txtHOME.BackColor = Color.FromArgb(255, 0, 0); }//red 
            else
            { txtHOME.BackColor = Color.FromArgb(212, 212, 212); }//light gray

            if ((wDIStatus & 0x0040) != 0)
            { txtNHOME.BackColor = Color.FromArgb(255, 0, 0); }//red 
            else
            { txtNHOME.BackColor = Color.FromArgb(212, 212, 212); }//light gray

            if ((wDIStatus & 0x0100) != 0)
            { txtEZ.BackColor = Color.FromArgb(255, 0, 0); }//red 
            else
            { txtEZ.BackColor = Color.FromArgb(212, 212, 212); }//light gray

            if ((wDIStatus & 0x0002) != 0)
            { txtLMTP.BackColor = Color.FromArgb(255, 0, 0); }//red 
            else
            { txtLMTP.BackColor = Color.FromArgb(212, 212, 212); }//light gray

            if ((wDIStatus & 0x0004) != 0)
            { txtLMTM.BackColor = Color.FromArgb(255, 0, 0); }//red 
            else
            { txtLMTM.BackColor = Color.FromArgb(212, 212, 212); }//light gray
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            short ret = 0;
            int LogicPos = 0;
            int EncPos = 0;

            ret = C96F.i8196f_set_cmdcounter(bSlotNo, eAxisNo, LogicPos);
            ret = C96F.i8196f_set_enccounter(bSlotNo, eAxisNo, EncPos);        
        }

        private void btnSVON_Click(object sender, EventArgs e)
        {
            short ret = 0;
            eCHANNEL_STATE OnOff = 0;

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

        private void btnRALM_Click(object sender, EventArgs e)
        {
            short ret = 0;
            eCHANNEL_STATE OnOff = 0;

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

        private void btnMove_Click(object sender, EventArgs e)
        {
            short ret = 0;
            uint Speed = System.UInt32.Parse(txtSpeed.Text);
            uint Acc = System.UInt32.Parse(txtAcc.Text);
            int Dist = System.Int32.Parse(txtDist.Text);

            ret = C96F.i8196f_t_move(bSlotNo, eAxisNo, Speed / 10, Speed, Speed / 10, Acc, Acc, Dist);           
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            short ret = 0;

            // StopMode = 1: slow down then stop, 2: sudden stop
            ret = C96F.i8196f_stop_move(bSlotNo, eAxisNo, eSTOP_MODE.STOP_SLOWDOWN); 
        }

        private void btnSetHome_Click(object sender, EventArgs e)
        {
 	        short ret = 0;
	        eLOGIC_ACTIVE_LEVEL HomeLogic, SLDLogic, EZLogic;
	        byte HomeSteps = 0;
	        int Step4Offset = 0;

	        if (rdoHLogicH.Checked)
	        {HomeLogic = eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_HIGH;}
	        else
	        {HomeLogic = eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_LOW;}

	        if (rdoNHLogicH.Checked)
	        {SLDLogic = eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_HIGH;}
	        else
	        {SLDLogic = eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_LOW;}

	        if (rdoEZLogicH.Checked)
	        {EZLogic = eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_HIGH;}
	        else
	        {EZLogic = eLOGIC_ACTIVE_LEVEL.LOGIC_ACTIVE_LOW;}

	        if (chkHomeStep1.Checked)
	        {HomeSteps |= 0x01;}//AUTO_HOME_STEP1_ENABLE

	        if (chkHomeStep2.Checked)
	        {HomeSteps |= 0x04;}//AUTO_HOME_STEP2_ENABLE

	        if (chkHomeStep3.Checked)
	        {HomeSteps |= 0x10;}//AUTO_HOME_STEP3_ENABLE

	        if (chkHomeStep4.Checked)
	        {HomeSteps |= 0x40;}//AUTO_HOME_STEP4_ENABLE

	        Step4Offset = System.Int32.Parse(txtHomeOffset.Text);

            ret = C96F.i8196f_set_home_cfg(bSlotNo, eAxisNo, HomeLogic, SLDLogic, EZLogic, HomeSteps, Step4Offset);
		        
        }

        private void btnHomeMove_Click(object sender, EventArgs e)
        {
            short ret;

            uint StartSpeed = System.UInt32.Parse(txtHStrSpeed.Text);
	        uint Acceleration = System.UInt32.Parse(txtHAcc.Text);
	        uint Deceleration = System.UInt32.Parse(txtHDec.Text);
	        uint HighSpeed = System.UInt32.Parse(txtHNHSpeed.Text);
	        uint LowSpeed = System.UInt32.Parse(txtHHSpeed.Text);
	        eHOME_DIR  HomingDirection;


	        if (rdoHDirP.Checked)
	        {HomingDirection = eHOME_DIR.AUTO_HOME_FORWARD;}
	        else
	        {HomingDirection = eHOME_DIR.AUTO_HOME_REVERSE;}

	        ret = C96F.i8196f_home_start(bSlotNo, eAxisNo, StartSpeed, Acceleration, Deceleration,
                                HighSpeed, LowSpeed, HomingDirection);
        }

        private void btnStopHome_Click(object sender, EventArgs e)
        {
            short ret = 0;

            // StopMode = 1: slow down then stop, 2: sudden stop
            ret = C96F.i8196f_stop_move(bSlotNo, eAxisNo, eSTOP_MODE.STOP_SLOWDOWN); 

        }
    }
}