private async void initLedMatrixDevice()
        {
            try
            {
                // I2C bus settings
                // Address of device is 0x70
                I2cConnectionSettings settings = new I2cConnectionSettings(0x70);
                // Using standard speed 100 kHZ
                settings.BusSpeed = I2cBusSpeed.StandardMode;

                // Get device on bus named I2C1
                string aqs = I2cDevice.GetDeviceSelector("I2C1");
                var    dis = await DeviceInformation.FindAllAsync(aqs);

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

                // diplay initialization
                _matrixDevice.Write(new byte[] { 0x21 });
                _matrixDevice.Write(new byte[] { 0x81 });

                // switch all LEDs off
                for (int i = 0; i < (MATRIX_SIZE * 2); i = i + 2)
                {
                    _matrixDevice.Write(new byte[] { (byte)i, 0x00 });
                }
            }
            catch
            {
                _matrixDevice = null;
            }
        }
        private async void InitI2c()
        {
            cvsBusSpeed.Source   = Enum.GetValues(typeof(I2cBusSpeed));
            i2cMasters           = new ObservableCollection <I2cMaster>();
            gridView.ItemsSource = i2cMasters;
            try
            {
                string i2cAqs = I2cDevice.GetDeviceSelector();
                var    dis    = await DeviceInformation.FindAllAsync(i2cAqs);

                foreach (DeviceInformation info in dis)
                {
                    i2cMasters.Add(new I2cMaster()
                    {
                        name = info.Id.Split('\\').Last(),
                        info = info,
                    });
                }
            }
            catch (Exception ex)
            {
                status.Log(string.Format(LocalizableStrings.IOT_I2C_NONE, ex.Message));
                gridView.IsEnabled = false;
                i2cMasters.Add(new I2cMaster());
                return;
            }
            status.Log(string.Format(LocalizableStrings.IOT_I2C_FOUND, i2cMasters.Count()));
        }
