using System;
namespace HMS_MiCom2022_MAC23_MAC
{
    internal class HMS_MiCom2022_MAC23_MAC34
    {
        static void Main()
        {
            string[] Arg = Environment.GetCommandLineArgs();
            string ComName = "COM19";
            int status;


            Display("HMS_MiCom2022_MAC23_MAC.exe   ==> how to use MiCom_v2022 for MAC23/MAC34 with standart commands");
            Display();
            Display("Usage : HMS_MiCom2022_MAC23_MAC \"comName\"  ");

            // contrôle / recherche du COMPORT sur la ligne d'appel de l'éxécutable
            if (Arg.Length == 1)
            {
                Display("no com name speficied in the argument list , so " + ComName + " is use");
            }
            else
            {
                ComName = Arg[1];
                Display(ComName + " will be use as COM");
            }

            // usage du myCom en tant qu'instance du type MiCom_v2022
            // the MiCom_v2022.dll assembly is referenced  project:
            // so the next line is valid and 'myCom' will be the instance of the MiCom_v2022 type
            Midi_Ingenierie.MiComm_2022 myCom = new Midi_Ingenierie.MiComm_2022();
            Display();
            Display("myCom is the instance of MiCom_v2022 type , from C# new Midi_Ingenierie.MiComm_2022() ");
            Display();

            // identification de l'assembly
            Display("Check version and copyright : ");
            Display("MiComm_2022.Identity := " + Midi_Ingenierie.MiComm_2022.Identity);
            Display();

            // initialisation d'une ligne de com sur un driver MAC23
            Display("Open Com Line :");
            Display("myCom.Open(\"MAC23\",\"" + ComName + "\",\"38400\")");
            status = myCom.Open("MAC23", ComName, "38400");
            //status MUST be 0x0000
            if (status != 0)
            {
                Display("Status :" + status.ToString("X4"));
                Display("Bad Status !! check ComPort");
                Quit();
                return;
            }

            //commande standart vs commande mode EXPERT
            //00mr  
            Display();
            Display("commande INITIALISATION module  ==>myCom.Send(\"00mr\")");
            myCom.Send("00mr");
            status = myCom.Status;
            if (status > 0x8000)
            {
                Display("Status :" + status.ToString("X4"));
                Display("Bad Status !! check Com , alimentation, connection ....");
                Quit();
                return;
            }

            ConsoleKey key_ = Ask("Start motor, only if motor is FREE  [Y/N] ?");
            if (key_ == ConsoleKey.Y)
            {
                Display();

                //00gi 20  page 9
                Display("commande LIMITATION du COUPLE     ==> myCom.Send(\"00gi 20\")");
                myCom.Send("00gi 20");

                //00di     page 8
                Display("commande RAZ POSITION             ==> myCom.Send(\"00di\")");
                myCom.Send("00di");

                //00gf 3000  page 13
                Display("commande MVT à VITESSE 300tr/mn   ==> myCom.Send(\"00gf 3000\")");
                myCom.Send("00gf 3000");
                Delay(100);

                //00gf 4000
                Display("commande MVT à VITESSE 400tr/mn   ==> myCom.Send(\"00gf 4000\")");
                myCom.Send("00gf 4000");
                Delay(100);

                //00gf 2500
                Display("commande MVT à VITESSE 250tr/mn   ==> myCom.Send(\"00gf 2500\")");
                myCom.Send("00gf 2500");
                Delay(100);

                //00gf 3000
                Display("commande MVT à VITESSE 300tr/mn   ==> myCom.Send(\"00gf 3000\")");
                myCom.Send("00gf 3000");
                Delay(100);

                //00gf 4000
                Display("commande MVT à VITESSE 400tr/mn   ==> myCom.Send(\"00gf 4000\")");
                myCom.Send("00gf 4000");
                Delay(100);

                //00gf 2500
                Display("commande MVT à VITESSE 250tr/mn   ==> myCom.Send(\"00gf 2500\")");
                myCom.Send("00gf 2500");
                Delay(100);

                //00gf 4000
                Display("commande MVT à VITESSE 400tr/mn   ==> myCom.Send(\"00gf 4000\")");
                myCom.Send("00gf 4000");
                Delay(100);

                //00ge  
                Display("commande ARRêt avec DéCéLéRATION  ==> myCom.Send(\"00ge\")");
                myCom.Send("00ge");
                Delay(1000);

                //00gr  
                Display("commande POWER OFF                ==> myCom.Send(\"00gr\")");
                myCom.Send("00gr");
            }
            Quit();
        }

        private static void Delay(int delay)           => System.Threading.Thread.Sleep(delay);
        private static void Display(string Texte = "") => Console.WriteLine(Texte);
        private static void Quit()                     => Ask("[ESC] = All is done ,Quit the program");

        private static ConsoleKey Ask(string Texte)
        {
            Console.WriteLine();
            Console.Write(Texte);
            return Console.ReadKey().Key;
        }
    }
}

    

