Exemple #1
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);
        }
        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);
        }
Exemple #3
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);
            }
        }