Ejemplo n.º 3
0
        private async void InitI2CBerryIMU()
        {
            string allIc2Controllers    = I2cDevice.GetDeviceSelector();                           // Get a selector string that will return all I2C controllers on the system
            var    discoveredI2cDevices = await DeviceInformation.FindAllAsync(allIc2Controllers); // Find the I2C bus controller device with our selector string

            if (discoveredI2cDevices.Count == 0)
            {
                DisplayTextStatus.Text = "No I2C controllers were found on the system";
                return;
            }

            try
            {
                var str = new StringBuilder();
                foreach (var device in discoveredI2cDevices)
                {
                    str.AppendLine($"{device.Name} ({device.Id}) {device.Kind}.");
                }
                DisplayTextStatus.Text = str.ToString();

                lsm9Ds = discoveredI2cDevices[0].Id[discoveredI2cDevices[0].Id.Length - 1] == '1' ?
                         (baseLSM9DS) new LSM9DS1() : (baseLSM9DS) new LSM9DS0();

                await lsm9Ds.Initialise(discoveredI2cDevices[0].Id);

                // Now that everything is initialized, create a timer so we read data every DT
                periodicTimer = new Timer(this.TimerCallback, null, 0, loopDeltaInMilliseconds);

                DisplayTextStatus.Text = "Initialized I2CBerryIMU";
            }
            catch (Exception ex)
            {
                DisplayTextStatus.Text = ex.ToString();
            }
        }
        private async Task EnsureInitializedAsync()
        {
            if (IsInitialised)
            {
                return;
            }

            try
            {
                var settings = new I2cConnectionSettings(I2C_ADDRESS);
                settings.BusSpeed = I2cBusSpeed.FastMode;

                string aqs = I2cDevice.GetDeviceSelector(I2cControllerName);  /* Find the selector string for the I2C bus controller                   */
                var    dis = await DeviceInformation.FindAllAsync(aqs);       /* Find the I2C bus controller device with our selector string           */

                I2CDevice = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */

                byte id = read8(Register.BMP085_REGISTER_CHIPID);
                if (id != 0x55)
                {
                    throw new Exception("Device is not a BMP180");
                }

                ReadCoefficients();

                IsInitialised = true;
            }
            catch (Exception ex)
            {
                throw new Exception("I2C Initialization Failed", ex);
            }
        }
    public async Task Initialize()
    {
        Debug.WriteLine("HIH8120: Initialize");
        try
        {
            I2cConnectionSettings settings = new I2cConnectionSettings(HIH8120_ADDRESS);
            settings.BusSpeed = I2cBusSpeed.FastMode;
            var deviceQuerySelector  = I2cDevice.GetDeviceSelector(I2C_CONTROLLER_NAME);
            var deviceInfoCollection = await DeviceInformation.FindAllAsync(deviceQuerySelector);

            _hih8120 = await I2cDevice.FromIdAsync(deviceInfoCollection[0].Id, settings);

            if (_hih8120 == null)
            {
                Debug.WriteLine("Device Not Found");
            }
            else
            {
                Debug.WriteLine("Device Initialized");
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Exception: " + ex.Message);
            throw;
        }
    }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes the MC9808.
        /// </summary>
        /// <returns>Returns an InitializationResult value indicating if the
        /// initialization was success or not.</returns>
        public async Task <InitializationResult> Initialize()
        {
            InitializationResult returnValue = InitializationResult.None;

            // ***
            // *** Get a selector string that will return all I2C controllers on the system
            // ***
            string aqs = I2cDevice.GetDeviceSelector();

            // ***
            // *** Find the I2C bus controller device with our selector string
            // ***
            var dis = await DeviceInformation.FindAllAsync(aqs).AsTask();

            if (dis.Count > 0)
            {
                var settings = new I2cConnectionSettings(this.DeviceAddress);
                settings.BusSpeed = this.BusSpeed;

                // ***
                // *** Create an I2cDevice with our selected bus controller and I2C settings
                // ***
                this.Device = await I2cDevice.FromIdAsync(dis[0].Id, settings);

                if (this.Device != null)
                {
                    IsInitialized = true;

                    // ***
                    // *** Check the Manufacturing and Device ID
                    // ***
                    if (this.ManufacturingId == 0x54 && this.DeviceId.Id == 0x04)
                    {
                        returnValue = InitializationResult.Successful;
                    }
                    else
                    {
                        IsInitialized = false;
                        returnValue   = InitializationResult.DeviceNotFound;
                    }
                }
                else
                {
                    // ***
                    // *** Slave address n on I2C Controller  is currently in use by
                    // *** another application. Please ensure that no other applications are using I2C.
                    // ***
                    returnValue = InitializationResult.DeviceInUse;
                }
            }
            else
            {
                // ***
                // *** No I2C controllers were found on the system
                // ***
                returnValue = InitializationResult.NoI2cController;
            }

            return(returnValue);
        }
Ejemplo n.º 7
0
        public async Task Initialize()
        {
            Debug.WriteLine("TCS34725::Initialize");

            try
            {
                var settings = new I2cConnectionSettings(TCS34725_Address);
                settings.BusSpeed = I2cBusSpeed.FastMode;

                string aqs = I2cDevice.GetDeviceSelector(I2CControllerName);
                var    dis = await DeviceInformation.FindAllAsync(aqs);

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

                // Now setup the LedControlPin
                gpio = GpioController.GetDefault();

                LedControlGPIOPin = gpio.OpenPin(LedControlPin);
                LedControlGPIOPin.SetDriveMode(GpioPinDriveMode.Output);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace);
                throw;
            }
        }
Ejemplo n.º 8
0
        private async Task ConnectToI2CDevices()
        {
            try {
                string aqsFilter = I2cDevice.GetDeviceSelector("I2C1");

                //DeviceInformationCollection collection = await DeviceInformation.FindAllAsync(aqsFilter);
                //if (collection.Count == 0) {
                //    throw new SensorException("I2C device not found");
                //}

                if (LightningProvider.IsLightningEnabled)
                {
                    // Set Lightning as the default provider
                    LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
                }

                I2cConnectionSettings i2CSettings = new I2cConnectionSettings(_i2CAddress)
                {
                    BusSpeed = I2cBusSpeed.FastMode
                };

                //_i2CDevice = await I2cDevice.FromIdAsync(collection[0].Id, i2CSettings);
                I2cController controller = await I2cController.GetDefaultAsync();

                if (controller == null)
                {
                    throw new SensorException("I2C device not found");
                }

                _i2CDevice = controller.GetDevice(i2CSettings);
            }
            catch (Exception exception) {
                throw new SensorException("Failed to connect to HTS221", exception);
            }
        }
