Skip to content

Commit

Permalink
ShureUHFREmu:
Browse files Browse the repository at this point in the history
 - Implemented metering
  • Loading branch information
space928 committed Jun 21, 2024
1 parent f7608ac commit 5294f3f
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions WirelessMicSuiteServer.Test/ShureUHFREmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ internal class ShureUHFREmulator : IDisposable
private readonly Socket socket;
private readonly Task serverRxTask;
private readonly Task serverTxTask;
private readonly Task meterTask1;
private readonly Task meterTask2;
private readonly Decoder decoder;
private readonly Encoder encoder;
private readonly byte[] buffer;
private readonly char[] charBuffer;
private readonly CancellationToken shouldQuit;
private readonly CancellationTokenSource shouldQuitSource;
private readonly Pipe txPipe;
private readonly Random random;

private UHFRProperties props;

Expand All @@ -35,6 +38,7 @@ public ShureUHFREmulator(int port)
txPipe = new();
shouldQuitSource = new();
shouldQuit = shouldQuitSource.Token;
random = new();

props = new();

Expand All @@ -47,6 +51,8 @@ public ShureUHFREmulator(int port)
Log($"Starting ShureUHFR Emulator on port {port}...");
serverRxTask = Task.Run(ServerRxTask);
serverTxTask = Task.Run(ServerTxTask);
meterTask1 = Task.Run(() => MeterTask(0));
meterTask2 = Task.Run(() => MeterTask(1));
}

private static void Log(object message, LogSeverity severity = LogSeverity.Info, [CallerMemberName] string? caller = null)
Expand Down Expand Up @@ -141,7 +147,7 @@ private void ServerRxTask()
int cmdEnd = str.IndexOf(' ');
var cmd = cmdEnd == -1 ? str : str[..cmdEnd];
str = str[cmdEnd..];
ParseCommand(type, receiver, cmd, str, fullMsg);
ParseCommand(type, receiver-1, cmd, str, fullMsg);
}
}

Expand All @@ -157,13 +163,13 @@ private void ParseCommand(CommandType type, int receiver, Span<char> cmd, Span<c
}
else if (type == CommandType.SET)
{
if (args.Length < 13)
if (args.Length < 12)
{
CommandError(fullMsg);
break;
}

var name = args[1..13];
var name = args[..13];
props.channelName[receiver] = name.ToString().PadLeft(12);
Report(receiver, cmd, props.channelName[receiver]);
break;
Expand Down Expand Up @@ -363,7 +369,7 @@ private static void CommandError(ReadOnlySpan<char> str)

private void Report(int channel, ReadOnlySpan<char> cmd, string msg)
{
Reply($"* REPORT {channel} {cmd} {msg} *");
Reply($"* REPORT {channel+1} {cmd} {msg} *");
}

private void ReportMeter(int channel)
Expand All @@ -383,6 +389,54 @@ private void Reply(string message)
txPipe.Writer.Advance(written);
}

private void MeterTask(int channel)
{
while (!shouldQuit.IsCancellationRequested)
{
int meterTime = props.meter[channel];
// If metering is disabled or if the metering time > 12 seconds, just wait...
if (meterTime < 0 || meterTime >= 400)
{
Thread.Sleep(100);
continue;
}

int rnd = random.Next();
string rfLevelA = (random.Next(7)) switch
{
0 => "020",
1 => "070",
2 => "075",
3 => "080",
4 => "085",
5 => "090",
6 => "100",
_ => "100"
};
string rfLevelB = (random.Next(7)) switch
{
0 => "020",
1 => "070",
2 => "075",
3 => "080",
4 => "085",
5 => "090",
6 => "100",
_ => "100"
};
int b = props.batt[channel];

Reply($"* SAMPLE {channel + 1} ALL " +
$"{((rnd&1) != 0 ? 'X' : 'A')}{((rnd & 2) != 0 ? 'X' : 'B')}" +
$"{rfLevelA} {rfLevelB}" +
$"{(b < 1 ? 'U' : b)}" +
$"{(rnd >> 2) & 0xff}" +
$" *");

Thread.Sleep(meterTime * 30);
}
}

private async Task ServerTxTask()
{
while (!shouldQuit.IsCancellationRequested)
Expand Down

0 comments on commit 5294f3f

Please sign in to comment.