Bijington
Bijington

Reputation: 3751

I2C control on Raspberry Pi Bright Pi with dotnet

I have connected a Bright Pi to my Raspberry Pi 3 B and through the use of Unosquare RaspberryIO and WiringPi dotnet I am trying to control the LEDs.

I have followed this Quick Start guide and can confirm the LEDs work based on the steps documented there...

If I run i2cdetect -y 1 on the device I see the following output.

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: 70 -- -- -- -- -- -- --  

And if I run this sudo i2cset -y 1 0x70 0x00 0x5a then I see the LEDs light up

Now I believe I am failing to understand how to translate the above to some actual code to get this to work. Here is what I have tried so far:

Pi.Init<BootstrapWiringPi>();

// Register a device on the bus
var myDevice = Pi.I2C.AddDevice(0x70);

// List registered devices on the I2C Bus
foreach (var device in Pi.I2C.Devices)
{
    Console.WriteLine($"Registered I2C Device: {device.DeviceId}, {device.FileDescriptor}");
}

// 1. First attempt
myDevice.Write(0x5a);

// 2. Second attempt
myDevice.Write(new byte[] { 0x00, 0x5a });

The Console.WriteLine outputs Registered I2C Device: 112, 44 in case that helps. I wasn't sure if I somehow need the 44 to be zero based on the commands I was previously sending.

To be clear nothing happens when writing, no LEDs turn on but also no exceptions are thrown.

Is anyone able to point out where I have gone wrong? Thanks in advance.

Upvotes: 1

Views: 1262

Answers (1)

Bijington
Bijington

Reputation: 3751

Thanks to comments on here and elsewhere it has been pointed out that WiringPi is no longer maintained and that there is a dotnet API available under the System.Device.I2c namespace. So using that here is the code that solved my issue:

using (var bus = I2cBus.Create(1)) // 1 for the Raspberry Pi 3B
{
    using (var device = bus.CreateDevice(0x70)) // Address of Bright Pi
    {
        device.Write(new byte[] { 0x00, 0xFF });

        // Make sure the LEDs are on
        await Task.Delay(TimeSpan.FromMilliseconds(200));

        CaptureImage();

        await Task.Delay(TimeSpan.FromMilliseconds(200));

        // Turns the LEDs off
        device.Write(new byte[] { 0x00, 0x00 });

        bus.RemoveDevice(0x70);
    }
}

Upvotes: 1

Related Questions