Ejemplo n.º 9
0
        public async Task Init()
        {
            try
            {
                var dis = await DeviceInformation.FindAllAsync(I2cDevice.GetDeviceSelector());                                              // Find the I2C bus controller device

                I2C = await I2cDevice.FromIdAsync(dis[0].Id, new I2cConnectionSettings(SLAVE_ADDRESS) { BusSpeed = I2cBusSpeed.FastMode }); // Create an I2cDevice with our selected bus controller and I2C settings (the bus speed also could be I2cBusSpeed.StandardMode)

                BothToLow();

                // start in 8bit mode, try to set 4 bit mode:
                // Figure 24 https://www.sparkfun.com/datasheets/LCD/HD44780.pdf
                Initial4bits(MODE_8_BIT, 410); // wait min 4.1ms
                Initial4bits(MODE_8_BIT, 410); // wait min 4.1ms
                Initial4bits(MODE_8_BIT, 10);  // wait min 0.1ms
                Initial4bits(MODE_4_BIT, 10);  // wait min 0.1ms

                BacklightOFF();
                DisplayOff();
                DisplayOn();
                BacklightON();
                ReturnHome();
            }
            catch (Exception e)
            {
                Debug.WriteLine("EXCEPTION! Init: " + e.Message);
                exceptioned = true;
            }
        }
Ejemplo n.º 10
0
        public BuiltInI2CBus(DeviceId id, ILogger logger)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            Id      = id;
            _logger = logger;

            string deviceSelector = I2cDevice.GetDeviceSelector();

            DeviceInformationCollection deviceInformation = DeviceInformation.FindAllAsync(deviceSelector).AsTask().Result;

            if (deviceInformation.Count == 0)
            {
                throw new InvalidOperationException("I2C bus not found.");
            }

            _i2CBusId = deviceInformation.First().Id;
        }
Ejemplo n.º 11
0
        private static async Task EnsureInitializedAsync()
        {
            // If already initialized, done
            if (_isInited)
            {
                return;
            }

            // Validate
            if (string.IsNullOrWhiteSpace(I2CControllerName))
            {
                throw new Exception("Controller name not set");
            }

            // Get a query for I2C
            var aqs = I2cDevice.GetDeviceSelector(I2CControllerName);

            // Find the first I2C device
            var di = (await DeviceInformation.FindAllAsync(aqs)).FirstOrDefault();

            // Make sure we found an I2C device
            if (di == null)
            {
                throw new Exception("Device Info null: " + I2CControllerName);
            }

            // Connection settings for primary device
            var primarySettings = new I2cConnectionSettings(PwmI2CAddr)
            {
                BusSpeed    = I2cBusSpeed.FastMode,
                SharingMode = I2cSharingMode.Exclusive
            };

            // Get the primary device
            _primaryDevice = await I2cDevice.FromIdAsync(di.Id, primarySettings);

            if (_primaryDevice == null)
            {
                throw new Exception("PCA9685 primary device not found");
            }

            // Connection settings for reset device
            var resetSettings = new I2cConnectionSettings(PwmI2CAddr);

            resetSettings.SlaveAddress = I2CResetAddress;

            // Get the reset device
            _resetDevice = await I2cDevice.FromIdAsync(di.Id, resetSettings);

            if (_resetDevice == null)
            {
                throw new Exception("PCA9685 reset device not found");
            }

            // Initialize the controller
            await InitializeControllerAsync();

            // Done initializing
            _isInited = true;
        }
        public async void Start()
        {
            string aqs = I2cDevice.GetDeviceSelector();
            DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs);

            I2CThreadListener(dis);
        }
