
//  V1.00 : 21/04/11 : MB-3CC : Creation
//  V1.01 : 31/10/14 : DB-3CC : Modif type variables en accord avec le wrapper modifié en 1.03


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;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int err18A = 0;
            uint NumeroSeries = 0;
            int Num = 1;
            short Ana_Init=0;
            err18A = TCUSB18A.TCUSB18A_OpenN(Num, ref NumeroSeries);
            if (err18A != 0)
            {
                if (err18A == 1) MessageBox.Show("Numero de serie incorrect");
                if (err18A == 2) MessageBox.Show("N° de module incorrect");
                if (err18A == 3) MessageBox.Show("Pas de module TC/USB-18A détecté");
                if (err18A == 4) MessageBox.Show("Module non initialisé");
                Close();
            }
            Txt_serie.Text = System.Convert.ToString(NumeroSeries);

            TCUSB18A.TCUSB18A_AnaOut(1, 1,  Ana_Init);
            TCUSB18A.TCUSB18A_AnaOut(1, 2,  Ana_Init);
            
            Btn_S1.BackColor = Color.Green;
            Btn_S2.BackColor = Color.Green;
            Btn_S3.BackColor = Color.Green;
            Btn_S4.BackColor = Color.Green;
            timer1.Enabled = true;
        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            TCUSB18A.TCUSB18A_Close(1);
        }

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

            TCUSB18A.TCUSB18A_Refresh(1);
            TCUSB18A.TCUSB18A_Input(1, ref D_input);

            TCUSB18A.TCUSB18A_Counter(1, 1, ref D_Temp); Txt_E1.Text = System.Convert.ToString(D_Temp);
            TCUSB18A.TCUSB18A_Counter(1, 2, ref D_Temp); Txt_E2.Text = System.Convert.ToString(D_Temp);
            TCUSB18A.TCUSB18A_Counter(1, 3, ref D_Temp); Txt_E3.Text = System.Convert.ToString(D_Temp);
            TCUSB18A.TCUSB18A_Counter(1, 4, ref D_Temp); Txt_E4.Text = System.Convert.ToString(D_Temp);
            TCUSB18A.TCUSB18A_Counter(1, 5, ref D_Temp); Txt_E5.Text = System.Convert.ToString(D_Temp);
            TCUSB18A.TCUSB18A_Counter(1, 6, ref D_Temp); Txt_E6.Text = System.Convert.ToString(D_Temp);
            TCUSB18A.TCUSB18A_Counter(1, 7, ref D_Temp); Txt_E7.Text = System.Convert.ToString(D_Temp);
            TCUSB18A.TCUSB18A_Counter(1, 8, ref D_Temp); Txt_E8.Text = System.Convert.ToString(D_Temp);
            
            TCUSB18A.TCUSB18A_AnaInVolts(1, 1, ref D_Ana); Txt_Ana1.Value = System.Convert.ToDecimal(D_Ana);
            TCUSB18A.TCUSB18A_AnaInVolts(1, 2, ref D_Ana); Txt_Ana2.Value = System.Convert.ToDecimal(D_Ana);
            TCUSB18A.TCUSB18A_AnaInVolts(1, 3, ref D_Ana); Txt_Ana3.Value = System.Convert.ToDecimal(D_Ana);
            TCUSB18A.TCUSB18A_AnaInVolts(1, 4, ref D_Ana); Txt_Ana4.Value = System.Convert.ToDecimal(D_Ana);

            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;
        }

        private void Btn_Ana1_ValueChanged(object sender, EventArgs e)
        {
            double D_ana = 0;
            D_ana =System.Convert.ToDouble( Btn_Ana1.Value);
            TCUSB18A.TCUSB18A_AnaOutVolts(1, 1,D_ana);
            TCUSB18A.TCUSB18A_Refresh(1);

        }

        private void Btn_Ana2_ValueChanged(object sender, EventArgs e)
        {
            double D_ana = 0;
            D_ana = System.Convert.ToDouble(Btn_Ana2.Value);
            TCUSB18A.TCUSB18A_AnaOutVolts(1, 2, D_ana);
            TCUSB18A.TCUSB18A_Refresh(1);
        }

        private void Btn_S1_Click(object sender, EventArgs e)
        {
            long err18A = 0;
            D_output = System.Convert.ToByte(D_output ^ 0x01);
            err18A = TCUSB18A.TCUSB18A_Output(1, D_output);
            TCUSB18A.TCUSB18A_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 err18A = 0;
            D_output = System.Convert.ToByte(D_output ^ 0x02);
            err18A = TCUSB18A.TCUSB18A_Output(1, D_output);
            TCUSB18A.TCUSB18A_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 err18A = 0;
            D_output = System.Convert.ToByte(D_output ^ 0x04);
            err18A = TCUSB18A.TCUSB18A_Output(1, D_output);
            TCUSB18A.TCUSB18A_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 err18A = 0;
            D_output = System.Convert.ToByte(D_output ^ 0x08);
            err18A = TCUSB18A.TCUSB18A_Output(1, D_output);
            TCUSB18A.TCUSB18A_Refresh(1);
            if (Btn_S4.BackColor == Color.Lime) Btn_S4.BackColor = Color.Green; else Btn_S4.BackColor = Color.Lime;
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }
}
