예제 #1
0
        async Task <bool> AS7000_I2C_Init()
        {
            var i2cSettings = new I2cConnectionSettings(AS7000_I2C_ADDRESS); // connect to default address;

            i2cSettings.BusSpeed = I2cBusSpeed.FastMode;
            //string deviceSelector = I2cDevice.GetDeviceSelector("I2C5");
            string deviceSelector       = I2cDevice.GetDeviceSelector(AS7000_I2C_PORT);
            var    i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);

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

            return(false);
        }
예제 #2
0
        //Setup device with i2c
        public async void Setup_device()
        {
            //return i2c controller
            string i2cDeviceSelector = I2cDevice.GetDeviceSelector();
            //find I2C bus controller device with selector string
            IReadOnlyList <DeviceInformation> devices = await DeviceInformation.FindAllAsync(i2cDeviceSelector);

            // create the settings and specify the device adress
            // device adress from data sheet (https://www.silabs.com/documents/public/data-sheets/Si7021-A20.pdf Page 18)
            var si7021_settings = new I2cConnectionSettings(0x40);

            i2cDevice = await I2cDevice.FromIdAsync(devices[0].Id, si7021_settings);
        }
예제 #3
0
        public static async Task <I2cDevice> GetDeviceAsync(int addr, I2cBusSpeed speed = I2cBusSpeed.StandardMode, I2cSharingMode sharing = I2cSharingMode.Exclusive)
        {
            var aqs   = I2cDevice.GetDeviceSelector();
            var infos = await DeviceInformation.FindAllAsync(aqs);

            var settings = new I2cConnectionSettings(addr)
            {
                BusSpeed    = speed,
                SharingMode = sharing
            };

            return(await I2cDevice.FromIdAsync(infos[0].Id, settings));
        }
예제 #4
0
        private async void InitI2CGyro()
        {
            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(GYRO_I2C_ADDR);

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

            if (I2CGyro == 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;
            }

            /*
             *      Initialize the 3-Axis MEMS Gyro Angular Rate Sensor
             *      For this device, we create 2-byte write buffers
             *      The first byte is the register address we want to write to
             *      The second byte is the contents that we want to write to the register
             */
            byte[] WriteBuf_Power = new byte[] { GYRO_REG_POWER, 0x01 };                // 0x01 Power ON's the sensor and clock source is set to PLL with X Gyro reference
            byte[] WriteBuf_Dlpf  = new byte[] { GYRO_REG_DLPF, 0x18 };                 // 0x18 sets gyro full-scale range to ±2000°/sec, low pass filter bandwidth to 256 Hz and internal sample rate to 8 Hz

            // Write the register settings
            try
            {
                I2CGyro.Write(WriteBuf_Power);
                I2CGyro.Write(WriteBuf_Dlpf);
            }
            // If the write fails display the error and stop running
            catch (Exception ex)
            {
                Text_Status.Text = "Failed to communicate with device: " + ex.Message;
                return;
            }

            // Create a timer to read data every 500ms
            periodicTimer = new Timer(this.TimerCallback, null, 0, 500);
        }
예제 #5
0
        /* Initialize GPIO, I2C, and the display
         * The device may not respond to multiple Init calls without being power cycled
         * so we allow an optional boolean to excuse failures which is useful while debugging
         * without power cycling the display */
        public async Task <bool> Init(int DeviceAddress)
        {
            Debug.WriteLine("SSD1306::Initialize");

            try
            {
                if (LightningProvider.IsLightningEnabled)
                {
                    LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

                    I2cController         controller = (await I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()))[0];
                    I2cConnectionSettings settings   = new I2cConnectionSettings(DeviceAddress);
                    //                settings.SharingMode = I2cSharingMode.Shared;
                    displayI2c = controller.GetDevice(settings);
                }
                else
                {
                    //Instantiate the I2CConnectionSettings using the device address
                    I2cConnectionSettings settings = new I2cConnectionSettings(DeviceAddress);
                    //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");
                    return(false);
                }
                else
                {
                    return(true);
                    //                return InitDisplay();
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace);
                return(false);
                //             throw;
            }
        }
