﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CdioCs;

namespace Interrupt
{
	/// <summary>
	/// Summary description for Interrupt.
	/// </summary>
	public class Interrupt : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.Button btnDioInit;
		private System.Windows.Forms.TextBox txtReturnCode;
		private System.Windows.Forms.Label lblReturnCode;
		private System.Windows.Forms.GroupBox grpTrigger;
		private System.Windows.Forms.Button btnIntStop;
		private System.Windows.Forms.Button btnIntStart;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton rdoFall;
		private System.Windows.Forms.RadioButton rdoRise;
		private System.Windows.Forms.TextBox txtDownCount_3;
		private System.Windows.Forms.TextBox txtDownCount_2;
		private System.Windows.Forms.TextBox txtDownCount_1;
		private System.Windows.Forms.TextBox txtDownCount_0;
		private System.Windows.Forms.TextBox txtUpCount_3;
		private System.Windows.Forms.TextBox txtUpCount_2;
		private System.Windows.Forms.TextBox txtUpCount_1;
		private System.Windows.Forms.TextBox txtUpCount_0;
		private System.Windows.Forms.Label lblTrigger_3;
		private System.Windows.Forms.Label lblTrigger_2;
		private System.Windows.Forms.Label lblTrigger_1;
		private System.Windows.Forms.Label lblTrigger_0;
		private System.Windows.Forms.Label lblDowncount;
		private System.Windows.Forms.Label lblUpcount;
		private System.Windows.Forms.Button btnDioExit;
		private System.Windows.Forms.TextBox txtDeviceName;
		private System.Windows.Forms.Label lblDeviceName;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private System.Windows.Forms.TextBox[] m_UpCountText = new System.Windows.Forms.TextBox[4];		// UpCounter Express
		private System.Windows.Forms.TextBox[] m_DownCountText = new System.Windows.Forms.TextBox[4];	// DownCounter Express

		Cdio	dio = new Cdio();
		short	m_Id;						// Device ID
		int		Ret;						// Return Code
		string	ErrorString;				// Error String
		int[]	m_UpCount	= new int[4];	// UpCounter
		int[]	m_DownCount	= new int[4];	// DownCounter

		public Interrupt()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			//-----------------------------------
			// Screen Item Initialization
			//-----------------------------------
			txtDeviceName.Text = "DIO000";
			txtReturnCode.Text  = "";
			// Up counter
			m_UpCountText[0] = txtUpCount_0;
			m_UpCountText[1] = txtUpCount_1;
			m_UpCountText[2] = txtUpCount_2;
			m_UpCountText[3] = txtUpCount_3;
			// Down counter
			m_DownCountText[0] = txtDownCount_0;
			m_DownCountText[1] = txtDownCount_1;
			m_DownCountText[2] = txtDownCount_2;
			m_DownCountText[3] = txtDownCount_3;
		
