Windows IoT API Reference

Windows.IoT Namespace API

This section provides detailed documentation for the Windows.IoT namespace, covering the core APIs for developing IoT solutions on Windows. Explore the classes, methods, and properties available to interact with hardware, manage devices, and build robust applications.

Classes

Explore the primary classes within the Windows.IoT namespace.

  • GpioController: Manages General Purpose Input/Output (GPIO) pins.
  • I2cController: Handles communication over the Inter-Integrated Circuit (I2C) bus.
  • SpiController: Facilitates Serial Peripheral Interface (SPI) communication.
  • AnalogInput: Reads analog values from connected sensors.
  • DigitalInput: Reads digital signals from sensors or switches.
  • DigitalOutput: Controls digital output pins to activate devices.
  • DeviceInformation: Provides details about connected IoT devices.
  • IoTHubConnection: Manages connections to Azure IoT Hub.

GpioController Details

The GpioController class is fundamental for interacting with GPIO pins on your Windows IoT device. It allows you to configure pins as inputs or outputs, read their states, and set their states.

Methods

  • OpenPin(pinNumber): Opens a specific GPIO pin for use.
    • Parameters: pinNumber (int) - The number of the GPIO pin.
    • Returns: GpioPin object.
  • GetDefault(): Gets the default GPIO controller for the system.
    • Returns: GpioController object.

GpioPin Object Properties & Methods

  • PinNumber (int): The number of the pin.
  • GetDriveMode(): Retrieves the current drive mode of the pin.
  • SetDriveMode(mode): Sets the drive mode (Input, Output, InputPullUp, etc.).
    • Parameters: mode (GpioPinDriveMode)
  • Read(): Reads the current value of an input pin.
    • Returns: GpioPinValue (High or Low).
  • Write(value): Writes a value to an output pin.
    • Parameters: value (GpioPinValue)
  • DebounceTimeout (TimeSpan): Configures debounce timing.
  • ValueChanged (GpioPinEventHandler): Event handler for pin state changes.

Example Usage (C#)


using Windows.Devices.Gpio;

// Get the default GPIO controller
var gpioController = await GpioController.GetDefaultAsync();

// Open GPIO pin 5 as an output
var pin = gpioController.OpenPin(5);
pin.SetDriveMode(GpioPinDriveMode.Output);

// Write a high value to the pin
pin.Write(GpioPinValue.High);

// Close the pin when done
pin.Dispose();
                    

I2cController Details

The I2cController class provides mechanisms for communicating with I2C devices. This is crucial for connecting a wide range of sensors and peripherals.

Methods

  • GetDefault(): Gets the default I2C controller.
    • Returns: I2cController object.
  • GetDevice(deviceSelector): Gets an I2C device with a specific selector.
    • Parameters: deviceSelector (string) - Typically a device ID or hardware ID.
    • Returns: I2cDevice object.

I2cDevice Object Properties & Methods

  • DeviceId (string): The unique identifier of the device.
  • ConnectionSettings (I2cConnectionSettings): Settings for the I2C connection (bus address, speed).
  • Write(buffer): Writes data to the I2C device.
    • Parameters: buffer (byte[])
  • WriteRead(writeBuffer, readBuffer): Writes data and then reads data.
    • Parameters: writeBuffer (byte[]), readBuffer (byte[])
  • Read(buffer): Reads data from the I2C device.
    • Parameters: buffer (byte[])

Example Usage (C#)


using Windows.Devices.I2c;
using System.Text;

// I2C address of the device (e.g., an accelerometer)
const int DEVICE_ADDRESS = 0x68; // Example address

// Get the default I2C controller
var controller = await I2cController.GetDefaultAsync();

// Create I2C connection settings
var settings = new I2cConnectionSettings(DEVICE_ADDRESS)
{
    BusSpeed = I2cBusSpeed.StandardMode // or FastMode
};

// Get the I2C device
var device = controller.GetDevice(settings);

// Example: Reading a register from the device
byte[] readBuffer = new byte[1];
device.WriteRead(new byte[] { 0x00 }, readBuffer); // Write register address, then read
// Process readBuffer data
                    

SpiController Details

The SpiController class is used for Serial Peripheral Interface (SPI) communication, commonly found in devices requiring high-speed data transfer.

Methods

  • GetDefault(): Gets the default SPI controller.
    • Returns: SpiController object.
  • GetDevice(settings): Gets an SPI device with specified settings.
    • Parameters: settings (SpiConnectionSettings)
    • Returns: SpiDevice object.

SpiDevice Object Properties & Methods

  • DeviceId (string): The unique identifier of the device.
  • ConnectionSettings (SpiConnectionSettings): Settings for the SPI connection (chip select line, mode, data shift order).
  • Write(buffer): Writes data to the SPI device.
    • Parameters: buffer (byte[])
  • Read(buffer): Reads data from the SPI device.
    • Parameters: buffer (byte[])
  • TransferFullDuplex(writeBuffer, readBuffer): Performs a full-duplex transfer.
    • Parameters: writeBuffer (byte[]), readBuffer (byte[])

Common API Concepts

Understanding these concepts will help you effectively use the Windows.IoT APIs:

  • Asynchronous Operations: Many I/O operations are asynchronous. Use await with methods returning Task or Task<T> to handle these operations efficiently without blocking the main thread.
  • Resource Management: Always dispose of objects like GpioPin, I2cDevice, and SpiDevice when you are finished with them to free up system resources. Use the using statement for automatic disposal.
  • Error Handling: Implement robust error handling using try-catch blocks to manage potential exceptions during hardware interaction.
  • Device Selectors: When retrieving devices, you'll often use device selectors (strings) to uniquely identify the hardware you want to communicate with.