﻿using CaioCs;

namespace Timer
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // クラスCaioインスタンス作成
        //----------------------------------------
        Caio aio = new Caio();

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short g_id;             // デバイスアクセス用ID
        public short g_timer_id;       // タイマID
        public int   g_event_count;    // インターバル成立イベント発生回数

        public Form1()
        {
            InitializeComponent();
        }

        private void Timer_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // インターバルの初期表示を更新
            //----------------------------------------
            textBox_Interval.Text = "1000";
            //----------------------------------------
            // 通知回数の初期表示を更新
            //----------------------------------------
            textBox_EventCount.Text = "0";
            //----------------------------------------
            // 経過時間の初期表示を更新
            //----------------------------------------
            textBox_LapTime.Text = "0";
            //----------------------------------------
            // グローバル変数の初期化
            //----------------------------------------
            g_id          = 0;
            g_timer_id    = 0;
            g_event_count = 0;
        }

        //================================================================================
        // AioInitボタンをクリックした際の処理になります
        // デバイスの初期化処理を行います
        //================================================================================
        private void button_AioInit_Click(object sender, EventArgs e)
        {
            string device_name;     // デバイス名
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // デバイス名をテキストボックスから取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // デバイス初期化
            // 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.Init(device_name, out g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Init = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioResetDeviceボタンをクリックした際の処理になります
        // デバイスのリセット処理を行います
        //================================================================================
        private void button_AioResetDevice_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // デバイスのリセット、ドライバの初期化
            //----------------------------------------
            ret = aio.ResetDevice(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // 動作開始(インターバルタイマー)ボタンをクリックした際の処理になります
        // 動作開始します
        //================================================================================
        private void button_IntervalStart_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            float  interval;        // インターバル

            //----------------------------------------
            // イベント要因の設定：インターバル成立イベント
            //----------------------------------------
            ret = aio.SetTmEvent(g_id, g_timer_id, Handle, (int)CaioConst.TME_INT);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "動作開始：aio.SetTmEvent = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 通知回数を初期化
            //----------------------------------------
            g_event_count = 0;
            //----------------------------------------
            // テキストボックスからインターバルを取得
            //----------------------------------------
            if (float.TryParse(textBox_Interval.Text, out interval) == false)
            {
                interval = 1000;
            }
            //----------------------------------------
            // 動作開始
            //----------------------------------------
            ret = aio.StartTmTimer(g_id, g_timer_id, interval);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "動作開始：aio.StartTmTimer = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 動作開始完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "動作開始：正常終了";
        }

        //================================================================================
        // 動作停止(インターバルタイマー)ボタンをクリックした際の処理になります
        // 動作停止します
        //================================================================================
        private void button_IntervalStop_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 動作停止
            //----------------------------------------
            ret = aio.StopTmTimer(g_id, g_timer_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "動作停止：aio.StopTmTimer = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 動作停止完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "動作停止：正常終了";
        }

        //================================================================================
        // 動作開始(ストップウォッチタイマー)ボタンをクリックした際の処理になります
        // 測定開始します
        //================================================================================
        private void button_WatchStart_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 測定開始
            //----------------------------------------
            ret = aio.StartTmCount(g_id, g_timer_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "動作開始：aio.StartTmCount = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 測定開始完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "動作開始：正常終了";
        }

        //================================================================================
        // AioLapTmCountボタンをクリックした際の処理になります
        // 経過時間を取得します
        //================================================================================
        private void button_LapTmCount_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            int    lap;             // 経過時間

            //----------------------------------------
            // 経過時間を取得
            //----------------------------------------
            ret = aio.LapTmCount(g_id, g_timer_id, out lap);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.LapTmCount = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // 経過時間取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //----------------------------------------
                // 経過時間を表示
                //----------------------------------------
                textBox_LapTime.Text = lap.ToString();
            }
        }

        //================================================================================
        // AioResetTmCountボタンをクリックした際の処理になります
        // 経過時間をリセットします
        //================================================================================
        private void button_ResetTmCount_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // リセット
            //----------------------------------------
            ret = aio.ResetTmCount(g_id, g_timer_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.ResetTmCount = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // 動作停止(ストップウォッチタイマー)ボタンをクリックした際の処理になります
        // 測定停止します
        //================================================================================
        private void button_WatchStop_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 測定停止
            //----------------------------------------
            ret = aio.StopTmCount(g_id, g_timer_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "動作停止：aio.StopTmCount = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 測定停止完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "動作停止：正常終了";
        }

        //================================================================================
        // AioExitボタンをクリックした際の処理になります
        // デバイスの終了処理を行います
        //================================================================================
        private void button_AioExit_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            ret = aio.Exit(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Exit = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // 閉じるをクリックした際の処理になります
        // デバイスの終了処理を行い、アプリケーションを終了します
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            aio.Exit(g_id);
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // イベントが発生した時の動作
        // 各イベントが発生したとき、イベント毎に処理を行います
        //================================================================================
        protected override void WndProc(ref Message m)
        {
            //----------------------------------------
            // インターバル成立イベント発生の処理
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_TME_INT)
            {
                //----------------------------------------
                // インターバル成立イベント発生回数の表示
                //----------------------------------------
                g_event_count++;
                textBox_EventCount.Text = g_event_count.ToString();
                return;
            }
            base.WndProc(ref m);
        }
    }
}