From 3075cf3896fb93c7788e6e1756dbd6a0524cb429 Mon Sep 17 00:00:00 2001 From: Nonoo Date: Wed, 4 Nov 2020 09:41:36 +0100 Subject: [PATCH] Add RF gain display/adjustment --- README.md | 1 + civcontrol.go | 47 ++++++++++++++++++++++++++++++++++++++++------- hotkeys.go | 12 ++++++++++++ statuslog.go | 17 ++++++++++++++++- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3fd1f3a..b996257 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ is up) with the following info: - First status bar line: - `S meter`: periodically refreshed S meter value, OVF is displayed on overflow + - `rfg`: RF gain in percent - Second status bar line: - `state`: RX/TX/TUNE depending on the PTT status diff --git a/civcontrol.go b/civcontrol.go index ba597d0..3a9e367 100644 --- a/civcontrol.go +++ b/civcontrol.go @@ -73,6 +73,7 @@ type civControlStruct struct { ptt bool tune bool pwrPercent int + rfGainPercent int operatingModeIdx int filterIdx int dataMode bool @@ -107,7 +108,7 @@ func (s *civControlStruct) decode(d []byte) { case 0x1a: s.decodeDataModeAndOVF(payload) case 0x14: - s.decodePower(payload) + s.decodePowerAndRFGain(payload) case 0x1c: s.decodeTransmitStatus(payload) case 0x15: @@ -249,15 +250,21 @@ func (s *civControlStruct) decodeDataModeAndOVF(d []byte) { } } -func (s *civControlStruct) decodePower(d []byte) { - if len(d) < 3 || d[0] != 0x0a { +func (s *civControlStruct) decodePowerAndRFGain(d []byte) { + if len(d) < 3 { return } - hex := uint16(d[1])<<8 | uint16(d[2]) - s.state.pwrPercent = int(math.Round((float64(hex) / 0x0255) * 100)) - - statusLog.reportTxPower(s.state.pwrPercent) + switch d[0] { + case 0x02: + hex := uint16(d[1])<<8 | uint16(d[2]) + s.state.rfGainPercent = int(math.Round((float64(hex) / 0x0255) * 100)) + statusLog.reportRFGain(s.state.rfGainPercent) + case 0x0a: + hex := uint16(d[1])<<8 | uint16(d[2]) + s.state.pwrPercent = int(math.Round((float64(hex) / 0x0255) * 100)) + statusLog.reportTxPower(s.state.pwrPercent) + } } func (s *civControlStruct) decodeTransmitStatus(d []byte) { @@ -367,6 +374,25 @@ func (s *civControlStruct) decPwr() error { return nil } +func (s *civControlStruct) setRFGain(percent int) error { + v := uint16(0x0255 * (float64(percent) / 100)) + return s.st.send([]byte{254, 254, civAddress, 224, 0x14, 0x02, byte(v >> 8), byte(v & 0xff), 253}) +} + +func (s *civControlStruct) incRFGain() error { + if s.state.rfGainPercent < 100 { + return s.setRFGain(s.state.rfGainPercent + 1) + } + return nil +} + +func (s *civControlStruct) decRFGain() error { + if s.state.rfGainPercent > 0 { + return s.setRFGain(s.state.rfGainPercent - 1) + } + return nil +} + func (s *civControlStruct) getDigit(v uint, n int) byte { f := float64(v) for n > 0 { @@ -575,6 +601,10 @@ func (s *civControlStruct) getTS() error { return s.st.send([]byte{254, 254, civAddress, 224, 0x10, 253}) } +func (s *civControlStruct) getRFGain() error { + return s.st.send([]byte{254, 254, civAddress, 224, 0x14, 0x02, 253}) +} + func (s *civControlStruct) loop() { for { select { @@ -624,6 +654,9 @@ func (s *civControlStruct) init(st *serialStream) error { if err := s.getTS(); err != nil { return err } + if err := s.getRFGain(); err != nil { + return err + } s.deinitNeeded = make(chan bool) s.deinitFinished = make(chan bool) diff --git a/hotkeys.go b/hotkeys.go index c2b86a0..4bc2fd3 100644 --- a/hotkeys.go +++ b/hotkeys.go @@ -24,6 +24,18 @@ func handleHotkey(k byte) { log.Error("can't decrease power: ", err) } } + case '\'': + if civControl != nil { + if err := civControl.incRFGain(); err != nil { + log.Error("can't increase rf gain: ", err) + } + } + case ';': + if civControl != nil { + if err := civControl.decRFGain(); err != nil { + log.Error("can't decrease rf gain: ", err) + } + } case ']': if civControl != nil { if err := civControl.incFreq(); err != nil { diff --git a/statuslog.go b/statuslog.go index fc18799..f614d9b 100644 --- a/statuslog.go +++ b/statuslog.go @@ -23,6 +23,7 @@ type statusLogData struct { preamp string vd string txPower string + rfGain string s string ovf bool ts string @@ -228,6 +229,16 @@ func (s *statusLogStruct) reportTxPower(percent int) { s.data.txPower = fmt.Sprint(percent, "%") } +func (s *statusLogStruct) reportRFGain(percent int) { + s.mutex.Lock() + defer s.mutex.Unlock() + + if s.data == nil { + return + } + s.data.rfGain = fmt.Sprint(percent, "%") +} + func (s *statusLogStruct) clearInternal() { fmt.Printf("%c[2K", 27) } @@ -266,7 +277,11 @@ func (s *statusLogStruct) update() { if s.data.ovf { ovfStr = " " + s.preGenerated.ovf } - s.data.line1 = fmt.Sprint(s.data.s, ovfStr) + var rfGainStr string + if s.data.rfGain != "" { + rfGainStr = " rfg " + s.data.rfGain + } + s.data.line1 = fmt.Sprint(s.data.s, ovfStr, rfGainStr) var tsStr string if s.data.ts != "" {