Ejemplo n.º 13
0
        public Bh1750(string i2CControllerName,
                      PinConnection pinConnection     = PinConnection.PIN_LOW,
                      MeasurementMode measurementMode = MeasurementMode.ContinuouslyHighResolutionMode2)
        {
            _pinConnection     = pinConnection;
            _i2CControllerName = i2CControllerName;
            try
            {
                var settings = new I2cConnectionSettings((byte)_pinConnection);
                settings.BusSpeed    = I2cBusSpeed.StandardMode;
                settings.SharingMode = I2cSharingMode.Shared;
                string aqs = I2cDevice.GetDeviceSelector(_i2CControllerName);
                _device = I2cDevice.FromId(_i2CControllerName, settings);
                if (_device == null)
                {
                    Logger.Log("Device not found", Logger.LogLevel.Warning);
                }

                SetMode(measurementMode);
            }
            catch (Exception e)
            {
                Logger.Log("Exception: " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
                throw;
            }
        }
Ejemplo n.º 14
0
        private async Task ConnectToI2CDevices()
        {
            try
            {
                string aqsFilter = I2cDevice.GetDeviceSelector("I2C1");

                DeviceInformationCollection collection = await DeviceInformation.FindAllAsync(aqsFilter);

                if (collection.Count == 0)
                {
                    throw new SensorException("I2C device not found");
                }

                I2cConnectionSettings i2CSettings = new I2cConnectionSettings(_i2CAddress)
                {
                    BusSpeed = I2cBusSpeed.FastMode
                };

                _i2CDevice = await I2cDevice.FromIdAsync(collection[0].Id, i2CSettings);
            }
            catch (Exception exception)
            {
                throw new SensorException("Failed to connect to LPS25H", exception);
            }
        }
Ejemplo n.º 15
0
        private async Task InitializeAdF_LSM303_Accel_I2CDevice()
        {
            try
            {
                // Initialize I2C device
                var settings = new I2cConnectionSettings(Adafruit_LSM303_Accel_Unified.LSM303_ADDRESS_ACCEL);

                settings.BusSpeed    = I2cBusSpeed.FastMode;
                settings.SharingMode = I2cSharingMode.Shared;

                string aqs = I2cDevice.GetDeviceSelector("I2C1");               /* Find the selector string for the I2C bus controller                   */
                var    dis = await DeviceInformation.FindAllAsync(aqs);         /* Find the I2C bus controller device with our selector string           */

                I2CDevaccel = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */

                accel       = new Adafruit_LSM303_Accel_Unified(ref I2CDevaccel, 30301);
                accelSensor = accel.getSensorObj();
                accel.getSensor(ref accelSensor);
                Debug.WriteLine("------------- ACCELEROMETER -----------");
                Debug.WriteLine("Sensor:       " + accelSensor.name);
                Debug.WriteLine("Driver Ver:   " + accelSensor.version);
                Debug.WriteLine("Unique ID:    " + accelSensor.sensor_id);
                Debug.WriteLine("Max Value:    " + accelSensor.max_value + " m/s^2");
                Debug.WriteLine("Min Value:    " + accelSensor.min_value + " m/s^2");
                Debug.WriteLine("Resolution:   " + accelSensor.resolution + " m/s^2");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());

                return;
            }
        }
Ejemplo n.º 16
0
        /**
         *
         * Start I2C Communication
         *
         **/

        public async void startI2C(byte deviceAddress, string controllerName)

        {
            try

            {
                var i2cSettings = new I2cConnectionSettings(deviceAddress);

                i2cSettings.BusSpeed = I2cBusSpeed.FastMode;

                string deviceSelector = I2cDevice.GetDeviceSelector(controllerName);

                var i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);

                this._i2cPortExpander = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);
            }

            catch (Exception e)

            {
                System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);

                return;
            }
        }
