0

I am working on a C# net micro framework project, in particular an I2C bus management/class.

I have several different I2C devices each defined in separate classes which contain all of the devices' unique methods. In my case, I can only have ONE I2CDevice object "active" to communicate with and to switch to another I2C device to communicate with it requires a call like I2CBus.Config = DeviceConfig1.

For example (pseudo-code):

Class Device1 
-DeviceConfig1
-Method 1
-Method 2
-Method 3

Class Device2
-DeviceConfig2
-Method 1
-Method 2
-Method 3


Main Program
{
I2cBus = new I2CDevice(configuration)
Device1.Method1
Device2.Method3
}

So, somewhere between Device1.Method1; and Device2.Method3; When switching between devices, I need to change the I2CDevice.Config property. I call it an "OnClassEnter event" that would check that the device settings are correct before it proceeds. I'm looking for an elegant way such that whenever I call a Method for a Device, that the I2CBus.Config is automatically checked and changed. Is there a way that I don't need to re-type this config code in each method? Have a separate internal method that's called each time I access any of those methods? Maybe something inherited from a base class or Interface, but where I don't have to remember call it from each method in the Device class.

1
  • 2
    You might want to look into Aspects. Also, have you considered writing a wrapper for I2CDevice, and using that in your classes instead. The wrapper would take your configuration as constructor parameter. For each of the I2CDevice methods, the wrapper has its own version which applies its configuration before calling the I2CDevice version.
    – Mr.Mindor
    Commented Apr 8, 2014 at 18:34

2 Answers 2

2

Instead of attempting to capture when control passes into your class and update the config then, explicitly update the configuration just before the I2CDevice is used.

Write a wrapper for I2CDevice which takes your DeviceConfig and I2CDevice as parameters to its constructor.

The wrapper can provide its own versions of the I2CDevice methods (looks like there are only 3), it would then ensure I2C configuration before executing the corresponding I2CDevice method.

You would then use this wrapper inside your Device1, Device2 classes in lieu of using I2CDevice directly.

public class I2CDeviceWrapper 
{
   private readonly I2CDevice _i2cdevice;
   private readonly DeviceConfig _config;

   public I2CDeviceWrapper(DeviceConfig config, I2CDevice device)
   {
       _config = config;
       _i2cdevice= device;
   }
   private void EnsureConfiguration()
   {
       //verify or set configuration so this wrapper's device is active.
   }
   public int Execute(I2CTransaction[] xActions, int timeout)
   {
       EnsureConfiguration();
       return _i2cdevice.Execute(xActions, timeout);
   }
   public I2CReadTransaction CreateReadTransaction (byte[] buffer)
   {
        EnsureConfiguration();
        return _i2cdevice.CreateReadTransaction(buffer);
   }
   public I2CWriteTransaction CreateWriteTransaction (byte[] buffer)
   {
        EnsureConfiguration();
        return _i2cdevice.CreateWriteTransaction(buffer);
   }

}

EDIT: if the I2CDevice is passed into the wrapper, the wrapper should not attempt to manage its lifecycle. I2CDevice provides a Dispose() method, in my experience (which does not include the micro framework) this indicates you risk a resource leak if it is not called. If this holds for the micro framework, you would want to provide one for the wrapper as well that calls the I2CDevice one.

Wrapper as Abstract Base:

If you use the wrapper as a base for each of your Device1, Device2 classes, the visibility of the Execute, I2CGetReadTransaction, I2CGetWriteTransaction methods and perhaps the _config field should be made protected.

public class Device1: I2CDeviceWrapper
{
    public Device1(DeviceConfig config, I2CDevice device):base(config, device)
    {}

    public something Method1()
    {
        ... 
        Execute(...);
        ...
    }        
}

Wrapper as member field: If the wrapper is a member of your Device1, Device2 classes, you would still pass your i2cBus as a parameter to those constructors, create A per Device wrapper using that devices' configuration and i2cBus and then use it going forward as you previously would have used i2cBuss.

public class Device1
{
    private readonly I2CDeviceWrapper _i2cBus;

    public Device1(I2CDevice i2cBus)
    {
         var config = //set up configuration
         _i2cBus = new I2CDeviceWrapper(config, i2cBus);
    }
    public void Method1()
    {
        ...
        _i2cBus.Execute(...);
        ...
    }
}
4
  • This is relatively straight forward. I will digest this approach a little more. Maybe Each Device Class could inherit this as a base class instead of passing class this into each device? Or some Abstract Base I2C Device Class that requires as instance of this I2C Bus Class?
    – GisMofx
    Commented Apr 8, 2014 at 19:33
  • @GisMofx I misread/misunderstood part of your original post. Namely that you have one instance of I2CDevice. In that case, thw wrapper should not create its own or manage the life, but it should also be passed into the constructor. How were you originally planning to pass ic2bus to your Device1, Device2 classes?
    – Mr.Mindor
    Commented Apr 8, 2014 at 19:39
  • I would not pass this class into each class (you would want/need one wrapper deviceconfiguration. The wrapper as an abstract base for each of your device classes would work, or the wrapper as a private member of one of your device classes.I'll add an example.
    – Mr.Mindor
    Commented Apr 8, 2014 at 19:51
  • I think passing the I2CDeviceWrapper Class into each Device Class makes the most sense. Calling I2CDeviceWrapper.Execute(...) from the Devices' methods would get that configuration checking that I need. I like this solution, but I think I was imagining something more tricky within C#. With this solution all of my i2c based projects need to bring this I2cbus wrapper class along with it. Also, I am making the I2CBus Class a singleton also.
    – GisMofx
    Commented Apr 9, 2014 at 23:41
1

Disclaimer: My C# experience is probably less than 8 hours total so the syntax that follows may not be accurate.

So, somewhere between Device1.Method1; and Device2.Method3; When switching between devices, I need to change the I2CDevice.Config property.

You could arrange it so the only way to get a device is through a method like

public void With(DeviceConfig config, Action<Device> function) {
    // configure the hardware
    Device device = /* get the device */
    function(device);
}

So that you could do this:

With(device1Config, (device1) => {
    device1.Method1();
    device1.Method2();
    device1.Method3();
});
With(device2Config, (device2) => {
    device2.Method1();
    device3.Method2();
});

That would ensure you can't get a hold of a device without first configuring it, but would enable you to make multiple calls without reconfiguration.

11
  • 1
    if you are returning function(device) should make that T With(DeviceConfig config, Func<Device, T> function)...
    – Mr.Mindor
    Commented Apr 8, 2014 at 18:41
  • 1
    Alternately, if you have no return value, you need to use Action<Device>, since C# doesn't have the concept of void type parameters. Commented Apr 8, 2014 at 18:43
  • 1
    While better, this is not foolproof. You can do things like taking device1 and assigning it to something in your closure and then using it outside the With.
    – Telastyn
    Commented Apr 8, 2014 at 18:49
  • 1
    @doval - No, though it's probably sufficient to leak a copy of the struct to use the device and break the idiom. You'd be better off using an actual IDisposable so the using block would prevent this (though could still be actively circumvented).
    – Telastyn
    Commented Apr 8, 2014 at 19:11
  • 1
    @doval - The using block helps the IDE tell you that things in the using block should not be captured by things out of its scope (if I remember correctly). I've seen this sort of thing used with tokens or contexts in the manner of "he who has the stick may speak" sort of things. "Using this authentication/lock/mutex, do this stuff".
    – Telastyn
    Commented Apr 8, 2014 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.