// 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); }
// perform the 2nd stage setup that requires YoctoAPI object protected void init(YPwmInput hwd) { if (hwd == null) { return; } base.init(hwd); InternalStuff.log("registering PwmInput callback"); _func.registerValueCallback(valueChangeCallback); }
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); }
/** * <summary> * Enumerates all functions of type PwmInput available on the devices * currently reachable by the library, and returns their unique hardware ID. * <para> * Each of these IDs can be provided as argument to the method * <c>YPwmInput.FindPwmInput</c> to obtain an object that can control the * corresponding device. * </para> * </summary> * <returns> * an array of strings, each string containing the unique hardwareId * of a device function currently connected. * </returns> */ public static new string[] GetSimilarFunctions() { List <string> res = new List <string>(); YPwmInput it = YPwmInput.FirstPwmInput(); while (it != null) { res.Add(it.get_hardwareId()); it = it.nextPwmInput(); } return(res.ToArray()); }
/** * <summary> * Retrieves a PWM input for a given identifier. * <para> * The identifier can be specified using several formats: * </para> * <para> * </para> * <para> * - FunctionLogicalName * </para> * <para> * - ModuleSerialNumber.FunctionIdentifier * </para> * <para> * - ModuleSerialNumber.FunctionLogicalName * </para> * <para> * - ModuleLogicalName.FunctionIdentifier * </para> * <para> * - ModuleLogicalName.FunctionLogicalName * </para> * <para> * </para> * <para> * This function does not require that the PWM input is online at the time * it is invoked. The returned object is nevertheless valid. * Use the method <c>YPwmInput.isOnline()</c> to test if the PWM input is * indeed online at a given time. In case of ambiguity when looking for * a PWM input by logical name, no error is notified: the first instance * found is returned. The search is performed first by hardware name, * then by logical name. * </para> * </summary> * <param name="func"> * a string that uniquely characterizes the PWM input * </param> * <returns> * a <c>YPwmInput</c> object allowing you to drive the PWM input. * </returns> */ public static YPwmInput FindPwmInput(string func) { YPwmInput obj; obj = (YPwmInput)YFunction._FindFromCache("PwmInput", func); if (obj == null) { obj = new YPwmInput(func); YFunction._AddToCache("PwmInput", func, obj); } return(obj); }
private void configurePWMInput(YPwmInput pwmInput) { this.log("Configure PWM Input"); // Set debounce value to 5 ms pwmInput.set_debouncePeriod(5); pwmInput.set_pwmReportMode(YPwmInput.PWMREPORTMODE_PWM_PULSECOUNT); pwmInput.set_logFrequency("1/m"); pwmInput.get_module().saveToFlash(); YDataLogger dataLogger = pwmInput.get_dataLogger(); dataLogger.set_autoStart(YDataLogger.AUTOSTART_ON); dataLogger.set_recording(YDataLogger.RECORDING_ON); }
private void count_callback(YPwmInput func, string value) { int count = Int32.Parse(value, CultureInfo.InvariantCulture); if (count == _initialCount) { return; } if (!_isRunning) { // we start the run at the first magnet pass ResetRun(count); _isRunning = true; Debug.WriteLine(String.Format("Start exeercise count = {0}", count)); return; } ulong now = YAPI.GetTickCount(); ulong deltaTime = now - _lastTickCount; long deltaCount; if (_lastCount > count) { deltaCount = count + 999999 - _lastCount; } else { deltaCount = (count - _lastCount); } double speed = (double)deltaCount / deltaTime; Debug.WriteLine(String.Format("count ={0} delta={1} time={2} speed={3}", count, deltaCount, deltaTime, speed)); //Update Max speed if (_maxSpeedCMS < speed) { _maxSpeedCMS = speed; } _totalCount += deltaCount; _lastSpeedCMS = speed; _lastTickCount = now; _lastCount = count; }
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(); } } }
// perform the initial setup that may be done without a YoctoAPI object (hwd can be null) internal override void base_init(YFunction hwd, string instantiationName) { _func = (YPwmInput)hwd; base.base_init(hwd, instantiationName); }
//--- (end of YPwmInput definitions) //--- (YPwmInput implementation) internal YPwmInputProxy(YPwmInput hwd, string instantiationName) : base(hwd, instantiationName) { InternalStuff.log("PwmInput " + instantiationName + " instantiation"); base_init(hwd, instantiationName); }
/** * <summary> * Retrieves a PWM input for a given identifier. * <para> * The identifier can be specified using several formats: * </para> * <para> * </para> * <para> * - FunctionLogicalName * </para> * <para> * - ModuleSerialNumber.FunctionIdentifier * </para> * <para> * - ModuleSerialNumber.FunctionLogicalName * </para> * <para> * - ModuleLogicalName.FunctionIdentifier * </para> * <para> * - ModuleLogicalName.FunctionLogicalName * </para> * <para> * </para> * <para> * This function does not require that the PWM input is online at the time * it is invoked. The returned object is nevertheless valid. * Use the method <c>YPwmInput.isOnline()</c> to test if the PWM input is * indeed online at a given time. In case of ambiguity when looking for * a PWM input by logical name, no error is notified: the first instance * found is returned. The search is performed first by hardware name, * then by logical name. * </para> * </summary> * <param name="func"> * a string that uniquely characterizes the PWM input * </param> * <returns> * a <c>YPwmInput</c> object allowing you to drive the PWM input. * </returns> */ public static YPwmInput FindPwmInput(string func) { YPwmInput obj; obj = (YPwmInput) YFunction._FindFromCache("PwmInput", func); if (obj == null) { obj = new YPwmInput(func); YFunction._AddToCache("PwmInput", func, obj); } return obj; }
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"); }