-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLevels.vb
More file actions
142 lines (135 loc) · 4.17 KB
/
Levels.vb
File metadata and controls
142 lines (135 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Imports System.Threading
Imports JJTrace
''' <summary>
''' provides peak holding for a level
''' </summary>
Friend Class Levels
''' <summary>
''' function delegate that returns the level.
''' </summary>
''' <returns>Integer level</returns>
Public Delegate Function fld() As Integer
Private userValue As fld
Private peakFlag As Boolean
Private peakValue As Integer
''' <summary>
''' Control whether the level peak or raw value is returned.
''' </summary>
''' <value>true to provide the peak value</value>
''' <returns>true if peak provided</returns>
Public Property Peak As Boolean
Get
Return peakFlag
End Get
Set(value As Boolean)
Tracing.TraceLine("Levels.Peak:" & value.ToString, TraceLevel.Info)
peakFlag = value
If value Then
stop_tmr = False
peakThread = New Thread(AddressOf peakProc)
peakThread.Name = "peakThread"
'peakThread.Priority = ThreadPriority.AboveNormal
peakThread.Start()
Else
stop_tmr = True
Thread.Sleep(baseInterval * 2)
End If
End Set
End Property
Const baseInterval As Integer = 50
Private Const defaultCycle As Integer = 500 ' .5 second default
Private cycleTicks As Integer ' timer ticks per cycle
Private WithEvents tmr As Timer
''' <summary>
''' time interval over which the peak is evaluated.
''' Zero turns peak evaluation off, peak=false.
''' </summary>
''' <value>millisecond time</value>
''' <returns>millisecond time</returns>
Public Property Cycle As Integer
Get
Return cycleTicks * baseInterval
End Get
Set(value As Integer)
Tracing.TraceLine("Levels.Cycle:" & value, TraceLevel.Info)
If value = 0 Then
' turn off peak
Peak = False
Else
' Round up cycles to nearest base interval
cycleTicks = (value + baseInterval - 1) / baseInterval
End If
End Set
End Property
''' <summary>
''' Provide the function to retrieve the level.
''' </summary>
''' <param name="f">delegate</param>
Public Sub New(f As fld)
Tracing.TraceLine("Levels.new", TraceLevel.Info)
userValue = f
peakFlag = False
tmr = Nothing
Cycle = defaultCycle
End Sub
''' <summary>
''' (ReadOnly) The level, or raw peak level.
''' </summary>
''' <returns>Integer level</returns>
Public ReadOnly Property Value As Integer
Get
If Peak AndAlso (peakValue <> 0) Then
Return peakValue
Else
Return userValue()
End If
End Get
End Property
Private peakThread As Thread
Private stop_tmr As Boolean
Private Sub peakProc()
Tracing.TraceLine("peakProc", TraceLevel.Info)
If tmr Is Nothing Then
' start timer
Dim inst As New TimerCallback(AddressOf tmr_tick)
tmr = New Timer(inst, Nothing, 0, baseInterval)
End If
' Just await timer ticks
While Not stop_tmr
Thread.Sleep(baseInterval)
End While
Tracing.TraceLine("peakProc stop_tmr", TraceLevel.Info)
If tmr IsNot Nothing Then
' stop timer
tmr.Dispose()
tmr = Nothing
End If
End Sub
Private myPeak As Integer = 0
Private myTicks As Integer = 0
Private Sub tmr_tick()
Dim v As Integer
Try
v = userValue() ' Read the real value
Catch ex As Exception
Tracing.TraceLine("tmr_tick exception:" & ex.Message, TraceLevel.Error)
v = 0
End Try
If v > myPeak Then
myPeak = v
End If
If myTicks = cycleTicks Then
myTicks = 0
peakValue = myPeak
myPeak = 0
Else
myTicks += 1
End If
End Sub
Public Sub ResetPeak()
'myPeak = 0
myPeak = userValue()
myTicks = cycleTicks
'peakValue = 0
End Sub
End Class