//***********************************************************************/
//*                                                                     */
//*     IF-50 API Sample Program                                        */
//*                                                                     */
//*      Copyright(c) 2014  TEXIO TECHNOLOGY CORPORATION                */
//***********************************************************************/

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

namespace LW_Access
{
	// サンプルフォーム
	public partial class LW_Access : Form
	{
		private int DeviceId;			// オープンしたデバイスのID。

		public LW_Access()
		{
			InitializeComponent();
		}

		// フォームロード
		private void LW_Access_Load(object sender, EventArgs e)
		{
			// 使用するインターフェースを選択できるようにする。
			comboBox1.Items.Add("USB");
			comboBox1.Items.Add("GPIB0");
			comboBox1.Items.Add("GPIB1");
			comboBox1.SelectedIndex = 0;

			// PCアドレス
			for (int address = 0; address < 31; address++)
			{
				comboBox2.Items.Add(address.ToString());
			}
			comboBox2.SelectedIndex = 1;

			// システムアドレス
			for(int address = 1; address < 32; address++)
			{
				comboBox3.Items.Add(address.ToString());
			}
			comboBox3.SelectedIndex = 0;
		}

		// フォームクローズ
		private void LW_Access_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (DeviceId != 0)
			{
				int res;

				res = IF_50_Api.LW_SetMainInput(DeviceId, 0);
				res = IF_50_Api.LW_SetLocal(DeviceId);
				res = IF_50_Api.LW_DeviceClose(DeviceId);

				DeviceId = 0;
			
			}
		}

		// Device Open
		private void button1_Click(object sender, EventArgs e)
		{
			int res;
			// Device open
			if (button1.Text == "OPEN")
			{
				// 電源名指定文字列を作成。
				// 「IF種類」+ ":" +「PCアドレス」+ ":" +「システムアドレス」

				int IF_Name = 0;
				int BDADDR = 0;
				int PCADDR = 0;
				int SYSADDR = 0;

				if (comboBox1.Text == "USB")
				{
					IF_Name = 2;
					BDADDR = 0;
				}
				else if (comboBox1.Text == "GPIB0")
				{
					IF_Name = 1;
					BDADDR = 0;
				}
				else if (comboBox1.Text == "GPIB1")
				{
					IF_Name = 1;
					BDADDR = 1;
				}

				PCADDR = Convert.ToInt32(comboBox2.Text);
				SYSADDR = Convert.ToInt32(comboBox3.Text);

				// デバイスオープン
				DeviceId = IF_50_Api.LW_DeviceOpen(IF_Name, BDADDR, PCADDR, SYSADDR);

				if (DeviceId > 0)
				{
					res = IF_50_Api.LW_SetMainInput(DeviceId, 0);
					res = IF_50_Api.LW_SetInputSelect(DeviceId, 1, 0);
					res = IF_50_Api.LW_SetDisplaySelect(DeviceId, 1);

					// 表示を更新
					button1.Text = "CLOSE";
					button2.Enabled = true;
					button2.Text = "ON";
					button3.Enabled = true;
					button4.Enabled = true;
					button5.Enabled = true;
					button6.Enabled = true;
					button7.Enabled = true;
					textBox1.Text = "";
				}
				else
				{
					textBox1.Text = " Device Open ERR";
				}
			}
			// Device close
			else
			{
				res = IF_50_Api.LW_SetMainInput(DeviceId, 0);
				res = IF_50_Api.LW_SetLocal(DeviceId);
				res = IF_50_Api.LW_DeviceClose(DeviceId);

				// 表示の更新
				button2.Text = "ON";
				button2.BackColor = System.Drawing.SystemColors.Control;
				button1.Text = "OPEN";
				button2.Enabled = false;
				button3.Enabled = false;
				button4.Enabled = false;
				button5.Enabled = false;
				button6.Enabled = false;
				button7.Enabled = false;
				textBox1.Text = "";
				DeviceId = 0;
			}
		}

