/// <summary> /// Initialize the Sparkfun Weather Shield /// </summary> /// <remarks> /// Setup and instantiate the I2C device objects for the HTDU21D and the MPL3115A2 /// and initialize the blue and green status LEDs. /// </remarks> internal async Task BeginAsync() { /* * Acquire the GPIO controller * MSDN GPIO Reference: https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.gpio.aspx * * Get the default GpioController */ GpioController gpio = GpioController.GetDefault(); /* * Initialize the blue LED and set to "off" * * Instantiate the blue LED pin object * Write the GPIO pin value of low on the pin * Set the GPIO pin drive mode to output */ BlueLEDPin = gpio.OpenPin(STATUS_LED_BLUE_PIN, GpioSharingMode.Exclusive); BlueLEDPin.Write(GpioPinValue.Low); BlueLEDPin.SetDriveMode(GpioPinDriveMode.Output); /* * Initialize the green LED and set to "off" * * Instantiate the green LED pin object * Write the GPIO pin value of low on the pin * Set the GPIO pin drive mode to output */ GreenLEDPin = gpio.OpenPin(STATUS_LED_GREEN_PIN, GpioSharingMode.Exclusive); GreenLEDPin.Write(GpioPinValue.Low); GreenLEDPin.SetDriveMode(GpioPinDriveMode.Output); /* * Acquire the I2C device * MSDN I2C Reference: https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.i2c.aspx * * Use the I2cDevice device selector to create an advanced query syntax string * Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string * Take the device id of the first device in the collection */ string advanced_query_syntax = I2cDevice.GetDeviceSelector("I2C1"); DeviceInformationCollection device_information_collection = await DeviceInformation.FindAllAsync(advanced_query_syntax); string deviceId = device_information_collection[0].Id; /* * Establish an I2C connection to the HTDU21D * * Instantiate the I2cConnectionSettings using the device address of the HTDU21D * - Set the I2C bus speed of connection to fast mode * - Set the I2C sharing mode of the connection to shared * * Instantiate the the HTDU21D I2C device using the device id and the I2cConnectionSettings */ I2cConnectionSettings htdu21d_connection = new I2cConnectionSettings(HTDU21D_I2C_ADDRESS); htdu21d_connection.BusSpeed = I2cBusSpeed.FastMode; htdu21d_connection.SharingMode = I2cSharingMode.Shared; htdu21d = await I2cDevice.FromIdAsync(deviceId, htdu21d_connection); /* * Establish an I2C connection to the MPL3115A2 * * Instantiate the I2cConnectionSettings using the device address of the MPL3115A2 * - Set the I2C bus speed of connection to fast mode * - Set the I2C sharing mode of the connection to shared * * Instantiate the the MPL3115A2 I2C device using the device id and the I2cConnectionSettings */ I2cConnectionSettings mpl3115a2_connection = new I2cConnectionSettings(MPL3115A2_I2C_ADDRESS); mpl3115a2_connection.BusSpeed = I2cBusSpeed.FastMode; mpl3115a2_connection.SharingMode = I2cSharingMode.Shared; mpl3115a2 = await I2cDevice.FromIdAsync(deviceId, mpl3115a2_connection); }
/// <summary> /// Initialize the Sparkfun Weather Shield /// </summary> /// <remarks> /// Setup and instantiate the I2C device objects for the HTDU21D and the MPL3115A2 /// and initialize the blue and green status LEDs. /// </remarks> internal async Task BeginAsync() { /* * Acquire the GPIO controller * MSDN GPIO Reference: https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.gpio.aspx * * Get the default GpioController */ GpioController gpio = GpioController.GetDefault(); /* * Test to see if the GPIO controller is available. * * If the GPIO controller is not available, this is * a good indicator the app has been deployed to a * computing environment that is not capable of * controlling the weather shield. Therefore we * will disable the weather shield functionality to * handle the failure case gracefully. This allows * the invoking application to remain deployable * across the Universal Windows Platform. */ if (null == gpio) { available = false; enable = false; return; } /* * Initialize the blue LED and set to "off" * * Instantiate the blue LED pin object * Write the GPIO pin value of low on the pin * Set the GPIO pin drive mode to output */ BlueLEDPin = gpio.OpenPin(STATUS_LED_BLUE_PIN, GpioSharingMode.Exclusive); BlueLEDPin.Write(GpioPinValue.Low); BlueLEDPin.SetDriveMode(GpioPinDriveMode.Output); /* * Initialize the green LED and set to "off" * * Instantiate the green LED pin object * Write the GPIO pin value of low on the pin * Set the GPIO pin drive mode to output */ GreenLEDPin = gpio.OpenPin(STATUS_LED_GREEN_PIN, GpioSharingMode.Exclusive); GreenLEDPin.Write(GpioPinValue.Low); GreenLEDPin.SetDriveMode(GpioPinDriveMode.Output); /* * Initialise the Wind Speed interrupt reading pin * set to low initial value * Set the GPIO pin drive mode to inputpullup */ WindSpeedPin = gpio.OpenPin(WSPEED_PIN, GpioSharingMode.Exclusive); WindSpeedPin.Write(GpioPinValue.Low); WindSpeedPin.SetDriveMode(GpioPinDriveMode.InputPullUp); /* * Initialise the Rain interrupt reading pin * set to low initial value * Set the GPIO pin drive mode to inputpullup */ RainPin = gpio.OpenPin(RAIN_PIN, GpioSharingMode.Exclusive); RainPin.Write(GpioPinValue.Low); RainPin.SetDriveMode(GpioPinDriveMode.InputPullUp); /* * Acquire the I2C device * MSDN I2C Reference: https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.i2c.aspx * * Use the I2cDevice device selector to create an advanced query syntax string * Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string * Take the device id of the first device in the collection */ string advanced_query_syntax = I2cDevice.GetDeviceSelector("I2C1"); DeviceInformationCollection device_information_collection = await DeviceInformation.FindAllAsync(advanced_query_syntax); string deviceId = device_information_collection[0].Id; /* * Establish an I2C connection to the HTDU21D * * Instantiate the I2cConnectionSettings using the device address of the HTDU21D * - Set the I2C bus speed of connection to fast mode * - Set the I2C sharing mode of the connection to shared * * Instantiate the the HTDU21D I2C device using the device id and the I2cConnectionSettings */ I2cConnectionSettings htdu21d_connection = new I2cConnectionSettings(HTDU21D_I2C_ADDRESS); htdu21d_connection.BusSpeed = I2cBusSpeed.FastMode; htdu21d_connection.SharingMode = I2cSharingMode.Shared; htdu21d = await I2cDevice.FromIdAsync(deviceId, htdu21d_connection); /* * Establish an I2C connection to the MPL3115A2 * * Instantiate the I2cConnectionSettings using the device address of the MPL3115A2 * - Set the I2C bus speed of connection to fast mode * - Set the I2C sharing mode of the connection to shared * * Instantiate the the MPL3115A2 I2C device using the device id and the I2cConnectionSettings */ I2cConnectionSettings mpl3115a2_connection = new I2cConnectionSettings(MPL3115A2_I2C_ADDRESS); mpl3115a2_connection.BusSpeed = I2cBusSpeed.FastMode; mpl3115a2_connection.SharingMode = I2cSharingMode.Shared; mpl3115a2 = await I2cDevice.FromIdAsync(deviceId, mpl3115a2_connection); /* * Test to see if the I2C devices are available. * * If the I2C devices are not available, this is * a good indicator the weather shield is either * missing or configured incorrectly. Therefore we * will disable the weather shield functionality to * handle the failure case gracefully. This allows * the invoking application to remain deployable * across the Universal Windows Platform. * * NOTE: For a more detailed description of the I2C * transactions used for testing below, please * refer to the "Raw___" functions provided below. */ if (null == mpl3115a2) { available = false; enable = false; return; } else { byte[] reg_data = new byte[1]; try { mpl3115a2.WriteRead(new byte[] { CTRL_REG1 }, reg_data); reg_data[0] &= 0xFE; // ensure SBYB (bit 0) is set to STANDBY reg_data[0] |= 0x02; // ensure OST (bit 1) is set to initiate measurement mpl3115a2.Write(new byte[] { CTRL_REG1, reg_data[0] }); } catch { available = false; enable = false; return; } } if (null == htdu21d) { available = false; enable = false; return; } else { byte[] i2c_temperature_data = new byte[3]; try { htdu21d.WriteRead(new byte[] { SAMPLE_TEMPERATURE_HOLD }, i2c_temperature_data); } catch { available = false; enable = false; return; } } available = true; enable = true; }