示例#1
0
 public void assignRedButton(YAnButton button)
 {
     // we store a pointer to the current instance of ColorMixer into
     // the userData Field
     button.set_userData(this);
     // and we register our static method to change red color as callback
     button.registerValueCallback(redCallback);
 }
        // link the instance to a real YoctoAPI object
        internal override void linkToHardware(string hwdName)
        {
            YAnButton hwd = YAnButton.FindAnButton(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(YAnButton hwd)
 {
     if (hwd == null)
     {
         return;
     }
     base.init(hwd);
     InternalStuff.log("registering AnButton callback");
     _func.registerValueCallback(valueChangeCallback);
 }
示例#4
0
        public override async Task <int> Run()
        {
            try {
                await YAPI.RegisterHub(HubURL);

                YAnButton input1;
                YAnButton input5;

                if (Target.ToLower() == "any")
                {
                    input1 = YAnButton.FirstAnButton();
                    if (input1 == null)
                    {
                        WriteLine("No module connected (check USB cable) ");
                        return(-1);
                    }

                    Target = await(await input1.get_module()).get_serialNumber();
                }

                input1 = YAnButton.FindAnButton(Target + ".anButton1");
                input5 = YAnButton.FindAnButton(Target + ".anButton5");

                while (await input1.isOnline())
                {
                    if (await input1.get_isPressed() == YAnButton.ISPRESSED_TRUE)
                    {
                        Write("Button 1: pressed      ");
                    }
                    else
                    {
                        Write("Button 1: not pressed  ");
                    }
                    WriteLine("- analog value:  " + await input1.get_calibratedValue());
                    if (await input5.get_isPressed() == YAnButton.ISPRESSED_TRUE)
                    {
                        Write("Button 5: pressed      ");
                    }
                    else
                    {
                        Write("Button 5: not pressed  ");
                    }
                    WriteLine("- analog value:  " + await input5.get_calibratedValue());

                    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);
        }
示例#5
0
        public HamsterButton(string hwid, PressedDelegate pressedDe)
        {
            _button = YAnButton.FindAnButton(hwid);
            if (!_button.isOnline())
            {
                throw new Exception("No anButton named " + hwid);
            }

            _pressedDe = pressedDe;
            _button.registerValueCallback(AnButtonListener);
        }
示例#6
0
    // --- (end of YAnButton implementation)

    // --- (AnButton functions)

    /**
     * <summary>
     *   Retrieves an analog 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 analog input is online at the time
     *   it is invoked. The returned object is nevertheless valid.
     *   Use the method <c>YAnButton.isOnline()</c> to test if the analog input is
     *   indeed online at a given time. In case of ambiguity when looking for
     *   an analog 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 analog input
     * </param>
     * <returns>
     *   a <c>YAnButton</c> object allowing you to drive the analog input.
     * </returns>
     */
    public static YAnButton FindAnButton(string func)
    {
        YAnButton res;

        if (_AnButtonCache.ContainsKey(func))
        {
            return((YAnButton)_AnButtonCache[func]);
        }
        res = new YAnButton(func);
        _AnButtonCache.Add(func, res);
        return(res);
    }
示例#7
0
        static void blueCallback(YAnButton button, String calibratedValue)
        {
            // calculate the blue component by scaling the calibratedValue
            // from 0..1000 to 0..255
            uint value = uint.Parse(calibratedValue);
            byte blue  = (byte)((value * 255 / 1000) & 0xff);
            // we used the userData to get the pointer to the instance of ColorMixer
            ColorMixer mixer = (ColorMixer)button.get_userData();

            // update the color
            mixer.changeBlue(blue);
        }
示例#8
0
    /**
     * <summary>
     *   Retrieves an analog 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 analog input is online at the time
     *   it is invoked. The returned object is nevertheless valid.
     *   Use the method <c>YAnButton.isOnline()</c> to test if the analog input is
     *   indeed online at a given time. In case of ambiguity when looking for
     *   an analog 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 analog input
     * </param>
     * <returns>
     *   a <c>YAnButton</c> object allowing you to drive the analog input.
     * </returns>
     */
    public static YAnButton FindAnButton(string func)
    {
        YAnButton obj;

        obj = (YAnButton)YFunction._FindFromCache("AnButton", func);
        if (obj == null)
        {
            obj = new YAnButton(func);
            YFunction._AddToCache("AnButton", func, obj);
        }
        return(obj);
    }
        /**
         * <summary>
         *   Enumerates all functions of type AnButton 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>YAnButton.FindAnButton</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>();
            YAnButton     it  = YAnButton.FirstAnButton();

            while (it != null)
            {
                res.Add(it.get_hardwareId());
                it = it.nextAnButton();
            }
            return(res.ToArray());
        }
        public static YAnButtonProxy FindAnButton(string name)
        {
            // cases to handle:
            // name =""  no matching unknwn
            // name =""  unknown exists
            // name != "" no  matching unknown
            // name !="" unknown exists
            YAnButton      func = null;
            YAnButtonProxy res  = (YAnButtonProxy)YFunctionProxy.FindSimilarUnknownFunction("YAnButtonProxy");

            if (name == "")
            {
                if (res != null)
                {
                    return(res);
                }
                res = (YAnButtonProxy)YFunctionProxy.FindSimilarKnownFunction("YAnButtonProxy");
                if (res != null)
                {
                    return(res);
                }
                func = YAnButton.FirstAnButton();
                if (func != null)
                {
                    name = func.get_hardwareId();
                    if (func.get_userData() != null)
                    {
                        return((YAnButtonProxy)func.get_userData());
                    }
                }
            }
            else
            {
                func = YAnButton.FindAnButton(name);
                if (func.get_userData() != null)
                {
                    return((YAnButtonProxy)func.get_userData());
                }
            }
            if (res == null)
            {
                res = new YAnButtonProxy(func, name);
            }
            if (func != null)
            {
                res.linkToHardware(name);
                if (func.isOnline())
                {
                    res.arrival();
                }
            }
            return(res);
        }
示例#11
0
        async Task deviceArrival(YModule m)
        {
            string serial = await m.get_serialNumber();

            Output.Text += "Device arrival : " + serial + "\n";
            await m.registerLogCallback(deviceLog);

            await m.registerConfigChangeCallback(configChange);

            await m.registerBeaconCallback(beaconChange);

            // First solution: look for a specific type of function (eg. anButton)
            int fctcount = await m.functionCount();

            for (int i = 0; i < fctcount; i++)
            {
                string hardwareId = serial + "." + await m.functionId(i);

                if (hardwareId.IndexOf(".anButton") >= 0)
                {
                    Output.Text += "- " + hardwareId + "\n";
                    YAnButton anButton = YAnButton.FindAnButton(hardwareId);
                    await anButton.registerValueCallback(anButtonValueChangeCallBack);
                }
            }

            // Alternate solution: register any kind of sensor on the device
            YSensor sensor = YSensor.FirstSensor();

            while (sensor != null)
            {
                YModule module = await sensor.get_module();

                if (await module.get_serialNumber() == serial)
                {
                    string hardwareId = await sensor.get_hardwareId();

                    Output.Text += "- " + hardwareId + "\n";
                    string unit = await sensor.get_unit();

                    await sensor.set_userData(unit);

                    await sensor.registerValueCallback(sensorValueChangeCallBack);

                    await sensor.registerTimedReportCallback(sensorTimedReportCallBack);
                }

                sensor = sensor.nextSensor();
            }
        }
示例#12
0
        private void AnButtonListener(YAnButton func, string value)
        {
            int intVal = Convert.ToInt32(value);

            if (intVal < 200)
            {
                if (isUp)
                {
                    isUp = false;
                }
            }
            else if (intVal > 800)
            {
                if (!isUp)
                {
                    _pressedDe(this);
                    isUp = true;
                }
            }
        }
示例#13
0
        static void deviceArrival(YModule m)
        {
            string serial = m.get_serialNumber();

            Console.WriteLine("Device arrival : " + serial);
            m.registerLogCallback(deviceLog);
            m.registerConfigChangeCallback(configChange);
            m.registerBeaconCallback(beaconChange);

            // First solution: look for a specific type of function (eg. anButton)
            int fctcount = m.functionCount();

            for (int i = 0; i < fctcount; i++)
            {
                string hardwareId = serial + "." + m.functionId(i);
                if (hardwareId.IndexOf(".anButton") >= 0)
                {
                    Console.WriteLine("- " + hardwareId);
                    YAnButton anButton = YAnButton.FindAnButton(hardwareId);
                    anButton.registerValueCallback(anButtonValueChangeCallBack);
                }
            }

            // Alternate solution: register any kind of sensor on the device
            YSensor sensor = YSensor.FirstSensor();

            while (sensor != null)
            {
                if (sensor.get_module().get_serialNumber() == serial)
                {
                    string hardwareId = sensor.get_hardwareId();
                    Console.WriteLine("- " + hardwareId);
                    string unit = sensor.get_unit();
                    sensor.set_userData(unit);
                    sensor.registerValueCallback(sensorValueChangeCallBack);
                    sensor.registerTimedReportCallback(sensorTimedReportCallBack);
                }

                sensor = sensor.nextSensor();
            }
        }
示例#14
0
 // --- (end of YAnButton implementation)
 // --- (AnButton functions)
 /**
    * <summary>
    *   Retrieves an analog 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 analog input is online at the time
    *   it is invoked. The returned object is nevertheless valid.
    *   Use the method <c>YAnButton.isOnline()</c> to test if the analog input is
    *   indeed online at a given time. In case of ambiguity when looking for
    *   an analog 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 analog input
    * </param>
    * <returns>
    *   a <c>YAnButton</c> object allowing you to drive the analog input.
    * </returns>
    */
 public static YAnButton FindAnButton(string func)
 {
     YAnButton res;
     if (_AnButtonCache.ContainsKey(func))
       return (YAnButton)_AnButtonCache[func];
     res = new YAnButton(func);
     _AnButtonCache.Add(func, res);
     return res;
 }
示例#15
0
        static void Main(string[] args)
        {
            string    errmsg = "";
            string    target, serial;
            YBuzzer   buz;
            YLed      led, led1, led2;
            YAnButton button1, button2;

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

            if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS)
            {
                Console.WriteLine("RegisterHub error: " + errmsg);
                Environment.Exit(0);
            }

            if (target == "ANY")
            {
                buz = YBuzzer.FirstBuzzer();
                if (buz == null)
                {
                    Console.WriteLine("No module connected (check USB cable) ");
                    Environment.Exit(0);
                }
            }
            else
            {
                buz = YBuzzer.FindBuzzer(target + ".buzzer");
            }

            if (!buz.isOnline())
            {
                Console.WriteLine("Module not connected");
                Console.WriteLine("check identification and USB cable");
                Environment.Exit(0);
            }

            serial  = buz.get_module().get_serialNumber();
            led1    = YLed.FindLed(serial + ".led1");
            led2    = YLed.FindLed(serial + ".led2");
            button1 = YAnButton.FindAnButton(serial + ".anButton1");
            button2 = YAnButton.FindAnButton(serial + ".anButton2");

            Console.WriteLine("press a test button or hit Ctrl-C");

            while (buz.isOnline())
            {
                int  frequency;
                bool b1 = (button1.get_isPressed() == YAnButton.ISPRESSED_TRUE);
                bool b2 = (button2.get_isPressed() == YAnButton.ISPRESSED_TRUE);
                if (b1 || b2)
                {
                    if (b1)
                    {
                        led       = led1;
                        frequency = 1500;
                    }
                    else
                    {
                        led       = led2;
                        frequency = 750;
                    }

                    led.set_power(YLed.POWER_ON);
                    led.set_luminosity(100);
                    led.set_blinking(YLed.BLINKING_PANIC);
                    for (int i = 0; i < 5; i++) // this can be done using sequence as well
                    {
                        buz.set_frequency(frequency);
                        buz.freqMove(2 * frequency, 250);
                        YAPI.Sleep(250, ref errmsg);
                    }
                    buz.set_frequency(0);
                    led.set_power(YLed.POWER_OFF);
                }
            }
            YAPI.FreeAPI();
        }
示例#16
0
        static void Main(string[] args)
        {
            string    errmsg = "";
            string    target, serial;
            YBuzzer   buz;
            YColorLed led;
            YAnButton button1, button2;

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

            if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS)
            {
                Console.WriteLine("RegisterHub error: " + errmsg);
                Environment.Exit(0);
            }

            if (target == "ANY")
            {
                buz = YBuzzer.FirstBuzzer();
                if (buz == null)
                {
                    Console.WriteLine("No module connected (check USB cable) ");
                    Environment.Exit(0);
                }
            }
            else
            {
                buz = YBuzzer.FindBuzzer(target + ".buzzer");
            }

            if (!buz.isOnline())
            {
                Console.WriteLine("Module not connected");
                Console.WriteLine("check identification and USB cable");
                Environment.Exit(0);
            }

            serial  = buz.get_module().get_serialNumber();
            led     = YColorLed.FindColorLed(serial + ".colorLed");
            button1 = YAnButton.FindAnButton(serial + ".anButton1");
            button2 = YAnButton.FindAnButton(serial + ".anButton2");

            Console.WriteLine("press a test button or hit Ctrl-C");

            while (buz.isOnline())
            {
                int  frequency;
                int  color;
                int  volume;
                bool b1 = (button1.get_isPressed() == YAnButton.ISPRESSED_TRUE);
                bool b2 = (button2.get_isPressed() == YAnButton.ISPRESSED_TRUE);
                if (b1 || b2)
                {
                    if (b1)
                    {
                        volume    = 60;
                        frequency = 1500;
                        color     = 0xff0000;
                    }
                    else
                    {
                        volume    = 30;
                        color     = 0x00ff00;
                        frequency = 750;
                    }
                    led.resetBlinkSeq();
                    led.addRgbMoveToBlinkSeq(color, 100);
                    led.addRgbMoveToBlinkSeq(0, 100);
                    led.startBlinkSeq();
                    buz.set_volume(volume);
                    for (int i = 0; i < 5; i++)
                    {
                        // this can be done using sequence as well
                        buz.set_frequency(frequency);
                        buz.freqMove(2 * frequency, 250);
                        YAPI.Sleep(250, ref errmsg);
                    }

                    buz.set_frequency(0);
                    led.stopBlinkSeq();
                    led.set_rgbColor(0);
                }
            }

            YAPI.FreeAPI();
        }
示例#17
0
        static void Main(string[] args)
        {
            string    errmsg = "";
            string    target;
            YAnButton input1;
            YAnButton input5;

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

            if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS)
            {
                Console.WriteLine("RegisterHub error: " + errmsg);
                Environment.Exit(0);
            }

            if (target == "ANY")
            {
                input1 = YAnButton.FirstAnButton();
                if (input1 == null)
                {
                    Console.WriteLine("No module connected (check USB cable) ");
                    Environment.Exit(0);
                }

                target = input1.get_module().get_serialNumber();
            }

            input1 = YAnButton.FindAnButton(target + ".anButton1");
            input5 = YAnButton.FindAnButton(target + ".anButton5");

            if (!input1.isOnline())
            {
                Console.WriteLine("Module not connected");
                Console.WriteLine("check identification and USB cable");
                Environment.Exit(0);
            }
            while (input1.isOnline())
            {
                if (input1.get_isPressed() == YAnButton.ISPRESSED_TRUE)
                {
                    Console.Write("Button 1: pressed      ");
                }
                else
                {
                    Console.Write("Button 1: not pressed  ");
                }
                Console.WriteLine("- analog value:  " + input1.get_calibratedValue().ToString());
                if (input5.get_isPressed() == YAnButton.ISPRESSED_TRUE)
                {
                    Console.Write("Button 5: pressed      ");
                }
                else
                {
                    Console.Write("Button 5: not pressed  ");
                }
                Console.WriteLine("- analog value:  " + input5.get_calibratedValue().ToString());

                YAPI.Sleep(1000, ref errmsg);
            }
            YAPI.FreeAPI();
        }
 // 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 = (YAnButton)hwd;
     base.base_init(hwd, instantiationName);
 }