		// Output On/Off
		private void button2_Click(object sender, EventArgs e)
		{
			int res;
			if (button2.Text == "ON")
			{
				// InputをONに設定
				res = IF_50_Api.LW_SetMainInput(DeviceId, 1);

				// 表示の更新
				button2.Text = "OFF";
				button2.BackColor = System.Drawing.Color.Red;
			}
			else
			{
				// InputをOFFに設定
				res = IF_50_Api.LW_SetMainInput(DeviceId, 0);

				// 表示の更新
				button2.Text = "ON";
				button2.BackColor = System.Drawing.SystemColors.Control;
			}
		}

		// Preset 1
		private void button3_Click(object sender, EventArgs e)
		{
			// Preset 1を選択
			int res = IF_50_Api.LW_SetPresetNumber(DeviceId, 1);
		}

		// Preset 2
		private void button4_Click(object sender, EventArgs e)
		{
			// Preset 2を選択
			int res = IF_50_Api.LW_SetPresetNumber(DeviceId, 2);
		}

		// Preset 3
		private void button5_Click(object sender, EventArgs e)
		{
			// Preset 3を選択
			int res = IF_50_Api.LW_SetPresetNumber(DeviceId, 3);
		}

		// Preset 4
		private void button6_Click(object sender, EventArgs e)
		{
			// Preset 4を選択
			int res = IF_50_Api.LW_SetPresetNumber(DeviceId, 4);
		}

		// Read
		private void button7_Click(object sender, EventArgs e)
		{
			IF_50_Api.MONITOR_DATA Monitor = new IF_50_Api.MONITOR_DATA();

			// モニタ値をリード
			int res = IF_50_Api.LW_GetMonitor(DeviceId, 1, ref Monitor);

			// リードしたモニタ電圧、モニタ電流、CV/CC状態を表示
			StringBuilder sb = new StringBuilder();
			sb.Append("V : ");
			sb.Append(Monitor.Voltage.ToString("#00.000"));
			sb.AppendLine(" V");
			sb.Append("C : ");
			sb.Append(Monitor.Current.ToString("#00.000"));
			sb.AppendLine(" A");
			sb.Append("P : ");
			sb.Append(Monitor.Wattage.ToString("#00.000"));
			sb.AppendLine(" W");
			textBox1.Text = sb.ToString();

		}

