Skip to content

Commit a312b73

Browse files
committed
mod: set bus speed to standard, optimization for read+write functions
1 parent 7dc194f commit a312b73

File tree

1 file changed

+29
-42
lines changed

1 file changed

+29
-42
lines changed

PiJuice.Uwp.Core/PiJuiceInterface.cs

+29-42
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,44 @@ public class PiJuiceInterfaceResult
3535
public int RetryCounter { get; set; }
3636
}
3737

38-
public class PiJuiceInterface: IDisposable
38+
public class PiJuiceInterface : IDisposable
3939
{
4040
private byte _Bus;
4141
private byte _Address;
42-
private I2cBusSpeed _Speed;
4342
private I2cDevice _Device = null;
4443
private object _DeviceLock = new object();
4544

4645
public bool Shutdown { get; set; }
46+
public int ReadRetryCounter { get; set; }
47+
public int ReadRetryDelay { get; set; }
48+
public I2cBusSpeed Speed { get; set; }
4749

48-
public PiJuiceInterface(byte bus = 1, byte address = 0x14, I2cBusSpeed speed = I2cBusSpeed.FastMode)
50+
public PiJuiceInterface(byte bus = 1, byte address = 0x14)
4951
{
52+
Speed = I2cBusSpeed.StandardMode;
53+
Shutdown = false;
54+
ReadRetryCounter = 3;
55+
ReadRetryDelay = 5;
56+
5057
_Bus = bus;
5158
_Address = address;
52-
_Speed = speed;
5359
}
5460

5561
public void Dispose()
5662
{
57-
if (_Device!=null)
63+
if (_Device != null)
5864
{
5965
_Device.Dispose();
6066
_Device = null;
6167
}
6268
}
6369

64-
private async Task<bool> InitAsync()
70+
public async Task<bool> InitAsync()
6571
{
6672
if (_Device == null && !Shutdown)
6773
{
6874
var controlerName = $"I2C{_Bus}";
69-
var i2cSettings = new I2cConnectionSettings(_Address) { BusSpeed = _Speed };
75+
var i2cSettings = new I2cConnectionSettings(_Address) { BusSpeed = Speed };
7076
var deviceSelector = I2cDevice.GetDeviceSelector(controlerName);
7177
var i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);
7278
if (i2cDeviceControllers != null && i2cDeviceControllers.Any())
@@ -80,18 +86,17 @@ private async Task<bool> InitAsync()
8086

8187
public async Task<PiJuiceInterfaceResult> ReadData(PiJuiceStatusCommands cmd, byte lenght)
8288
{
83-
return await ReadData((byte) cmd, lenght);
89+
return await ReadData((byte)cmd, lenght);
8490
}
8591

8692
public async Task<PiJuiceInterfaceResult> ReadData(PiJuicePowerCommands cmd, byte lenght)
8793
{
88-
return await ReadData((byte) cmd, lenght);
94+
return await ReadData((byte)cmd, lenght);
8995
}
9096

9197
public async Task<PiJuiceInterfaceResult> ReadData(byte cmd, byte lenght)
9298
{
9399
PiJuiceInterfaceResult result = new PiJuiceInterfaceResult();
94-
int RetryCounter = 3;
95100
var StartTime = DateTime.Now;
96101

97102
if (Shutdown)
@@ -105,34 +110,24 @@ public async Task<PiJuiceInterfaceResult> ReadData(byte cmd, byte lenght)
105110
throw new Exception("Device initialisation error");
106111

107112
// do transfer
108-
for (int i = 0; i < RetryCounter; i++)
113+
for (int i = 0; i < ReadRetryCounter; i++)
109114
{
110115
// init
111116
result.RetryCounter = i;
112117
result.Request = new byte[] { (byte)cmd };
113118
result.Response = new byte[lenght + 1];
114-
115119
// send request and read response
116-
lock (_DeviceLock)
120+
_Device.WriteRead(result.Request, result.Response);
121+
// calc checksum
122+
var cs = GetCheckSum(result.Response);
123+
if (cs == result.Response[lenght])
117124
{
118-
// write
119-
_Device.Write(result.Request);
120-
// wait to minimize errors
121-
Thread.Sleep(1);
122-
// read
123-
_Device.Read(result.Response);
124-
// calc checksum
125-
var cs = GetCheckSum(result.Response);
126-
if (cs == result.Response[lenght])
127-
{
128-
// finish
129-
result.Success = true;
130-
return result;
131-
}
132-
125+
// finish
126+
result.Success = true;
127+
return result;
133128
}
134129

135-
await Task.Delay(10);
130+
await Task.Delay(ReadRetryDelay);
136131
}
137132

138133
result.Success = false;
@@ -175,19 +170,11 @@ public async Task<PiJuiceInterfaceResult> WriteData(byte cmd, byte[] data)
175170
buffer[0] = cmd;
176171
buffer[buffer.Length - 1] = GetCheckSum(data, all: true);
177172
result.Request = buffer.ToArray();
178-
179-
// send request
180-
lock (_DeviceLock)
181-
{
182-
// write
183-
_Device.Write(result.Request);
184-
// wait to minimize errors
185-
Thread.Sleep(1);
186-
// finish
187-
result.Success = true;
188-
return result;
189-
}
190-
173+
// write
174+
_Device.Write(result.Request);
175+
// finish
176+
result.Success = true;
177+
return result;
191178
}
192179
catch (Exception ex)
193180
{

0 commit comments

Comments
 (0)