예제 #6
0
        private async void InitI2CAccel()
        {
            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(ACCEL_I2C_ADDR);

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

            if (I2CAccel == 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;
            }

            /*
             *      Initialize the accelerometer:
             *      For this device, we create 2-byte write buffers:
             *      The first byte is the register address we want to write to.
             *      The second byte is the contents that we want to write to the register.
             */
            byte[] WriteBuf_Control1 = new byte[] { ACCEL_REG_CONTROL1, 0x27 };                 // 0x27 sets Power ON Mode and Output Data Rate = 10 Hz, X, Y, Z axes enabled
            byte[] WriteBuf_Control4 = new byte[] { ACCEL_REG_CONTROL4, 0x00 };                 // 0x00 sets Continuous update and range to +- 2Gs

            // Write the register settings
            try
            {
                I2CAccel.Write(WriteBuf_Control1);
                I2CAccel.Write(WriteBuf_Control4);
            }
            // If the write fails display the error and stop running
            catch (Exception ex)
            {
                Text_Status.Text = "Failed to communicate with device: " + ex.Message;
                return;
            }

            // Now that everything is initialized, create a timer so we read data every 300mS
            periodicTimer = new Timer(this.TimerCallback, null, 0, 300);
        }
        private async void InitializeSystem()
        {
            // initialize I2C communications
            try
            {
                string deviceSelector       = I2cDevice.GetDeviceSelector(I2C_CONTROLLER_NAME);
                var    i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);

                //Port expander controlling the Button Panel
                var buttonPanelSettings = new I2cConnectionSettings(BUTTON_PANEL_I2C_ADDRESS);
                buttonPanelSettings.BusSpeed = I2cBusSpeed.FastMode;
                i2cButtonPanel = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, buttonPanelSettings);

                //Port expander controlling the LCD Screen
                var lcdSettings = new I2cConnectionSettings(LCD_SCREEN_I2C_ADDRESS);
                lcdSettings.BusSpeed = I2cBusSpeed.FastMode;
                i2cLcdScreen         = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, lcdSettings);

                Screen = new LcdScreen(i2cLcdScreen);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
                return;
            }

            // initialize I2C Port Expander registers
            try
            {
                InitializeButtonPanel();
                InitializeLcd();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
                return;
            }

            try
            {
                buttonStatusCheckTimer          = new DispatcherTimer();
                buttonStatusCheckTimer.Interval = TimeSpan.FromMilliseconds(BUTTON_STATUS_CHECK_TIMER_INTERVAL);
                buttonStatusCheckTimer.Tick    += ButtonStatusCheckTimer_Tick;
                buttonStatusCheckTimer.Start();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
                return;
            }
        }
예제 #8
0
        private async void InitI2CAccel()
        {
            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(ACCEL_I2C_ADDR);

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

            if (I2CAccel == 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;
            }

            /*
             * Initialize the accelerometer
             * For this device, we create 2-byte write buffers
             * The first byte is the register address we want to write to
             * The second byte is the contents that we want to write to the register
             */
            byte[] WriteBuf_RangeSel  = new byte[] { ACCEL_REG_RANGE_SLCT, 0x03 };                      // 0x03 sets range to +- 2Gs
            byte[] WriteBuf_Bandwidth = new byte[] { ACCEL_REG_BANDWIDTH, 0x08 };                       // 0x08 sets Bandwidth = 7.81 Hz

            // Write the register settings
            try
            {
                I2CAccel.Write(WriteBuf_RangeSel);
                I2CAccel.Write(WriteBuf_Bandwidth);
            }
            // If the write fails display the error and stop running
            catch (Exception ex)
            {
                Text_Status.Text = "Failed to communicate with device: " + ex.Message;
                return;
            }

            // A timer so we read data every 300mS
            periodicTimer = new Timer(this.TimerCallback, null, 0, 300);
        }
예제 #9
0
        private async Task <I2cDevice> InitializeI2CAsync()
        {
            string aqs = I2cDevice.GetDeviceSelector("I2C1");
            DeviceInformationCollection device_information_collection = await DeviceInformation.FindAllAsync(aqs);

            string deviceId = device_information_collection[0].Id;

            I2cConnectionSettings connection = new I2cConnectionSettings(I2C_ADDR);

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

            return(await I2cDevice.FromIdAsync(deviceId, connection));
        }
        //Initilizes the I2C connection and starts the timer to read I2C Data
        async public static void InitSensors()
        {
            //Set up the I2C connection the Arduino
            var settings = new I2cConnectionSettings(0x40); // Arduino address

            settings.BusSpeed = I2cBusSpeed.StandardMode;
            string aqs = I2cDevice.GetDeviceSelector("I2C1");
            var    dis = await DeviceInformation.FindAllAsync(aqs);

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

            //Create a timer to periodicly read the temps from the Arduino
            periodicTimer = new Timer(Sensors.TimerCallback, null, 0, ReadInterval);
        }
