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

namespace Interrupt
{
	/// <summary>
	/// Interrupt の概要の説明です。
	/// </summary>
	public class Interrupt : System.Windows.Forms.Form
	{
		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 btnExit;
		private System.Windows.Forms.Button btnDioInit;
		private System.Windows.Forms.TextBox txtDeviceName;
		private System.Windows.Forms.Label lblDeviceName;
		private System.Windows.Forms.Button btnDioExit;
		private System.Windows.Forms.TextBox txtReturnCode;
		private System.Windows.Forms.Label lblReturnCode;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		private System.Windows.Forms.TextBox[] m_UpCountText = new System.Windows.Forms.TextBox[8];
		private System.Windows.Forms.TextBox[] m_DownCountText = new System.Windows.Forms.TextBox[8];
		
		/// <summary>
		/// Create instance Cdio class
		/// </summary>
		Cdio  dio = new Cdio();

		public short	m_Id;						// デバイスID
		public int[]	m_UpCount	= new int[8];	// アップカウント
		public int[]	m_DownCount	= new int[8];	// ダウンカウント


		public Interrupt()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
			m_UpCountText[0] = txtUpCount_0;
			m_UpCountText[1] = txtUpCount_1;
			m_UpCountText[2] = txtUpCount_2;
			m_UpCountText[3] = txtUpCount_3;
	
			m_DownCountText[0] = txtDownCount_0;
			m_DownCountText[1] = txtDownCount_1;
			m_DownCountText[2] = txtDownCount_2;
			m_DownCountText[3] = txtDownCount_3;
		
			rdoRise.Checked		= true;
	
