//  V1.00 : 21/04/11 : MB-3CC : Creation 
//  V1.01 : 30/10/14 : DB-3CC : Modif en accord avec la nouvelle version du wrapper, quelques fonctionnalités en plus.



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
    {
        byte D_output = 0;
        bool ModuleOpened = false;
        long err16IO = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
                int err16IO = 0;
                uint NumeroSeries = 0;
                int Num = 1;
                err16IO = TCUSB16IOx64.TCUSB16IO_OpenN(Num, ref NumeroSeries);
                if (err16IO != 0)
                {
                    if (err16IO == 1) MessageBox.Show("Numero de serie incorrect");
                    if (err16IO == 2) MessageBox.Show("N° de module incorrect");
                    if (err16IO == 3) MessageBox.Show("Pas de module TC/USB-16IO détecté");
                    if (err16IO == 4) MessageBox.Show("Module non initialisé");
                    Close();
                }
                ModuleOpened = true;
                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;

                timer1.Enabled = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ModuleOpened)
                TCUSB16IOx64.TCUSB16IO_Close(1);
        }

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

            TCUSB16IOx64.TCUSB16IO_Refresh(1);
            TCUSB16IOx64.TCUSB16IO_Input(1, ref D_input);

            TCUSB16IOx64.TCUSB16IO_Counter(1, 1, ref D_Temp); Txt_E1.Text = System.Convert.ToString(D_Temp);
            TCUSB16IOx64.TCUSB16IO_Counter(1, 2, ref D_Temp); Txt_E2.Text = System.Convert.ToString(D_Temp);
            TCUSB16IOx64.TCUSB16IO_Counter(1, 3, ref D_Temp); Txt_E3.Text = System.Convert.ToString(D_Temp);
            TCUSB16IOx64.TCUSB16IO_Counter(1, 4, ref D_Temp); Txt_E4.Text = System.Convert.ToString(D_Temp);
            TCUSB16IOx64.TCUSB16IO_Counter(1, 5, ref D_Temp); Txt_E5.Text = System.Convert.ToString(D_Temp);
            TCUSB16IOx64.TCUSB16IO_Counter(1, 6, ref D_Temp); Txt_E6.Text = System.Convert.ToString(D_Temp);
            TCUSB16IOx64.TCUSB16IO_Counter(1, 7, ref D_Temp); Txt_E7.Text = System.Convert.ToString(D_Temp);
            TCUSB16IOx64.TCUSB16IO_Counter(1, 8, ref D_Temp); Txt_E8.Text = System.Convert.ToString(D_Temp);

            if ((D_input & 0x0001) > 0) Btn_E1.BackColor = Color.Lime; else Btn_E1.BackColor = Color.Green;
            if ((D_input & 0x0002) > 0) Btn_E2.BackColor = Color.Lime; else Btn_E2.BackColor = Color.Green;
            if ((D_input & 0x0004) > 0) Btn_E3.BackColor = Color.Lime; else Btn_E3.BackColor = Color.Green;
            if ((D_input & 0x0008) > 0) Btn_E4.BackColor = Color.Lime; else Btn_E4.BackColor = Color.Green;
            if ((D_input & 0x0010) > 0) Btn_E5.BackColor = Color.Lime; else Btn_E5.BackColor = Color.Green;
            if ((D_input & 0x0020) > 0) Btn_E6.BackColor = Color.Lime; else Btn_E6.BackColor = Color.Green;
            if ((D_input & 0x0040) > 0) Btn_E7.BackColor = Color.Lime; else Btn_E7.BackColor = Color.Green;
            if ((D_input & 0x0080) > 0) Btn_E8.BackColor = Color.Lime; else Btn_E8.BackColor = Color.Green;

            TCUSB16IOx64.TCUSB16IO_Pos(1,ref DataPos);
            labelPos.Text = DataPos.ToString("000000000000");

			TCUSB16IOx64.TCUSB16IO_Latch(1,1,ref DataPos);
			labelLatch1.Text = DataPos.ToString("000000000000");
			TCUSB16IOx64.TCUSB16IO_Latch(1,2,ref DataPos);
			labelLatch2.Text = DataPos.ToString("000000000000");
		}

        private void Btn_S1_Click(object sender, EventArgs e)
        {
            D_output = System.Convert.ToByte(D_output ^ 0x01);
            err16IO = TCUSB16IOx64.TCUSB16IO_Output(1, D_output);
            TCUSB16IOx64.TCUSB16IO_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)
        {
            D_output = System.Convert.ToByte(D_output ^ 0x02);
            err16IO = TCUSB16IOx64.TCUSB16IO_Output(1, D_output);
            TCUSB16IOx64.TCUSB16IO_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)
        {
            D_output = System.Convert.ToByte(D_output ^ 0x04);
            err16IO = TCUSB16IOx64.TCUSB16IO_Output(1, D_output);
            TCUSB16IOx64.TCUSB16IO_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)
        {
            D_output = System.Convert.ToByte(D_output ^ 0x08);
            err16IO = TCUSB16IOx64.TCUSB16IO_Output(1, D_output);
            TCUSB16IOx64.TCUSB16IO_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)
        {
            D_output = System.Convert.ToByte(D_output ^ 0x10);
            err16IO = TCUSB16IOx64.TCUSB16IO_Output(1, D_output);
            TCUSB16IOx64.TCUSB16IO_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)
        {
            D_output = System.Convert.ToByte(D_output ^ 0x20);
            err16IO = TCUSB16IOx64.TCUSB16IO_Output(1, D_output);
            TCUSB16IOx64.TCUSB16IO_Refresh(1);
            if (Btn_S6.BackColor == Color.Lime) Btn_S6.BackColor = Color.Green; else Btn_S6.BackColor = Color.Lime;
        }

        private void buttonRaz_Click(object sender, EventArgs e)
        {
            TCUSB16IOx64.TCUSB16IO_ResetPos(1);
        }


		Byte ConfigLatch1 = 0, ConfigLatch2 = 0;
		private void comboBoxLatch1_SelectedIndexChanged(object sender, EventArgs e)
		{
			ConfigLatch1 = Convert.ToByte(comboBoxLatch1.SelectedIndex);
			TCUSB16IOx64.TCUSB16IO_ConfigLatch(1, Convert.ToByte((ConfigLatch2 & 0xf0) | (ConfigLatch1 & 0x0f)));
		}

		private void comboBoxLatch2_SelectedIndexChanged(object sender, EventArgs e)
		{
			ConfigLatch2 = Convert.ToByte(comboBoxLatch2.SelectedIndex << 4);
			TCUSB16IOx64.TCUSB16IO_ConfigLatch(1, Convert.ToByte((ConfigLatch2 & 0xf0) | (ConfigLatch1 & 0x0f)));
		}
	}
}
