//***********************************************************************/
//*                                                                     */
//*     IF-40 API Sample Program                                        */
//*     Copyright(c) 2012  TEXIO TECHNOLOGY CORPORATION                 */
//***********************************************************************/

#pragma once

#include "Tmi_Api_m.h"

namespace PWA_Access {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Runtime::InteropServices;

	/// <summary>
	/// PWA_Access の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class PWA_Access : public System::Windows::Forms::Form
	{
	public:
		PWA_Access(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//
		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~PWA_Access()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::GroupBox^  groupBox1;
	protected: 
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::ComboBox^  comboBox3;
	private: System::Windows::Forms::ComboBox^  comboBox2;
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::GroupBox^  groupBox2;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::GroupBox^  groupBox3;
	private: System::Windows::Forms::Button^  button6;
	private: System::Windows::Forms::Button^  button5;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Button^  button7;
	private: System::Windows::Forms::TextBox^  textBox1;


	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(PWA_Access::typeid));
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->comboBox3 = (gcnew System::Windows::Forms::ComboBox());
			this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->groupBox3 = (gcnew System::Windows::Forms::GroupBox());
			this->button6 = (gcnew System::Windows::Forms::Button());
			this->button5 = (gcnew System::Windows::Forms::Button());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->button7 = (gcnew System::Windows::Forms::Button());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->groupBox1->SuspendLayout();
			this->groupBox2->SuspendLayout();
			this->groupBox3->SuspendLayout();
			this->SuspendLayout();
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->label3);
			this->groupBox1->Controls->Add(this->label2);
			this->groupBox1->Controls->Add(this->label1);
			this->groupBox1->Controls->Add(this->comboBox3);
			this->groupBox1->Controls->Add(this->comboBox2);
			this->groupBox1->Controls->Add(this->comboBox1);
			this->groupBox1->Controls->Add(this->button1);
			this->groupBox1->Location = System::Drawing::Point(12, 12);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(349, 66);
			this->groupBox1->TabIndex = 0;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"Device";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(271, 19);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(62, 12);
			this->label3->TabIndex = 6;
			this->label3->Text = L"SYS_ADDR";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(186, 19);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(56, 12);
			this->label2->TabIndex = 5;
			this->label2->Text = L"PC_ADDR";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(105, 19);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(50, 12);
			this->label1->TabIndex = 4;
			this->label1->Text = L"Interface";
			// 
			// comboBox3
			// 
			this->comboBox3->FormattingEnabled = true;
			this->comboBox3->Location = System::Drawing::Point(273, 34);
			this->comboBox3->Name = L"comboBox3";
			this->comboBox3->Size = System::Drawing::Size(60, 20);
			this->comboBox3->TabIndex = 3;
			// 
			// comboBox2
			// 
			this->comboBox2->FormattingEnabled = true;
			this->comboBox2->Location = System::Drawing::Point(188, 34);
			this->comboBox2->Name = L"comboBox2";
			this->comboBox2->Size = System::Drawing::Size(66, 20);
			this->comboBox2->TabIndex = 2;
			// 
			// comboBox1
			// 
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Location = System::Drawing::Point(107, 34);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(63, 20);
			this->comboBox1->TabIndex = 1;
			this->comboBox1->Text = L"USB";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(18, 32);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(71, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"OPEN";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &PWA_Access::button1_Click);
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->button2);
			this->groupBox2->Location = System::Drawing::Point(14, 88);
			this->groupBox2->Name = L"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(103, 52);
			this->groupBox2->TabIndex = 1;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = L"Output";
			// 
			// button2
			// 
			this->button2->Enabled = false;
			this->button2->Location = System::Drawing::Point(17, 18);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(70, 25);
			this->button2->TabIndex = 0;
			this->button2->Text = L"ON";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &PWA_Access::button2_Click);
			// 
			// groupBox3
			// 
			this->groupBox3->Controls->Add(this->button6);
			this->groupBox3->Controls->Add(this->button5);
			this->groupBox3->Controls->Add(this->button4);
			this->groupBox3->Controls->Add(this->button3);
			this->groupBox3->Location = System::Drawing::Point(132, 91);
			this->groupBox3->Name = L"groupBox3";
			this->groupBox3->Size = System::Drawing::Size(229, 49);
			this->groupBox3->TabIndex = 2;
			this->groupBox3->TabStop = false;
			this->groupBox3->Text = L"Preset";
			// 
			// button6
			// 
			this->button6->Enabled = false;
			this->button6->Location = System::Drawing::Point(174, 17);
			this->button6->Name = L"button6";
			this->button6->Size = System::Drawing::Size(39, 21);
			this->button6->TabIndex = 3;
			this->button6->Text = L"4";
			this->button6->UseVisualStyleBackColor = true;
			this->button6->Click += gcnew System::EventHandler(this, &PWA_Access::button6_Click);
			// 
			// button5
			// 
			this->button5->Enabled = false;
			this->button5->Location = System::Drawing::Point(123, 18);
			this->button5->Name = L"button5";
			this->button5->Size = System::Drawing::Size(39, 21);
			this->button5->TabIndex = 2;
			this->button5->Text = L"3";
			this->button5->UseVisualStyleBackColor = true;
			this->button5->Click += gcnew System::EventHandler(this, &PWA_Access::button5_Click);
			// 
			// button4
			// 
			this->button4->Enabled = false;
			this->button4->Location = System::Drawing::Point(68, 18);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(39, 21);
			this->button4->TabIndex = 1;
			this->button4->Text = L"2";
			this->button4->UseVisualStyleBackColor = true;
			this->button4->Click += gcnew System::EventHandler(this, &PWA_Access::button4_Click);
			// 
			// button3
			// 
			this->button3->Enabled = false;
			this->button3->Location = System::Drawing::Point(14, 18);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(39, 21);
			this->button3->TabIndex = 0;
			this->button3->Text = L"1";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &PWA_Access::button3_Click);
			// 
			// button7
			// 
			this->button7->Enabled = false;
			this->button7->Location = System::Drawing::Point(31, 154);
			this->button7->Name = L"button7";
			this->button7->Size = System::Drawing::Size(70, 27);
			this->button7->TabIndex = 3;
			this->button7->Text = L"READ";
			this->button7->UseVisualStyleBackColor = true;
			this->button7->Click += gcnew System::EventHandler(this, &PWA_Access::button7_Click);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(132, 158);
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(229, 85);
			this->textBox1->TabIndex = 4;
			// 
			// PWA_Access
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(377, 251);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->button7);
			this->Controls->Add(this->groupBox3);
			this->Controls->Add(this->groupBox2);
			this->Controls->Add(this->groupBox1);
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->Name = L"PWA_Access";
			this->Text = L"PWA_Access";
			this->Load += gcnew System::EventHandler(this, &PWA_Access::PWA_Access_Load);
			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &PWA_Access::PWA_Access_FormClosing);
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->groupBox2->ResumeLayout(false);
			this->groupBox3->ResumeLayout(false);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

	private: int DeviceId;           // オープンしたデバイスのID。

    // フォームロード
	private: System::Void PWA_Access_Load(System::Object^  sender, System::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: System::Void PWA_Access_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e)
	{
        if (DeviceId != 0)
        {
            // OutputをOFFに設定する。
            TMI_MainOutput(DeviceId, 0);

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

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

            DeviceId = 0;
        }
	}

	// Device Open
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
	{
        // Device open
        if (button1->Text == "OPEN")
        {
            // 電源名指定文字列を作成。
            // 「IF種類」+ ":" +「PCアドレス」+ ":" +「システムアドレス」
            String^ Model = "";
            if(comboBox1->Text == "USB")
			{
                Model = "USB:";
			}
			else if(comboBox1->Text == "GPIB0")
			{
                Model = "DEV0:";
			}
			else if(comboBox1->Text == "COM1")
			{
                Model = "COM1:";
            }
            Model = Model + comboBox2->Text + ":" + comboBox3->Text;

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

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

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

				// 出力CHAをLEDディスプレイに表示
				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_MainOutput(DeviceId, 0);

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

            // Deviceクローズ
            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: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
	{
        if(button2->Text == "ON")
        {
            // OutputをONに設定
            TMI_MainOutput(DeviceId, 1);

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

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

    // Preset 1
	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e)
	{
        // Preset 1を選択
        TMI_Preset(DeviceId, 1);
	}

    // Preset 2
	private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e)
	{
        // Preset 2を選択
        TMI_Preset(DeviceId, 2);
	}

    // Preset 3
	private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e)
	{
        // Preset 3を選択
        TMI_Preset(DeviceId, 3);
	}

    // Preset 4
	private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e)
	{
        // Preset 4を選択
        TMI_Preset(DeviceId, 4);
	}

    // Read
	private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e)
	{
        double MVoltage;
        double MCurrent;
        unsigned char MCvcc;

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

        // リードしたモニタ電圧、モニタ電流、CV/CC状態を表示
        StringBuilder^ sb = gcnew 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;
        unsigned char Ch_A;
        unsigned char Pre_1;
        double Voltage;
        double Current;
        unsigned char cv_cc;
        unsigned char Addr;
        unsigned char Output;
        unsigned char DelayOn = 0;
        double DelayValue = 0;
        unsigned char TrackOn = 0;
        unsigned char v_Set = 0;
        unsigned char TrackGroup = 0;
        double TrackValue = 0;
        StringBuilder^ PName = gcnew StringBuilder(256);
        StringBuilder^ strPresetData = gcnew StringBuilder(256);
		array<double>^ PresetData = gcnew array<double>(32);
        StringBuilder^ Que = gcnew StringBuilder(256);

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

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

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

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

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

        iRet = TMI_AdrQ(DeviceId, Addr);
        iRet = TMI_RemoteLocal(DeviceId);

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

        iRet = TMI_LocalLockOut(DeviceId);
    }
};
}