			txtReturnCode.Text  = "";
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			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.btnExit = new System.Windows.Forms.Button();
			this.btnDioInit = new System.Windows.Forms.Button();
			this.txtDeviceName = new System.Windows.Forms.TextBox();
			this.lblDeviceName = new System.Windows.Forms.Label();
			this.btnDioExit = new System.Windows.Forms.Button();
			this.txtReturnCode = new System.Windows.Forms.TextBox();
			this.lblReturnCode = new System.Windows.Forms.Label();
			this.grpTrigger.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// 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(16, 55);
			this.grpTrigger.Name = "grpTrigger";
			this.grpTrigger.Size = new System.Drawing.Size(512, 195);
			this.grpTrigger.TabIndex = 2;
			this.grpTrigger.TabStop = false;
			this.grpTrigger.Text = "トリガーモニター";
			// 
			// btnIntStop
			// 
			this.btnIntStop.Location = new System.Drawing.Point(408, 157);
			this.btnIntStop.Name = "btnIntStop";
			this.btnIntStop.Size = new System.Drawing.Size(88, 27);
			this.btnIntStop.TabIndex = 15;
			this.btnIntStop.Text = "割り込み停止";
			this.btnIntStop.Click += new System.EventHandler(this.btnIntStop_Click);
			// 
			// btnIntStart
			// 
			this.btnIntStart.Location = new System.Drawing.Point(408, 122);
			this.btnIntStart.Name = "btnIntStart";
			this.btnIntStart.Size = new System.Drawing.Size(88, 27);
			this.btnIntStart.TabIndex = 14;
			this.btnIntStart.Text = "割り込み開始";
			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(320, 32);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(176, 80);
			this.groupBox1.TabIndex = 11;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "割り込み論理";
			// 
			// rdoFall
			// 
			this.rdoFall.Location = new System.Drawing.Point(16, 48);
			this.rdoFall.Name = "rdoFall";
			this.rdoFall.Size = new System.Drawing.Size(152, 24);
			this.rdoFall.TabIndex = 13;
			this.rdoFall.Text = "1 -> 0 (立ち下がりエッジ)";
			// 
			// rdoRise
			// 
			this.rdoRise.Location = new System.Drawing.Point(16, 24);
			this.rdoRise.Name = "rdoRise";
			this.rdoRise.Size = new System.Drawing.Size(152, 24);
			this.rdoRise.TabIndex = 12;
			this.rdoRise.Text = "0 -> 1 (立ち上がりエッジ)";
			// 
			// txtDownCount_3
			// 
			this.txtDownCount_3.Location = new System.Drawing.Point(256, 93);
			this.txtDownCount_3.Name = "txtDownCount_3";
			this.txtDownCount_3.Size = new System.Drawing.Size(46, 19);
			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(208, 93);
			this.txtDownCount_2.Name = "txtDownCount_2";
			this.txtDownCount_2.Size = new System.Drawing.Size(46, 19);
			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(160, 93);
			this.txtDownCount_1.Name = "txtDownCount_1";
			this.txtDownCount_1.Size = new System.Drawing.Size(46, 19);
			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(112, 93);
			this.txtDownCount_0.Name = "txtDownCount_0";
			this.txtDownCount_0.Size = new System.Drawing.Size(46, 19);
			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(256, 56);
			this.txtUpCount_3.Name = "txtUpCount_3";
			this.txtUpCount_3.Size = new System.Drawing.Size(46, 19);
			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(208, 56);
			this.txtUpCount_2.Name = "txtUpCount_2";
			this.txtUpCount_2.Size = new System.Drawing.Size(46, 19);
			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(160, 56);
			this.txtUpCount_1.Name = "txtUpCount_1";
			this.txtUpCount_1.Size = new System.Drawing.Size(46, 19);
			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(112, 56);
			this.txtUpCount_0.Name = "txtUpCount_0";
			this.txtUpCount_0.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtUpCount_0.Size = new System.Drawing.Size(46, 19);
			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(272, 28);
			this.lblTrigger_3.Name = "lblTrigger_3";
			this.lblTrigger_3.Size = new System.Drawing.Size(16, 27);
			this.lblTrigger_3.TabIndex = 27;
			this.lblTrigger_3.Text = "3";
			// 
			// lblTrigger_2
			// 
			this.lblTrigger_2.Location = new System.Drawing.Point(224, 28);
			this.lblTrigger_2.Name = "lblTrigger_2";
			this.lblTrigger_2.Size = new System.Drawing.Size(16, 27);
			this.lblTrigger_2.TabIndex = 26;
			this.lblTrigger_2.Text = "2";
			// 
			// lblTrigger_1
			// 
			this.lblTrigger_1.Location = new System.Drawing.Point(176, 28);
			this.lblTrigger_1.Name = "lblTrigger_1";
			this.lblTrigger_1.Size = new System.Drawing.Size(16, 27);
			this.lblTrigger_1.TabIndex = 25;
			this.lblTrigger_1.Text = "1";
			// 
			// lblTrigger_0
			// 
			this.lblTrigger_0.Location = new System.Drawing.Point(128, 28);
			this.lblTrigger_0.Name = "lblTrigger_0";
			this.lblTrigger_0.Size = new System.Drawing.Size(16, 27);
			this.lblTrigger_0.TabIndex = 24;
			this.lblTrigger_0.Text = "0";
			// 
			// lblDowncount
			// 
			this.lblDowncount.Location = new System.Drawing.Point(16, 96);
			this.lblDowncount.Name = "lblDowncount";
			this.lblDowncount.Size = new System.Drawing.Size(80, 19);
			this.lblDowncount.TabIndex = 3;
			this.lblDowncount.Text = "カウントダウン：";
			// 
			// lblUpcount
			// 
			this.lblUpcount.Location = new System.Drawing.Point(16, 60);
			this.lblUpcount.Name = "lblUpcount";
			this.lblUpcount.Size = new System.Drawing.Size(80, 15);
			this.lblUpcount.TabIndex = 2;
			this.lblUpcount.Text = "カウントアップ：";
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(424, 297);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(88, 27);
			this.btnExit.TabIndex = 18;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// btnDioInit
			// 
			this.btnDioInit.Location = new System.Drawing.Point(424, 18);
			this.btnDioInit.Name = "btnDioInit";
			this.btnDioInit.Size = new System.Drawing.Size(88, 26);
			this.btnDioInit.TabIndex = 1;
			this.btnDioInit.Text = "DioInit";
			this.btnDioInit.Click += new System.EventHandler(this.btnDioInit_Click);
			// 
			// txtDeviceName
			// 
			this.txtDeviceName.Location = new System.Drawing.Point(120, 18);
			this.txtDeviceName.Name = "txtDeviceName";
			this.txtDeviceName.TabIndex = 0;
			this.txtDeviceName.Text = "DIO000";
			// 
			// lblDeviceName
			// 
			this.lblDeviceName.Location = new System.Drawing.Point(16, 18);
			this.lblDeviceName.Name = "lblDeviceName";
			this.lblDeviceName.Size = new System.Drawing.Size(100, 26);
			this.lblDeviceName.TabIndex = 61;
			this.lblDeviceName.Text = "デバイス名：";
			// 
			// btnDioExit
			// 
			this.btnDioExit.Location = new System.Drawing.Point(424, 260);
			this.btnDioExit.Name = "btnDioExit";
			this.btnDioExit.Size = new System.Drawing.Size(88, 26);
			this.btnDioExit.TabIndex = 17;
			this.btnDioExit.Text = "DioExit";
			this.btnDioExit.Click += new System.EventHandler(this.btnDioExit_Click);
			// 
			// txtReturnCode
			// 
			this.txtReturnCode.Location = new System.Drawing.Point(120, 260);
			this.txtReturnCode.Name = "txtReturnCode";
			this.txtReturnCode.Size = new System.Drawing.Size(288, 19);
			this.txtReturnCode.TabIndex = 16;
			this.txtReturnCode.Text = "";
			// 
			// lblReturnCode
			// 
			this.lblReturnCode.Location = new System.Drawing.Point(16, 260);
			this.lblReturnCode.Name = "lblReturnCode";
			this.lblReturnCode.Size = new System.Drawing.Size(88, 20);
			this.lblReturnCode.TabIndex = 64;
			this.lblReturnCode.Text = "戻り値：";
			// 
			// Interrupt
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(544, 342);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.grpTrigger,
																		  this.btnExit,
																		  this.btnDioInit,
																		  this.txtDeviceName,
																		  this.lblDeviceName,
																		  this.btnDioExit,
																		  this.txtReturnCode,
																		  this.lblReturnCode});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Interrupt";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "割り込み";
			this.grpTrigger.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// アプリケーションのメイン エントリ ポイントです。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Interrupt());
		}

		
		//---------------------------------------------------------
		// DioInit()
		//---------------------------------------------------------
		private void btnDioInit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------
			// 初期化処理
			//-----------------------------
			int	Ret;
			Ret = dio.Init(txtDeviceName.Text, out m_Id);
			//-----------------------------
			// エラー処理
			//-----------------------------
			string	ErrorString;
			dio.GetErrorString(Ret, out ErrorString);
			txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
			if (Ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				return;
			}
		}


		//---------------------------------------------------------
		// 割り込み開始
		//---------------------------------------------------------
		private void btnIntStart_Click(object sender, System.EventArgs e)
		{
			int		Ret = 0;
			short	BitNo;
			short	Kind;
			string	ErrorString;
			//-----------------------------
			// パラメータ初期化
			//-----------------------------
			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;
			}
			//-----------------------------
			// 各ビットごとに
			//-----------------------------
			for (BitNo = 0; BitNo < 4; BitNo++) 
			{
				//-----------------------------
				// 割り込みイベント設定
				//-----------------------------
				Ret = dio.NotifyInterrupt(m_Id, BitNo, Kind, this.Handle);
				if (Ret != (int)CdioConst.DIO_ERR_SUCCESS) 
				{
					break;
				}
			}
			//-----------------------------
			// エラー処理
			//-----------------------------
			dio.GetErrorString(Ret, out ErrorString);
			txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
		}

		//---------------------------------------------------------
		// 割り込み停止
		//---------------------------------------------------------
		private void btnIntStop_Click(object sender, System.EventArgs e)
		{
			int		Ret = 0;
			short	BitNo;
			string	ErrorString;

			//-----------------------------
			// 各ビットごとに
			//-----------------------------
			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;
				}
			}
			//-----------------------------
			// エラー処理
			//-----------------------------
			dio.GetErrorString(Ret, out ErrorString);
			txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
		}

		//---------------------------------------------------
		//	DioExit()
		//---------------------------------------------------
		private void btnDioExit_Click(object sender, System.EventArgs e)
		{
			int		Ret;
			string	ErrorString;

			//-----------------------------
			// DioExit処理
			//-----------------------------
			Ret = dio.Exit(m_Id);
			//-----------------------------
			// エラー処理
			//-----------------------------
			dio.GetErrorString(Ret, out ErrorString);
			txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
		}

		//---------------------------------------------------------
		// ダイアログExit
		//---------------------------------------------------------
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			//-----------------------------
			// DioExit処理
			//-----------------------------
			dio.Exit(m_Id);
			//-----------------------------
			// ダイアログExit
			//-----------------------------
			this.Close();
		}

		//---------------------------------------------------------
		// Message handler
		//---------------------------------------------------------
		protected override void WndProc(ref Message m)
		{
			int BitNo;
			int Kind;

			switch (m.Msg)
			{
				//-----------------------------
				// 割り込みイベント
				//-----------------------------
				case((int)CdioConst.DIOM_INTERRUPT):
					BitNo	= m.LParam.ToInt32() & 0x0000ffff;
					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);
		}
	}
}