예제 #11
0
        public async void Initialiasecom()
        {
            var settings = new I2cConnectionSettings(0x40);   // Slave Address of Arduino Uno

            settings.BusSpeed = I2cBusSpeed.FastMode;         // this bus has 400Khz speed
            string aqs = I2cDevice.GetDeviceSelector("I2C1"); // This will return Advanced Query String which is used to select i2c device
            var    dis = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(aqs);

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

            timer.Tick    += Timer_Tick;                    // We will create an event handler
            timer.Interval = new TimeSpan(0, 0, 0, 0, 500); // Timer_Tick is executed every 500 milli second
            timer.Start();
        }
        internal async Task <I2CDeviceNativeAdapter> CreateAsync()
        {
            var aqsFilter = I2cDevice.GetDeviceSelector();
            var deviceInformationCollection = await DeviceInformation.FindAllAsync(aqsFilter);

            var settings = new I2cConnectionSettings(DEVICE_ADDRESS)
            {
                BusSpeed = I2cBusSpeed.StandardMode
            };

            var i2CDevice = await I2cDevice.FromIdAsync(deviceInformationCollection[0].Id, settings);

            return(new I2CDeviceNativeAdapter(i2CDevice));
        }
예제 #13
0
        private async void initcomunica()
        {
            var settings = new I2cConnectionSettings(0x40); // Arduino address

            settings.BusSpeed = I2cBusSpeed.FastMode;

            string aqs = I2cDevice.GetDeviceSelector("I2C1");

            var dis = await DeviceInformation.FindAllAsync(aqs);

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

            periodicTimer = new Timer(this.TimerCallback, null, 0, 2000); // Create a timmer
        }
예제 #14
0
        public async Task Initialize()
        {
            var settings = new I2cConnectionSettings(TSL2561Sensor.TSL2561_ADDR);

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

            string aqs = I2cDevice.GetDeviceSelector("I2C1");
            DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs);

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

            _current.PowerUp();
        }
예제 #15
0
        private async void InitI2Ccompass()
        {
            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(COMPASS_I2C_ADDR);

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

            if (I2Ccompass == 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;
            }

            /*
             *      Initialize the compass:
             *      For this device, we create 2-byte write buffers:
             *      The first byte is the register address we want to write to.
             *      The second byte is the contents that we want to write to the register.
             */
            byte[] WriteBuf_Ctrl1 = new byte[] { COMPASS_REG_CTRL_1, 0x01 };                    // 0x01 sets data output rate = 80.00 Hz, Over sample ratio = 16, 16-bit value read, normal operation, Active mode

            // Write the register settings
            try
            {
                I2Ccompass.Write(WriteBuf_Ctrl1);
            }
            // If the write fails display the error and stop running
            catch (Exception ex)
            {
                Text_Status.Text = "Failed to communicate with device: " + ex.Message;
                return;
            }

            // Create a timer to read data every 300ms
            periodicTimer = new Timer(this.TimerCallback, null, 0, 300);
        }
예제 #16
0
        public async void CallmeBaby()
        {
            string aqsFilter = I2cDevice.GetDeviceSelector("I2C1");

            DeviceInformationCollection collection = await DeviceInformation.FindAllAsync(aqsFilter);

            if (collection.Count == 0)
            {
                return;
            }
            I2cConnectionSettings settings = new I2cConnectionSettings(0x5f);

            settings.BusSpeed = I2cBusSpeed.FastMode;

            I2cDevice device = await I2cDevice.FromIdAsync(collection[0].Id, settings);


            if (!RTI2C.Write(device, 0x20, 0x87))
            {
                var ErrorMessage = "Failed to set HTS221 CTRL_REG_1";
                return;
            }

            if (!RTI2C.Write(device, 0x10, 0x1b))
            {
                var ErrorMessage = "Failed to set HTS221 AV_CONF";
                return;
            }

            initTemp(device);
            byte[] twoByte = new byte[2];

            if (!RTI2C.Read(device, 0x2a + 0x80, twoByte))
            {
                var ErrorMessage = "Failed to read HTS221 temperature";
                return;
            }

            double mTemperature = (Int16)((((UInt16)twoByte[1]) << 8) | (UInt16)twoByte[0]);

            mTemperature = mTemperature * mTemperature_m + mTemperature_c;
            var mTemperatureValid = true;


            //double mTemperature = (Int16)((((UInt16)twoByte[1]) << 8) | (UInt16)twoByte[0]);
            // mTemperature = mTemperature * mTemperature_m + mTemperature_c;
            //var mTemperatureValid = true;
            //var q = mTemperature;
        }
