//***********************************************************************/
//*                                                                     */
//*     IF-40 API Sample Program                                        */
//*     Copyright(c) 2012  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 PWA_Access
{
    // サンプルフォーム
    public partial class PWA_Access : Form
    {
        private int DeviceId;			// オープンしたデバイスのID。

        public PWA_Access()
        {
            InitializeComponent();
        }

        // フォームロード
        private void PWA_Access_Load(object sender, EventArgs e)
        {
            // 使用するインターフェースを選択できるようにする。
            comboBox1.Items.Add("USB");
            comboBox1.Items.Add("GPIB0");
            comboBox1.Items.Add("COM1");
            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 PWA_Access_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DeviceId != 0)
            {
                // OutputをOFFに設定する。
                TMI_Api.TMI_MainOutput(DeviceId, 0);

                // リモート状態をローカルに戻す
                TMI_Api.TMI_RemoteLocal(DeviceId);

                // Deviceクローズ
                TMI_Api.TMI_HandleClose(DeviceId);

                DeviceId = 0;
            }
        }

        // Device Open
        private void button1_Click(object sender, EventArgs e)
        {
            // Device open
            if (button1.Text == "OPEN")
            {
                // 電源名指定文字列を作成。
                // 「IF種類」+ ":" +「PCアドレス」+ ":" +「システムアドレス」
                string Model = "";
                switch (comboBox1.Text)
                {
                    case "USB": Model = "USB:"; break;
                    case "GPIB0": Model = "DEV0:"; break;
                    case "COM1": Model = "COM1:"; break;
                }
                Model = Model + comboBox2.Text + ":" + comboBox3.Text;

                // デバイスオープン
                DeviceId = TMI_Api.TMI_HandleOpen("PW-A", Model);

                if (DeviceId > 0)
                {
                    // OutputをOFF
                    TMI_Api.TMI_MainOutput(DeviceId, 0);

                    // 出力CHAのアウトプットセレクトをONに設定
                    TMI_Api.TMI_OutputSel(DeviceId, 1, 1);

                    // 出力CHAをLEDディスプレイに表示
                    TMI_Api.TMI_Display(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
            {
                // OutputをOFFに設定する
                TMI_Api.TMI_MainOutput(DeviceId, 0);

                // リモート状態をローカルに戻す
                TMI_Api.TMI_RemoteLocal(DeviceId);

                // Deviceクローズ
                TMI_Api.TMI_HandleClose(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)
        {
            if(button2.Text == "ON")
            {
                // OutputをONに設定
                TMI_Api.TMI_MainOutput(DeviceId, 1);

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

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

        // Preset 1
        private void button3_Click(object sender, EventArgs e)
        {
            // Preset 1を選択
            TMI_Api.TMI_Preset(DeviceId, 1);
        }

        // Preset 2
        private void button4_Click(object sender, EventArgs e)
        {
            // Preset 2を選択
            TMI_Api.TMI_Preset(DeviceId, 2);
        }

        // Preset 3
        private void button5_Click(object sender, EventArgs e)
        {
            // Preset 3を選択
            TMI_Api.TMI_Preset(DeviceId, 3);
        }

        // Preset 4
        private void button6_Click(object sender, EventArgs e)
        {
            // Preset 4を選択
            TMI_Api.TMI_Preset(DeviceId, 4);
        }

        // Read
        private void button7_Click(object sender, EventArgs e)
        {
            double MVoltage = 0;
			double MCurrent = 0;
            byte MCvcc = 0;

            // モニタ値をリード
			TMI_Api.TMI_MoniDataQ(DeviceId, 1, out MVoltage, out MCurrent, out MCvcc);

            // リードしたモニタ電圧、モニタ電流、CV/CC状態を表示
            StringBuilder sb = new StringBuilder();
            sb.Append("V : ");
            sb.Append(MVoltage.ToString("#00.000"));
            sb.AppendLine(" V");
            sb.Append("C : ");
            sb.Append(MCurrent.ToString("#00.000"));
            sb.AppendLine(" A");
            sb.Append("CVCC : ");
            if( MCvcc == 0)
            {
                sb.Append("CV");
            }
            else
            {
                sb.Append("CC");
            }
            textBox1.Text = sb.ToString();
        }

		private void API_test()
		{
			int iRet;
			byte Ch_A = 0;
			byte Pre_1 = 0;
			double Voltage = 0;
			double Current = 0;
			byte cv_cc = 0;
			byte Addr = 0;
			byte Output = 0;
			byte DelayOn = 0;
			double DelayValue = 0;
			byte TrackOn = 0;
			byte v_Set = 0;
			byte TrackGroup = 0;
			double TrackValue = 0;
			StringBuilder PName = new StringBuilder(256);
			//double[] PresetData = new double[32];
			StringBuilder strPresetData = new StringBuilder(256);
			StringBuilder Que = new StringBuilder(256);

			Voltage = 3.124;
			Current = 1.235;
			Ch_A = 1;
			Pre_1 = 1;
			Output = 1;
			iRet = TMI_Api.TMI_ModelNameQ(DeviceId, PName);
			iRet = TMI_Api.TMI_TimeOut(DeviceId, 5);
			//iRet = TMI_Api.TMI_AllPresetQ(DeviceId, out PresetData[0]);
			iRet = TMI_Api.TMI_AllPresetQS(DeviceId, strPresetData);

			iRet = TMI_Api.TMI_DataBackUp(DeviceId);
			iRet = TMI_Api.TMI_SRQEnable(DeviceId, 1);

			iRet = TMI_Api.TMI_Refresh(DeviceId);
			iRet = TMI_Api.TMI_Voltage(DeviceId, Ch_A, Pre_1, Voltage);
			iRet = TMI_Api.TMI_VoltageQ(DeviceId, Ch_A, Pre_1, out Voltage);
			iRet = TMI_Api.TMI_Current(DeviceId, Ch_A, Pre_1, Current);
			iRet = TMI_Api.TMI_CurrentQ(DeviceId, Ch_A, Pre_1, out Current);
			iRet = TMI_Api.TMI_MainOutput(DeviceId, Output);
			iRet = TMI_Api.TMI_MainOutputQ(DeviceId, out Output);
			iRet = TMI_Api.TMI_OutputSel(DeviceId, Ch_A, Output);
			iRet = TMI_Api.TMI_OutputSelQ(DeviceId, Ch_A, out Output);

			iRet = TMI_Api.TMI_Display(DeviceId, Ch_A);
			iRet = TMI_Api.TMI_DisplayQ(DeviceId, out Ch_A);
			iRet = TMI_Api.TMI_Preset(DeviceId, Pre_1);
			iRet = TMI_Api.TMI_PresetQ(DeviceId, out Pre_1);
			iRet = TMI_Api.TMI_MoniDataQ(DeviceId, Ch_A, out Voltage, out Current, out cv_cc);

			iRet = TMI_Api.TMI_TrackingOnOff(DeviceId, TrackOn);
			iRet = TMI_Api.TMI_TrackingOnOffQ(DeviceId, out TrackOn);
			iRet = TMI_Api.TMI_TrackingMode(DeviceId, TrackOn);
			iRet = TMI_Api.TMI_TrackingModeQ(DeviceId, out TrackOn);
			iRet = TMI_Api.TMI_TrackingGroup(DeviceId, Ch_A, TrackGroup);
			iRet = TMI_Api.TMI_TrackingGroupQ(DeviceId, Ch_A, out TrackGroup);
			iRet = TMI_Api.TMI_TrackingData(DeviceId, Ch_A, v_Set, TrackValue);
			iRet = TMI_Api.TMI_TrackingDataQ(DeviceId, Ch_A, v_Set, out TrackValue);

			iRet = TMI_Api.TMI_Delay(DeviceId, DelayOn);
			iRet = TMI_Api.TMI_DelayQ(DeviceId, out DelayOn);
			iRet = TMI_Api.TMI_DelayTime(DeviceId, Ch_A, DelayValue);
			iRet = TMI_Api.TMI_DelayTimeQ(DeviceId, Ch_A, out DelayValue);

			iRet = TMI_Api.TMI_AdrQ(DeviceId, out Addr);
			iRet = TMI_Api.TMI_RemoteLocal(DeviceId);

			iRet = TMI_Api.TMI_Out(DeviceId, "ST3");
			iRet = TMI_Api.TMI_In(DeviceId, Que);

			iRet = TMI_Api.TMI_LocalLockOut(DeviceId);

		}
		private void Disp_Log(string dispLog)
		{

			/// ログ出力フォルダ存在チェック
			if (Directory.Exists("C:\\Temp") == false)
			{
				/// ログ出力フォルダ作成
				Directory.CreateDirectory("C:\\Temp");
			}

			string date = DateTime.Now.ToString("yyyyMMdd");
			string time = DateTime.Now.ToLongTimeString();
			string path = string.Format(@"{0}\{1}.log", "C:\\Temp", date);
			StreamWriter sw;

			/// ファイル存在チェック
			if (File.Exists(path))
			{
				/// 追加
				sw = File.AppendText(path);
			}
			else
			{
				/// 新規
				sw = File.CreateText(path);
			}

			sw.Write("[{0}] : ", time);
			sw.WriteLine(dispLog);
			sw.Close();

		}
	}
}
