I2C控制复盆子Pi Bright Pi与dotnet



我已经将Bright Pi连接到我的Raspberry Pi 3 B,并通过使用Unosquare RaspberryIO和WiringPi网络,我正在尝试控制LED。

我已经遵循了这个快速入门指南,可以根据那里记录的步骤来确认LED的工作。。。

如果我在设备上运行i2cdetect -y 1,我会看到以下输出。

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 -- -- -- -- -- -- --  

如果我运行这个sudo i2cset -y 1 0x70 0x00 0x5a,我会看到LED点亮

现在我想我不明白如何将上面的内容翻译成一些实际的代码来实现这一点。以下是我迄今为止所尝试的:

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 });

Console.WriteLine在有帮助的情况下输出Registered I2C Device: 112, 44。根据我之前发送的命令,我不确定我是否需要44为零。

要明确的是,写入时没有发生任何事情,没有LED点亮,但也没有引发异常。

有人能指出我哪里错了吗?提前谢谢。

由于这里和其他地方的评论,已经指出不再维护WiringPi,并且在System.Device.I2c命名空间下有一个dotnet API可用。因此,使用它是解决我的问题的代码:

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);
}
}

最新更新