예제 #17
0
        public static async Task <byte[]> WriteRead(int slaveAddress, byte[] byteToBeSend)
        {
            byte[] receivedData = new byte[32];


            /* Arduino Nano's I2C SLAVE address */

            try
            {
                // Initialize I2C
                var settings = new I2cConnectionSettings(slaveAddress)
                {
                    BusSpeed = I2cBusSpeed.StandardMode
                };

                if (_aqs == null || _dis == null)
                {
                    _aqs = I2cDevice.GetDeviceSelector("I2C1");
                    _dis = await DeviceInformation.FindAllAsync(_aqs);
                }


                using (I2cDevice device = await I2cDevice.FromIdAsync(_dis[0].Id, settings))
                {
                    var send = device.WritePartial((byteToBeSend));
                    if (send.Status == I2cTransferStatus.SlaveAddressNotAcknowledged)
                    {
                        throw new Exception(String.Format("Urz¹dzenie i2c o adresie {0} nie jest dostêpne!", settings.SlaveAddress));
                    }
                    await Task.Delay(1000);

                    var a = device.ReadPartial(receivedData);
                    if (a.Status != I2cTransferStatus.FullTransfer)
                    {
                        throw new Exception("Nie ostrzymano komunikatu zwrotnego od urz¹dzenia: " + settings.SlaveAddress.ToString());
                    }
                    //  Debug.WriteLine(a.Status);
                }
            }
            catch (Exception ex)
            {
                // SUPPRESS ANY ERROR
                string val = String.Join(",", byteToBeSend);
                LoggerFactory.LogException(ex, "WriteRead", new { val });
            }

            /* Return received data or ZERO on error */
            return(receivedData);
        }
 public async Task Initialize()
 {
     if (_display == null)
     {
         string            i2cDeviceSelector    = I2cDevice.GetDeviceSelector();
         DeviceInformation?i2cDisplayConnection = (await DeviceInformation.FindAllAsync(i2cDeviceSelector)).FirstOrDefault();
         if (i2cDisplayConnection == null)
         {
             Debug.WriteLine("Couldn't find any I2C devices.");
             return;
         }
         var settings = new I2cConnectionSettings(DISPLAY_ADDRESS);
         _display = await I2cDevice.FromIdAsync(i2cDisplayConnection.Id, settings);
     }
 }
예제 #19
0
        internal async Task InitializeAsync()
        {
            var selectorString = I2cDevice.GetDeviceSelector();
            var devices        = await DeviceInformation.FindAllAsync(selectorString);

            var deviceId = devices[0].Id;
            var settings = new I2cConnectionSettings(MP180_ADDR);

            settings.BusSpeed = I2cBusSpeed.StandardMode;
            _sensor           = await I2cDevice.FromIdAsync(deviceId, settings);

            _calibrationData = new CalibrationData();
            ReadCalibrationData();
            IsInitialized = true;
        }
        private async Task InitI2CDevice()
        {
            try {
                var settings = new I2cConnectionSettings(I2C_ADDRESS);
                settings.BusSpeed = I2cBusSpeed.StandardMode;

                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           */

                I2CDevice = await I2cDevice.FromIdAsync(dis[0].Id, settings);   /* Create an I2cDevice with our selected bus controller and I2C settings */
            }
            catch (Exception ex) {
                throw new Exception("I2C Initialization Failed", ex);
            }
        }
예제 #21
0
        private async Task InitializeConnection()
        {
            //sets up connection
            var settings = new I2cConnectionSettings(0x40);

            settings.BusSpeed = I2cBusSpeed.StandardMode;
            //looks for certain type of device
            string aqs = I2cDevice.GetDeviceSelector("I2c1");
            //Gets the first device (the only one)
            var devices = await DeviceInformation.FindAllAsync(aqs);

            _arduino = await I2cDevice.FromIdAsync(devices[0].Id, settings);

            _updateTimer = new Timer(DataReadCallback, null, 0, 1000);
        }
