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;

namespace Test_TCUSB48IO
{
    public partial class Form_48IO : Form
    {
        //Variable global
        int[] Etat_Sortie = new int[17];
        
        public Form_48IO()
        {
            InitializeComponent();
        }
        private void Form_48IO_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 = TCUSB48IOx64.TCUSB48IO_OpenN(1, ref num);
            if (retour == 0) txt_num.Text = Convert.ToString(num);
        }


        private void btn_O_Click(object sender, EventArgs e)
        {
            int Voie=Convert.ToInt32((sender as Button).Tag);
            Etat_Sortie[Voie] = Etat_Sortie[Voie] ^ 1;
            TCUSB48IOx64.TCUSB48IO_BitOutput(1, Voie, Etat_Sortie[Voie]);
            TCUSB48IOx64.TCUSB48IO_Refresh(1);
        }

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

            Boolean Etat_inter = false;

            TCUSB48IOx64.TCUSB48IO_Refresh(1);
            TCUSB48IOx64.TCUSB48IO_Input(1, ref Etat_Entree);

            for (i = 1; i < 33; i++)
            {
                TCUSB48IOx64.TCUSB48IO_Counter(1, i, ref Etat_Counter[i]);
                Etat_inter = (Etat_Entree & (1 << (i-1))) > 0;
                if (Etat_inter) Color_Entree[i] = DemoTCUSB48IO.Properties.Resources.vert5050; else Color_Entree[i] = DemoTCUSB48IO.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]);
            Counter_I09.Text = Convert.ToString(Etat_Counter[9]);
            Counter_I10.Text = Convert.ToString(Etat_Counter[10]);
            Counter_I11.Text = Convert.ToString(Etat_Counter[11]);
            Counter_I12.Text = Convert.ToString(Etat_Counter[12]);
            Counter_I13.Text = Convert.ToString(Etat_Counter[13]);
            Counter_I14.Text = Convert.ToString(Etat_Counter[14]);
            Counter_I15.Text = Convert.ToString(Etat_Counter[15]);
            Counter_I16.Text = Convert.ToString(Etat_Counter[16]);
            Counter_I17.Text = Convert.ToString(Etat_Counter[17]);
            Counter_I18.Text = Convert.ToString(Etat_Counter[18]);
            Counter_I19.Text = Convert.ToString(Etat_Counter[19]);
            Counter_I20.Text = Convert.ToString(Etat_Counter[20]);
            Counter_I21.Text = Convert.ToString(Etat_Counter[21]);
            Counter_I22.Text = Convert.ToString(Etat_Counter[22]);
            Counter_I23.Text = Convert.ToString(Etat_Counter[23]);
            Counter_I24.Text = Convert.ToString(Etat_Counter[24]);
            Counter_I25.Text = Convert.ToString(Etat_Counter[25]);
            Counter_I26.Text = Convert.ToString(Etat_Counter[26]);
            Counter_I27.Text = Convert.ToString(Etat_Counter[27]);
            Counter_I28.Text = Convert.ToString(Etat_Counter[28]);
            Counter_I29.Text = Convert.ToString(Etat_Counter[29]);
            Counter_I30.Text = Convert.ToString(Etat_Counter[30]);
            Counter_I31.Text = Convert.ToString(Etat_Counter[31]);
            Counter_I32.Text = Convert.ToString(Etat_Counter[32]);

            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];
            Shape_I09.Image = Color_Entree[9];
            Shape_I10.Image = Color_Entree[10];
            Shape_I11.Image = Color_Entree[11];
            Shape_I12.Image = Color_Entree[12];
            Shape_I13.Image = Color_Entree[13];
            Shape_I14.Image = Color_Entree[14];
            Shape_I15.Image = Color_Entree[15];
            Shape_I16.Image = Color_Entree[16];
            Shape_I17.Image = Color_Entree[17];
            Shape_I18.Image = Color_Entree[18];
            Shape_I19.Image = Color_Entree[19];
            Shape_I20.Image = Color_Entree[20];
            Shape_I21.Image = Color_Entree[21];
            Shape_I22.Image = Color_Entree[22];
            Shape_I23.Image = Color_Entree[23];
            Shape_I24.Image = Color_Entree[24];
            Shape_I25.Image = Color_Entree[25];
            Shape_I26.Image = Color_Entree[26];
            Shape_I27.Image = Color_Entree[27];
            Shape_I28.Image = Color_Entree[28];
            Shape_I29.Image = Color_Entree[29];
            Shape_I30.Image = Color_Entree[30];
            Shape_I31.Image = Color_Entree[31];
            Shape_I32.Image = Color_Entree[32];


        }

        private void btn_wdg_onoff_Click(object sender, EventArgs e)
        {
            if (btn_wdg_onoff.Text.Contains("WDG ON"))
            {
                TCUSB48IOx64.TCUSB48IO_WdgRun(1);
                btn_wdg_onoff.Text="WDG OFF";
            }
            else 
            {
                TCUSB48IOx64.TCUSB48IO_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;
            }
        }
    }
}
