Alternate CQ between the two radios
Ctrl+B calls CQ on one radio, and when that message has gone out, moves to the other and calls there. The keyboard, the entry window and the SO2R box follow each turn. N1MM calls this dueling CQs and puts it on the same key. The turn is taken when the keyer says the message has ended, not when a timer guesses it has. So MessageSender grew a Finished event and a ReportsCompletion flag, and both keyers fill them in: cwdaemon answers the <ESC>h reply request that now goes out in front of every message, and a WinKeyer clears the busy bit in the status bytes it sends of its own accord. The status-byte reading is in WinkeyerStatus, away from the serial port, because that is the half that can be tested without a keyer on the desk. A keyer that reports nothing refuses to start alternating CQ rather than keying the second radio over the first. AlternatingCq itself takes the keyer, a callback that calls CQ on a radio, the gap and a wait function, so the alternation is tested without sleeping. The gap is in Config ▸ Keyer and messages and will not go below 100 ms, which is N1MM's floor too: an SO2R box works relays. docs/keying.md writes down why cwdaemon does the timing and we do not. N1MM keys DTR itself with a coarse sleep, a busy-wait and a margin that grows every time the sleep overshoots, and it raises the thread to TIME_CRITICAL for the length of the message. The busy-wait ports to Linux; the priority does not, without CAP_SYS_NICE, and a garbage collection mid-element is audible. A direct serial keyer stays a reasonable third option, to be taken knowingly. Running it against a fake daemon that takes 1.5 seconds to play a message: six CQs went out back to back and the keyboard moved between the two entry windows each time. Escape stopped it, let the message in flight finish, and started nothing further. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,21 +7,40 @@ namespace Nonemm.Keying;
|
||||
/// Sends CW through `cwdaemon`, which keys the radio from a serial or parallel
|
||||
/// port and is what Linux stations usually run. Its protocol is one UDP
|
||||
/// datagram per command; escape sequences start with a 0x1B byte.
|
||||
///
|
||||
/// `<ESC>h<text>` asks for a reply once the next message has been played, and
|
||||
/// cwdaemon answers with `h` and that text. The request covers one message
|
||||
/// only, so it goes out in front of every message.
|
||||
///
|
||||
/// Why the timing is cwdaemon's job and not ours: `docs/keying.md`.
|
||||
public sealed class CwDaemonSender : MessageSender
|
||||
{
|
||||
private readonly UdpClient socket = new();
|
||||
private const string ReplyToken = "nonemm";
|
||||
|
||||
// bound before anything is sent, so the reply has somewhere to arrive
|
||||
private readonly UdpClient socket = new(new IPEndPoint(IPAddress.Any, 0));
|
||||
private readonly IPEndPoint daemon;
|
||||
private readonly CancellationTokenSource reading = new();
|
||||
|
||||
public CwDaemonSender(string host = "127.0.0.1", int port = 6789)
|
||||
{
|
||||
daemon = new IPEndPoint(IPAddress.Parse(host), port);
|
||||
IsReady = true;
|
||||
_ = ReadRepliesAsync(reading.Token);
|
||||
}
|
||||
|
||||
public bool IsReady { get; private set; }
|
||||
|
||||
public Task SendAsync(string text, CancellationToken cancellation = default) =>
|
||||
WriteAsync(Encoding.ASCII.GetBytes(text.ToUpperInvariant()), cancellation);
|
||||
public bool ReportsCompletion => true;
|
||||
|
||||
public event EventHandler? Finished;
|
||||
|
||||
public async Task SendAsync(string text, CancellationToken cancellation = default)
|
||||
{
|
||||
await WriteAsync(Escape('h', ReplyToken), cancellation).ConfigureAwait(false);
|
||||
await WriteAsync(Encoding.ASCII.GetBytes(text.ToUpperInvariant()), cancellation)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public Task AbortAsync(CancellationToken cancellation = default) =>
|
||||
WriteAsync([0x1B, (byte)'4'], cancellation);
|
||||
@@ -29,11 +48,44 @@ public sealed class CwDaemonSender : MessageSender
|
||||
public Task SetSpeedAsync(int wordsPerMinute, CancellationToken cancellation = default) =>
|
||||
WriteAsync(Escape('2', wordsPerMinute.ToString()), cancellation);
|
||||
|
||||
public void Dispose() => socket.Dispose();
|
||||
public void Dispose()
|
||||
{
|
||||
reading.Cancel();
|
||||
reading.Dispose();
|
||||
socket.Dispose();
|
||||
}
|
||||
|
||||
private static byte[] Escape(char command, string argument) =>
|
||||
[0x1B, (byte)command, .. Encoding.ASCII.GetBytes(argument)];
|
||||
|
||||
/// cwdaemon's reply arrives on the same socket the commands went out on.
|
||||
/// A reply for anything other than our own token is another program's
|
||||
/// business and is passed over.
|
||||
private async Task ReadRepliesAsync(CancellationToken cancellation)
|
||||
{
|
||||
while (!cancellation.IsCancellationRequested)
|
||||
{
|
||||
UdpReceiveResult received;
|
||||
try
|
||||
{
|
||||
received = await socket.ReceiveAsync(cancellation).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception e) when (e is OperationCanceledException or ObjectDisposedException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
// a datagram that could not be read says nothing about the next
|
||||
continue;
|
||||
}
|
||||
if (Encoding.ASCII.GetString(received.Buffer).TrimEnd('\r', '\n') == $"h{ReplyToken}")
|
||||
{
|
||||
Finished?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task WriteAsync(byte[] message, CancellationToken cancellation)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user