-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.cs
323 lines (303 loc) · 7.43 KB
/
Test.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using GameReaderCommon;
using SimHub.Plugins;
using System;
/* Tension test sliders
; https://github.com/blekenbleu/SimHub-profiles/blob/main/Fake8.shsds
; f0: PWM period (usec / 50) 1 - 10000
; f1: max PWM % 0 - 100
; f2: min PWM % 0 - 10
; f3: predistortion amplitude: % of PWM change 0 - 64
; f4: predistortion count (number of PWM cycles) 0 - 64
; f5: Test period count (number of SimHub Run() invocations 60 - 600
; f6: Test rise count 0 - 64
; f7: Test hold count 0 - 64
; f8: Test fall count 0 - 64
; f9:
*/
namespace Fake8plugin
{
public class Test // modulate PWM percent
{
bool start;
byte state; // 0=reset, 1=rise, 2=hold, 3=fall, 4=wait
byte max, min;
internal byte verbosity;
byte[] cmd, buffer; // cmd[0] is Arduino PWM command, cmd[1] is PWM 7-bit value
ushort rise, hold, fall;
ushort count, period, granularity, damping, attenuation;
int error, range;
Fake8 F8;
Fake7 F7;
/// <summary>
/// SimHub.Logging.Current.Info() with prefix
/// </summary>
private bool Info(string str)
{
SimHub.Logging.Current.Info(F7.old = "Test." + str); // bool Info()
return true;
}
/// <summary>
/// Apply high-pass IIR to PWM
/// </summary>
private bool PWM(int c)
{
int d = c - buffer[0];
int lp = c + (d * granularity + ((granularity + damping) >>1)) / (granularity + damping);
buffer[0] = (byte)(1 > lp ? 1 : lp);
d = c + (d * granularity + ((granularity + attenuation) >> 1)) / (granularity + attenuation);
d = (byte)(1 > d ? 1 : (100 < d) ? 100 : d);
if (d != cmd[1])
{
cmd[1] = (byte)d;
if (2 == verbosity)
F8.CustomWrite($"{c}\t"); // e.g. gnuplot unfiltered vs filtered
F8.TryWrite(cmd, 2);
cmd[1] = (byte)c;
return true;
}
return false;
}
/// <summary>
/// Custom Serial settings from strings; prepare Run() state machine; update Arduino PWM
/// https://github.com/blekenbleu/Arduino-Blue-Pill/tree/main/PWM_FullConfiguration
/// </summary>
internal bool State(byte index)
{
bool ret = true;
if (0 == index) // f0: PWM period: 1 = 500 Usec
{
uint value = Default(index);
byte[] ard = new byte[3];
ard[2] = (byte)(127 & value);
value >>= 7;
ard[1] = (byte)(127 & value);
value >>= 7;
ard[0] = (byte)((7 << 5) | (3 & value)); // Ardiono case 7: 3-byte 16-bit PWM period
return F8.TryWrite(ard, 3);
}
if (1 == index) // f1: max PWM %
{
max = (byte)Default(index);
if (min > max && min > 1)
{
Info($"State(min) reduced from {min} to 1");
min = 1;
}
if (max <= min)
{
Fake8.msg = $"# State(max) increased from {max} to {min+1}";
F8.CustomWrite(Fake8.msg + "\n");
max = min;
max++;
}
range = max - min; // think positive
if (cmd[1] < max)
state = 1;
else if (cmd[1] > min)
state = 3;
}
else if (2 == index)
{
min = (byte)Default(index);
if (min > max && min > 1)
{
Info($"State(min) reduced from {min} to 1");
min = 1;
}
if (cmd[1] < min)
state = 1;
range = max - min; // think positive
}
else if (3 == index)
granularity = Default(index);
else if (4 == index)
damping = Default(index);
else if (10 == index)
attenuation = Default(index);
else if (9 == index)
{
if (verbosity != Default(index))
{
byte[] v = { (byte)(1 | cmd[0]), verbosity = (byte)Default(index) };
F8.TryWrite(v, 2);
}
}
else if (5 == index)
{
int pulse = (rise + hold + fall) << 1;
period = Default(index);
if (period < pulse)
{
Fake8.msg = $"# State(period) increased from {period} to {pulse}";
F8.CustomWrite(Fake8.msg + "\n");
period = (ushort)pulse;
}
if (count > period)
state = 1;
}
else if (6 == index)
rise = Default(index);
else if (7 == index)
hold = Default(index);
else if (8 == index)
fall = Default(index);
else ret = false;
/*
// for looping
if (ret)
{
if (count < rise + hold)
state = 2;
else if (count < rise + hold + fall)
state = 3;
else if (count < period)
state = 4;
else state = 1;
}
if (1 == state)
count = 0;
*/
// single cycle, instead of looping
count = (ushort)(period - fall);
start = true;
state = 4;
for (int i = 0; i < buffer.Length; i++) // initialize for IIR
buffer[i] = min;
return ret;
} // State()
private UInt16 Default(byte i)
{
try
{
return UInt16.Parse(F7.Settings.Prop[i]);
}
catch
{
UInt16[] array = { 1146, 57, 1, 23, 23, 22, 28, 28, 29, 1, 0, 0 };
if (i >= F7.Settings.Prop.Length)
{
Info($"Default({i}): invalid index");
return 1;
}
Info($"Default({i}): invalid '{F7.Settings.Prop[i]}'");
return array[i];
}
}
/// <summary>
/// [re]initialize state machine settings and restart
/// </summary>
internal bool Reset(byte index)
{
error = count = state = 0;
max = (byte)Default(1);
min = (byte)Default(2);
granularity = Default(3);
damping = Default(4);
attenuation = Default(10);
period = Default(5);
rise = Default(6);
hold = Default(7);
fall = Default(8);
verbosity = (byte)Default(9);
if (period < (range = ((rise + hold + fall) << 1)))
{
Info(Fake8.msg = $"Reset(period) increased from {period} to {range}");
period = (ushort)range;
}
else Fake8.msg = $"Test.Reset({index}) complete.";
range = max - min;
return State(index);
}
/// <summary>
/// Test state machine; constant max, min in states 2, 4
/// Bresenham rise and fall in states 1 and 3
/// invoke PWM() for each invocation to process high-pass steps
/// </summary>
internal bool Run()
{
int dy;
if (0 == state)
return PWM(cmd[1]); // continue any high-pass filtering
count++;
if ( count >= period)
{
if (!start)
{
state = 0;
return PWM(cmd[1]); // continue any high-pass filtering
}
state = 1;
error = count = 0;
}
if (1 == state)
{
if (count > rise)
state++;
else {
start = false;
if (0 == rise)
return PWM(max);
dy = error + range;
error = dy % rise;
dy /= rise;
if (range < (error << 1))
{
error -= rise;
dy++;
}
return PWM(cmd[1] + dy);
}
}
if (2 == state)
{
error = 0;
if (count > rise + hold)
state++;
else return PWM(max); // F8.TryWrite(cmd, 2);
}
if (3 == state)
{
if (count > rise + hold + fall)
state++;
else if (0 == fall)
return PWM(min);
else
{
dy = error + range;
error = dy % fall;
dy /= fall;
if (range < (error << 1))
{
error -= fall;
dy++;
}
return PWM(cmd[1] - dy);
}
}
return PWM((4 == state) ? min : cmd[1]); // F8.TryWrite(cmd, 2);
// could check for invalid state here...
}
/// <summary>
/// Called at plugin manager stop, close/dispose anything needed here!
/// Plugins are rebuilt at game changes, but NCalc files are not re-read
/// </summary>
public void End()
{
state = 0;
byte[] reset = { 0xBF };
F8.TryWrite(reset, 1);
}
/// <summary>
/// Called at SimHub start then after game changes
/// </summary>
public void Init(Fake7 f7, Fake8 f8)
{
start = false;
cmd = new byte[] { (4<<5), 1 }; // Arduino case 4: 7-bit (PWM %)
buffer = new byte[8];
F7 = f7;
F8 = f8;
Reset(1); // rise
} // Init()
}
}