feat: 完善I2C读的逻辑

This commit is contained in:
alivender 2025-07-15 17:57:15 +08:00
parent 533e2561ab
commit 4b140ef683
2 changed files with 19 additions and 5 deletions

View File

@ -303,6 +303,20 @@ class Camera
return true;
}
public async ValueTask<Result<byte>> ReadRegisters(UInt16 register)
{
var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.taskID, this.timeout);
// Convert 16-bit register address to byte array
var registerBytes = new byte[] { (byte)(register >> 8), (byte)(register & 0xFF) };
var ret = await i2c.ReadData(CAM_I2C_ADDR, registerBytes, 1, CAM_PROTO);
if (!ret.IsSuccessful)
return new(ret.Error);
return new Result<byte>(ret.Value[0]);
}
/// <summary>
/// 配置摄像头分辨率和相关参数
/// </summary>
@ -1282,11 +1296,11 @@ class Camera
// 步骤2: 读取寄存器 0x3029 的状态,如果返回值为 0x10代表对焦已完成
int iteration = 5000;
byte focusStatus = 0x00;
byte focusStatus;
do
{
var readResult = await ReadRegister(0x3029);
var readResult = await ReadRegisters(0x3029);
if (!readResult.IsSuccessful)
{
logger.Error($"读取对焦状态寄存器(0x3029)失败: {readResult.Error}");
@ -1307,7 +1321,7 @@ class Camera
return new(new Exception($"自动对焦超时,状态: 0x{focusStatus:X2}"));
}
await Task.Delay(10);
await Task.Delay(100);
}
while (focusStatus != 0x10);

View File

@ -201,7 +201,7 @@ public class I2c
/// <returns>操作结果,成功返回读取到的数据,否则返回异常信息</returns>
public async ValueTask<Result<byte[]>> ReadData(UInt32 devAddr, byte[] data, int dataReadLength, I2cProtocol proto)
{
if (dataReadLength <= 0 || dataReadLength > 0x0000_FFFF)
if (dataReadLength < 1 || dataReadLength > 0x0000_FFFF)
{
logger.Error($"Read length {dataReadLength} is invalid or exceeds maximum allowed 0x0000_FFFF");
return new(new ArgumentException($"Read length {dataReadLength} is invalid or exceeds maximum allowed 0x0000_FFFF"));