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

        PictureBox[,] picGroup;
        PictureBox[] picGDO;
        PictureBox[] picGDI;

        public Form1()
        {
            InitializeComponent();

            picGroup = new PictureBox[,] {{ picGroup0_0, picGroup0_1, picGroup0_2, picGroup0_3, picGroup0_4, picGroup0_5, picGroup0_6, picGroup0_7, picGroup0_8, picGroup0_9, picGroup0_10, picGroup0_11, picGroup0_12, picGroup0_13, picGroup0_14, picGroup0_15 },
                                     { picGroup1_0, picGroup1_1, picGroup1_2, picGroup1_3, picGroup1_4, picGroup1_5, picGroup1_6, picGroup1_7, picGroup1_8, picGroup1_9, picGroup1_10, picGroup1_11, picGroup1_12, picGroup1_13, picGroup1_14, picGroup1_15 },
                                     { picGroup2_0, picGroup2_1, picGroup2_2, picGroup2_3, picGroup2_4, picGroup2_5, picGroup2_6, picGroup2_7, picGroup2_8, picGroup2_9, picGroup2_10, picGroup2_11, picGroup2_12, picGroup2_13, picGroup2_14, picGroup2_15 },
                                     { picGroup3_0, picGroup3_1, picGroup3_2, picGroup3_3, picGroup3_4, picGroup3_5, picGroup3_6, picGroup3_7, picGroup3_8, picGroup3_9, picGroup3_10, picGroup3_11, picGroup3_12, picGroup3_13, picGroup3_14, picGroup3_15 },
                                     { picGroup4_0, picGroup4_1, picGroup4_2, picGroup4_3, picGroup4_4, picGroup4_5, picGroup4_6, picGroup4_7, picGroup4_8, picGroup4_9, picGroup4_10, picGroup4_11, picGroup4_12, picGroup4_13, picGroup4_14, picGroup4_15 },
                                     { picGroup5_0, picGroup5_1, picGroup5_2, picGroup5_3, picGroup5_4, picGroup5_5, picGroup5_6, picGroup5_7, picGroup5_8, picGroup5_9, picGroup5_10, picGroup5_11, picGroup5_12, picGroup5_13, picGroup5_14, picGroup5_15 },
                                     { picGroup6_0, picGroup6_1, picGroup6_2, picGroup6_3, picGroup6_4, picGroup6_5, picGroup6_6, picGroup6_7, picGroup6_8, picGroup6_9, picGroup6_10, picGroup6_11, picGroup6_12, picGroup6_13, picGroup6_14, picGroup6_15 },
                                     { picGroup7_0, picGroup7_1, picGroup7_2, picGroup7_3, picGroup7_4, picGroup7_5, picGroup7_6, picGroup7_7, picGroup7_8, picGroup7_9, picGroup7_10, picGroup7_11, picGroup7_12, picGroup7_13, picGroup7_14, picGroup7_15 },
                                     { picGroup8_0, picGroup8_1, picGroup8_2, picGroup8_3, picGroup8_4, picGroup8_5, picGroup8_6, picGroup8_7, picGroup8_8, picGroup8_9, picGroup8_10, picGroup8_11, picGroup8_12, picGroup8_13, picGroup8_14, picGroup8_15 },
                                     { picGroup9_0, picGroup9_1, picGroup9_2, picGroup9_3, picGroup9_4, picGroup9_5, picGroup9_6, picGroup9_7, picGroup9_8, picGroup9_9, picGroup9_10, picGroup9_11, picGroup9_12, picGroup9_13, picGroup9_14, picGroup9_15 },
                                     { picGroup10_0, picGroup10_1, picGroup10_2, picGroup10_3, picGroup10_4, picGroup10_5, picGroup10_6, picGroup10_7, picGroup10_8, picGroup10_9, picGroup10_10, picGroup10_11, picGroup10_12, picGroup10_13, picGroup10_14, picGroup10_15 },
                                     { picGroup11_0, picGroup11_1, picGroup11_2, picGroup11_3, picGroup11_4, picGroup11_5, picGroup11_6, picGroup11_7, picGroup11_8, picGroup11_9, picGroup11_10, picGroup11_11, picGroup11_12, picGroup11_13, picGroup11_14, picGroup11_15 },
                                     { picGroup12_0, picGroup12_1, picGroup12_2, picGroup12_3, picGroup12_4, picGroup12_5, picGroup12_6, picGroup12_7, picGroup12_8, picGroup12_9, picGroup12_10, picGroup12_11, picGroup12_12, picGroup12_13, picGroup12_14, picGroup12_15 },
                                     { picGroup13_0, picGroup13_1, picGroup13_2, picGroup13_3, picGroup13_4, picGroup13_5, picGroup13_6, picGroup13_7, picGroup13_8, picGroup13_9, picGroup13_10, picGroup13_11, picGroup13_12, picGroup13_13, picGroup13_14, picGroup13_15 },
                                     { picGroup14_0, picGroup14_1, picGroup14_2, picGroup14_3, picGroup14_4, picGroup14_5, picGroup14_6, picGroup14_7, picGroup14_8, picGroup14_9, picGroup14_10, picGroup14_11, picGroup14_12, picGroup14_13, picGroup14_14, picGroup14_15 },
                                     { picGroup15_0, picGroup15_1, picGroup15_2, picGroup15_3, picGroup15_4, picGroup15_5, picGroup15_6, picGroup15_7, picGroup15_8, picGroup15_9, picGroup15_10, picGroup15_11, picGroup15_12, picGroup15_13, picGroup15_14, picGroup15_15 }};

            picGDO = new PictureBox[] { picGDO0, picGDO1, picGDO2 };
            picGDI = new PictureBox[] { picGDI0, picGDI1, picGDI2, picGDI3, picGDI4, picGDI5, picGDI6, picGDI7, picGDI8, picGDI9, picGDI10, picGDI11 };

        }

        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)
                {
                    cmbSlotNo.Items.Add(i.ToString());
                    flgExistCard = true;

                    ret = C96F.i8196f_registration(i);
                    ret = C96F.i8196f_load_power_on_cfg(i, ePOWER_ON_SETTING.DEFAULT_POWER_ON_SETTING);

                }
            }

            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 cmbSlotNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            bSlotNo = System.Byte.Parse(cmbSlotNo.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = 0;
            int j = 0;
            short ret = 0;

            ushort DIModules = 0;
            ushort IOStatus = 0;

            //FRnet DIO
            
            for (i = 0; i < 8; i++)//DO Group 0 ~ 7
            {
                ret = C96F.i8196f_get_frnet_group_do(bSlotNo, (eFRNET_DO_ADDR)i, ref IOStatus);
                for (j = 0; j < 16; j++)//Channel 0 ~15
                {
                    if ((IOStatus & (0x0001 << j)) != 0)
                    { picGroup[i, j].BackColor = Color.FromArgb(0, 255, 0); }//green
                    else
                    { picGroup[i, j].BackColor = Color.FromArgb(212, 212, 212); }//light gray
                }
            }

            ret = C96F.i8196f_scan_frnet_di(bSlotNo, ref DIModules);
            
            for (i = 8; i < 16; i++)//DI Group 8 ~ 15
            {
                ret = C96F.i8196f_get_frnet_group_di(bSlotNo, (eFRNET_DI_ADDR)i, ref IOStatus);
                if ((DIModules & (0x0001 << (i - 8))) != 0)//bit0 ~ bit7
                {
                    for (j = 0; j < 16; j++)//Channel 0 ~15
                    {
                        if ((IOStatus & (0x0001 << j)) != 0)
                        { picGroup[i, j].BackColor = Color.FromArgb(255, 0, 0); }//red
                        else
                        { picGroup[i, j].BackColor = Color.FromArgb(212, 212, 212); }//light gray
                    }
                }
                else
                {
                    for (j = 0; j < 16; j++)//Channel 0 ~15
                    { picGroup[i, j].BackColor = Color.FromArgb(212, 212, 212); }
                }
            }

            //GPIO
            ret = C96F.i8196f_get_all_general_do(bSlotNo, ref IOStatus);
            for (i = 0; i < 3; i++)
            {
                if ((IOStatus & (0x0001 << i)) != 0)
                { picGDO[i].BackColor = Color.FromArgb(0, 255, 0); }
                else
                { picGDO[i].BackColor = Color.FromArgb(212, 212, 212); }
            }

            ret = C96F.i8196f_get_all_general_di(bSlotNo, ref IOStatus);
            for (i = 0; i < 12; i++)
            {
                if ((IOStatus & (0x0001 << i)) != 0)
                { picGDI[i].BackColor = Color.FromArgb(255, 0, 0); }
                else
                { picGDI[i].BackColor = Color.FromArgb(212, 212, 212); }
            }

        }

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

            short ret = 0;
            ushort GroupNo = (ushort)((index >> 4) & 0x000F);
            ushort Channel = (ushort)(index & 0x000F);
            ushort Status = 0;

            ret = C96F.i8196f_get_frnet_group_do(bSlotNo, (eFRNET_DO_ADDR)GroupNo, ref Status);

            if ((Status & (0x0001 << Channel)) != 0)
            { Status &= (ushort)(~(0x0001 << Channel)); }
            else
            { Status |= (ushort)(0x0001 << Channel); }

            ret = C96F.i8196f_set_frnet_group_do(bSlotNo, (eFRNET_DO_ADDR)GroupNo, Status);
        }

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

            short ret = 0;
            eCHANNEL_STATE OnOff = 0;
            eGENERAL_DO Channel = (eGENERAL_DO)index;

            ret = C96F.i8196f_get_general_do(bSlotNo, Channel, ref OnOff);
            if (OnOff == eCHANNEL_STATE.ON)
            { ret = C96F.i8196f_set_general_do(bSlotNo, Channel, eCHANNEL_STATE.OFF); }
            else
            { ret = C96F.i8196f_set_general_do(bSlotNo, Channel, eCHANNEL_STATE.ON); }
        }


    }
}