示例#19
0
        public override async Task <int> Run()
        {
            try {
                await YAPI.RegisterHub(HubURL);

                YBuzzer   buz;
                YColorLed led;
                YAnButton button1, button2;

                if (Target.ToLower() == "any")
                {
                    buz = YBuzzer.FirstBuzzer();
                    if (buz == null)
                    {
                        WriteLine("No module connected (check USB cable) ");
                        return(-1);
                    }
                }
                else
                {
                    buz = YBuzzer.FindBuzzer(Target + ".buzzer");
                }

                if (!await buz.isOnline())
                {
                    WriteLine("Module not connected (check identification and USB cable)");
                    return(-1);
                }

                string serial = await(await buz.get_module()).get_serialNumber();
                led     = YColorLed.FindColorLed(serial + ".colorLed");
                button1 = YAnButton.FindAnButton(serial + ".anButton1");
                button2 = YAnButton.FindAnButton(serial + ".anButton2");

                WriteLine("press a test button");

                while (await buz.isOnline())
                {
                    int  frequency, volume, color;
                    bool b1 = (await button1.get_isPressed() == YAnButton.ISPRESSED_TRUE);
                    bool b2 = (await button2.get_isPressed() == YAnButton.ISPRESSED_TRUE);
                    if (b1 || b2)
                    {
                        if (b1)
                        {
                            volume    = 60;
                            frequency = 1500;
                            color     = 0xff0000;
                        }
                        else
                        {
                            volume    = 30;
                            color     = 0x00ff00;
                            frequency = 750;
                        }

                        await led.resetBlinkSeq();

                        await led.addRgbMoveToBlinkSeq(color, 100);

                        await led.addRgbMoveToBlinkSeq(0, 100);

                        await led.startBlinkSeq();

                        await buz.set_volume(volume);

                        for (int i = 0; i < 5; i++)
                        {
                            // this can be done using sequence as well
                            await buz.set_frequency(frequency);

                            await buz.freqMove(2 *frequency, 250);

                            await YAPI.Sleep(250);
                        }

                        await buz.set_frequency(0);

                        await led.stopBlinkSeq();

                        await led.set_rgbColor(0);
                    }
                }
            } catch (YAPI_Exception ex) {
                WriteLine("error: " + ex.Message);
            }

            YAPI.FreeAPI();
            return(0);
        }
