From 27a86293bf3718008d31d1e7225d628cbe72288d Mon Sep 17 00:00:00 2001 From: Nonoo Date: Fri, 30 Oct 2020 23:06:34 +0100 Subject: [PATCH] Add TX power adjustment --- README.md | 2 ++ civcontrol.go | 30 ++++++++++++++++++++++++++---- keyboard-linux.go | 12 ++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3310f14..b3a06a8 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,8 @@ not available. You can transmit your own voice using a mic attached to your computer for example. - `t`: toggles the tune process +- `+`: increase TX power +- `-`: decrease TX power ## Authors diff --git a/civcontrol.go b/civcontrol.go index c074ab4..727d731 100644 --- a/civcontrol.go +++ b/civcontrol.go @@ -11,8 +11,9 @@ type civControlStruct struct { st *serialStream state struct { - ptt bool - tune bool + ptt bool + tune bool + pwrPercent int } } @@ -129,9 +130,9 @@ func (s *civControlStruct) decodePower(d []byte) { } hex := uint16(d[1])<<8 | uint16(d[2]) - percent := int(math.Round((float64(hex) / 0x0255) * 100)) + s.state.pwrPercent = int(math.Round((float64(hex) / 0x0255) * 100)) - statusLog.reportTxPower(percent) + statusLog.reportTxPower(s.state.pwrPercent) } func (s *civControlStruct) decodeTransmitStatus(d []byte) { @@ -161,6 +162,27 @@ func (s *civControlStruct) decodeTransmitStatus(d []byte) { statusLog.reportPTT(s.state.ptt, s.state.tune) } +func (s *civControlStruct) setPwr(percent int) error { + v := uint16(0x0255 * (float64(percent) / 100)) + return s.st.send([]byte{254, 254, civAddress, 224, 0x14, 0x0a, byte(v >> 8), byte(v & 0xff), 253}) +} + +func (s *civControlStruct) incPwr() error { + if s.state.pwrPercent < 100 { + s.state.pwrPercent++ + return s.setPwr(s.state.pwrPercent) + } + return nil +} + +func (s *civControlStruct) decPwr() error { + if s.state.pwrPercent > 0 { + s.state.pwrPercent-- + return s.setPwr(s.state.pwrPercent) + } + return nil +} + func (s *civControlStruct) setPTT(enable bool) error { var b byte if enable { diff --git a/keyboard-linux.go b/keyboard-linux.go index 590c559..ebe2d9e 100644 --- a/keyboard-linux.go +++ b/keyboard-linux.go @@ -24,6 +24,18 @@ func (s *keyboardStruct) handleKey(k byte) { log.Error("can't toggle tune: ", err) } } + case '+': + if civControl != nil { + if err := civControl.incPwr(); err != nil { + log.Error("can't increase power: ", err) + } + } + case '-': + if civControl != nil { + if err := civControl.decPwr(); err != nil { + log.Error("can't decrease power: ", err) + } + } } }