예제 #22
0
        async Task <bool> BMC156_I2C_Init()
        {
            var i2cSettings = new I2cConnectionSettings(BMC156_I2C_ADDRESS); // connect to default address;

            i2cSettings.BusSpeed = I2cBusSpeed.FastMode;
            //string deviceSelector = I2cDevice.GetDeviceSelector("I2C5");
            string deviceSelector       = I2cDevice.GetDeviceSelector(BMC156_I2C_PORT);
            var    i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);

            Debug.WriteLine("i2cDeviceControllers:" + i2cDeviceControllers);
            BMCSensor = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);

            Debug.WriteLine("BMCSensor:" + BMCSensor);
            return(false);
        }
예제 #23
0
        public async Task initialize()
        {
            try {
                string                aqs      = I2cDevice.GetDeviceSelector();
                DeviceInformation     device   = (await DeviceInformation.FindAllAsync(aqs))[0];
                I2cConnectionSettings settings = new I2cConnectionSettings(i2cDevAddress);
                settings.BusSpeed = I2cBusSpeed.StandardMode;
                i2cPort           = await I2cDevice.FromIdAsync(device.Id, settings);

                Debug.WriteLine("The I2C port has been initialized");
                i2cIsInitialized = true;
            } catch (Exception e) {
                Debug.WriteLine("I2C port initialization Error: " + e.Message + "\n" + e.StackTrace);
            }
        }
예제 #24
0
        private static async Task <I2cDevice> GetI2cDeviceHelper(byte address)
        {
            I2cDevice device               = null;
            var       settings             = new I2cConnectionSettings(address);
            string    deviceSelectorString = I2cDevice.GetDeviceSelector();
            var       matchedDeviceList    = await DeviceInformation.FindAllAsync(deviceSelectorString);

            if (matchedDeviceList.Count > 0)
            {
                var deviceInformation = matchedDeviceList.First();
                device = await I2cDevice.FromIdAsync(deviceInformation.Id, settings);
            }

            return(device);
        }
예제 #25
0
        private static async Task <MainI2CDevice> CreateDisplayJoystickI2CDevice()
        {
            string aqsFilter = I2cDevice.GetDeviceSelector();

            DeviceInformationCollection collection = await DeviceInformation.FindAllAsync(aqsFilter);

            I2cConnectionSettings settings = new I2cConnectionSettings(DeviceAddress)
            {
                BusSpeed = I2cBusSpeed.StandardMode
            };

            I2cDevice i2CDevice = await I2cDevice.FromIdAsync(collection[0].Id, settings);

            return(new MainI2CDevice(i2CDevice));
        }
예제 #26
0
        private async Task I2cConnect(int panel)
        {
            try {
                var settings = new I2cConnectionSettings(I2CAddress[panel]);
                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[panel] = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */
            }
            catch (Exception e) {
                throw new Exception("ht16k33 initisation problem: " + e.Message);
            }
        }