示例#20
0
        static int Main(string[] args)
        {
            string errmsg = "";
            int    i;
            int    nbled = 0;

            Console.WriteLine("Yoctopuce Library v" + YAPI.GetAPIVersion());
            Console.WriteLine("ColorMixer");
            if (args.Length < 1)
            {
                Console.WriteLine("usage: demo [usb | ip_address]");
                return(1);
            }

            for (i = 0; i < args.Length; i++)
            {
                // Setup the API to use local USB devices
                if (YAPI.RegisterHub(args[i], ref errmsg) != YAPI.SUCCESS)
                {
                    Console.WriteLine("Unable to get acces to devices on " + args[i]);
                    Console.WriteLine("error: " + errmsg);
                    return(1);
                }
            }

            // create our ColorMixer Object
            ColorMixer mixer = new ColorMixer();

            // get our pointer on our 3 knob
            // we use will reference the 3 knob by the logical name
            // that we have configured using the VirtualHub
            YAnButton knobRed   = YAnButton.FindAnButton("Red");
            YAnButton knobGreen = YAnButton.FindAnButton("Green");
            YAnButton knobBlue  = YAnButton.FindAnButton("Blue");

            // register these 3 knob to the mixer
            mixer.assignRedButton(knobRed);
            mixer.assignGreenButton(knobGreen);
            mixer.assignBlueButton(knobBlue);

            // display a warning if we miss a knob
            if (!knobRed.isOnline())
            {
                Console.WriteLine("Warning: knob \"" + knobRed + "\" is not connected");
            }
            if (!knobGreen.isOnline())
            {
                Console.WriteLine("Warning: knob \"" + knobGreen + "\" is not connected");
            }
            if (!knobBlue.isOnline())
            {
                Console.WriteLine("Warning: knob \"" + knobBlue + "\" is not connected");
            }

            // register all led that is connected to our "network"
            YColorLed led = YColorLed.FirstColorLed();

            while (led != null)
            {
                mixer.addLED(led);
                nbled++;
                led = led.nextColorLed();
            }
            Console.WriteLine(nbled + " Color Led detected", nbled);
            // never hanling loop that will..
            while (true)
            {
                // ... handle all event durring 5000ms without using lots of CPU ...
                YAPI.Sleep(1000, ref errmsg);
                // ... and check for device plug/unplug
                YAPI.UpdateDeviceList(ref errmsg);
            }
        }
 /**
  * <summary>
  *   Retrieves an analog 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 analog input is online at the time
  *   it is invoked. The returned object is nevertheless valid.
  *   Use the method <c>YAnButton.isOnline()</c> to test if the analog input is
  *   indeed online at a given time. In case of ambiguity when looking for
  *   an analog 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 analog input
  * </param>
  * <returns>
  *   a <c>YAnButton</c> object allowing you to drive the analog input.
  * </returns>
  */
 public static YAnButton FindAnButton(string func)
 {
     YAnButton obj;
     obj = (YAnButton) YFunction._FindFromCache("AnButton", func);
     if (obj == null) {
         obj = new YAnButton(func);
         YFunction._AddToCache("AnButton", func, obj);
     }
     return obj;
 }
