using CtimerwCs;
using System.Runtime.InteropServices;

namespace IntervalCallback
{
    public partial class Form1 : Form
    {
        Ctimerw timerw = new Ctimerw();

        public uint interval_count;
        public short TimerId;
        public GCHandle gCh;
        public PTM_INTERVAL_CALLBACK pdelegate_func;
        public IntPtr pfunc;

        unsafe public Form1()
        {
            InitializeComponent();
            //-----------------------------------
            // Initialize the delegate for Interval Timer event processing
            //-----------------------------------
            pdelegate_func = new PTM_INTERVAL_CALLBACK(CallBackProc);
        }

        private void IntervalCallback_Load(object sender, System.EventArgs e)
        {
            Text_Interval.Text = "1000";
            Button_TimStopTimer.Enabled = false;

            //-----------------------------------
            // Do not release the delegate
            //-----------------------------------
            if (gCh.IsAllocated == false)
            {
                gCh = GCHandle.Alloc(pdelegate_func);
            }
        }

        //==============================================================================
        // TimInit
        //==============================================================================
        private void Button_TimInit_Click(object sender, System.EventArgs e)
        {
            string DeviceName;
            string ProductName;
            string ErrorString;
            int ret;

            //-----------------------------------
            // Get Device Name
            //-----------------------------------
            DeviceName = Text_DeviceName.Text;
            //-----------------------------------
            // Initialization
            //-----------------------------------
            ret = timerw.Init(ref DeviceName, out ProductName);
            //-----------------------------------
            // Error handle
            //-----------------------------------
            timerw.GetErrorString(ret, out ErrorString);
            Text_Ret.Text = "TimInit ret = " + System.Convert.ToString(ret) + " " + ErrorString;
            if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS)
            {
                Text_DeviceName.Text = DeviceName;
                Label_ProductName.Text = ProductName;
            }
        }

        //==============================================================================
        // TimStartTimer
        //==============================================================================
        unsafe private void Button_TimStartTimer_Click(object sender, System.EventArgs e)
        {
            string ErrorString;
            uint Microseconds;
            int ret;

            //-----------------------------------
            // Interval Count Initialization
            //-----------------------------------
            interval_count = 0;
            //-----------------------------------
            // Get the specified value of Interval
            //-----------------------------------
            Microseconds = System.Convert.ToUInt32(Text_Interval.Text);
            //-----------------------------------
            // Start Interval Timer 
            //-----------------------------------
            ret = timerw.StartTimer(out TimerId, Microseconds, this.Handle.ToInt32());
            //-----------------------------------
            // Error handle
            //-----------------------------------
            timerw.GetErrorString(ret, out ErrorString);
            Text_Ret.Text = "TimStartTimer ret = " + System.Convert.ToString(ret) + " " + ErrorString;
            if (ret != (int)CtimerwConst.TIM_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------------
            // Get the fixed pointer of the delegate for for Interval Timer event processing
            //-----------------------------------
            pfunc = Marshal.GetFunctionPointerForDelegate(pdelegate_func);
            //-----------------------------------
            // Interval Timer Callback registration
            //-----------------------------------
            fixed (short* Temp = &TimerId)
                ret = timerw.TimerCallBackProc(TimerId, pfunc, Temp);
            //-----------------------------------
            // Error handle
            //-----------------------------------
            timerw.GetErrorString(ret, out ErrorString);
            Text_Ret.Text = "TimTimerCallBackProc ret = " + System.Convert.ToString(ret) + " " + ErrorString;
            if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS)
            {
                //-----------------------------------
                // Enable the TimStopTimer button 
                //-----------------------------------
                Button_TimStopTimer.Enabled = true;
                //-----------------------------------
                // Disable the TimStartTimer button
                //-----------------------------------
                Button_TimStartTimer.Enabled = false;
            }
        }

        //==============================================================================
        // TimStopTimer
        //==============================================================================
        private void Button_TimStopTimer_Click(object sender, System.EventArgs e)
        {
            string ErrorString;
            int ret;

            //-----------------------------------
            // Stop Interval Timer 
            //-----------------------------------
            ret = timerw.StopTimer(TimerId);
            //-----------------------------------
            // Error handle
            //-----------------------------------
            timerw.GetErrorString(ret, out ErrorString);
            Text_Ret.Text = "TimStopTimer ret = " + System.Convert.ToString(ret) + " " + ErrorString;
            if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS)
            {
                //-----------------------------------
                // Disable the TimStopTimer button 
                //-----------------------------------
                Button_TimStopTimer.Enabled = false;
                //-----------------------------------
                // Enable the TimStartTimer button 
                //-----------------------------------
                Button_TimStartTimer.Enabled = true;
            }
        }

        //==============================================================================
        // TimExit
        //==============================================================================
        private void Button_TimExit_Click(object sender, System.EventArgs e)
        {
            string ErrorString;
            int ret;

            //-----------------------------------
            // Exit handle
            //-----------------------------------
            ret = timerw.Exit();
            //-----------------------------------
            // Error handle
            //-----------------------------------
            timerw.GetErrorString(ret, out ErrorString);
            Text_Ret.Text = "TimExit ret = " + System.Convert.ToString(ret) + " " + ErrorString;
            //-----------------------------------
            // Disable the TimStopTimer button 
            //-----------------------------------
            Button_TimStopTimer.Enabled = false;
            //-----------------------------------
            // Enable the TimStartTimer button 
            //-----------------------------------
            Button_TimStartTimer.Enabled = true;
        }

        //==============================================================================
        // Exit
        //==============================================================================
        private void Button_Exit_Click(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // Release the garbage collection handle of the delegate for Interval Timer event processing
            //-----------------------------------
            if (gCh.IsAllocated == true)
            {
                gCh.Free();
            }
            this.Close();
        }

        delegate void SetCountDelegate();
        //==============================================================================
        // Synchronous function for control operation
        //==============================================================================
        void SetCountText()
        {
            Label_CallbackCount.Text = System.Convert.ToString(interval_count);
        }

        //==============================================================================
        // Callback routine - Interval Timer event processing
        //==============================================================================
        unsafe public void CallBackProc(short TimerId, int wParam, int lParam, void* Param)
        {
            interval_count++;
            //-----------------------------------
            // Show the count value
            //-----------------------------------
            this.Invoke(new SetCountDelegate(SetCountText));
            return;
        }
    }
}