예제 #27
0
        /// <summary>
        /// Open a connection with the IO Pi
        /// </summary>
        /// <returns></returns>
        public async Task Connect()
        {
            /* Initialize the I2C bus */
            try
            {
                string aqs = I2cDevice.GetDeviceSelector(helper.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           */

                var settings = new I2cConnectionSettings(Address);
                settings.BusSpeed = I2cBusSpeed.FastMode;

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

                if (i2cbus != null)
                {
                    // i2c bus is connected so set up the initial configuration for the IO Pi
                    helper.WriteI2CByte(i2cbus, IOCON, config);
                    portaval = helper.ReadI2CByte(i2cbus, GPIOA);
                    portbval = helper.ReadI2CByte(i2cbus, GPIOB);
                    helper.WriteI2CByte(i2cbus, IODIRA, 0xFF);
                    helper.WriteI2CByte(i2cbus, IODIRB, 0xFF);
                    SetPortPullups(0, 0x00);
                    SetPortPullups(1, 0x00);
                    InvertPort(0, 0x00);
                    InvertPort(1, 0x00);

                    // Set IsConnected to true and fire the Connected event handler
                    IsConnected = true;

                    EventHandler handler = Connected;
                    if (handler != null)
                    {
                        handler(this, EventArgs.Empty);
                    }
                }
                else
                {
                    IsConnected = false;
                }
                return;
            }
            /* If initialization fails, display the exception and stop running */
            catch (Exception e)
            {
                IsConnected = false;
                throw e;
            }
        }
예제 #28
0
        private async void InitI2CBerryIMU()
        {
            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)
            {
                DisplayText_Status.Text = "No I2C controllers were found on the system";
                return;
            }

            // Specify I2C slave addresses for the Gyro and Accelerometer on BerryIMU.
            //      Note; the magenetormeter(compass) uses the same address as the accelerometer.
            //      We will use the ACC I2C settings to access both the accelerometer and the magnetometer.
            var settingsGYR = new I2cConnectionSettings(berrryIMU.GYR_ADDRESS);
            var settingsACC = new I2cConnectionSettings(berrryIMU.ACC_ADDRESS);


            // Enable 400kHz I2C buss
            settingsGYR.BusSpeed = I2cBusSpeed.FastMode;
            settingsACC.BusSpeed = I2cBusSpeed.FastMode;


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

            I2CACC = await I2cDevice.FromIdAsync(dis[0].Id, settingsACC);



            // Enable the gyrscope
            writeByteGYR(berrryIMU.CTRL_REG1_G, 0x0F);    // Normal power mode, all axes enabled)
            writeByteGYR(berrryIMU.CTRL_REG4_G, 0x30);    // Continuos update, 2000 dps full scale

            //Enable the accelerometer
            writeByteACC(berrryIMU.CTRL_REG1_XM, 0x67);    // z,y,x axis enabled, continuous update,  100Hz data rate
            writeByteACC(berrryIMU.CTRL_REG2_XM, 0x20);    // +/- 16G full scale

            // Enable the magnetometer
            writeByteMAG(berrryIMU.CTRL_REG5_XM, 0xF0); // Temp enable, M data rate = 50Hz
            writeByteMAG(berrryIMU.CTRL_REG6_XM, 0x60); // +/-12gauss
            writeByteMAG(berrryIMU.CTRL_REG7_XM, 0x0);  // Continuous - conversion mode



            // Now that everything is initialized, create a timer so we read data every DT
            periodicTimer = new Timer(this.TimerCallback, null, 0, DT);
        }
예제 #29
0
        private async void InitI2CAccel()
        {
            /* Initialize the I2C bus */
            try {
                var settings = new I2cConnectionSettings(ACCEL_I2C_ADDR);
                settings.BusSpeed = I2cBusSpeed.FastMode;

                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           */

                I2CAccel = await I2cDevice.FromIdAsync(dis[0].Id, settings);    /* Create an I2cDevice with our selected bus controller and I2C settings */
            }
            /* If initialization fails, display the exception and stop running */
            catch (Exception e)
            {
                Text_Status.Text = "Exception: " + e.Message;
                return;
            }

            /*
             * Initialize the accelerometer:
             *
             * For this device, we create 2-byte write buffers:
             * The first byte is the register address we want to write to.
             * The second byte is the contents that we want to write to the register.
             */
            byte[] WriteBuf_DataFormat   = new byte[] { ACCEL_REG_DATA_FORMAT, 0x01 };      /* 0x01 sets range to +- 4Gs                         */
            byte[] WriteBuf_PowerControl = new byte[] { ACCEL_REG_POWER_CONTROL, 0x08 };    /* 0x08 puts the accelerometer into measurement mode */

            /* Write the register settings */
            try
            {
                I2CAccel.Write(WriteBuf_DataFormat);
                I2CAccel.Write(WriteBuf_PowerControl);
            }
            /* If the write fails display the error and stop running */
            catch (Exception)
            {
                Text_Status.Text = "Status: Accelerometer initialization failed";
                return;
            }

            /* Now that everything is initialized, create a timer so we read data every 100mS */
            periodicTimer          = new DispatcherTimer();
            periodicTimer.Interval = TimeSpan.FromMilliseconds(100);
            periodicTimer.Tick    += Timer_Tick;
            periodicTimer.Start();
        }
예제 #30
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;
            }

            /*
             *  Initialize the Humidity and Temperature Sensor
             *  For this device, we create 1-byte write buffer
             *  The byte is the content that we want to write to
             */
            byte[] WriteBuf_ComByte = new byte[] { 0x80 };          // 0x80 Ends Command Mode and transits to Normal Operation Mode

            //	Write the register settings
            try
            {
                I2CHumtemp.Write(WriteBuf_ComByte);
            }
            // If the write fails display the error and stop running
            catch (Exception ex)
            {
                Text_Status.Text = "Failed to communicate with device: " + ex.Message;
                return;
            }

            // Create a timer to read data every 100ms
            periodicTimer = new Timer(this.TimerCallback, null, 0, 100);
        }