示例#22
0
        public override async Task <int> Run()
        {
            try {
                await YAPI.RegisterHub(HubURL);

                YBuzzer   buz;
                YLed      led, led1, led2;
                YAnButton button1, button2;

                if (Target.ToLower() == "any")
                {
                    buz = YBuzzer.FirstBuzzer();
                    if (buz == null)
                    {
                        WriteLine("No module connected (check USB cable) ");
                        return(-1);
                    }
                }
                else
                {
                    buz = YBuzzer.FindBuzzer(Target + ".buzzer");
                }

                if (!await buz.isOnline())
                {
                    WriteLine("Module not connected (check identification and USB cable)");
                    return(-1);
                }

                string serial = await(await buz.get_module()).get_serialNumber();
                led1    = YLed.FindLed(serial + ".led1");
                led2    = YLed.FindLed(serial + ".led2");
                button1 = YAnButton.FindAnButton(serial + ".anButton1");
                button2 = YAnButton.FindAnButton(serial + ".anButton2");

                WriteLine("press a test button");

                while (await buz.isOnline())
                {
                    int  frequency;
                    bool b1 = (await button1.get_isPressed() == YAnButton.ISPRESSED_TRUE);
                    bool b2 = (await button2.get_isPressed() == YAnButton.ISPRESSED_TRUE);
                    if (b1 || b2)
                    {
                        if (b1)
                        {
                            led       = led1;
                            frequency = 1500;
                        }
                        else
                        {
                            led       = led2;
                            frequency = 750;
                        }

                        await led.set_power(YLed.POWER_ON);

                        await led.set_luminosity(100);

                        await led.set_blinking(YLed.BLINKING_PANIC);

                        for (int i = 0; i < 5; i++)
                        {
                            // this can be done using sequence as well
                            await buz.set_frequency(frequency);

                            await buz.freqMove(2 *frequency, 250);

                            await YAPI.Sleep(250);
                        }

                        await buz.set_frequency(0);

                        await led.set_power(YLed.POWER_OFF);
                    }
                }
            } catch (YAPI_Exception ex) {
                WriteLine("error: " + ex.Message);
            }

            YAPI.FreeAPI();
            return(0);
        }
