Making, Raspberry Pi 3

Creating a NuGet package to simplify development for I2C devices

Last time, I looked at the MCP9808 temperature sensor, and how to get it to work with the Raspberry Pi 3 using the I2C protocol. As part of this development, I identified an initialisation function which I thought could be useful across a number of other I2C devices. I abstracted this out into a separate project, and I added a reference to this project from my MCP9808 project.

However, I’d prefer to refer to this class using a NuGet package – this allows me to pull the code for my MCP9808 project from GitHub, and know that it’ll work out of the box (rather than have to manually pull the code down from another repo on GitHub and refer to it).

The image below shows the contents of the NuGet package I’ve uploaded – there’s some package metadata, and also just one binary library, targeting the Windows 10 UAP.

screenshot.1465742125

I uploaded the NuGet package to the nuget.org site here, and I pull the reference into my project by either using the NuGet package manager through Visual Studio, or I can use the code:

install-package Magellanic.I2c -pre

Of course the Visual Studio solution must target the ARM architecture for the Raspberry Pi, rather than just any CPU.

screenshot.1465742389

The code in this library specifies an abstract class, which the code for my other I2C devices can inherit from. I’ve shown below how I’ve used this library to simplify the initialisation code for the MCP9808 – the Initialize method is available when I extend the AbstractI2CDevice class.

public class MCP9808 : AbstractI2CDevice
{
    private const byte I2C_ADDRESS = 0x18;
 
    private byte[] DeviceIdAddress = new byte[] { 0x07 };
 
    public MCP9808()
    {
        this.DeviceIdentifier = new byte[2] { 0x040x00 };
    }
 
    public override byte GetI2cAddress()
    {
        return I2C_ADDRESS;
    }
 
    //...other code...
}

I have specified the device’s unique I2C address, and the device’s identifier code. This means I can now simply initialise the device in an asynchronous way. I can also implement a method to check that the actual values held in the device’s identification registers match those in the datasheet (and therefore this can act as a good proxy to determine if I’ve successfully connected to the right device).

This makes the code for querying the device very simple.

private async Task WriteTemperatureSettingsToDebug()
{
    var temperatureSensor = new MCP9808();
 
    await temperatureSensor.Initialize();
 
    if (temperatureSensor.IsConnected())
    {
        while(true)
        {
            var temperature = temperatureSensor.GetTemperature();
 
            Debug.WriteLine("Temperature = " + temperature);
 
            Task.Delay(1000).Wait();
        }
    }
}

The full class for the MCP9808 is linked to here.

I hope this simple library will be helpful in controlling other I2C devices with my Raspberry Pi – the next device I’ll try to control will be the HMC588L digital compass.