@@ -35,38 +35,44 @@ public class PiJuiceInterfaceResult
35
35
public int RetryCounter { get ; set ; }
36
36
}
37
37
38
- public class PiJuiceInterface : IDisposable
38
+ public class PiJuiceInterface : IDisposable
39
39
{
40
40
private byte _Bus ;
41
41
private byte _Address ;
42
- private I2cBusSpeed _Speed ;
43
42
private I2cDevice _Device = null ;
44
43
private object _DeviceLock = new object ( ) ;
45
44
46
45
public bool Shutdown { get ; set ; }
46
+ public int ReadRetryCounter { get ; set ; }
47
+ public int ReadRetryDelay { get ; set ; }
48
+ public I2cBusSpeed Speed { get ; set ; }
47
49
48
- public PiJuiceInterface ( byte bus = 1 , byte address = 0x14 , I2cBusSpeed speed = I2cBusSpeed . FastMode )
50
+ public PiJuiceInterface ( byte bus = 1 , byte address = 0x14 )
49
51
{
52
+ Speed = I2cBusSpeed . StandardMode ;
53
+ Shutdown = false ;
54
+ ReadRetryCounter = 3 ;
55
+ ReadRetryDelay = 5 ;
56
+
50
57
_Bus = bus ;
51
58
_Address = address ;
52
- _Speed = speed ;
53
59
}
54
60
55
61
public void Dispose ( )
56
62
{
57
- if ( _Device != null )
63
+ if ( _Device != null )
58
64
{
59
65
_Device . Dispose ( ) ;
60
66
_Device = null ;
61
67
}
62
68
}
63
69
64
- private async Task < bool > InitAsync ( )
70
+ public async Task < bool > InitAsync ( )
65
71
{
66
72
if ( _Device == null && ! Shutdown )
67
73
{
68
74
var controlerName = $ "I2C{ _Bus } ";
69
- var i2cSettings = new I2cConnectionSettings ( _Address ) { BusSpeed = _Speed } ;
75
+ var i2cSettings = new I2cConnectionSettings ( _Address ) { BusSpeed = Speed } ;
70
76
var deviceSelector = I2cDevice . GetDeviceSelector ( controlerName ) ;
71
77
var i2cDeviceControllers = await DeviceInformation . FindAllAsync ( deviceSelector ) ;
72
78
if ( i2cDeviceControllers != null && i2cDeviceControllers . Any ( ) )
@@ -80,18 +86,17 @@ private async Task<bool> InitAsync()
80
86
81
87
public async Task < PiJuiceInterfaceResult > ReadData ( PiJuiceStatusCommands cmd , byte lenght )
82
88
{
83
- return await ReadData ( ( byte ) cmd , lenght ) ;
89
+ return await ReadData ( ( byte ) cmd , lenght ) ;
84
90
}
85
91
86
92
public async Task < PiJuiceInterfaceResult > ReadData ( PiJuicePowerCommands cmd , byte lenght )
87
93
{
88
- return await ReadData ( ( byte ) cmd , lenght ) ;
94
+ return await ReadData ( ( byte ) cmd , lenght ) ;
89
95
}
90
96
91
97
public async Task < PiJuiceInterfaceResult > ReadData ( byte cmd , byte lenght )
92
98
{
93
99
PiJuiceInterfaceResult result = new PiJuiceInterfaceResult ( ) ;
94
- int RetryCounter = 3 ;
95
100
var StartTime = DateTime . Now ;
96
101
97
102
if ( Shutdown )
@@ -105,34 +110,24 @@ public async Task<PiJuiceInterfaceResult> ReadData(byte cmd, byte lenght)
105
110
throw new Exception ( "Device initialisation error" ) ;
106
111
107
112
// do transfer
108
- for ( int i = 0 ; i < RetryCounter ; i ++ )
113
+ for ( int i = 0 ; i < ReadRetryCounter ; i ++ )
109
114
{
110
115
// init
111
116
result . RetryCounter = i ;
112
117
result . Request = new byte [ ] { ( byte ) cmd } ;
113
118
result . Response = new byte [ lenght + 1 ] ;
114
-
115
119
// 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 ] )
117
124
{
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 ;
133
128
}
134
129
135
- await Task . Delay ( 10 ) ;
130
+ await Task . Delay ( ReadRetryDelay ) ;
136
131
}
137
132
138
133
result . Success = false ;
@@ -175,19 +170,11 @@ public async Task<PiJuiceInterfaceResult> WriteData(byte cmd, byte[] data)
175
170
buffer [ 0 ] = cmd ;
176
171
buffer [ buffer . Length - 1 ] = GetCheckSum ( data , all : true ) ;
177
172
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 ;
191
178
}
192
179
catch ( Exception ex )
193
180
{
0 commit comments