feat: Support for Antenna Genius
This commit is contained in:
@@ -356,6 +356,70 @@ func (m *Manager) FlexDo(fn func(FlexController) error) error {
|
||||
})
|
||||
}
|
||||
|
||||
// IcomTXState is the Icom receive-DSP state surfaced to the dedicated Icom
|
||||
// control tab. Levels are 0-100 (scaled from the rig's 0-255). Unlike Flex,
|
||||
// the Icom doesn't push changes, so these reflect the last RefreshIcom() read
|
||||
// plus the optimistic updates each setter applies.
|
||||
type IcomTXState struct {
|
||||
Available bool `json:"available"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Mode string `json:"mode,omitempty"`
|
||||
AFGain int `json:"af_gain"`
|
||||
RFGain int `json:"rf_gain"`
|
||||
NB bool `json:"nb"`
|
||||
NBLevel int `json:"nb_level"`
|
||||
NR bool `json:"nr"`
|
||||
NRLevel int `json:"nr_level"`
|
||||
ANF bool `json:"anf"`
|
||||
AGC string `json:"agc,omitempty"` // FAST | MID | SLOW
|
||||
Preamp int `json:"preamp"` // 0=off, 1=P.AMP1, 2=P.AMP2
|
||||
Att int `json:"att"` // dB attenuation, 0=off
|
||||
Filter int `json:"filter"` // 1 | 2 | 3 (FIL1/2/3)
|
||||
}
|
||||
|
||||
// IcomController is an OPTIONAL backend capability (the Icom CI-V backend): the
|
||||
// receive-DSP controls shown on the Icom tab. IcomState() is mutex-guarded in
|
||||
// the backend so it's safe off the CAT goroutine; setters dispatch via IcomDo.
|
||||
type IcomController interface {
|
||||
IcomState() IcomTXState
|
||||
RefreshIcom() error // re-read all DSP state from the rig
|
||||
SetAFGain(int) error
|
||||
SetRFGain(int) error
|
||||
SetNB(bool) error
|
||||
SetNBLevel(int) error
|
||||
SetNR(bool) error
|
||||
SetNRLevel(int) error
|
||||
SetANF(bool) error
|
||||
SetAGC(string) error
|
||||
SetPreamp(int) error
|
||||
SetAtt(int) error
|
||||
SetIcomFilter(int) error
|
||||
}
|
||||
|
||||
// IcomState returns the current Icom DSP state, or (zero, false) when the active
|
||||
// backend isn't an Icom. Safe to call from any goroutine.
|
||||
func (m *Manager) IcomState() (IcomTXState, bool) {
|
||||
m.mu.RLock()
|
||||
b := m.backend
|
||||
m.mu.RUnlock()
|
||||
if ic, ok := b.(IcomController); ok {
|
||||
return ic.IcomState(), true
|
||||
}
|
||||
return IcomTXState{}, false
|
||||
}
|
||||
|
||||
// IcomDo dispatches an Icom control onto the CAT goroutine. Errors if the
|
||||
// active backend isn't an Icom.
|
||||
func (m *Manager) IcomDo(fn func(IcomController) error) error {
|
||||
return m.exec(func(b Backend) error {
|
||||
ic, ok := b.(IcomController)
|
||||
if !ok {
|
||||
return fmt.Errorf("active CAT backend is not an Icom")
|
||||
}
|
||||
return fn(ic)
|
||||
})
|
||||
}
|
||||
|
||||
// exec marshals a backend operation onto the CAT goroutine. Returns the
|
||||
// operation's error or a "busy"/"not running" error if dispatch failed.
|
||||
func (m *Manager) exec(fn func(Backend) error) error {
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
// Package civ implements the Icom CI-V protocol independently of the transport
|
||||
// carrying it. The exact same frames travel over a USB/serial port (local
|
||||
// control) and, wrapped in Icom's UDP "serial" stream, over the network
|
||||
// (remote control). Keeping the wire format in one place means the USB backend
|
||||
// (icomserial) and a future network backend (icomnet) share all of it — only
|
||||
// the transport differs.
|
||||
//
|
||||
// Frame layout: FE FE <to> <from> <cmd> [sub] [data…] FD
|
||||
package civ
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Protocol bytes.
|
||||
const (
|
||||
Pre = 0xFE // preamble (sent twice at the start of every frame)
|
||||
End = 0xFD // end-of-message
|
||||
OK = 0xFB // rig acknowledged a set command
|
||||
NG = 0xFA // rig rejected a set command
|
||||
|
||||
// AddrController is the conventional address software uses for itself.
|
||||
AddrController = 0xE0
|
||||
)
|
||||
|
||||
// Commands (the few Phase-1 control needs; more get added with the panel).
|
||||
const (
|
||||
CmdTransceiveFreq = 0x00 // unsolicited freq update (dial turned)
|
||||
CmdTransceiveMode = 0x01 // unsolicited mode update
|
||||
CmdReadFreq = 0x03
|
||||
CmdReadMode = 0x04
|
||||
CmdSetFreq = 0x05
|
||||
CmdSetMode = 0x06
|
||||
CmdPTT = 0x1C // sub 0x00 = PTT
|
||||
CmdExtra = 0x1A // sub 0x06 = data mode on modern Icoms
|
||||
CmdReadID = 0x19 // sub 0x00 = rig's own CI-V address (identifies model)
|
||||
|
||||
CmdAtt = 0x11 // attenuator (1 BCD byte of dB; 0x00 = off)
|
||||
CmdLevel = 0x14 // analogue levels (sub + 2 BCD bytes, 0000-0255)
|
||||
CmdSwitch = 0x16 // on/off + multi-state DSP settings (sub + 1 byte)
|
||||
|
||||
SubDataMode = 0x06
|
||||
SubPTT = 0x00
|
||||
|
||||
// CmdLevel sub-commands.
|
||||
SubLevelAF = 0x01 // AF (volume)
|
||||
SubLevelRF = 0x02 // RF gain
|
||||
SubLevelNR = 0x06 // noise-reduction depth
|
||||
SubLevelNB = 0x12 // noise-blanker depth
|
||||
|
||||
// CmdSwitch sub-commands.
|
||||
SubSwPreamp = 0x02 // 0=off, 1=P.AMP1, 2=P.AMP2
|
||||
SubSwAGC = 0x12 // 1=FAST, 2=MID, 3=SLOW
|
||||
SubSwNB = 0x22 // noise blanker on/off
|
||||
SubSwNR = 0x40 // noise reduction on/off
|
||||
SubSwANF = 0x41 // auto-notch on/off
|
||||
)
|
||||
|
||||
// Icom mode codes (used by CmdReadMode / CmdSetMode).
|
||||
const (
|
||||
ModeLSB = 0x00
|
||||
ModeUSB = 0x01
|
||||
ModeAM = 0x02
|
||||
ModeCW = 0x03
|
||||
ModeRTTY = 0x04
|
||||
ModeFM = 0x05
|
||||
ModeCWR = 0x07
|
||||
ModeRTTYR = 0x08
|
||||
)
|
||||
|
||||
// Frame builds a complete CI-V frame (preamble … end) for payload, which is the
|
||||
// command byte followed by any sub-command/data bytes.
|
||||
func Frame(to, from byte, payload ...byte) []byte {
|
||||
f := make([]byte, 0, len(payload)+5)
|
||||
f = append(f, Pre, Pre, to, from)
|
||||
f = append(f, payload...)
|
||||
f = append(f, End)
|
||||
return f
|
||||
}
|
||||
|
||||
// FreqToBCD encodes a frequency in Hz as the 5 little-endian BCD bytes Icom
|
||||
// expects (10 digits, 2 per byte, least-significant byte first).
|
||||
func FreqToBCD(hz int64) []byte {
|
||||
if hz < 0 {
|
||||
hz = 0
|
||||
}
|
||||
b := make([]byte, 5)
|
||||
for i := 0; i < 5; i++ {
|
||||
lo := hz % 10
|
||||
hz /= 10
|
||||
hi := hz % 10
|
||||
hz /= 10
|
||||
b[i] = byte(lo) | byte(hi)<<4
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// BCDToFreq decodes Icom little-endian BCD frequency bytes back to Hz.
|
||||
func BCDToFreq(b []byte) int64 {
|
||||
var hz int64
|
||||
mult := int64(1)
|
||||
for i := 0; i < len(b) && i < 5; i++ {
|
||||
hz += int64(b[i]&0x0F) * mult
|
||||
mult *= 10
|
||||
hz += int64(b[i]>>4) * mult
|
||||
mult *= 10
|
||||
}
|
||||
return hz
|
||||
}
|
||||
|
||||
// LevelToBCD encodes a 0-255 level as the 2 big-endian BCD bytes Icom's
|
||||
// CmdLevel commands use (e.g. 128 → 0x01 0x28, 255 → 0x02 0x55).
|
||||
func LevelToBCD(v int) []byte {
|
||||
if v < 0 {
|
||||
v = 0
|
||||
}
|
||||
if v > 255 {
|
||||
v = 255
|
||||
}
|
||||
return []byte{byte(v / 100), byte(((v/10)%10)<<4 | v%10)}
|
||||
}
|
||||
|
||||
// BCDToLevel decodes the 2 BCD bytes of a CmdLevel response back to 0-255.
|
||||
func BCDToLevel(b []byte) int {
|
||||
if len(b) < 2 {
|
||||
return 0
|
||||
}
|
||||
return int(b[0])*100 + int(b[1]>>4)*10 + int(b[1]&0x0F)
|
||||
}
|
||||
|
||||
// ByteToBCD / BCDToByte handle a single packed-BCD byte (used by the
|
||||
// attenuator, where the value is dB: 0x00, 0x06, 0x12, 0x18…).
|
||||
func ByteToBCD(v int) byte {
|
||||
if v < 0 {
|
||||
v = 0
|
||||
}
|
||||
if v > 99 {
|
||||
v = 99
|
||||
}
|
||||
return byte((v/10)<<4 | v%10)
|
||||
}
|
||||
|
||||
func BCDToByte(b byte) int { return int(b>>4)*10 + int(b&0x0F) }
|
||||
|
||||
// ModeToADIF maps an Icom mode byte (plus the data-mode flag) to an ADIF mode
|
||||
// string. Data mode on USB/LSB is surfaced as "DATA" so the app can substitute
|
||||
// the user's preferred digital mode (FT8/RTTY/…), matching the OmniRig backend.
|
||||
func ModeToADIF(m byte, data bool) string {
|
||||
switch m {
|
||||
case ModeCW, ModeCWR:
|
||||
return "CW"
|
||||
case ModeRTTY, ModeRTTYR:
|
||||
return "RTTY"
|
||||
case ModeAM:
|
||||
return "AM"
|
||||
case ModeFM:
|
||||
return "FM"
|
||||
case ModeLSB, ModeUSB:
|
||||
if data {
|
||||
return "DATA"
|
||||
}
|
||||
return "SSB"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ModelName maps a rig's default CI-V address (from CmdReadID) to a readable
|
||||
// model. Unknown addresses fall back to a hex label.
|
||||
func ModelName(addr byte) string {
|
||||
switch addr {
|
||||
case 0x94:
|
||||
return "IC-7300"
|
||||
case 0x98:
|
||||
return "IC-7610"
|
||||
case 0xA2:
|
||||
return "IC-9700"
|
||||
case 0xA4:
|
||||
return "IC-705"
|
||||
case 0x88:
|
||||
return "IC-7700"
|
||||
case 0x80:
|
||||
return "IC-7800"
|
||||
}
|
||||
return fmt.Sprintf("Icom (0x%02X)", addr)
|
||||
}
|
||||
|
||||
// Decoded is one parsed CI-V frame. Data is everything after the command byte
|
||||
// (so it still carries the sub-command for multi-byte commands like 1A 06).
|
||||
type Decoded struct {
|
||||
To byte
|
||||
From byte
|
||||
Cmd byte
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// Scan extracts every complete frame from buf and reports how many leading
|
||||
// bytes the caller may now discard. A trailing partial frame (or a lone
|
||||
// preamble byte) is left unconsumed so it can be completed by the next read.
|
||||
func Scan(buf []byte) (frames []Decoded, consumed int) {
|
||||
pos := 0
|
||||
for {
|
||||
p := indexPreamble(buf, pos)
|
||||
if p < 0 {
|
||||
// No further preamble. Keep a trailing FE (possible start of the
|
||||
// next preamble); otherwise everything seen is consumable.
|
||||
if len(buf) > 0 && buf[len(buf)-1] == Pre {
|
||||
return frames, len(buf) - 1
|
||||
}
|
||||
return frames, len(buf)
|
||||
}
|
||||
start := p + 2
|
||||
for start < len(buf) && buf[start] == Pre { // tolerate padding FEs
|
||||
start++
|
||||
}
|
||||
end := bytes.IndexByte(buf[start:], End)
|
||||
if end < 0 {
|
||||
return frames, p // incomplete frame — keep from its preamble
|
||||
}
|
||||
end += start
|
||||
if body := buf[start:end]; len(body) >= 3 {
|
||||
frames = append(frames, Decoded{
|
||||
To: body[0],
|
||||
From: body[1],
|
||||
Cmd: body[2],
|
||||
Data: append([]byte(nil), body[3:]...),
|
||||
})
|
||||
}
|
||||
pos = end + 1
|
||||
consumed = pos
|
||||
}
|
||||
}
|
||||
|
||||
// indexPreamble returns the index of the next FE FE pair at or after from.
|
||||
func indexPreamble(buf []byte, from int) int {
|
||||
for i := from; i+1 < len(buf); i++ {
|
||||
if buf[i] == Pre && buf[i+1] == Pre {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package civ
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFreqBCDRoundTrip(t *testing.T) {
|
||||
cases := []int64{0, 1, 7074000, 14250000, 28074000, 50313000, 144174000, 1296000000}
|
||||
for _, hz := range cases {
|
||||
b := FreqToBCD(hz)
|
||||
if len(b) != 5 {
|
||||
t.Fatalf("FreqToBCD(%d) len=%d, want 5", hz, len(b))
|
||||
}
|
||||
if got := BCDToFreq(b); got != hz {
|
||||
t.Errorf("round trip %d → % X → %d", hz, b, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreqBCDKnownEncoding(t *testing.T) {
|
||||
// 14.250.000 Hz → little-endian BCD 00 00 25 14 00.
|
||||
want := []byte{0x00, 0x00, 0x25, 0x14, 0x00}
|
||||
if got := FreqToBCD(14250000); !bytes.Equal(got, want) {
|
||||
t.Errorf("FreqToBCD(14250000) = % X, want % X", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrame(t *testing.T) {
|
||||
// Read-frequency request to a 7610 (0x98) from the controller (0xE0).
|
||||
got := Frame(0x98, AddrController, CmdReadFreq)
|
||||
want := []byte{0xFE, 0xFE, 0x98, 0xE0, 0x03, 0xFD}
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Errorf("Frame = % X, want % X", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanSingleFreqResponse(t *testing.T) {
|
||||
// Rig (0x98) → controller (0xE0): freq read response for 14.250 MHz.
|
||||
in := Frame(AddrController, 0x98, CmdReadFreq, 0x00, 0x00, 0x25, 0x14, 0x00)
|
||||
frames, consumed := Scan(in)
|
||||
if consumed != len(in) {
|
||||
t.Fatalf("consumed=%d, want %d", consumed, len(in))
|
||||
}
|
||||
if len(frames) != 1 {
|
||||
t.Fatalf("got %d frames, want 1", len(frames))
|
||||
}
|
||||
f := frames[0]
|
||||
if f.From != 0x98 || f.To != AddrController || f.Cmd != CmdReadFreq {
|
||||
t.Errorf("addrs/cmd wrong: %+v", f)
|
||||
}
|
||||
if hz := BCDToFreq(f.Data); hz != 14250000 {
|
||||
t.Errorf("decoded freq %d, want 14250000", hz)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanSkipsEchoAndKeepsPartial(t *testing.T) {
|
||||
echo := Frame(0x98, AddrController, CmdReadFreq) // our outgoing (echoed back)
|
||||
resp := Frame(AddrController, 0x98, CmdReadMode, ModeCW, 0x01) // a real response
|
||||
buf := append(append([]byte{}, echo...), resp...)
|
||||
buf = append(buf, 0xFE, 0xFE, 0x98) // a partial third frame (no FD yet)
|
||||
|
||||
frames, consumed := Scan(buf)
|
||||
if len(frames) != 2 {
|
||||
t.Fatalf("got %d frames, want 2", len(frames))
|
||||
}
|
||||
// The partial frame must be left unconsumed so the next read can finish it.
|
||||
if consumed != len(echo)+len(resp) {
|
||||
t.Errorf("consumed=%d, want %d (partial frame retained)", consumed, len(echo)+len(resp))
|
||||
}
|
||||
if frames[1].Cmd != CmdReadMode || len(frames[1].Data) < 1 || frames[1].Data[0] != ModeCW {
|
||||
t.Errorf("second frame wrong: %+v", frames[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestModeToADIF(t *testing.T) {
|
||||
cases := []struct {
|
||||
m byte
|
||||
data bool
|
||||
want string
|
||||
}{
|
||||
{ModeUSB, false, "SSB"},
|
||||
{ModeLSB, false, "SSB"},
|
||||
{ModeUSB, true, "DATA"},
|
||||
{ModeCW, false, "CW"},
|
||||
{ModeCWR, false, "CW"},
|
||||
{ModeRTTY, false, "RTTY"},
|
||||
{ModeAM, false, "AM"},
|
||||
{ModeFM, false, "FM"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := ModeToADIF(c.m, c.data); got != c.want {
|
||||
t.Errorf("ModeToADIF(0x%02X, %v) = %q, want %q", c.m, c.data, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLevelBCDRoundTrip(t *testing.T) {
|
||||
for _, v := range []int{0, 1, 50, 99, 100, 128, 200, 255} {
|
||||
b := LevelToBCD(v)
|
||||
if len(b) != 2 {
|
||||
t.Fatalf("LevelToBCD(%d) len=%d", v, len(b))
|
||||
}
|
||||
if got := BCDToLevel(b); got != v {
|
||||
t.Errorf("level round trip %d → % X → %d", v, b, got)
|
||||
}
|
||||
}
|
||||
// Known encodings from the Icom CI-V reference.
|
||||
if got := LevelToBCD(128); !bytes.Equal(got, []byte{0x01, 0x28}) {
|
||||
t.Errorf("LevelToBCD(128) = % X, want 01 28", got)
|
||||
}
|
||||
if got := LevelToBCD(255); !bytes.Equal(got, []byte{0x02, 0x55}) {
|
||||
t.Errorf("LevelToBCD(255) = % X, want 02 55", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestByteBCDRoundTrip(t *testing.T) {
|
||||
for _, v := range []int{0, 6, 12, 18, 21} {
|
||||
if got := BCDToByte(ByteToBCD(v)); got != v {
|
||||
t.Errorf("byte BCD round trip %d → %d", v, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelName(t *testing.T) {
|
||||
if got := ModelName(0x98); got != "IC-7610" {
|
||||
t.Errorf("ModelName(0x98) = %q, want IC-7610", got)
|
||||
}
|
||||
if got := ModelName(0x12); got != "Icom (0x12)" {
|
||||
t.Errorf("ModelName(0x12) = %q, want fallback", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,557 @@
|
||||
package cat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"hamlog/internal/cat/civ"
|
||||
|
||||
"go.bug.st/serial"
|
||||
)
|
||||
|
||||
// IcomSerial controls an Icom transceiver over its USB/serial CI-V port (local
|
||||
// control). It speaks the shared civ protocol, so when the network backend
|
||||
// (icomnet) is added it will reuse the same encode/decode — only the transport
|
||||
// changes. Implements Backend; all methods run on the Manager's CAT goroutine,
|
||||
// so the port is accessed single-threaded (no locking needed).
|
||||
type IcomSerial struct {
|
||||
portName string
|
||||
baud int
|
||||
rigAddr byte // rig's CI-V address (IC-7610 default 0x98)
|
||||
digital string // mode to command for DATA (FT8/RTTY/…)
|
||||
|
||||
port serial.Port
|
||||
rx []byte // accumulated bytes awaiting a complete frame
|
||||
model string
|
||||
|
||||
curFreq int64 // last frequency read (for sideband choice)
|
||||
curModeByte byte // last raw Icom mode byte (for filter re-send)
|
||||
lastSetFreq int64 // last frequency commanded (spot click: freq then mode)
|
||||
lastSetFreqAt time.Time
|
||||
|
||||
// dsp caches the receive-DSP state for the Icom control tab. Read off the
|
||||
// CAT goroutine via IcomState(), written on the CAT goroutine (RefreshIcom
|
||||
// / setters) — hence the mutex.
|
||||
dspMu sync.Mutex
|
||||
dsp IcomTXState
|
||||
}
|
||||
|
||||
const (
|
||||
icomReadTimeout = 350 * time.Millisecond // wait for a poll response
|
||||
icomCmdTimeout = 400 * time.Millisecond // wait for a set ack (FB/FA)
|
||||
)
|
||||
|
||||
// NewIcomSerial builds an (unconnected) Icom serial backend. baud defaults to
|
||||
// 115200, rig address to the IC-7610's 0x98 when out of range.
|
||||
func NewIcomSerial(portName string, baud, civAddr int, digitalDefault string) *IcomSerial {
|
||||
if baud <= 0 {
|
||||
baud = 115200
|
||||
}
|
||||
if civAddr <= 0 || civAddr > 0xFF {
|
||||
civAddr = 0x98 // IC-7610
|
||||
}
|
||||
if digitalDefault == "" {
|
||||
digitalDefault = "FT8"
|
||||
}
|
||||
return &IcomSerial{
|
||||
portName: portName,
|
||||
baud: baud,
|
||||
rigAddr: byte(civAddr),
|
||||
digital: strings.ToUpper(digitalDefault),
|
||||
model: "Icom",
|
||||
}
|
||||
}
|
||||
|
||||
func (b *IcomSerial) Name() string { return "icom" }
|
||||
|
||||
func (b *IcomSerial) Connect() error {
|
||||
if b.portName == "" {
|
||||
return fmt.Errorf("no serial port configured")
|
||||
}
|
||||
port, err := serial.Open(b.portName, &serial.Mode{BaudRate: b.baud})
|
||||
if err != nil {
|
||||
return fmt.Errorf("open %s @ %d baud: %w", b.portName, b.baud, err)
|
||||
}
|
||||
// Short read timeout so recv() polls in a tight loop without blocking the
|
||||
// CAT goroutine when the rig is silent.
|
||||
_ = port.SetReadTimeout(60 * time.Millisecond)
|
||||
b.port = port
|
||||
b.rx = b.rx[:0]
|
||||
b.model = civ.ModelName(b.rigAddr)
|
||||
|
||||
// Best-effort model identification: ask the rig for its own CI-V address.
|
||||
if err := b.write(civ.CmdReadID, civ.SubPTT); err == nil {
|
||||
if f, err := b.recv(icomReadTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.CmdReadID && len(d.Data) >= 2 && d.Data[0] == 0x00
|
||||
}); err == nil {
|
||||
b.model = civ.ModelName(f.Data[1])
|
||||
}
|
||||
}
|
||||
b.readDSP() // best-effort initial snapshot for the control tab
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) Disconnect() {
|
||||
if b.port != nil {
|
||||
_ = b.port.Close()
|
||||
b.port = nil
|
||||
}
|
||||
}
|
||||
|
||||
// ReadState polls the rig for frequency and mode. A failed frequency read is
|
||||
// treated as "lost the rig" so the Manager reconnects.
|
||||
func (b *IcomSerial) ReadState() (RigState, error) {
|
||||
if b.port == nil {
|
||||
return RigState{}, fmt.Errorf("not connected")
|
||||
}
|
||||
s := RigState{Backend: b.Name(), Connected: true, Rig: b.model}
|
||||
|
||||
hz, err := b.readFreq()
|
||||
if err != nil {
|
||||
return RigState{}, err
|
||||
}
|
||||
s.FreqHz = hz
|
||||
b.curFreq = hz
|
||||
|
||||
if m, ok := b.readMode(); ok {
|
||||
b.curModeByte = m
|
||||
data := b.readDataMode() // best-effort; ignored on failure
|
||||
s.Mode = civ.ModeToADIF(m, data)
|
||||
if s.Mode == "DATA" {
|
||||
s.Mode = b.digital
|
||||
}
|
||||
b.dspMu.Lock()
|
||||
b.dsp.Mode = s.Mode
|
||||
b.dspMu.Unlock()
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetFrequency(hz int64) error {
|
||||
if hz <= 0 {
|
||||
return fmt.Errorf("invalid frequency")
|
||||
}
|
||||
b.lastSetFreq, b.lastSetFreqAt = hz, time.Now()
|
||||
return b.exec(append([]byte{civ.CmdSetFreq}, civ.FreqToBCD(hz)...)...)
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetMode(mode string) error {
|
||||
code, data, err := b.modeCode(mode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the base mode (keeping the rig's current filter by sending only the
|
||||
// mode byte), then set the data-mode flag for digital modes.
|
||||
if err := b.exec(civ.CmdSetMode, code); err != nil {
|
||||
return err
|
||||
}
|
||||
dataByte := byte(0)
|
||||
if data {
|
||||
dataByte = 1
|
||||
}
|
||||
// Filter 0x01 (FIL1) is the conventional default for the data-mode set.
|
||||
_ = b.exec(civ.CmdExtra, civ.SubDataMode, dataByte, 0x01)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetPTT(on bool) error {
|
||||
state := byte(0)
|
||||
if on {
|
||||
state = 1
|
||||
}
|
||||
return b.exec(civ.CmdPTT, civ.SubPTT, state)
|
||||
}
|
||||
|
||||
// ── helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
func (b *IcomSerial) write(payload ...byte) error {
|
||||
_, err := b.port.Write(civ.Frame(b.rigAddr, civ.AddrController, payload...))
|
||||
return err
|
||||
}
|
||||
|
||||
// recv reads from the port until a frame from the rig satisfies match or the
|
||||
// timeout elapses. Frames that are our own echo (from == controller) or don't
|
||||
// match are discarded.
|
||||
func (b *IcomSerial) recv(timeout time.Duration, match func(civ.Decoded) bool) (civ.Decoded, error) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
tmp := make([]byte, 256)
|
||||
for time.Now().Before(deadline) {
|
||||
n, err := b.port.Read(tmp)
|
||||
if err != nil {
|
||||
return civ.Decoded{}, err
|
||||
}
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
b.rx = append(b.rx, tmp[:n]...)
|
||||
frames, consumed := civ.Scan(b.rx)
|
||||
if consumed > 0 {
|
||||
b.rx = append(b.rx[:0], b.rx[consumed:]...)
|
||||
}
|
||||
for _, f := range frames {
|
||||
if f.From != b.rigAddr {
|
||||
continue // skip echo of our own commands
|
||||
}
|
||||
if match(f) {
|
||||
return f, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return civ.Decoded{}, fmt.Errorf("icom: timeout waiting for response")
|
||||
}
|
||||
|
||||
// exec sends a set command and waits for the rig's OK (FB) / NG (FA) ack.
|
||||
func (b *IcomSerial) exec(payload ...byte) error {
|
||||
if err := b.write(payload...); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := b.recv(icomCmdTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.OK || d.Cmd == civ.NG
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if f.Cmd == civ.NG {
|
||||
return fmt.Errorf("icom: rig rejected command 0x%02X", payload[0])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) readFreq() (int64, error) {
|
||||
if err := b.write(civ.CmdReadFreq); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
f, err := b.recv(icomReadTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.CmdReadFreq || d.Cmd == civ.CmdTransceiveFreq
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return civ.BCDToFreq(f.Data), nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) readMode() (byte, bool) {
|
||||
if err := b.write(civ.CmdReadMode); err != nil {
|
||||
return 0, false
|
||||
}
|
||||
f, err := b.recv(icomReadTimeout, func(d civ.Decoded) bool {
|
||||
return (d.Cmd == civ.CmdReadMode || d.Cmd == civ.CmdTransceiveMode) && len(d.Data) >= 1
|
||||
})
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return f.Data[0], true
|
||||
}
|
||||
|
||||
func (b *IcomSerial) readDataMode() bool {
|
||||
if err := b.write(civ.CmdExtra, civ.SubDataMode); err != nil {
|
||||
return false
|
||||
}
|
||||
f, err := b.recv(icomReadTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.CmdExtra && len(d.Data) >= 2 && d.Data[0] == civ.SubDataMode
|
||||
})
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return f.Data[1] != 0
|
||||
}
|
||||
|
||||
// modeCode maps an ADIF mode to an Icom mode byte plus whether the data-mode
|
||||
// flag should be set. SSB sideband follows the usual convention (LSB below
|
||||
// 10 MHz, USB above); the frequency just commanded is preferred over the last
|
||||
// poll so a clicked spot (freq then mode) picks the right sideband immediately.
|
||||
func (b *IcomSerial) modeCode(mode string) (code byte, data bool, err error) {
|
||||
freq := b.curFreq
|
||||
if b.lastSetFreq > 0 && time.Since(b.lastSetFreqAt) < 5*time.Second {
|
||||
freq = b.lastSetFreq
|
||||
}
|
||||
usb := byte(civ.ModeUSB)
|
||||
if freq > 0 && freq < 10_000_000 {
|
||||
usb = civ.ModeLSB
|
||||
}
|
||||
switch strings.ToUpper(strings.TrimSpace(mode)) {
|
||||
case "CW":
|
||||
return civ.ModeCW, false, nil
|
||||
case "SSB":
|
||||
return usb, false, nil
|
||||
case "AM":
|
||||
return civ.ModeAM, false, nil
|
||||
case "FM":
|
||||
return civ.ModeFM, false, nil
|
||||
case "RTTY", "FSK":
|
||||
return civ.ModeRTTY, false, nil
|
||||
case "FT8", "FT4", "PSK31", "MFSK", "JS8", "JT65", "JT9", "OLIVIA", "DATA", "DIGITALVOICE":
|
||||
// Digital data modes ride on USB with the data flag set (FT8 etc.).
|
||||
return civ.ModeUSB, true, nil
|
||||
}
|
||||
return 0, false, fmt.Errorf("icom: unsupported mode %q", mode)
|
||||
}
|
||||
|
||||
// ── IcomController: receive-DSP controls for the Icom tab ───────────────────
|
||||
|
||||
func (b *IcomSerial) IcomState() IcomTXState {
|
||||
b.dspMu.Lock()
|
||||
defer b.dspMu.Unlock()
|
||||
return b.dsp
|
||||
}
|
||||
|
||||
// RefreshIcom re-reads the whole DSP snapshot from the rig. Runs on the CAT
|
||||
// goroutine (dispatched via IcomDo).
|
||||
func (b *IcomSerial) RefreshIcom() error {
|
||||
if b.port == nil {
|
||||
return fmt.Errorf("not connected")
|
||||
}
|
||||
b.readDSP()
|
||||
return nil
|
||||
}
|
||||
|
||||
// readDSP polls every DSP value once and replaces the cache. Best-effort: a
|
||||
// value the rig doesn't answer keeps its previous cached value rather than
|
||||
// stalling (each read has a short timeout).
|
||||
func (b *IcomSerial) readDSP() {
|
||||
st := IcomTXState{Available: true, Model: b.model}
|
||||
b.dspMu.Lock()
|
||||
st.Mode = b.dsp.Mode // preserve mode (set by ReadState)
|
||||
b.dspMu.Unlock()
|
||||
|
||||
if v, ok := b.readLevel(civ.SubLevelAF); ok {
|
||||
st.AFGain = from255(v)
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelRF); ok {
|
||||
st.RFGain = from255(v)
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelNR); ok {
|
||||
st.NRLevel = from255(v)
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelNB); ok {
|
||||
st.NBLevel = from255(v)
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwNB); ok {
|
||||
st.NB = v != 0
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwNR); ok {
|
||||
st.NR = v != 0
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwANF); ok {
|
||||
st.ANF = v != 0
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwAGC); ok {
|
||||
st.AGC = agcName(v)
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwPreamp); ok {
|
||||
st.Preamp = int(v)
|
||||
}
|
||||
if v, ok := b.readAtt(); ok {
|
||||
st.Att = v
|
||||
}
|
||||
if _, f, ok := b.readModeFilter(); ok {
|
||||
st.Filter = int(f)
|
||||
}
|
||||
|
||||
b.dspMu.Lock()
|
||||
b.dsp = st
|
||||
b.dspMu.Unlock()
|
||||
}
|
||||
|
||||
const icomDSPTimeout = 150 * time.Millisecond // shorter: unsupported reads mustn't stall the poll
|
||||
|
||||
func (b *IcomSerial) readLevel(sub byte) (int, bool) {
|
||||
if err := b.write(civ.CmdLevel, sub); err != nil {
|
||||
return 0, false
|
||||
}
|
||||
f, err := b.recv(icomDSPTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.CmdLevel && len(d.Data) >= 3 && d.Data[0] == sub
|
||||
})
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return civ.BCDToLevel(f.Data[1:3]), true
|
||||
}
|
||||
|
||||
func (b *IcomSerial) readSwitch(sub byte) (byte, bool) {
|
||||
if err := b.write(civ.CmdSwitch, sub); err != nil {
|
||||
return 0, false
|
||||
}
|
||||
f, err := b.recv(icomDSPTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.CmdSwitch && len(d.Data) >= 2 && d.Data[0] == sub
|
||||
})
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return f.Data[1], true
|
||||
}
|
||||
|
||||
func (b *IcomSerial) readAtt() (int, bool) {
|
||||
if err := b.write(civ.CmdAtt); err != nil {
|
||||
return 0, false
|
||||
}
|
||||
f, err := b.recv(icomDSPTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.CmdAtt && len(d.Data) >= 1
|
||||
})
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return civ.BCDToByte(f.Data[0]), true
|
||||
}
|
||||
|
||||
func (b *IcomSerial) readModeFilter() (mode, filter byte, ok bool) {
|
||||
if err := b.write(civ.CmdReadMode); err != nil {
|
||||
return 0, 0, false
|
||||
}
|
||||
f, err := b.recv(icomDSPTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.CmdReadMode && len(d.Data) >= 2
|
||||
})
|
||||
if err != nil {
|
||||
return 0, 0, false
|
||||
}
|
||||
return f.Data[0], f.Data[1], true
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetAFGain(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelAF}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.AFGain = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetRFGain(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelRF}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.RFGain = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetNB(on bool) error {
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwNB, boolByte(on)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.NB = on })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetNBLevel(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelNB}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.NBLevel = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetNR(on bool) error {
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwNR, boolByte(on)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.NR = on })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetNRLevel(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelNR}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.NRLevel = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetANF(on bool) error {
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwANF, boolByte(on)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.ANF = on })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetAGC(name string) error {
|
||||
v := agcValue(name)
|
||||
if v == 0 {
|
||||
return fmt.Errorf("icom: invalid AGC %q", name)
|
||||
}
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwAGC, v); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.AGC = strings.ToUpper(name) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetPreamp(n int) error {
|
||||
if n < 0 || n > 2 {
|
||||
return fmt.Errorf("icom: invalid preamp %d", n)
|
||||
}
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwPreamp, byte(n)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.Preamp = n })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetAtt(db int) error {
|
||||
if err := b.exec(civ.CmdAtt, civ.ByteToBCD(db)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.Att = db })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetIcomFilter(n int) error {
|
||||
if n < 1 || n > 3 {
|
||||
return fmt.Errorf("icom: invalid filter %d", n)
|
||||
}
|
||||
if b.curModeByte == 0 {
|
||||
// Need the current mode to re-send with the chosen filter.
|
||||
if m, _, ok := b.readModeFilter(); ok {
|
||||
b.curModeByte = m
|
||||
}
|
||||
}
|
||||
if err := b.exec(civ.CmdSetMode, b.curModeByte, byte(n)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.Filter = n })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) setCache(fn func(*IcomTXState)) {
|
||||
b.dspMu.Lock()
|
||||
fn(&b.dsp)
|
||||
b.dspMu.Unlock()
|
||||
}
|
||||
|
||||
// ── small helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
func to255(p int) int { return clampPct(p) * 255 / 100 }
|
||||
func from255(v int) int { return (v*100 + 127) / 255 }
|
||||
func clampPct(p int) int { return min(100, max(0, p)) }
|
||||
|
||||
func boolByte(on bool) byte {
|
||||
if on {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func agcName(v byte) string {
|
||||
switch v {
|
||||
case 1:
|
||||
return "FAST"
|
||||
case 2:
|
||||
return "MID"
|
||||
case 3:
|
||||
return "SLOW"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func agcValue(name string) byte {
|
||||
switch strings.ToUpper(strings.TrimSpace(name)) {
|
||||
case "FAST":
|
||||
return 1
|
||||
case "MID":
|
||||
return 2
|
||||
case "SLOW":
|
||||
return 3
|
||||
}
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user