Ejemplo n.º 17
0
        private async Task InitializeAdF_BMP085_U_I2CDevice()
        {
            try
            {
                // Initialize I2C device
                var settings = new I2cConnectionSettings(Adafruit_BMP085_Unified.BMP085_ADDRESS);

                settings.BusSpeed    = I2cBusSpeed.FastMode;
                settings.SharingMode = I2cSharingMode.Shared;

                string aqs = I2cDevice.GetDeviceSelector("I2C1");             /* Find the selector string for the I2C bus controller                   */
                var    dis = await DeviceInformation.FindAllAsync(aqs);       /* Find the I2C bus controller device with our selector string           */

                I2CDevbmp = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */

                bmp       = new Adafruit_BMP085_Unified(ref I2CDevbmp, 18001);
                bmpSensor = bmp.getSensorObj();
                bmp.getSensor(ref bmpSensor);
                Debug.WriteLine("-------- PRESSURE/ALTITUDE ---------");
                Debug.WriteLine("Sensor:       " + bmpSensor.name);
                Debug.WriteLine("Driver Ver:   " + bmpSensor.version);
                Debug.WriteLine("Unique ID:    " + bmpSensor.sensor_id);
                Debug.WriteLine("Max Value:    " + bmpSensor.max_value + " hPa");
                Debug.WriteLine("Min Value:    " + bmpSensor.min_value + " hpa");
                Debug.WriteLine("Resolution:   " + bmpSensor.resolution + " hPa");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());

                return;
            }
        }
        private async Task EnsureInitializedAsync()
        {
            if (IsInitialised)
            {
                return;
            }

            try
            {
                var settings = new I2cConnectionSettings(I2C_ADDRESS);
                settings.BusSpeed = I2cBusSpeed.FastMode;

                string aqs = I2cDevice.GetDeviceSelector(I2cControllerName);  /* Find the selector string for the I2C bus controller                   */
                var    dis = await DeviceInformation.FindAllAsync(aqs);       /* Find the I2C bus controller device with our selector string           */

                I2CDevice = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */


                IsInitialised = true;
            }
            catch (Exception ex)
            {
                throw new Exception("I2C Initialization Failed", ex);
            }
        }
        /// <summary>
        /// InitI2C
        /// Initialize I2C Communications
        /// </summary>
        /// <returns>async Task</returns>
        public async Task InitI2CAsync(I2CSpeed i2cSpeed = I2CSpeed.I2C_100kHz)
        {
            // initialize I2C communications
            try
            {
                I2cConnectionSettings i2cSettings = new I2cConnectionSettings(I2CAddr);
                if (i2cSpeed == I2CSpeed.I2C_400kHz)
                {
                    i2cSettings.BusSpeed = I2cBusSpeed.FastMode;
                }
                else
                {
                    i2cSettings.BusSpeed = I2cBusSpeed.StandardMode;
                }

                string deviceSelector       = I2cDevice.GetDeviceSelector();
                var    i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);

                Device = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
                return;
            }
        }
Ejemplo n.º 20
0
        private async Task StartScenarioAsync()
        {
            string i2cDeviceSelector = I2cDevice.GetDeviceSelector();
            IReadOnlyList <DeviceInformation> devices = await DeviceInformation.FindAllAsync(i2cDeviceSelector);

            // 0x40 was determined by looking at the datasheet for the HTU21D sensor.
            var HTU21D_settings = new I2cConnectionSettings(0x40);

            // If this next line crashes with an ArgumentOutOfRangeException,
            // then the problem is that no I2C devices were found.
            //
            // If the next line crashes with Access Denied, then the problem is
            // that access to the I2C device (HTU21D) is denied.
            //
            // The call to FromIdAsync will also crash if the settings are invalid.
            //
            // FromIdAsync produces null if there is a sharing violation on the device.
            // This will result in a NullReferenceException in Timer_Tick below.
            htu21dSensor = await I2cDevice.FromIdAsync(devices[0].Id, HTU21D_settings);

            // Start the polling timer.
            timer = new DispatcherTimer()
            {
                Interval = TimeSpan.FromMilliseconds(500)
            };
            timer.Tick += Timer_Tick;
            timer.Start();
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 初期化
        /// </summary>
        public async void Initialize()
        {
            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 Exception("No I2C controllers were found on the system");
            }
            var settings = new I2cConnectionSettings(PCA9685_I2C_ADDR);

            settings.BusSpeed    = I2cBusSpeed.StandardMode;
            settings.SharingMode = I2cSharingMode.Shared;
            _device = await I2cDevice.FromIdAsync(dis[0].Id, settings);    /* Create an I2cDevice with our selected bus controller and I2C settings */

            if (_device == null)
            {
                throw new Exception(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));
            }
            SetPWMFreq(60.0);
            return;
        }
Ejemplo n.º 22
0
        /// <summary>
        /// InitI2C
        /// Initialize I2C Communications
        /// </summary>
        /// <returns>async Task</returns>
        public async Task InitI2CAsync(I2CSpeed i2cSpeed = I2CSpeed.I2C_100kHz)
        {
            // initialize I2C communications
            try
            {
                I2cConnectionSettings i2cSettings = new I2cConnectionSettings(I2CAddr);
                if (i2cSpeed == I2CSpeed.I2C_400kHz)
                {
                    i2cSettings.BusSpeed = I2cBusSpeed.FastMode;
                }
                else
                {
                    i2cSettings.BusSpeed = I2cBusSpeed.StandardMode;
                }

                string deviceSelector       = I2cDevice.GetDeviceSelector();
                var    i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);

                Device = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);

                if (Device == null)
                {
                    throw new NullReferenceException($"{nameof(I2cDevice)} was null. {nameof(InitI2CAsync)} operation failed.");
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
                throw e;
            }
        }
