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

namespace DIO
{
	public partial class DIOSample : Form
	{
		UInt32 iBoard;

		public DIOSample()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			if (ListDetectedBoards())
			{
				DIOPanel.Visible = true;
				timer1.Enabled = true;
			}
		}

		private bool ListDetectedBoards()
		{
			UInt64 iBoardMax = AIOAIO.BRD_GetHighestBoardIndex();
			if (AIOAIO.biNone == iBoardMax)
			{
				NumberOfDetectedBoards.Text = "No compatible devices detected.";
				return false;
			}
			else if (0 == iBoardMax)
				NumberOfDetectedBoards.Text = (iBoardMax + 1).ToString() + " compatible device detected.";
			else
				NumberOfDetectedBoards.Text = (iBoardMax + 1).ToString() + " compatible devices detected."
			;
			DeviceListBox.Items.Clear();
			for (UInt64 iBoard = 0; iBoard <= iBoardMax; iBoard++)
			{
				string Name;
				Name = "Device[" + iBoard + "]----------------";
				AIOAIO.BRD_GetName(iBoard, out Name);
				DeviceListBox.Items.Add(iBoard.ToString() + "=" + Name);
			}
			DeviceListBox.SelectedIndex = 0;
			iBoard = 0;
			return true;
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			UInt32 DIOControlRegister = AIOAIO.PIO_In32(iBoard, AIOAIO.DIOControlRegisterOffset);
			DIO0_Direction.Checked = (DIOControlRegister & 0x01) != 0;
			DIO1_Direction.Checked = (DIOControlRegister & 0x02) != 0;

			UInt32 DIOData = AIOAIO.PIO_In32(iBoard, AIOAIO.DIODataRegisterOffset);
			DIO0_Data.Checked = (DIOData & 0x01) != 0;
			DIO1_Data.Checked = (DIOData & 0x02) != 0;

			DIO0_Data.Text = DIO0_Data.Checked ? "Digital I/O #0 is High" : "Digital I/O #0 is Low";
			DIO1_Data.Text = DIO1_Data.Checked ? "Digital I/O #1 is High" : "Digital I/O #1 is Low";
		}

		// read modify write bit 0 or 1 of the DIO Control register; 1=output for DIO Bit 0
		private void DIO0_Direction_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox cb = sender as CheckBox;
			int bit = Convert.ToInt32(cb.Tag);
			UInt32 DIOControlRegister = AIOAIO.PIO_In32(iBoard, AIOAIO.DIOControlRegisterOffset);
			if (cb.Checked)
			{
				DIOControlRegister |= (uint)(0x01 << bit);
			}
			else
			{
				DIOControlRegister &= ~(uint)(0x01 << bit);
			}
			AIOAIO.PIO_Out32(iBoard, AIOAIO.DIOControlRegisterOffset, DIOControlRegister);
		}

		private void DeviceListBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			iBoard = (uint)DeviceListBox.SelectedIndex;
		}

		private void DIO_Data_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox cb = sender as CheckBox;
			int bit = Convert.ToInt32(cb.Tag);
			UInt32 DIOData = AIOAIO.PIO_In32(iBoard, AIOAIO.DIODataRegisterOffset);
			if (cb.Checked)
			{
				DIOData |= (uint)(0x01 << bit);
			}
			else
			{
				DIOData &= ~(uint)(0x01 << bit);
			}
			AIOAIO.PIO_Out32(iBoard, AIOAIO.DIODataRegisterOffset, DIOData);
		}
	}
}
