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;
using System.Runtime.InteropServices;
using System.Resources;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //Variable global
        int[] Etat_Sortie = new int[5];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btn_open_Click(object sender, EventArgs e)
        {
            //Ouverture du premier module 48IO dispo
            uint num = 0;
            int retour = 0;

            retour = Test_TCUSB18A.TCUSB18A.TCUSB18A_OpenN(1, ref num);
            if (retour == 0) txt_num.Text = Convert.ToString(num);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Double Data=0;
            UInt32 Etat_Entree = 0;
            Image[] Color_Entree = new Image[33];
            byte[] Etat_Counter = new byte[33];
            byte i = 0;
            Boolean Etat_inter = false;

            Test_TCUSB18A.TCUSB18A.TCUSB18A_Refresh(1);
            Test_TCUSB18A.TCUSB18A.TCUSB18A_Input(1, ref Etat_Entree);

            Test_TCUSB18A.TCUSB18A.TCUSB18A_AnaInVolts(1, 1, ref Data);
            ADC1.Text = Convert.ToString(Data);
            Test_TCUSB18A.TCUSB18A.TCUSB18A_AnaInVolts(1, 2, ref Data);
            ADC2.Text = Convert.ToString(Data);
            Test_TCUSB18A.TCUSB18A.TCUSB18A_AnaInVolts(1, 3, ref Data);
            ADC3.Text = Convert.ToString(Data);
            Test_TCUSB18A.TCUSB18A.TCUSB18A_AnaInVolts(1, 4, ref Data);
            ADC4.Text = Convert.ToString(Data);

            for (i = 1; i < 9; i++)
            {
                Test_TCUSB18A.TCUSB18A.TCUSB18A_Counter(1, i, ref Etat_Counter[i]);
                Etat_inter = (Etat_Entree & (1 << (i - 1))) > 0;

                if (Etat_inter) Color_Entree[i] = DemoTCUSB18A.Properties.Resources.vert5050; else Color_Entree[i] = DemoTCUSB18A.Properties.Resources.rouge5050;
                
                
            }

            Counter_I01.Text = Convert.ToString(Etat_Counter[1]);
            Counter_I02.Text = Convert.ToString(Etat_Counter[2]);
            Counter_I03.Text = Convert.ToString(Etat_Counter[3]);
            Counter_I04.Text = Convert.ToString(Etat_Counter[4]);
            Counter_I05.Text = Convert.ToString(Etat_Counter[5]);
            Counter_I06.Text = Convert.ToString(Etat_Counter[6]);
            Counter_I07.Text = Convert.ToString(Etat_Counter[7]);
            Counter_I08.Text = Convert.ToString(Etat_Counter[8]);
            Shape_I01.Image = Color_Entree[1];
            Shape_I02.Image = Color_Entree[2];
            Shape_I03.Image = Color_Entree[3];
            Shape_I04.Image = Color_Entree[4];
            Shape_I05.Image = Color_Entree[5];
            Shape_I06.Image = Color_Entree[6];
            Shape_I07.Image = Color_Entree[7];
            Shape_I08.Image = Color_Entree[8];
        }

        private void btn_wdg_onoff_Click(object sender, EventArgs e)
        {
            if (btn_wdg_onoff.Text.Contains("WDG ON"))
            {
                Test_TCUSB18A.TCUSB18A.TCUSB18A_WdgRun(1);
                btn_wdg_onoff.Text = "WDG OFF";
            }
            else
            {
                Test_TCUSB18A.TCUSB18A.TCUSB18A_WdgStop(1);
                btn_wdg_onoff.Text = "WDG ON";
            }
        }

        private void btn_timer_onoff_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                btn_timer_onoff.Text = "TIMER ON";
                timer1.Enabled = false;
            }
            else
            {
                btn_timer_onoff.Text = "TIMER OFF";
                timer1.Enabled = true;
            }
        }

        private void btn_wdg_rearm_Click(object sender, EventArgs e)
        {
            Test_TCUSB18A.TCUSB18A.TCUSB18A_WdgRearm(8);
        }

        private void btn_O_Click(object sender, EventArgs e)
        {
            int Voie = Convert.ToInt32((sender as Button).Tag);
            Etat_Sortie[Voie] = Etat_Sortie[Voie] ^ 1;
            Test_TCUSB18A.TCUSB18A.TCUSB18A_BitOutput(1, Voie, Etat_Sortie[Voie]);
            Test_TCUSB18A.TCUSB18A.TCUSB18A_Refresh(1);
        }

        private void btn_DAC_1_Click(object sender, EventArgs e)
        {
            Test_TCUSB18A.TCUSB18A.TCUSB18A_AnaOutVolts(1, 1, Convert.ToDouble(DAC_1.Value));
            Test_TCUSB18A.TCUSB18A.TCUSB18A_Refresh(1);
        }

        private void btn_DAC_2_Click(object sender, EventArgs e)
        {
            Test_TCUSB18A.TCUSB18A.TCUSB18A_AnaOutVolts(1, 2, Convert.ToDouble(DAC_2.Value));
            Test_TCUSB18A.TCUSB18A.TCUSB18A_Refresh(1);
        }

        private void button1_Click(object sender, EventArgs e)
        {


        }
    }
}
