/* ---------------------------------------------------------------------------
 * test_control.cs                                      (c) 2009 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: This file handles all Actions of the "Test" Group
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Jan 08, 2009, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System use files
 * ---------------------------------------------------------------------------
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;


/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

namespace QUA_2475_designtest
{
    public partial class mainWindow
    {
        private void TestIsUntested(System.UInt32 TestID)
        {
            /* Resets the to the parameter corresponding Textbox            */
            TestTextBoxes[TestID].Text = "untested";
            TestTextBoxes[TestID].BackColor = System.Drawing.Color.White;
        }

        private void TestIsActive(System.UInt32 TestID)
        {
            /* Sets status of the corresponding TextBox to active           */
            TestTextBoxes[TestID].Text = "Active";
            TestTextBoxes[TestID].BackColor = System.Drawing.Color.Yellow;
        }

        private void TestIsPassed(System.UInt32 TestID)
        {
            /* Sets status of the corresponding TextBox to a passed test    */
            TestTimeoutThread.Abort();  /* Abort Timeout Thread             */
            TestTextBoxes[TestID].Text = "Passed";
            TestTextBoxes[TestID].BackColor = System.Drawing.Color.Green;

            /* If Test was called by automatic test routine, go back there  */
            if (activeAutomatictest == true)
            {
                //automaticTest();
            }
        }

        private void TestIsFailed(System.UInt32 TestID)
        {
            /* Sets status of the corresponding TextBox to a failed test    */
            TestTimeoutThread.Abort();  /* Abort Timeout Thread             */
            TestTextBoxes[TestID].Text = "Failed";
            TestTextBoxes[TestID].BackColor = System.Drawing.Color.Red;

            /* If Test was called by automatic test routine, go back there  */
            if (activeAutomatictest == true)
            {
                //automaticTest();
            }
        }

        private void TestIsUnavailable(System.UInt32 TestID)
        {
            /* Sets status of the corresponding TextBox to unreachable test */
            TestTimeoutThread.Abort();  /* Abort Timeout Thread             */
            TestTextBoxes[TestID].Text = "unavailable";
            TestTextBoxes[TestID].BackColor = System.Drawing.Color.CornflowerBlue;

            /* If Test was called by automatic test routine, go back there  */
            if (activeAutomatictest == true)
            {
                //automaticTest();
            }
        }

        delegate void TestIsTimeoutCallback(System.UInt32 TestID);
        private void TestIsTimeout(System.UInt32 TestID)
        {
            /* Check if Application is called from a subThread              */
            if (this.TestTextBoxes[TestID].InvokeRequired)
            {
                /* Handle Parameters to main Thread                         */
                TestIsTimeoutCallback d = new TestIsTimeoutCallback(TestIsTimeout);
                this.Invoke(d, new object[] { TestID });
            }
            else
            {
                /* Set corresponding TextBox to Timeout Status              */
                TestTextBoxes[TestID].Text = "Timeout";
                TestTextBoxes[TestID].BackColor = System.Drawing.Color.DarkOrange;
                activeTest = false;

                /* If Test was called by automatic test routine, go there   */
                if (activeAutomatictest == true)
                {
                    //automaticTest();
                }
            }
        }

        private void B_test_MB_LED_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.MB_LED);
        }

        private void B_test_MB_EEPROM_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.MB_EEPROM);
        }

        private void B_test_eth0_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.ETH0);
        }

        private void B_test_eth1_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.ETH1);
        }

        private void B_test_CF_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.CF);
        }

        private void B_test_USB_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.USB);
        }

        private void B_test_MB_DIO_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.MB_DIO);
        }

        private void B_test_MB_AIO_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.MB_AIO);
        }

        private void B_test_MB_RLY_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.MB_RLY);
        }

        private void B_test_CAN_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.CAN);
        }

        private void B_test_EB_LED_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.EB_LED);
        }

        private void B_test_EB_EEPROM_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.EB_EEPROM);
        }

        private void B_test_EB_DIO_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.EB_DIO);
        }

        private void B_test_EB_AIO_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.EB_AIO);
        }

        private void B_test_EB_RLY_Click(object sender, EventArgs e)
        {
            /* CallTestRoutine corresponding TestResult with Parameter      */
            CallTestRoutine((int)TestType.EB_RLY);
        }

        private void B_test_clear_Click(object sender, EventArgs e)
        {
            /* If no Test is Active:                                        */
            if (activeTest == false)
            {
                /* Reset all Test TextBoxes to untested status              */
                for (uint loopcnt = 0; loopcnt < NrOfTests; loopcnt++)
                {
                    T_MasterOutput.BackColor = System.Drawing.Color.White;
                    TestIsUntested(loopcnt);
                }

            }
        }


        private void CallTestRoutine(uint TestID)
        {
            /* Call Test just if no other Test is active                    */
            if (activeTest == false)
            {
                actualTest = TestID;    /* Set actual Test ID to active Test*/
                /* Define and Start Timeout Thread                          */
                TestTimeoutThread = new Thread(new ThreadStart(TimeoutThread));
                TestTimeoutThread.Start();

                activeTest = true;      /* Set Test to global active        */
                com1.Write(TestCallCommands[actualTest]); /* Call Test      */
                TestIsActive(actualTest);   /* Set Test TextBox             */
            }
        }
    }
}