Ejemplo n.º 23
0
        //Method to initialize the BME280 sensor
        public async Task Initialize()
        {
            Debug.WriteLine("BME280::Initialize");

            try
            {
                //Instantiate the I2CConnectionSettings using the device address of the BME280
                I2cConnectionSettings settings = new I2cConnectionSettings(BME280_Address);
                //Set the I2C bus speed of connection to fast mode
                settings.BusSpeed = I2cBusSpeed.FastMode;
                //Use the I2CBus device selector to create an advanced query syntax string
                string aqs = I2cDevice.GetDeviceSelector(I2CControllerName);
                //Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string
                DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs);

                //Instantiate the the BME280 I2C device using the device id of the I2CBus and the I2CConnectionSettings
                bme280 = await I2cDevice.FromIdAsync(dis[0].Id, settings);

                //Check if device was found
                if (bme280 == null)
                {
                    Debug.WriteLine("Device not found");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace);
                throw;
            }

            byte[] readChipID = new byte[] { (byte)eRegisters.BME280_REGISTER_CHIPID };
            byte[] ReadBuffer = new byte[] { 0xFF };

            //Read the device signature
            bme280.WriteRead(readChipID, ReadBuffer);
            Debug.WriteLine("BME280 Signature: " + ReadBuffer[0].ToString());

            //Verify the device signature
            if (ReadBuffer[0] != BME280_Signature)
            {
                Debug.WriteLine("BME280::Begin Signature Mismatch.");
                return;
            }

            //Read the coefficients table
            CalibrationData = ReadCoefficeints();

            //Set configuration registers
            WriteConfigRegister();
            WriteControlMeasurementRegister();
            WriteControlRegisterHumidity();

            //Set configuration registers again to ensure configuration of humidity
            WriteConfigRegister();
            WriteControlMeasurementRegister();
            WriteControlRegisterHumidity();

            //Dummy read temp to setup t_fine
            ReadTemperature();
        }
Ejemplo n.º 24
0
        /// <summary>
        /// 制御開始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void clickStart(object sender, RoutedEventArgs e)
        {
            try {
                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           */

                var settings = new I2cConnectionSettings(PWM_I2C_ADDR);
                settings.BusSpeed = I2cBusSpeed.StandardMode;
                I2CPwm            = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */

                pwm1 = new PwmServo(I2CPwm, 0);
                pwm2 = new PwmServo(I2CPwm, 1);
                pwm3 = new PwmServo(I2CPwm, 2);
                pwm4 = new PwmServo(I2CPwm, 3);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return;
            }

            // 一つだけ設定する
            pwm1.Begin();
            pwm1.SetPWMFreq(60); // 60Hz
        }
Ejemplo n.º 25
0
        private async void InitializeI2CDevice()
        {
            try
            {
                // Initialize I2C device
                var settings = new I2cConnectionSettings(TSL2561.TSL2561_ADDR)
                {
                    BusSpeed    = I2cBusSpeed.FastMode,
                    SharingMode = I2cSharingMode.Shared
                };

                string aqs = I2cDevice.GetDeviceSelector(I2C_CONTROLLER_NAME); /* Find the selector string for the I2C bus controller                   */
                var    dis = await DeviceInformation.FindAllAsync(aqs);        /* Find the I2C bus controller device with our selector string           */

                I2CDev = await I2cDevice.FromIdAsync(dis[0].Id, settings);     /* Create an I2cDevice with our selected bus controller and I2C settings */
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());

                return;
            }

            InitializeSensor();
        }
