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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ushort D_output = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int err48IO = 0;
            uint NumeroSeries = 0;
            int Num = 1;
            err48IO = TCUSB48IO.TCUSB48IO_OpenN(Num, ref NumeroSeries);
            if (err48IO != 0)
            {
                if (err48IO == 1) MessageBox.Show("Numero de serie incorrect");
                if (err48IO == 2) MessageBox.Show("N° de module incorrect");
                if (err48IO == 3) MessageBox.Show("Pas de module TC/USB-48IO détecté");
                if (err48IO == 4) MessageBox.Show("Module non initialisé");
                if (err48IO > 4) MessageBox.Show("Erreur inconue n°"+System.Convert.ToString(err48IO));
                Close();
            }
            Txt_serie.Text = System.Convert.ToString(NumeroSeries);

            Btn_S1.BackColor = Color.Green;
            Btn_S2.BackColor = Color.Green;
            Btn_S3.BackColor = Color.Green;
            Btn_S4.BackColor = Color.Green;
            Btn_S5.BackColor = Color.Green;
            Btn_S6.BackColor = Color.Green;
            Btn_S7.BackColor = Color.Green;
            Btn_S8.BackColor = Color.Green;
            Btn_S9.BackColor = Color.Green;
            Btn_S10.BackColor = Color.Green;
            Btn_S11.BackColor = Color.Green;
            Btn_S12.BackColor = Color.Green;
            Btn_S13.BackColor = Color.Green;
            Btn_S14.BackColor = Color.Green;
            Btn_S15.BackColor = Color.Green;
            Btn_S16.BackColor = Color.Green;

        }

        private void Btn_S1_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0001);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S1.BackColor == Color.Lime) Btn_S1.BackColor = Color.Green; else Btn_S1.BackColor = Color.Lime;

        }

        private void Btn_S2_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0002);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S2.BackColor == Color.Lime) Btn_S2.BackColor = Color.Green; else Btn_S2.BackColor = Color.Lime;
        }

        private void Btn_S3_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0004);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S3.BackColor == Color.Lime) Btn_S3.BackColor = Color.Green; else Btn_S3.BackColor = Color.Lime;
        }

        private void Btn_S4_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0008);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S4.BackColor == Color.Lime) Btn_S4.BackColor = Color.Green; else Btn_S4.BackColor = Color.Lime;
        }

        private void Btn_S5_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0010);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S5.BackColor == Color.Lime) Btn_S5.BackColor = Color.Green; else Btn_S5.BackColor = Color.Lime;
        }

        private void Btn_S6_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0020);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S6.BackColor == Color.Lime) Btn_S6.BackColor = Color.Green; else Btn_S6.BackColor = Color.Lime;
        }

        private void Btn_S7_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0040);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S7.BackColor == Color.Lime) Btn_S7.BackColor = Color.Green; else Btn_S7.BackColor = Color.Lime;
        }

        private void Btn_S8_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0080);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S8.BackColor == Color.Lime) Btn_S8.BackColor = Color.Green; else Btn_S8.BackColor = Color.Lime;
        }

        private void Btn_S9_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0100);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S9.BackColor == Color.Lime) Btn_S9.BackColor = Color.Green; else Btn_S9.BackColor = Color.Lime;
        }

        private void Btn_S10_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0200);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S10.BackColor == Color.Lime) Btn_S10.BackColor = Color.Green; else Btn_S10.BackColor = Color.Lime;
        }

        private void Btn_S11_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0400);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S11.BackColor == Color.Lime) Btn_S11.BackColor = Color.Green; else Btn_S11.BackColor = Color.Lime;
        }

        private void Btn_S12_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x0800);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S12.BackColor == Color.Lime) Btn_S12.BackColor = Color.Green; else Btn_S12.BackColor = Color.Lime;
        }

        private void Btn_S13_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x1000);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S13.BackColor == Color.Lime) Btn_S13.BackColor = Color.Green; else Btn_S13.BackColor = Color.Lime;
        }

        private void Btn_S14_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x2000);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S14.BackColor == Color.Lime) Btn_S14.BackColor = Color.Green; else Btn_S14.BackColor = Color.Lime;

        }

        private void Btn_S15_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x4000);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S15.BackColor == Color.Lime) Btn_S15.BackColor = Color.Green; else Btn_S15.BackColor = Color.Lime;
        }

        private void Btn_S16_Click(object sender, EventArgs e)
        {
            long err48IO = 0;
            D_output = System.Convert.ToUInt16(D_output ^ 0x8000);
            err48IO = TCUSB48IO.TCUSB48IO_Output(1, D_output);
            TCUSB48IO.TCUSB48IO_Refresh(1);
            if (Btn_S16.BackColor == Color.Lime) Btn_S16.BackColor = Color.Green; else Btn_S16.BackColor = Color.Lime;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            uint D_input = 0;
            byte D_Temp = 0;

            TCUSB48IO.TCUSB48IO_Refresh(1);
            TCUSB48IO.TCUSB48IO_Input(1, ref D_input);

            TCUSB48IO.TCUSB48IO_Counter(1, 1, ref D_Temp); Txt_E1.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 2, ref D_Temp); Txt_E2.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 3, ref D_Temp); Txt_E3.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 4, ref D_Temp); Txt_E4.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 5, ref D_Temp); Txt_E5.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 6, ref D_Temp); Txt_E6.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 7, ref D_Temp); Txt_E7.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 8, ref D_Temp); Txt_E8.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 9, ref D_Temp); Txt_E9.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 10, ref D_Temp); Txt_E10.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 11, ref D_Temp); Txt_E11.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 12, ref D_Temp); Txt_E12.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 13, ref D_Temp); Txt_E13.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 14, ref D_Temp); Txt_E14.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 15, ref D_Temp); Txt_E15.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 16, ref D_Temp); Txt_E16.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 17, ref D_Temp); Txt_E17.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 18, ref D_Temp); Txt_E18.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 19, ref D_Temp); Txt_E19.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 20, ref D_Temp); Txt_E20.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 21, ref D_Temp); Txt_E21.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 22, ref D_Temp); Txt_E22.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 23, ref D_Temp); Txt_E23.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 24, ref D_Temp); Txt_E24.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 25, ref D_Temp); Txt_E25.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 26, ref D_Temp); Txt_E26.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 27, ref D_Temp); Txt_E27.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 28, ref D_Temp); Txt_E28.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 29, ref D_Temp); Txt_E29.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 30, ref D_Temp); Txt_E30.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 31, ref D_Temp); Txt_E31.Text = System.Convert.ToString(D_Temp);
            TCUSB48IO.TCUSB48IO_Counter(1, 32, ref D_Temp); Txt_E32.Text = System.Convert.ToString(D_Temp);

            if ((D_input & 0x00000001) > 0) Btn_E1.BackColor = Color.Lime; else Btn_E1.BackColor = Color.Green;
            if ((D_input & 0x00000002) > 0) Btn_E2.BackColor = Color.Lime; else Btn_E2.BackColor = Color.Green;
            if ((D_input & 0x00000004) > 0) Btn_E3.BackColor = Color.Lime; else Btn_E3.BackColor = Color.Green;
            if ((D_input & 0x00000008) > 0) Btn_E4.BackColor = Color.Lime; else Btn_E4.BackColor = Color.Green;
            if ((D_input & 0x00000010) > 0) Btn_E5.BackColor = Color.Lime; else Btn_E5.BackColor = Color.Green;
            if ((D_input & 0x00000020) > 0) Btn_E6.BackColor = Color.Lime; else Btn_E6.BackColor = Color.Green;
            if ((D_input & 0x00000040) > 0) Btn_E7.BackColor = Color.Lime; else Btn_E7.BackColor = Color.Green;
            if ((D_input & 0x00000080) > 0) Btn_E8.BackColor = Color.Lime; else Btn_E8.BackColor = Color.Green;

            if ((D_input & 0x00000100) > 0) Btn_E9.BackColor = Color.Lime; else Btn_E9.BackColor = Color.Green;
            if ((D_input & 0x00000200) > 0) Btn_E10.BackColor = Color.Lime; else Btn_E10.BackColor = Color.Green;
            if ((D_input & 0x00000400) > 0) Btn_E11.BackColor = Color.Lime; else Btn_E11.BackColor = Color.Green;
            if ((D_input & 0x00000800) > 0) Btn_E12.BackColor = Color.Lime; else Btn_E12.BackColor = Color.Green;
            if ((D_input & 0x00001000) > 0) Btn_E13.BackColor = Color.Lime; else Btn_E13.BackColor = Color.Green;
            if ((D_input & 0x00002000) > 0) Btn_E14.BackColor = Color.Lime; else Btn_E14.BackColor = Color.Green;
            if ((D_input & 0x00004000) > 0) Btn_E15.BackColor = Color.Lime; else Btn_E15.BackColor = Color.Green;
            if ((D_input & 0x00008000) > 0) Btn_E16.BackColor = Color.Lime; else Btn_E16.BackColor = Color.Green;

            if ((D_input & 0x00010000) > 0) Btn_E17.BackColor = Color.Lime; else Btn_E17.BackColor = Color.Green;
            if ((D_input & 0x00020000) > 0) Btn_E18.BackColor = Color.Lime; else Btn_E18.BackColor = Color.Green;
            if ((D_input & 0x00040000) > 0) Btn_E19.BackColor = Color.Lime; else Btn_E19.BackColor = Color.Green;
            if ((D_input & 0x00080000) > 0) Btn_E20.BackColor = Color.Lime; else Btn_E20.BackColor = Color.Green;
            if ((D_input & 0x00100000) > 0) Btn_E21.BackColor = Color.Lime; else Btn_E21.BackColor = Color.Green;
            if ((D_input & 0x00200000) > 0) Btn_E22.BackColor = Color.Lime; else Btn_E22.BackColor = Color.Green;
            if ((D_input & 0x00400000) > 0) Btn_E23.BackColor = Color.Lime; else Btn_E23.BackColor = Color.Green;
            if ((D_input & 0x00800000) > 0) Btn_E24.BackColor = Color.Lime; else Btn_E24.BackColor = Color.Green;

            if ((D_input & 0x01000000) > 0) Btn_E25.BackColor = Color.Lime; else Btn_E25.BackColor = Color.Green;
            if ((D_input & 0x02000000) > 0) Btn_E26.BackColor = Color.Lime; else Btn_E26.BackColor = Color.Green;
            if ((D_input & 0x04000000) > 0) Btn_E27.BackColor = Color.Lime; else Btn_E27.BackColor = Color.Green;
            if ((D_input & 0x08000000) > 0) Btn_E28.BackColor = Color.Lime; else Btn_E28.BackColor = Color.Green;
            if ((D_input & 0x10000000) > 0) Btn_E29.BackColor = Color.Lime; else Btn_E29.BackColor = Color.Green;
            if ((D_input & 0x20000000) > 0) Btn_E30.BackColor = Color.Lime; else Btn_E30.BackColor = Color.Green;
            if ((D_input & 0x40000000) > 0) Btn_E31.BackColor = Color.Lime; else Btn_E31.BackColor = Color.Green;
            if ((D_input & 0x80000000) > 0) Btn_E32.BackColor = Color.Lime; else Btn_E32.BackColor = Color.Green;

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            TCUSB48IO.TCUSB48IO_Close(1);
        }
    }
}
