using ICC4cPwmSdk.Device;
using ICC4cPwmSdk.Dtos;
using System;
using System.Threading;

namespace Icc4c_SimpleExample
{
    internal class SMAExample
    {
        // To run this example, rename SMAMain to Main and in Example.cs rename Main to ExMain for example
        // static void Main(string[] args) -> is always an entry point
        static void SMAMain(string[] args)
        {
            var controller = new ICC4cPwmSdkDeviceController();
            // Change comport number to correct value
            controller.Connect(14);

            // List all connected devices
            var connectedDevices = controller.GetConnectedDeviceTypes();

            // Control lens connected to channel 0 
            // Set input system to Static input
            controller.InputStage.ChangeActiveSystem(EChannel.Channel0, EInputSignalStageSystem.StaticInput);

            // Set input conditioning transition rate input value per second (in this case dpt/sec)
            // If set to 0 transition rate function is disabled and system applies focal power to control loop
            // as fast as possible.
            controller.InputConditioningStage.OffsetAndScale.SetTransitionRate(EChannel.Channel0, 3.5f);

            string input;
            do
            {
                float fp_set_pt = 0.0f;
                float hall_value = 0.0f;
                float fp_value = 0.0f;
                float current = 0.0f;
                Console.WriteLine("Enter focal power [dpt]:");
                input = Console.ReadLine();

                if (float.TryParse(input, out fp_set_pt))
                {
                    // Set diopter
                    controller.InputStage.StaticValue.SetDiopter(EChannel.Channel0, fp_set_pt);
                    Console.WriteLine("Set fp to " + fp_set_pt.ToString());

                    // Wait for settling time of lens depends on transition rate that is set with SetTransitionRate function
                    Thread.Sleep(1000);
                    // Read back values from driver
                    float set_pt = controller.InputStage.StaticValue.GetDiopter(EChannel.Channel0);
                    Console.WriteLine("Set point [dpt]:" + set_pt.ToString());
                    // Read diopter
                    fp_value = controller.SignalFlowManager.GetStageOutput(ESignalFlowPath.FeedBack, EStage.InputConditioning, EChannel.Channel0);
                    Console.WriteLine("FP value [dpt]: " + fp_value.ToString());
                    // Read output current
                    current = controller.OutputStage.GetOutputStageOutputCurrent(EChannel.Channel0);
                    Console.WriteLine("Current [A]: " + current.ToString());
                    // Read hall value
                    hall_value = controller.SignalFlowManager.HallFeedback.GetNormalisedHallSensor(ESensor.Sensor0);
                    Console.WriteLine("Hall value [-]: " + hall_value.ToString());
                    Console.WriteLine();
                }
            } while (input != "q");

            // Disconnect
            controller.Disconnect(true);
        }
    }
}