		private void API_test()
		{

			int res;
			double Data = 0;
			int IData = 0;
			IF_50_Api.MONITOR_DATA Monitor = new IF_50_Api.MONITOR_DATA();
			IF_50_Api.MODE_DATA ModeData = new IF_50_Api.MODE_DATA();
			IF_50_Api.TIME_DATA Time = new IF_50_Api.TIME_DATA();
			IF_50_Api.TRACK_DATA Track = new IF_50_Api.TRACK_DATA();
			IF_50_Api.ALARM_DATA Alarm = new IF_50_Api.ALARM_DATA();
			IF_50_Api.LIMIT_DATA Limit = new IF_50_Api.LIMIT_DATA();
			IF_50_Api.CH_DATA ChData = new IF_50_Api.CH_DATA();

			res = IF_50_Api.LW_DeviceRefresh(DeviceId);
			res = IF_50_Api.LW_SetTimeOut(DeviceId, 1);
			res = IF_50_Api.LW_SetMainInput(DeviceId, 1);
			res = IF_50_Api.LW_GetMainInput(DeviceId);
			res = IF_50_Api.LW_SetInputSelect(DeviceId, 1, 1);
			res = IF_50_Api.LW_GetInputSelect(DeviceId, 1);
			res = IF_50_Api.LW_SetPresetNumber(DeviceId, 1);
			res = IF_50_Api.LW_GetPresetNumber(DeviceId);
			res = IF_50_Api.LW_SavePreset(DeviceId, 1, 2);
			res = IF_50_Api.LW_GetMonitor(DeviceId, 1, ref Monitor);
			res = IF_50_Api.LW_SetMonitorSelect(DeviceId, 1);
			res = IF_50_Api.LW_GetMonitorSelect(DeviceId);
			res = IF_50_Api.LW_SetDisplaySelect(DeviceId, 1);
			res = IF_50_Api.LW_GetDisplaySelect(DeviceId);
			res = IF_50_Api.LW_SetLoadMode(DeviceId, 1, 1, 2, 0);
			res = IF_50_Api.LW_GetLoadMode(DeviceId, 1, 1, ref ModeData);
			res = IF_50_Api.LW_SetLoadValue(DeviceId, 1, 1, 1.1);
			res = IF_50_Api.LW_GetLoadValue(DeviceId, 1, 1, ref Data);

//			res = IF_50_Api.LW_SetSiemensValue(DeviceId, 1, 1, 30000);
//			res = IF_50_Api.LW_GetSiemensValue(DeviceId, 1, 1, ref IData);
//			res = IF_50_Api.LW_SetCurrentLimit(DeviceId, 1, 1, 1.2);
//			res = IF_50_Api.LW_GetCurrentLimit(DeviceId, 1, 1, ref Data);
			
            res = IF_50_Api.LW_SaveLoadValue(DeviceId, 1, 1);
			res = IF_50_Api.LW_SaveCurrentLimit(DeviceId, 1, 1);
			res = IF_50_Api.LW_SaveMemoryData(DeviceId, 3);
			res = IF_50_Api.LW_SetSwSelect(DeviceId, 1, 0);
			res = IF_50_Api.LW_GetSwSelect(DeviceId, 1);
			res = IF_50_Api.LW_SetSwFreq(DeviceId, 100);
			res = IF_50_Api.LW_GetSwFreq(DeviceId, ref IData);
			res = IF_50_Api.LW_SetSwDuty(DeviceId, 56);
			res = IF_50_Api.LW_GetSwDuty(DeviceId, ref IData);
			res = IF_50_Api.LW_SetSwTime(DeviceId, 400.1, 300.1);
			res = IF_50_Api.LW_GetSwTime(DeviceId, ref Time);
			res = IF_50_Api.LW_SetSwMethod(DeviceId, 0);
			res = IF_50_Api.LW_GetSwMethod(DeviceId);
			res = IF_50_Api.LW_SetDelayFunction(DeviceId, 0);
			res = IF_50_Api.LW_GetDelayFunction(DeviceId);
			res = IF_50_Api.LW_SetDelayTime(DeviceId, 1, 2.1);
			res = IF_50_Api.LW_GetDelayTime(DeviceId, 1, ref Data);
			res = IF_50_Api.LW_SetTrackFunction(DeviceId, 0);
			res = IF_50_Api.LW_GetTrackFunction(DeviceId);
			res = IF_50_Api.LW_SetTrackDirection(DeviceId, 1, 2, 0, 0);
			res = IF_50_Api.LW_GetTrackDirection(DeviceId, ref Track);
			res = IF_50_Api.LW_SetTrackMode(DeviceId, 0);
			res = IF_50_Api.LW_GetTrackMode(DeviceId);
			res = IF_50_Api.LW_SetTrackValue(DeviceId, 110.0);
			res = IF_50_Api.LW_GetTrackValue(DeviceId, ref Data);
			res = IF_50_Api.LW_ResetAlarm(DeviceId);
			res = IF_50_Api.LW_GetAlarm(DeviceId, ref Alarm);
			res = IF_50_Api.LW_GetLimit(DeviceId, ref Limit);
			res = IF_50_Api.LW_SetOperationTimeOut(DeviceId, 5);
			res = IF_50_Api.LW_GetOperationTimeOut(DeviceId);
			res = IF_50_Api.LW_SetLocalLockOut(DeviceId, 0);
			res = IF_50_Api.LW_GetLocalLockOut(DeviceId);
			res = IF_50_Api.LW_GetIdNumber(DeviceId);
			res = IF_50_Api.LW_GetChExist(DeviceId, ref ChData);

		}
	}
}
