Snippet - I2CDevice extension methods

I2CDevice extension methods

This class contains several extension methods for I2CDevice in order to read and write register based data.

For every extension method “” there’s also an extension method “Try<MethodName”, whereas the “Try…” method returns a boolean value indicating, whether the transactions have been successfull, while the methods not starting with Try… throw an Exception.

Those methods allow you to
read / write registers,
read / write consecutive registers,
read / write single bits in registers,
read / write multiple bits in registers

It also includes and uses the extension methods Execute and Broadcast published by “Bec a Fuel”, which provide a simple way to address multiple I2C slaves.

Thank you for sharing!

Good idea!

I noticed you didn’t included…


// Required for NETMF to recognized extension methods
namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class ExtensionAttribute : Attribute { }
}

Is this no longer necessary in 4.2? That must have snuck past me :smiley:

Actually, I had included it, but received a compiler warning, that it had been already declared and the one from mscorlib.dll would be used. So it seems, that it is already included in 4.2.

Nice! That’s the type of compiler warning that I welcome.