Ejemplo n.º 26
0
        private async Task LCD_Setup()
        {
            try
            {
                string aqs = I2cDevice.GetDeviceSelector();
                var    dis = await DeviceInformation.FindAllAsync(aqs);

                if (dis.Count == 0)
                {
                    return;
                }

                var settings = new I2cConnectionSettings(_addr);
                settings.BusSpeed = I2cBusSpeed.StandardMode;
                I2C = await I2cDevice.FromIdAsync(dis[0].Id, settings);

                if (I2C == null)
                {
                    return;
                }
            }
            catch (Exception)
            {
                return;
            }
        }
Ejemplo n.º 27
0
        private async void InitI2CHumtemp()
        {
            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)
            {
                Text_Status.Text = "No I2C controllers were found on the system";
                return;
            }

            var settings = new I2cConnectionSettings(HUMTEMP_I2C_ADDR);

            settings.BusSpeed = I2cBusSpeed.FastMode;
            I2CHumtemp        = await I2cDevice.FromIdAsync(dis[0].Id, settings);       // Create an I2C Device with our selected bus controller and I2C settings

            if (I2CHumtemp == null)
            {
                Text_Status.Text = 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);
                return;
            }

            // Create a timer to read data every 100ms
            periodicTimer = new Timer(this.TimerCallback, null, 0, 100);
        }
Ejemplo n.º 28
0
        private static readonly byte[] CMD_RESETPAGEADDR = { 0x22, 0x00, 0x07 }; /* Reset the page address pointer                           */

        public async Task Init(bool proceedOnFail = false)
        {
            Debug.WriteLine("SSD1306::Initialize");

            try
            {
                //Instantiate the I2CConnectionSettings using the device address
                I2cConnectionSettings settings = new I2cConnectionSettings(SSD1306_Address);
                //Set the I2C bus speed of connection to fast mode
                settings.BusSpeed = I2cBusSpeed.FastMode;
                //Use the I2CBus device selector to create an advanced query syntax string
                string aqs = I2cDevice.GetDeviceSelector(I2CControllerName);
                //Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string
                DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs);

                //Instantiate the the I2C device using the device id of the I2CBus and the I2CConnectionSettings
                displayI2c = await I2cDevice.FromIdAsync(dis[0].Id, settings);

                //Check if device was found
                if (displayI2c == null)
                {
                    Debug.WriteLine("Device not found");
                }
                else
                {
                    InitDisplay(proceedOnFail);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace);
                throw;
            }
        }
        //Method to initialize the BME280 sensor
        public async Task Initialize()
        {
            Log.Instance.Write("BME280::Initialize");

            try
            {
                //Instantiate the I2CConnectionSettings using the device address of the BME280
                I2cConnectionSettings settings = new I2cConnectionSettings(BME280_Address);
                //Set the I2C bus speed of connection to fast mode
                settings.BusSpeed = I2cBusSpeed.FastMode;
                //Use the I2CBus device selector to create an advanced query syntax string
                string aqs = I2cDevice.GetDeviceSelector(I2CControllerName);
                //Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string
                DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs);

                //Instantiate the the BME280 I2C device using the device id of the I2CBus and the I2CConnectionSettings
                bme280 = await I2cDevice.FromIdAsync(dis[0].Id, settings);

                //Check if device was found
                if (bme280 == null)
                {
                    Log.Instance.Write("Device not found");
                }
            }
            catch (Exception e)
            {
                Log.Instance.Write("Exception: " + e.Message + "\n" + e.StackTrace);
                throw;
            }
        }
Ejemplo n.º 30
0
        public async Task InitializeAsync(byte picoBorgRevDeviceId = DEFAULT_I2C_ADDRESS)
        {
            // Set the I2C address and speed
            _settings = new I2cConnectionSettings(picoBorgRevDeviceId)
            {
                BusSpeed = I2cBusSpeed.StandardMode
            };

            Debug.WriteLine("Initializing PicoBorgRev");
            OnControllerMessageReceived("Initializing PicoBorgRev");

            string aqs = I2cDevice.GetDeviceSelector();

            var dis = await DeviceInformation.FindAllAsync(aqs);

            Controller = await I2cDevice.FromIdAsync(dis.First().Id, _settings);

            if (Controller == null)
            {
                Debug.WriteLine("PicoBorgRev failed to initialize");
                OnControllerMessageReceived("PicoBorgRev failed to initialize");
            }

            Debug.WriteLine("PicoBorgRev successfully initialized");
            OnControllerMessageReceived("PicoBorgRev successfully initialized");

            IsEnabled = true;
        }