@@ -19,7 +19,6 @@ public class Hcsr04 : IDisposable
19
19
{
20
20
ReceiverChannel _rxChannel ;
21
21
TransmitterChannel _txChannel ;
22
- RmtCommand _txPulse ;
23
22
long _lastMeasurment ;
24
23
25
24
const double _speedOfSound = 340.29 ;
@@ -39,26 +38,37 @@ public Hcsr04(int trigger, int echo)
39
38
{
40
39
// Set-up TX & RX channels
41
40
// We need to send a 10us pulse to initiate measurement
42
- _txChannel = new TransmitterChannel ( trigger ) ;
41
+ var txChannelSettings = new TransmitChannelSettings ( pinNumber : trigger )
42
+ {
43
+ // 1us clock ( 80Mhz / 80 ) = 1Mhz
44
+ ClockDivider = 80 ,
45
+ EnableCarrierWave = true ,
46
+ IdleLevel = false ,
47
+ } ;
48
+
49
+ _txChannel = new TransmitterChannel ( txChannelSettings ) ;
43
50
// we only need 1 pulse of 10 us high
44
51
_txChannel . AddCommand ( new RmtCommand ( 10 , true , 0 , false ) ) ;
45
52
46
- _txChannel . ClockDivider = 80 ;
47
- _txChannel . CarrierEnabled = false ;
48
- _txChannel . IdleLevel = false ;
49
-
50
53
// The received echo pulse width represents the distance to obstacle
51
54
// 150us to 38ms
52
- _rxChannel = new ReceiverChannel ( echo ) ;
53
-
54
- // 1us clock ( 80Mhz / 80 ) = 1Mhz
55
- _rxChannel . ClockDivider = 80 ;
56
- // filter out 200Us / noise
57
- _rxChannel . EnableFilter ( true , 200 ) ;
58
- // 40ms based on 1us clock
59
- _rxChannel . SetIdleThresold ( 40000 ) ;
60
- // 100 millisecond timeout is enough
61
- _rxChannel . ReceiveTimeout = TimeSpan . FromMilliseconds ( 100 ) ;
55
+ var rxChannelSettings = new ReceiverChannelSettings ( pinNumber : echo )
56
+ {
57
+ // 1us clock ( 80Mhz / 80 ) = 1Mhz
58
+ ClockDivider = 80 ,
59
+
60
+ // filter out 200Us / noise
61
+ EnableFilter = true ,
62
+ FilterThreshold = 200 ,
63
+
64
+ // 40ms based on 1us clock
65
+ IdleThreshold = 40_000 ,
66
+
67
+ // 100 millisecond timeout is enough
68
+ ReceiveTimeout = TimeSpan . FromMilliseconds ( 100 )
69
+ } ;
70
+
71
+ _rxChannel = new ReceiverChannel ( rxChannelSettings ) ;
62
72
}
63
73
64
74
/// <summary>
0 commit comments