// link the instance to a real YoctoAPI object
        internal override void linkToHardware(string hwdName)
        {
            YPwmInput hwd = YPwmInput.FindPwmInput(hwdName);

            // first redo base_init to update all _func pointers
            base_init(hwd, hwdName);
            // then setup Yocto-API pointers and callbacks
            init(hwd);
        }
Beispiel #2
0
        public override async Task <int> Run()
        {
            try {
                await YAPI.RegisterHub(HubURL);

                YPwmInput pwm;
                YPwmInput pwm1 = null;
                YPwmInput pwm2 = null;
                YModule   m    = null;
                if (Target.ToLower() == "any")
                {
                    // retreive any pwm input available
                    pwm = YPwmInput.FirstPwmInput();
                    if (pwm == null)
                    {
                        WriteLine("No module connected");
                        return(-1);
                    }
                }
                else
                {
                    // retreive the first pwm input from the device given on command line
                    pwm = YPwmInput.FindPwmInput(Target + ".pwmInput1");
                }

                // we need to retreive both channels from the device.
                if (await pwm.isOnline())
                {
                    m = await pwm.get_module();

                    pwm1 = YPwmInput.FindPwmInput(await m.get_serialNumber() + ".pwmInput1");
                    pwm2 = YPwmInput.FindPwmInput(await m.get_serialNumber() + ".pwmInput2");
                }

                while (await m.isOnline())
                {
                    WriteLine("PWM1: " + await pwm1.get_frequency() + " Hz " + await
                              pwm1.get_dutyCycle() +
                              " % " + await pwm1.get_pulseCounter() + " pulse edges ");
                    WriteLine("PWM2: " + await pwm2.get_frequency() + " Hz " + await
                              pwm2.get_dutyCycle() +
                              " % " + await pwm2.get_pulseCounter() + " pulse edges ");
                    await YAPI.Sleep(1000);
                }

                WriteLine("Module not connected (check identification and USB cable)");
            } catch (YAPI_Exception ex) {
                WriteLine("error: " + ex.Message);
            }

            YAPI.FreeAPI();
            return(0);
        }
        public static YPwmInputProxy FindPwmInput(string name)
        {
            // cases to handle:
            // name =""  no matching unknwn
            // name =""  unknown exists
            // name != "" no  matching unknown
            // name !="" unknown exists
            YPwmInput      func = null;
            YPwmInputProxy res  = (YPwmInputProxy)YFunctionProxy.FindSimilarUnknownFunction("YPwmInputProxy");

            if (name == "")
            {
                if (res != null)
                {
                    return(res);
                }
                res = (YPwmInputProxy)YFunctionProxy.FindSimilarKnownFunction("YPwmInputProxy");
                if (res != null)
                {
                    return(res);
                }
                func = YPwmInput.FirstPwmInput();
                if (func != null)
                {
                    name = func.get_hardwareId();
                    if (func.get_userData() != null)
                    {
                        return((YPwmInputProxy)func.get_userData());
                    }
                }
            }
            else
            {
                func = YPwmInput.FindPwmInput(name);
                if (func.get_userData() != null)
                {
                    return((YPwmInputProxy)func.get_userData());
                }
            }
            if (res == null)
            {
                res = new YPwmInputProxy(func, name);
            }
            if (func != null)
            {
                res.linkToHardware(name);
                if (func.isOnline())
                {
                    res.arrival();
                }
            }
            return(res);
        }
Beispiel #4
0
        public void runForever()
        {
            YPwmInput pwmInput;

            if (_hwid != "")
            {
                pwmInput = YPwmInput.FindPwmInput(_hwid);
                if (!pwmInput.isOnline())
                {
                    throw new Exception("No Yocto-PWM-Rx name " + _hwid + " found");
                }
            }
            else
            {
                pwmInput = YPwmInput.FirstPwmInput();
                if (pwmInput == null)
                {
                    throw new Exception("No Yocto-PWM-Rx connected");
                }
            }

            log("use PWM " + pwmInput.get_hardwareId());

            configurePWMInput(pwmInput);

            pwmInput.resetCounter();
            ResetRun(pwmInput.get_pulseCounter());
            pwmInput.registerValueCallback(count_callback);
            string errmsg = "";

            while (true)
            {
                YAPI.Sleep(1000, ref errmsg);
                _liveUpdateCb(getCurrentSpeedKmh(), getDistanceM(), getDurationS(), getMaxSpeedKmh(), getAVGSpeedKmh());
                if (CheckExpiration())
                {
                    _endOfExerciceCb(getStartTime(), getDurationS(), getAVGSpeedKmh(), getMaxSpeedKmh(), getDistanceM());
                    ResetRun(pwmInput.get_pulseCounter());
                    Stop();
                }
            }
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            string    errmsg = "";
            string    target;
            YPwmInput pwm;
            YPwmInput pwm1 = null;
            YPwmInput pwm2 = null;
            YModule   m    = null;

            if (args.Length < 1)
            {
                usage();
            }
            target = args[0].ToUpper();

            // Setup the API to use local USB devices
            if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS)
            {
                Console.WriteLine("RegisterHub error: " + errmsg);
                Environment.Exit(0);
            }

            if (target == "ANY")
            {
                // retreive any pwm input available
                pwm = YPwmInput.FirstPwmInput();
                if (pwm == null)
                {
                    die("No module connected");
                }
            }
            else
            {
                // retreive the first pwm input from the device given on command line
                pwm = YPwmInput.FindPwmInput(target + ".pwmInput1");
            }

            // we need to retreive both channels from the device.
            if (pwm.isOnline())
            {
                m    = pwm.get_module();
                pwm1 = YPwmInput.FindPwmInput(m.get_serialNumber() + ".pwmInput1");
                pwm2 = YPwmInput.FindPwmInput(m.get_serialNumber() + ".pwmInput2");
            }
            else
            {
                die("Module not connected");
            }

            while (m.isOnline())
            {
                Console.WriteLine("PWM1: " + pwm1.get_frequency().ToString() + " Hz "
                                  + pwm1.get_dutyCycle().ToString() + " % "
                                  + pwm1.get_pulseCounter().ToString() + " pulse edges ");
                Console.WriteLine("PWM2: " + pwm2.get_frequency().ToString() + " Hz "
                                  + pwm2.get_dutyCycle().ToString() + " % "
                                  + pwm2.get_pulseCounter().ToString() + " pulse edges ");
                Console.WriteLine("  (press Ctrl-C to exit)");
                YAPI.Sleep(1000, ref errmsg);
            }
            YAPI.FreeAPI();
            die("Module not connected");
        }