示例#1
0
        private int ReadI2CLux()
        {
            byte[] regAddrBuf = new byte[] { 0x23 };
            byte[] readBuf    = new byte[2];
            I2CLightSensor.WriteRead(regAddrBuf, readBuf);

            // is this calculation correct?
            var valf = ((readBuf[0] << 8) | readBuf[1]) / 1.2;

            return((int)valf);
        }
示例#2
0
        public async Task InitLightSensorAsync()
        {
            string aqs = I2cDevice.GetDeviceSelector();
            /* Get a selector string that will return all I2C controllers on the system */
            var dis = await DeviceInformation.FindAllAsync(aqs);

            /* Find the I2C bus controller device with our selector string           */
            if (dis.Count == 0)
            {
                throw new FileNotFoundException("No I2C controllers were found on the system");
            }

            var foundAddress = false;
            var count        = 0;
            var address      = 0;

            do
            {
                var settings = new I2cConnectionSettings(address)
                {
                    BusSpeed = I2cBusSpeed.FastMode
                };

                I2CLightSensor = await I2cDevice.FromIdAsync(dis[0].Id, settings);

                /* Create an I2cDevice with our selected bus controller and I2C settings */
                if (I2CLightSensor == null)
                {
                    throw new UnauthorizedAccessException(string.Format("Slave address {0} on I2C Controller {1} is currently in use by " +
                                                                        "another application. Please ensure that no other applications are using I2C.", settings.SlaveAddress, dis[0].Id));
                }

                /* Write the register settings */
                try
                {
                    I2CLightSensor.Write(new byte[] { 0x10 }); // 1 [lux] aufloesung
                }
                /* If the write fails display the error and stop running */
                catch (Exception ex)
                {
                    ++address;
                    ++count;

                    Debug.WriteLine("Failed to communicate with device: " + ex.Message);
                    continue;
                }

                Debug.WriteLine("【ADDRESS】" + address);
                foundAddress = true;
            } while (!foundAddress && count < 78);

            PeriodicTimer = new Timer(this.TimerCallback, null, 0, TimerIntervalMs);
        }