コード例 #1
0
ファイル: Demo.cs プロジェクト: yoctopuce/yoctolib_uwp
        public override async Task <int> Run()
        {
            try {
                await YAPI.RegisterHub(HubURL);

                YI2cPort i2cPort;

                int value = Convert.ToInt32(Value);

                if (Target.ToLower() == "any")
                {
                    i2cPort = YI2cPort.FirstI2cPort();
                    if (i2cPort == null)
                    {
                        WriteLine("No module connected (check USB cable) ");
                        return(-1);
                    }

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

                i2cPort = YI2cPort.FindI2cPort(Target + ".i2cPort");
                if (await i2cPort.isOnline())
                {
                    await i2cPort.set_i2cMode("400kbps");

                    await i2cPort.set_i2cVoltageLevel(YI2cPort.I2CVOLTAGELEVEL_3V3);

                    await i2cPort.reset();

                    // do not forget to configure the powerOutput and
                    // of the Yocto-I2C as well if used
                    WriteLine("****************************");
                    WriteLine("* make sure voltage levels *");
                    WriteLine("* are properly configured  *");
                    WriteLine("****************************");

                    List <int> toSend   = new List <int>(new int[] { 0x05 });
                    List <int> received = await i2cPort.i2cSendAndReceiveArray(0x1f, toSend, 2);

                    int tempReg = (received[0] << 8) + received[1];
                    if ((tempReg & 0x1000) != 0)
                    {
                        tempReg -= 0x2000; // perform sign extension
                    }
                    else
                    {
                        tempReg &= 0x0fff; // clear status bits
                    }
                    WriteLine("Ambiant temperature: " + String.Format("{0:0.000}", (tempReg / 16.0)));
                }
                else
                {
                    WriteLine("Module not connected (check identification and USB cable)");
                }
            } catch (YAPI_Exception ex) {
                WriteLine("error: " + ex.Message);
            }

            YAPI.FreeAPI();
            return(0);
        }
コード例 #2
0
ファイル: main.cs プロジェクト: attackgithub/yoctolib_cs
        static void Main(string[] args)
        {
            string   errmsg = "";
            string   target;
            YI2cPort i2cPort;

            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")
            {
                i2cPort = YI2cPort.FirstI2cPort();
                if (i2cPort == null)
                {
                    Console.WriteLine("No module connected (check USB cable) ");
                    Environment.Exit(0);
                }
                target = i2cPort.get_module().get_serialNumber();
            }

            i2cPort = YI2cPort.FindI2cPort(target + ".i2cPort");
            if (i2cPort.isOnline())
            {
                i2cPort.set_i2cMode("400kbps");
                i2cPort.set_i2cVoltageLevel(YI2cPort.I2CVOLTAGELEVEL_3V3);
                i2cPort.reset();
                // do not forget to configure the powerOutput and
                // of the Yocto-I2C as well if used
                Console.WriteLine("****************************");
                Console.WriteLine("* make sure voltage levels *");
                Console.WriteLine("* are properly configured  *");
                Console.WriteLine("****************************");

                List <int> toSend   = new List <int>(new int[] { 0x05 });
                List <int> received = i2cPort.i2cSendAndReceiveArray(0x1f, toSend, 2);
                int        tempReg  = (received[0] << 8) + received[1];
                if ((tempReg & 0x1000) != 0)
                {
                    tempReg -= 0x2000; // perform sign extension
                }
                else
                {
                    tempReg &= 0x0fff; // clear status bits
                }
                Console.WriteLine("Ambiant temperature: " + String.Format("{0:0.000}", (tempReg / 16.0)));
            }
            else
            {
                Console.WriteLine("Module not connected");
                Console.WriteLine("check identification and USB cable");
            }
            YAPI.FreeAPI();

            // wait 5 sec to show the output
            ulong now = YAPI.GetTickCount();

            while (YAPI.GetTickCount() - now < 5000)
            {
                ;
            }
        }