示例#23
0
        public override async Task <int> Run()
        {
            try {
                await YAPI.RegisterHub(HubURL);

                YBuzzer            buz;
                YColorLedCluster   leds;
                YQuadratureDecoder qd;
                YAnButton          button;

                if (Target.ToLower() == "any")
                {
                    buz = YBuzzer.FirstBuzzer();
                    if (buz == null)
                    {
                        WriteLine("No module connected (check USB cable) ");
                        return(-1);
                    }
                }
                else
                {
                    buz = YBuzzer.FindBuzzer(Target + ".buzzer");
                }

                if (!await buz.isOnline())
                {
                    WriteLine("Module not connected (check identification and USB cable)");
                    return(-1);
                }

                string serial = await(await buz.get_module()).get_serialNumber();
                leds   = YColorLedCluster.FindColorLedCluster(serial + ".colorLedCluster");
                button = YAnButton.FindAnButton(serial + ".anButton1");
                qd     = YQuadratureDecoder.FindQuadratureDecoder(serial + ".quadratureDecoder1");

                if ((!await button.isOnline()) || (!await qd.isOnline()))
                {
                    WriteLine("Make sure the Yocto-MaxiBuzzer is configured with at least one anButton and one quadrature Decoder");
                    return(-1);
                }

                WriteLine("press a test button, or turn the encoder or hit Ctrl-C");
                int lastPos = (int)await qd.get_currentValue();

                await buz.set_volume(75);

                while (await button.isOnline())
                {
                    if ((await button.get_isPressed() == YAnButton.ISPRESSED_TRUE) &&
                        (lastPos != 0))
                    {
                        lastPos = 0;
                        await qd.set_currentValue(0);

                        await buz.playNotes("'E32 C8");

                        await leds.set_rgbColor(0, 1, 0x000000);
                    }
                    else
                    {
                        int p = (int)await qd.get_currentValue();

                        if (lastPos != p)
                        {
                            lastPos = p;
                            await buz.pulse(notefreq(p), 500);

                            await leds.set_hslColor(0, 1, 0x00FF7f | (p % 255) << 16);
                        }
                    }
                }
            } catch (YAPI_Exception ex) {
                WriteLine("error: " + ex.Message);
            }

            YAPI.FreeAPI();
            return(0);
        }
        //--- (end of YAnButton definitions)

        //--- (YAnButton implementation)
        internal YAnButtonProxy(YAnButton hwd, string instantiationName) : base(hwd, instantiationName)
        {
            InternalStuff.log("AnButton " + instantiationName + " instantiation");
            base_init(hwd, instantiationName);
        }