mirror of
https://git.vladimir.cc/vladimir/ewsdr.git
synced 2026-08-25 18:43:51 +00:00
CAT becomes a peer adapter over the controller, alongside WebAdapter. TCATAdapter owns the CAT engine + serial/TCP transports and builds the TCATContext: getters read FController directly (CAT thread), setters/commands call controller commands marshaled via FController.Invoke. No MainForm or WebAdapter dependency — the historical CAT->WebAdapter.OnXxx coupling (Mode/AGC/Band/etc.) is gone; CAT talks only to the controller. MainForm keeps CAT *settings* (FCATLastGlobal + OnCATSettingsChange + SettingsForm wiring), now calling FCATAdapter.ApplySettings on change/connect. Removed InitCATEngine, CATApplySettings, all CATGet*/CATSet*/CATDo*/SyncCAT* and the FCATEngine/FCATSerial/FCATTcp/FCATSyncFreq fields. Fixes a latent bug: CATSetFilterIdx routed a filter index through the web BW handler as a negative-encoded value, but that branch was dead since batch 21 — CAT now calls FController.SetFilterIdx directly. Adds StoreVfoA (store VFO A without retune when B is active) and uses the now-real SetBand/BandUp/BandDown/TuneActiveBy commands. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
325 lines
12 KiB
ObjectPascal
325 lines
12 KiB
ObjectPascal
unit CATAdapter;
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// TCATAdapter — адаптер CAT (Kenwood TS-2000) поверх TRadioController.
|
|
//
|
|
// Равноправный клиент контроллера наряду с WebAdapter и будущей Arduino-панелью.
|
|
// Владеет CAT-движком (TCATEngine) + транспортами (serial/TCP) и строит TCATContext:
|
|
// • геттеры читают состояние FController напрямую (CAT-поток, read-only);
|
|
// • сеттеры/команды зовут команды контроллера, маршалинг через FController.Invoke
|
|
// (GUI = TThread.Synchronize; демон подставит свою очередь).
|
|
// Никаких обращений к MainForm/WebAdapter — CAT говорит только с контроллером.
|
|
//
|
|
// Конфиг serial/TCP применяется снаружи через ApplySettings (UI-настройки CAT
|
|
// остаются в MainForm/SettingsForm и зовут ApplySettings при изменении/connect).
|
|
// -----------------------------------------------------------------------------
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils,
|
|
RadioController, CATEngine, CATSerial, CATTcp, Settings, WDSPEngine;
|
|
|
|
type
|
|
TCATAdapter = class
|
|
private
|
|
FController: TRadioController;
|
|
FEngine: TCATEngine;
|
|
FSerial: TCATSerialManager;
|
|
FTcp: TCATTcpServer;
|
|
// Параметры команды для маршалинга в поток контроллера (через Invoke).
|
|
FSyncFreq: Double;
|
|
FSyncInt: Integer;
|
|
FSyncBool: Boolean;
|
|
|
|
// ── Геттеры (CAT-поток, read-only) ──
|
|
function GetVfoA: Double;
|
|
function GetVfoB: Double;
|
|
function GetMode: Integer;
|
|
function GetActiveVfo: Integer;
|
|
function GetAGCMode: Integer;
|
|
function GetVolume: Integer;
|
|
function GetDriveLevel: Integer;
|
|
function GetFilterIdx: Integer;
|
|
function GetFilterBW: Integer;
|
|
function GetNRMode: Integer;
|
|
function GetNBMode: Integer;
|
|
function GetSNB: Boolean;
|
|
function GetANF: Boolean;
|
|
function GetTX: Boolean;
|
|
function GetRunning: Boolean;
|
|
function GetSMeter: Double;
|
|
function GetBand: Integer;
|
|
|
|
// ── Сеттеры/команды (из CAT-потока → Invoke в поток контроллера) ──
|
|
procedure SetVfoA(V: Double);
|
|
procedure SetVfoB(V: Double);
|
|
procedure SetMode(V: Integer);
|
|
procedure SetActiveVfo(V: Integer);
|
|
procedure SetAGCMode(V: Integer);
|
|
procedure SetVolume(V: Integer);
|
|
procedure SetDriveLevel(V: Integer);
|
|
procedure SetFilterIdx(V: Integer);
|
|
procedure SetNRMode(V: Integer);
|
|
procedure SetNBMode(V: Integer);
|
|
procedure SetSNB(V: Boolean);
|
|
procedure SetANF(V: Boolean);
|
|
procedure SetTransmitting(V: Boolean);
|
|
procedure DoBandUp;
|
|
procedure DoBandDown;
|
|
procedure DoTuneUp;
|
|
procedure DoTuneDown;
|
|
procedure DoBandByIndex(Idx: Integer);
|
|
|
|
// ── Sync-методы (выполняются в потоке контроллера) ──
|
|
procedure SyncVfoA;
|
|
procedure SyncVfoB;
|
|
procedure SyncMode;
|
|
procedure SyncActiveVfo;
|
|
procedure SyncAGC;
|
|
procedure SyncVolume;
|
|
procedure SyncDrive;
|
|
procedure SyncFilterIdx;
|
|
procedure SyncNR;
|
|
procedure SyncNB;
|
|
procedure SyncSNB;
|
|
procedure SyncANF;
|
|
procedure SyncMOX;
|
|
procedure SyncBandUp;
|
|
procedure SyncBandDown;
|
|
procedure SyncTuneUp;
|
|
procedure SyncTuneDown;
|
|
procedure SyncBandByIndex;
|
|
public
|
|
constructor Create(AController: TRadioController);
|
|
destructor Destroy; override;
|
|
// Применить serial/TCP-конфиг из глобальных настроек (вызывает UI на change/connect).
|
|
procedure ApplySettings(const G: TGlobalSettings);
|
|
end;
|
|
|
|
implementation
|
|
|
|
constructor TCATAdapter.Create(AController: TRadioController);
|
|
var
|
|
Ctx: TCATContext;
|
|
begin
|
|
inherited Create;
|
|
FController := AController;
|
|
|
|
FillChar(Ctx, SizeOf(Ctx), 0);
|
|
Ctx.GetVfoA := @GetVfoA;
|
|
Ctx.GetVfoB := @GetVfoB;
|
|
Ctx.GetMode := @GetMode;
|
|
Ctx.GetActiveVfo := @GetActiveVfo;
|
|
Ctx.GetAGCMode := @GetAGCMode;
|
|
Ctx.GetVolume := @GetVolume;
|
|
Ctx.GetDriveLevel := @GetDriveLevel;
|
|
Ctx.GetFilterIdx := @GetFilterIdx;
|
|
Ctx.GetFilterBW := @GetFilterBW;
|
|
Ctx.GetNRMode := @GetNRMode;
|
|
Ctx.GetNBMode := @GetNBMode;
|
|
Ctx.GetSNBEnabled := @GetSNB;
|
|
Ctx.GetANFEnabled := @GetANF;
|
|
Ctx.GetTransmitting := @GetTX;
|
|
Ctx.GetRunning := @GetRunning;
|
|
Ctx.GetSMeter := @GetSMeter;
|
|
Ctx.GetCurrentBand := @GetBand;
|
|
Ctx.SetVfoA := @SetVfoA;
|
|
Ctx.SetVfoB := @SetVfoB;
|
|
Ctx.SetMode := @SetMode;
|
|
Ctx.SetActiveVfo := @SetActiveVfo;
|
|
Ctx.SetAGCMode := @SetAGCMode;
|
|
Ctx.SetVolume := @SetVolume;
|
|
Ctx.SetDriveLevel := @SetDriveLevel;
|
|
Ctx.SetFilterIdx := @SetFilterIdx;
|
|
Ctx.SetNRMode := @SetNRMode;
|
|
Ctx.SetNBMode := @SetNBMode;
|
|
Ctx.SetSNBEnabled := @SetSNB;
|
|
Ctx.SetANFEnabled := @SetANF;
|
|
Ctx.SetTransmitting := @SetTransmitting;
|
|
Ctx.DoBandUp := @DoBandUp;
|
|
Ctx.DoBandDown := @DoBandDown;
|
|
Ctx.DoTuneUp := @DoTuneUp;
|
|
Ctx.DoTuneDown := @DoTuneDown;
|
|
Ctx.DoBandByIndex := @DoBandByIndex;
|
|
|
|
FEngine := TCATEngine.Create(Ctx);
|
|
FSerial := TCATSerialManager.Create(FEngine);
|
|
FTcp := TCATTcpServer.Create(FEngine);
|
|
end;
|
|
|
|
destructor TCATAdapter.Destroy;
|
|
begin
|
|
if Assigned(FTcp) then begin FTcp.Stop; FreeAndNil(FTcp); end;
|
|
if Assigned(FSerial) then begin FSerial.StopAll; FreeAndNil(FSerial); end;
|
|
FreeAndNil(FEngine);
|
|
inherited Destroy;
|
|
end;
|
|
|
|
procedure TCATAdapter.ApplySettings(const G: TGlobalSettings);
|
|
var
|
|
Cfgs: array[0..3] of TCATSerialConfig;
|
|
i: Integer;
|
|
begin
|
|
for i := 0 to 3 do
|
|
begin
|
|
Cfgs[i].Enabled := G.CATSerialEnabled[i];
|
|
Cfgs[i].PortName := G.CATSerialPort[i];
|
|
Cfgs[i].BaudRate := G.CATSerialBaud[i];
|
|
Cfgs[i].DataBits := G.CATSerialDataBits[i];
|
|
Cfgs[i].StopBits := G.CATSerialStopBits[i];
|
|
case G.CATSerialParity[i] of
|
|
1: Cfgs[i].Parity := cspOdd;
|
|
2: Cfgs[i].Parity := cspEven;
|
|
else Cfgs[i].Parity := cspNone;
|
|
end;
|
|
end;
|
|
FSerial.ApplyConfig(Cfgs);
|
|
FTcp.Stop;
|
|
if G.CATTcpEnabled then
|
|
begin
|
|
FTcp.Port := G.CATTcpPort;
|
|
FTcp.Start;
|
|
end;
|
|
end;
|
|
|
|
// ── Геттеры ──────────────────────────────────────────────────────────────────
|
|
function TCATAdapter.GetVfoA: Double; begin Result := FController.FVfoA; end;
|
|
function TCATAdapter.GetVfoB: Double; begin Result := FController.FVfoB; end;
|
|
function TCATAdapter.GetMode: Integer; begin Result := FController.FMode; end;
|
|
function TCATAdapter.GetActiveVfo: Integer; begin Result := FController.FActiveVfo; end;
|
|
function TCATAdapter.GetAGCMode: Integer; begin Result := FController.FAGCMode; end;
|
|
function TCATAdapter.GetVolume: Integer; begin Result := FController.FVolume; end;
|
|
function TCATAdapter.GetDriveLevel: Integer; begin Result := FController.FDrivePercent; end;
|
|
function TCATAdapter.GetFilterIdx: Integer; begin Result := FController.FFilter; end;
|
|
function TCATAdapter.GetFilterBW: Integer; begin Result := FController.FFilterBW; end;
|
|
function TCATAdapter.GetNRMode: Integer; begin Result := FController.FNRMode; end;
|
|
function TCATAdapter.GetNBMode: Integer; begin Result := FController.FNBMode; end;
|
|
function TCATAdapter.GetSNB: Boolean; begin Result := FController.FSNB; end;
|
|
function TCATAdapter.GetANF: Boolean; begin Result := FController.FANF; end;
|
|
function TCATAdapter.GetTX: Boolean; begin Result := FController.FTransmitting; end;
|
|
function TCATAdapter.GetRunning: Boolean; begin Result := FController.FRunning; end;
|
|
function TCATAdapter.GetSMeter: Double; begin Result := FController.FLastSMeter; end;
|
|
function TCATAdapter.GetBand: Integer; begin Result := FController.FCurrentBand; end;
|
|
|
|
// ── Сеттеры (маршалинг) ──────────────────────────────────────────────────────
|
|
procedure TCATAdapter.SetVfoA(V: Double);
|
|
begin FSyncFreq := V; FController.Invoke(@SyncVfoA); end;
|
|
|
|
procedure TCATAdapter.SyncVfoA;
|
|
begin
|
|
// A активен — полная перестройка (CTUN/band/сеть + канальная оркестрация через
|
|
// OnAfterTune). B активен — только сохраняем/отображаем A (приёмник на B).
|
|
if FController.FActiveVfo = 0 then FController.SetVfoA(FSyncFreq)
|
|
else FController.StoreVfoA(FSyncFreq);
|
|
end;
|
|
|
|
procedure TCATAdapter.SetVfoB(V: Double);
|
|
begin FSyncFreq := V; FController.Invoke(@SyncVfoB); end;
|
|
|
|
procedure TCATAdapter.SyncVfoB;
|
|
begin FController.SetVfoB(FSyncFreq); end;
|
|
|
|
procedure TCATAdapter.SetMode(V: Integer);
|
|
begin FSyncInt := V; FController.Invoke(@SyncMode); end;
|
|
|
|
procedure TCATAdapter.SyncMode;
|
|
begin
|
|
if (FSyncInt < 0) or (FSyncInt > MODE_SAM) then Exit;
|
|
FController.SetMode(FSyncInt);
|
|
end;
|
|
|
|
procedure TCATAdapter.SetActiveVfo(V: Integer);
|
|
begin FSyncInt := V; FController.Invoke(@SyncActiveVfo); end;
|
|
|
|
procedure TCATAdapter.SyncActiveVfo;
|
|
begin FController.SetActiveVfo(FSyncInt); end;
|
|
|
|
procedure TCATAdapter.SetAGCMode(V: Integer);
|
|
begin FSyncInt := V; FController.Invoke(@SyncAGC); end;
|
|
|
|
procedure TCATAdapter.SyncAGC;
|
|
begin FController.SetAGCMode(FSyncInt); end;
|
|
|
|
procedure TCATAdapter.SetVolume(V: Integer);
|
|
begin FSyncInt := V; FController.Invoke(@SyncVolume); end;
|
|
|
|
procedure TCATAdapter.SyncVolume;
|
|
begin FController.SetVolume(FSyncInt); end;
|
|
|
|
procedure TCATAdapter.SetDriveLevel(V: Integer);
|
|
begin FSyncInt := V; FController.Invoke(@SyncDrive); end;
|
|
|
|
procedure TCATAdapter.SyncDrive;
|
|
begin FController.SetDrive(FSyncInt); end;
|
|
|
|
procedure TCATAdapter.SetFilterIdx(V: Integer);
|
|
begin FSyncInt := V; FController.Invoke(@SyncFilterIdx); end;
|
|
|
|
procedure TCATAdapter.SyncFilterIdx;
|
|
begin FController.SetFilterIdx(FSyncInt); end; // прямой индекс пресета
|
|
|
|
procedure TCATAdapter.SetNRMode(V: Integer);
|
|
begin FSyncInt := V; FController.Invoke(@SyncNR); end;
|
|
|
|
procedure TCATAdapter.SyncNR;
|
|
begin FController.SetNR(FSyncInt); end;
|
|
|
|
procedure TCATAdapter.SetNBMode(V: Integer);
|
|
begin FSyncInt := V; FController.Invoke(@SyncNB); end;
|
|
|
|
procedure TCATAdapter.SyncNB;
|
|
begin FController.SetNB(FSyncInt); end;
|
|
|
|
procedure TCATAdapter.SetSNB(V: Boolean);
|
|
begin FSyncBool := V; FController.Invoke(@SyncSNB); end;
|
|
|
|
procedure TCATAdapter.SyncSNB;
|
|
begin FController.SetSNB(FSyncBool); end;
|
|
|
|
procedure TCATAdapter.SetANF(V: Boolean);
|
|
begin FSyncBool := V; FController.Invoke(@SyncANF); end;
|
|
|
|
procedure TCATAdapter.SyncANF;
|
|
begin FController.SetANF(FSyncBool); end;
|
|
|
|
procedure TCATAdapter.SetTransmitting(V: Boolean);
|
|
begin FSyncBool := V; FController.Invoke(@SyncMOX); end;
|
|
|
|
procedure TCATAdapter.SyncMOX;
|
|
begin FController.SetMOX(FSyncBool); end;
|
|
|
|
procedure TCATAdapter.DoBandUp;
|
|
begin FController.Invoke(@SyncBandUp); end;
|
|
|
|
procedure TCATAdapter.SyncBandUp;
|
|
begin FController.BandUp; end;
|
|
|
|
procedure TCATAdapter.DoBandDown;
|
|
begin FController.Invoke(@SyncBandDown); end;
|
|
|
|
procedure TCATAdapter.SyncBandDown;
|
|
begin FController.BandDown; end;
|
|
|
|
procedure TCATAdapter.DoTuneUp;
|
|
begin FController.Invoke(@SyncTuneUp); end;
|
|
|
|
procedure TCATAdapter.SyncTuneUp;
|
|
begin FController.TuneActiveBy(10); end;
|
|
|
|
procedure TCATAdapter.DoTuneDown;
|
|
begin FController.Invoke(@SyncTuneDown); end;
|
|
|
|
procedure TCATAdapter.SyncTuneDown;
|
|
begin FController.TuneActiveBy(-10); end;
|
|
|
|
procedure TCATAdapter.DoBandByIndex(Idx: Integer);
|
|
begin FSyncInt := Idx; FController.Invoke(@SyncBandByIndex); end;
|
|
|
|
procedure TCATAdapter.SyncBandByIndex;
|
|
begin FController.SetBand(FSyncInt); end;
|
|
|
|
end.
|