			rdoRise.Checked =true;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnExit = new System.Windows.Forms.Button();
			this.btnDioInit = new System.Windows.Forms.Button();
			this.txtReturnCode = new System.Windows.Forms.TextBox();
			this.lblReturnCode = new System.Windows.Forms.Label();
			this.grpTrigger = new System.Windows.Forms.GroupBox();
			this.btnIntStop = new System.Windows.Forms.Button();
			this.btnIntStart = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.rdoFall = new System.Windows.Forms.RadioButton();
			this.rdoRise = new System.Windows.Forms.RadioButton();
			this.txtDownCount_3 = new System.Windows.Forms.TextBox();
			this.txtDownCount_2 = new System.Windows.Forms.TextBox();
			this.txtDownCount_1 = new System.Windows.Forms.TextBox();
			this.txtDownCount_0 = new System.Windows.Forms.TextBox();
			this.txtUpCount_3 = new System.Windows.Forms.TextBox();
			this.txtUpCount_2 = new System.Windows.Forms.TextBox();
			this.txtUpCount_1 = new System.Windows.Forms.TextBox();
			this.txtUpCount_0 = new System.Windows.Forms.TextBox();
			this.lblTrigger_3 = new System.Windows.Forms.Label();
			this.lblTrigger_2 = new System.Windows.Forms.Label();
			this.lblTrigger_1 = new System.Windows.Forms.Label();
			this.lblTrigger_0 = new System.Windows.Forms.Label();
			this.lblDowncount = new System.Windows.Forms.Label();
			this.lblUpcount = new System.Windows.Forms.Label();
			this.btnDioExit = new System.Windows.Forms.Button();
			this.txtDeviceName = new System.Windows.Forms.TextBox();
			this.lblDeviceName = new System.Windows.Forms.Label();
			this.grpTrigger.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(409, 301);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(88, 24);
			this.btnExit.TabIndex = 70;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// btnDioInit
			// 
			this.btnDioInit.Location = new System.Drawing.Point(409, 9);
			this.btnDioInit.Name = "btnDioInit";
			this.btnDioInit.Size = new System.Drawing.Size(88, 24);
			this.btnDioInit.TabIndex = 66;
			this.btnDioInit.Text = "DioInit";
			this.btnDioInit.Click += new System.EventHandler(this.btnDioInit_Click);
			// 
			// txtReturnCode
			// 
			this.txtReturnCode.Location = new System.Drawing.Point(104, 270);
			this.txtReturnCode.Name = "txtReturnCode";
			this.txtReturnCode.Size = new System.Drawing.Size(288, 20);
			this.txtReturnCode.TabIndex = 68;
			this.txtReturnCode.Text = "";
			// 
			// lblReturnCode
			// 
			this.lblReturnCode.Location = new System.Drawing.Point(19, 270);
			this.lblReturnCode.Name = "lblReturnCode";
			this.lblReturnCode.Size = new System.Drawing.Size(88, 20);
			this.lblReturnCode.TabIndex = 72;
			this.lblReturnCode.Text = "Return Code :";
			// 
			// grpTrigger
			// 
			this.grpTrigger.Controls.AddRange(new System.Windows.Forms.Control[] {
																					 this.btnIntStop,
																					 this.btnIntStart,
																					 this.groupBox1,
																					 this.txtDownCount_3,
																					 this.txtDownCount_2,
																					 this.txtDownCount_1,
																					 this.txtDownCount_0,
																					 this.txtUpCount_3,
																					 this.txtUpCount_2,
																					 this.txtUpCount_1,
																					 this.txtUpCount_0,
																					 this.lblTrigger_3,
																					 this.lblTrigger_2,
																					 this.lblTrigger_1,
																					 this.lblTrigger_0,
																					 this.lblDowncount,
																					 this.lblUpcount});
			this.grpTrigger.Location = new System.Drawing.Point(11, 51);
			this.grpTrigger.Name = "grpTrigger";
			this.grpTrigger.Size = new System.Drawing.Size(501, 205);
			this.grpTrigger.TabIndex = 67;
			this.grpTrigger.TabStop = false;
			this.grpTrigger.Text = "Trigger Monitor";
			// 
			// btnIntStop
			// 
			this.btnIntStop.Location = new System.Drawing.Point(398, 169);
			this.btnIntStop.Name = "btnIntStop";
			this.btnIntStop.Size = new System.Drawing.Size(88, 24);
			this.btnIntStop.TabIndex = 15;
			this.btnIntStop.Text = "Stop Interrupt";
			this.btnIntStop.Click += new System.EventHandler(this.btnIntStop_Click);
			// 
			// btnIntStart
			// 
			this.btnIntStart.Location = new System.Drawing.Point(398, 132);
			this.btnIntStart.Name = "btnIntStart";
			this.btnIntStart.Size = new System.Drawing.Size(88, 24);
			this.btnIntStart.TabIndex = 14;
			this.btnIntStart.Text = "Start Interrupt";
			this.btnIntStart.Click += new System.EventHandler(this.btnIntStart_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rdoFall,
																					this.rdoRise});
			this.groupBox1.Location = new System.Drawing.Point(317, 23);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(169, 93);
			this.groupBox1.TabIndex = 11;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Interrupt Logic";
			// 
			// rdoFall
			// 
			this.rdoFall.Location = new System.Drawing.Point(10, 57);
			this.rdoFall.Name = "rdoFall";
			this.rdoFall.Size = new System.Drawing.Size(142, 22);
			this.rdoFall.TabIndex = 13;
			this.rdoFall.Text = "1 -> 0 (Falling edge)";
			// 
			// rdoRise
			// 
			this.rdoRise.Location = new System.Drawing.Point(10, 25);
			this.rdoRise.Name = "rdoRise";
			this.rdoRise.Size = new System.Drawing.Size(142, 22);
			this.rdoRise.TabIndex = 12;
			this.rdoRise.Text = "0 -> 1 (Rising edge)";
			// 
			// txtDownCount_3
			// 
			this.txtDownCount_3.Location = new System.Drawing.Point(235, 80);
			this.txtDownCount_3.Name = "txtDownCount_3";
			this.txtDownCount_3.Size = new System.Drawing.Size(46, 20);
			this.txtDownCount_3.TabIndex = 10;
			this.txtDownCount_3.Text = "";
			this.txtDownCount_3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtDownCount_2
			// 
			this.txtDownCount_2.Location = new System.Drawing.Point(188, 80);
			this.txtDownCount_2.Name = "txtDownCount_2";
			this.txtDownCount_2.Size = new System.Drawing.Size(46, 20);
			this.txtDownCount_2.TabIndex = 9;
			this.txtDownCount_2.Text = "";
			this.txtDownCount_2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtDownCount_1
			// 
			this.txtDownCount_1.Location = new System.Drawing.Point(141, 80);
			this.txtDownCount_1.Name = "txtDownCount_1";
			this.txtDownCount_1.Size = new System.Drawing.Size(46, 20);
			this.txtDownCount_1.TabIndex = 8;
			this.txtDownCount_1.Text = "";
			this.txtDownCount_1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtDownCount_0
			// 
			this.txtDownCount_0.Location = new System.Drawing.Point(94, 80);
			this.txtDownCount_0.Name = "txtDownCount_0";
			this.txtDownCount_0.Size = new System.Drawing.Size(46, 20);
			this.txtDownCount_0.TabIndex = 7;
			this.txtDownCount_0.Text = "";
			this.txtDownCount_0.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtUpCount_3
			// 
			this.txtUpCount_3.Location = new System.Drawing.Point(235, 51);
			this.txtUpCount_3.Name = "txtUpCount_3";
			this.txtUpCount_3.Size = new System.Drawing.Size(46, 20);
			this.txtUpCount_3.TabIndex = 6;
			this.txtUpCount_3.Text = "";
			this.txtUpCount_3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtUpCount_2
			// 
			this.txtUpCount_2.Location = new System.Drawing.Point(188, 51);
			this.txtUpCount_2.Name = "txtUpCount_2";
			this.txtUpCount_2.Size = new System.Drawing.Size(46, 20);
			this.txtUpCount_2.TabIndex = 5;
			this.txtUpCount_2.Text = "";
			this.txtUpCount_2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtUpCount_1
			// 
			this.txtUpCount_1.Location = new System.Drawing.Point(141, 51);
			this.txtUpCount_1.Name = "txtUpCount_1";
			this.txtUpCount_1.Size = new System.Drawing.Size(46, 20);
			this.txtUpCount_1.TabIndex = 4;
			this.txtUpCount_1.Text = "";
			this.txtUpCount_1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtUpCount_0
			// 
			this.txtUpCount_0.Location = new System.Drawing.Point(94, 51);
			this.txtUpCount_0.Name = "txtUpCount_0";
			this.txtUpCount_0.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtUpCount_0.Size = new System.Drawing.Size(46, 20);
			this.txtUpCount_0.TabIndex = 3;
			this.txtUpCount_0.Text = "";
			this.txtUpCount_0.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// lblTrigger_3
			// 
			this.lblTrigger_3.Location = new System.Drawing.Point(250, 27);
			this.lblTrigger_3.Name = "lblTrigger_3";
			this.lblTrigger_3.Size = new System.Drawing.Size(16, 15);
			this.lblTrigger_3.TabIndex = 27;
			this.lblTrigger_3.Text = "3";
			// 
			// lblTrigger_2
			// 
			this.lblTrigger_2.Location = new System.Drawing.Point(203, 27);
			this.lblTrigger_2.Name = "lblTrigger_2";
			this.lblTrigger_2.Size = new System.Drawing.Size(16, 15);
			this.lblTrigger_2.TabIndex = 26;
			this.lblTrigger_2.Text = "2";
			// 
			// lblTrigger_1
			// 
			this.lblTrigger_1.Location = new System.Drawing.Point(156, 27);
			this.lblTrigger_1.Name = "lblTrigger_1";
			this.lblTrigger_1.Size = new System.Drawing.Size(16, 15);
			this.lblTrigger_1.TabIndex = 25;
			this.lblTrigger_1.Text = "1";
			// 
			// lblTrigger_0
			// 
			this.lblTrigger_0.Location = new System.Drawing.Point(109, 27);
			this.lblTrigger_0.Name = "lblTrigger_0";
			this.lblTrigger_0.Size = new System.Drawing.Size(16, 15);
			this.lblTrigger_0.TabIndex = 24;
			this.lblTrigger_0.Text = "0";
			// 
			// lblDowncount
			// 
			this.lblDowncount.Location = new System.Drawing.Point(10, 81);
			this.lblDowncount.Name = "lblDowncount";
			this.lblDowncount.Size = new System.Drawing.Size(77, 18);
			this.lblDowncount.TabIndex = 3;
			this.lblDowncount.Text = "Down-count :";
			// 
			// lblUpcount
			// 
			this.lblUpcount.Location = new System.Drawing.Point(10, 52);
			this.lblUpcount.Name = "lblUpcount";
			this.lblUpcount.Size = new System.Drawing.Size(77, 18);
			this.lblUpcount.TabIndex = 2;
			this.lblUpcount.Text = "Up-count :";
			// 
			// btnDioExit
			// 
			this.btnDioExit.Location = new System.Drawing.Point(409, 268);
			this.btnDioExit.Name = "btnDioExit";
			this.btnDioExit.Size = new System.Drawing.Size(88, 24);
			this.btnDioExit.TabIndex = 69;
			this.btnDioExit.Text = "DioExit";
			this.btnDioExit.Click += new System.EventHandler(this.btnDioExit_Click);
			// 
			// txtDeviceName
			// 
			this.txtDeviceName.Location = new System.Drawing.Point(104, 11);
			this.txtDeviceName.Name = "txtDeviceName";
			this.txtDeviceName.Size = new System.Drawing.Size(85, 20);
			this.txtDeviceName.TabIndex = 65;
			this.txtDeviceName.Text = "DIO000";
			// 
			// lblDeviceName
			// 
			this.lblDeviceName.Location = new System.Drawing.Point(19, 11);
			this.lblDeviceName.Name = "lblDeviceName";
			this.lblDeviceName.Size = new System.Drawing.Size(77, 20);
			this.lblDeviceName.TabIndex = 71;
			this.lblDeviceName.Text = "Device Name:";
			// 
			// Interrupt
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(522, 334);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExit,
																		  this.btnDioInit,
																		  this.txtReturnCode,
																		  this.lblReturnCode,
																		  this.grpTrigger,
																		  this.btnDioExit,
																		  this.txtDeviceName,
																		  this.lblDeviceName});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Interrupt";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Interrupt";
			this.grpTrigger.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Interrupt());
		}

		//---------------------------------------------------------
		// DioInit()
		//---------------------------------------------------------
		private void btnDioInit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------
			// Execute initialization function and get ID
			//-----------------------------
			Ret = dio.Init(txtDeviceName.Text, out m_Id);
			//-----------------------------
			// Error process
			//-----------------------------
			dio.GetErrorString(Ret, out ErrorString);
			txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
			if (Ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				return;
			}
		}


		//---------------------------------------------------------
		// Interrupt observe start
		//---------------------------------------------------------
		private void btnIntStart_Click(object sender, System.EventArgs e)
		{
			short	BitNo;
			short	Kind;
			//-----------------------------
			// Parameters initialization
			//-----------------------------
			for (BitNo = 0; BitNo < 4; BitNo++) 
			{
				m_UpCount[BitNo] = 0;		// Up counter
				m_DownCount[BitNo] = 0;		// Down counter
			}
			if (rdoRise.Checked == true)
			{
				Kind = (short)CdioConst.DIO_INT_RISE;
			}
			else
			{
				Kind=(short)CdioConst.DIO_INT_FALL;
			}			
			//-----------------------------
			// Interrupt start
			//-----------------------------
			for (BitNo = 0; BitNo < 4; BitNo++) 
			{				
				Ret = dio.NotifyInterrupt(m_Id, BitNo, Kind, this.Handle);
				if (Ret != (int)CdioConst.DIO_ERR_SUCCESS) 
				{
					break;
				}
			}
			//-----------------------------
			// Error process
			//-----------------------------
			dio.GetErrorString(Ret, out ErrorString);
			txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
		}

		//---------------------------------------------------------
		// Interrupt observe stop
		//---------------------------------------------------------
		private void btnIntStop_Click(object sender, System.EventArgs e)
		{
			short	BitNo;
			for (BitNo = 0; BitNo < 4; BitNo++) 
			{				
				Ret = dio.NotifyInterrupt(m_Id, BitNo, (short)CdioConst.DIO_INT_NONE, this.Handle);
				if (Ret != (int)CdioConst.DIO_ERR_SUCCESS) 
				{
					break;
				}
			}
			//-----------------------------
			// Error process
			//-----------------------------
			dio.GetErrorString(Ret, out ErrorString);
			txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
		}

		//---------------------------------------------------
		// DioExit()
		//---------------------------------------------------
		private void btnDioExit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------
			// Exit function execute
			//-----------------------------
			Ret = dio.Exit(m_Id);
			//-----------------------------
			// Error process
			//-----------------------------
			dio.GetErrorString(Ret, out ErrorString);
			txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
		}

		//---------------------------------------------------------
		// Dialog exit
		//---------------------------------------------------------
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------
			// Exit function execute
			//-----------------------------
			dio.Exit(m_Id);
			//-----------------------------
			// Dialog exit
			//-----------------------------
			this.Close();
		}

		//---------------------------------------------------------
		// Message handler
		//---------------------------------------------------------
		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
				//-----------------------------
				// Message interpret
				//-----------------------------
				case((int)CdioConst.DIOM_INTERRUPT):
					int BitNo = m.LParam.ToInt32() & 0x0000ffff;
					int Kind = (int)((m.LParam.ToInt32() & 0x0ffff0000) >> 16);
					if ((Kind & (int)CdioConst.DIO_INT_RISE) == (int)CdioConst.DIO_INT_RISE)
					{
						m_UpCount[BitNo]++;
					}
					if ((Kind & (int)CdioConst.DIO_INT_FALL) == (int)CdioConst.DIO_INT_FALL)
					{
						m_DownCount[BitNo]++;
					}
					m_UpCountText[BitNo].Text = System.Convert.ToString(m_UpCount[BitNo]);
					m_DownCountText[BitNo].Text = System.Convert.ToString(m_DownCount[BitNo]);
					break;
			}
			base.WndProc(ref m);
		}
	}
}
