diff --git a/civcontrol.go b/civcontrol.go index badd55c..de769b5 100644 --- a/civcontrol.go +++ b/civcontrol.go @@ -69,9 +69,27 @@ type civControlStruct struct { resetSReadTimer chan bool state struct { - getSSent bool - getOVFSent bool - getSWRSent bool + getFreqSent bool + getModeSent bool + getDataModeSent bool + getSSent bool + getOVFSent bool + getSWRSent bool + getTransmitStatusSent bool + getVdSent bool + + setPwrSent bool + setRFGainSent bool + setSQLSent bool + setNRSent bool + setFreqSent bool + setModeSent bool + setPTTSent bool + setTuneSent bool + setDataModeSent bool + setPreampSent bool + setNREnabledSent bool + setTSSent bool freq uint ptt bool @@ -104,30 +122,38 @@ func (s *civControlStruct) decode(d []byte) bool { switch d[4] { case 0x00: - s.decodeFreq(payload) + return s.decodeFreq(payload) case 0x01: - s.decodeMode(payload) + return s.decodeMode(payload) case 0x03: - s.decodeFreq(payload) + return s.decodeFreq(payload) case 0x04: - s.decodeMode(payload) + return s.decodeMode(payload) + case 0x05: + return s.decodeFreq(payload) + case 0x06: + return s.decodeMode(payload) case 0x10: - s.decodeTS(payload) + return s.decodeTS(payload) case 0x1a: return s.decodeDataModeAndOVF(payload) case 0x14: - s.decodePowerRFGainSQLNR(payload) + return s.decodePowerRFGainSQLNR(payload) case 0x1c: - s.decodeTransmitStatus(payload) + return s.decodeTransmitStatus(payload) case 0x15: return s.decodeVdSWRS(payload) case 0x16: - s.decodePreampAndNR(payload) + return s.decodePreampAndNR(payload) } return true } -func (s *civControlStruct) decodeFreq(d []byte) { +func (s *civControlStruct) decodeFreq(d []byte) bool { + if len(d) < 2 { + return !s.state.getFreqSent && !s.state.setFreqSent + } + var f uint var pos int for _, v := range d { @@ -138,9 +164,7 @@ func (s *civControlStruct) decodeFreq(d []byte) { f += uint(s2) * uint(math.Pow(10, float64(pos))) pos++ } - if f == 0 { - return - } + s.state.freq = f statusLog.reportFrequency(s.state.freq) @@ -152,6 +176,16 @@ func (s *civControlStruct) decodeFreq(d []byte) { break } } + + if s.state.getFreqSent { + s.state.getFreqSent = false + return false + } + if s.state.setFreqSent { + s.state.setFreqSent = false + return false + } + return true } func (s *civControlStruct) decodeFilterValueToFilterIdx(v byte) int { @@ -163,9 +197,9 @@ func (s *civControlStruct) decodeFilterValueToFilterIdx(v byte) int { return -1 } -func (s *civControlStruct) decodeMode(d []byte) { +func (s *civControlStruct) decodeMode(d []byte) bool { if len(d) < 1 { - return + return !s.state.getModeSent && !s.state.setModeSent } var mode string @@ -184,13 +218,20 @@ func (s *civControlStruct) decodeMode(d []byte) { } statusLog.reportMode(mode, filter) - // The transceiver does not send the data mode setting automatically. - _ = s.getDataMode() + if s.state.getModeSent { + s.state.getModeSent = false + return false + } + if s.state.setModeSent { + s.state.setModeSent = false + return false + } + return true } -func (s *civControlStruct) decodeTS(d []byte) { +func (s *civControlStruct) decodeTS(d []byte) bool { if len(d) < 1 { - return + return !s.state.setTSSent } s.state.tsValue = d[0] @@ -226,17 +267,19 @@ func (s *civControlStruct) decodeTS(d []byte) { s.state.ts = 100000 } statusLog.reportTS(s.state.ts) + + if s.state.setTSSent { + s.state.setTSSent = false + return false + } + return true } func (s *civControlStruct) decodeDataModeAndOVF(d []byte) bool { - if len(d) < 1 { - return true - } - switch d[0] { case 0x06: if len(d) < 3 { - return true + return !s.state.getDataModeSent && !s.state.setDataModeSent } var dataMode string var filter string @@ -250,6 +293,15 @@ func (s *civControlStruct) decodeDataModeAndOVF(d []byte) bool { } statusLog.reportDataMode(dataMode, filter) + + if s.state.getDataModeSent { + s.state.getDataModeSent = false + return false + } + if s.state.setDataModeSent { + s.state.setDataModeSent = false + return false + } case 0x09: if len(d) < 2 { return !s.state.getOVFSent @@ -267,34 +319,59 @@ func (s *civControlStruct) decodeDataModeAndOVF(d []byte) bool { return true } -func (s *civControlStruct) decodePowerRFGainSQLNR(d []byte) { - if len(d) < 3 { - return - } - +func (s *civControlStruct) decodePowerRFGainSQLNR(d []byte) bool { switch d[0] { case 0x02: + if len(d) < 3 { + return !s.state.setRFGainSent + } hex := uint16(d[1])<<8 | uint16(d[2]) s.state.rfGainPercent = int(math.Round((float64(hex) / 0x0255) * 100)) statusLog.reportRFGain(s.state.rfGainPercent) + if s.state.setRFGainSent { + s.state.setRFGainSent = false + return false + } case 0x03: + if len(d) < 3 { + return !s.state.setSQLSent + } hex := uint16(d[1])<<8 | uint16(d[2]) s.state.sqlPercent = int(math.Round((float64(hex) / 0x0255) * 100)) statusLog.reportSQL(s.state.sqlPercent) + if s.state.setSQLSent { + s.state.setSQLSent = false + return false + } case 0x06: + if len(d) < 3 { + return !s.state.setNRSent + } hex := uint16(d[1])<<8 | uint16(d[2]) s.state.nrPercent = int(math.Round((float64(hex) / 0x0255) * 100)) statusLog.reportNR(s.state.nrPercent) + if s.state.setNRSent { + s.state.setNRSent = false + return false + } case 0x0a: + if len(d) < 3 { + return !s.state.setPwrSent + } hex := uint16(d[1])<<8 | uint16(d[2]) s.state.pwrPercent = int(math.Round((float64(hex) / 0x0255) * 100)) statusLog.reportTxPower(s.state.pwrPercent) + if s.state.setPwrSent { + s.state.setPwrSent = false + return false + } } + return true } -func (s *civControlStruct) decodeTransmitStatus(d []byte) { +func (s *civControlStruct) decodeTransmitStatus(d []byte) bool { if len(d) < 2 { - return + return !s.state.getTransmitStatusSent && !s.state.setPTTSent } switch d[0] { @@ -307,6 +384,11 @@ func (s *civControlStruct) decodeTransmitStatus(d []byte) { _ = s.getVd() } } + statusLog.reportPTT(s.state.ptt, s.state.tune) + if s.state.setPTTSent { + s.state.setPTTSent = false + return false + } case 1: if d[1] == 2 { s.state.tune = true @@ -321,15 +403,21 @@ func (s *civControlStruct) decodeTransmitStatus(d []byte) { _ = s.getVd() } } + + statusLog.reportPTT(s.state.ptt, s.state.tune) + if s.state.setTuneSent { + s.state.setTuneSent = false + return false + } } - statusLog.reportPTT(s.state.ptt, s.state.tune) + if s.state.getTransmitStatusSent { // TODO + s.state.getTransmitStatusSent = false + return false + } + return true } func (s *civControlStruct) decodeVdSWRS(d []byte) bool { - if len(d) < 1 { - return true - } - switch d[0] { case 0x02: if len(d) < 3 { @@ -381,33 +469,49 @@ func (s *civControlStruct) decodeVdSWRS(d []byte) bool { } case 0x15: if len(d) < 3 { - return true + return !s.state.getVdSent } statusLog.reportVd(((float64(int(d[1])<<8) + float64(d[2])) / 0x0241) * 16) + if s.state.getVdSent { + s.state.getVdSent = false + return false + } } return true } -func (s *civControlStruct) decodePreampAndNR(d []byte) { - if len(d) < 2 { - return - } - +func (s *civControlStruct) decodePreampAndNR(d []byte) bool { switch d[0] { case 0x02: + if len(d) < 2 { + return !s.state.setPreampSent + } s.state.preamp = int(d[1]) statusLog.reportPreamp(s.state.preamp) + if s.state.setPreampSent { + s.state.setPreampSent = false + return false + } case 0x40: + if len(d) < 2 { + return !s.state.setNREnabledSent + } if d[1] == 1 { s.state.nrEnabled = true } else { s.state.nrEnabled = false } statusLog.reportNREnabled(s.state.nrEnabled) + if s.state.setNREnabledSent { + s.state.setNREnabledSent = false + return false + } } + return true } func (s *civControlStruct) setPwr(percent int) error { + s.state.setPwrSent = true v := uint16(0x0255 * (float64(percent) / 100)) return s.st.send([]byte{254, 254, civAddress, 224, 0x14, 0x0a, byte(v >> 8), byte(v & 0xff), 253}) } @@ -427,6 +531,7 @@ func (s *civControlStruct) decPwr() error { } func (s *civControlStruct) setRFGain(percent int) error { + s.state.setRFGainSent = true v := uint16(0x0255 * (float64(percent) / 100)) return s.st.send([]byte{254, 254, civAddress, 224, 0x14, 0x02, byte(v >> 8), byte(v & 0xff), 253}) } @@ -446,6 +551,7 @@ func (s *civControlStruct) decRFGain() error { } func (s *civControlStruct) setSQL(percent int) error { + s.state.setSQLSent = true v := uint16(0x0255 * (float64(percent) / 100)) return s.st.send([]byte{254, 254, civAddress, 224, 0x14, 0x03, byte(v >> 8), byte(v & 0xff), 253}) } @@ -465,6 +571,7 @@ func (s *civControlStruct) decSQL() error { } func (s *civControlStruct) setNR(percent int) error { + s.state.setNRSent = true v := uint16(0x0255 * (float64(percent) / 100)) return s.st.send([]byte{254, 254, civAddress, 224, 0x14, 0x06, byte(v >> 8), byte(v & 0xff), 253}) } @@ -501,6 +608,7 @@ func (s *civControlStruct) decFreq() error { } func (s *civControlStruct) setFreq(f uint) error { + s.state.setFreqSent = true var b [5]byte v0 := s.getDigit(f, 9) v1 := s.getDigit(f, 8) @@ -517,11 +625,7 @@ func (s *civControlStruct) setFreq(f uint) error { v0 = s.getDigit(f, 1) v1 = s.getDigit(f, 0) b[0] = v0<<4 | v1 - if err := s.st.send([]byte{254, 254, civAddress, 224, 5, b[0], b[1], b[2], b[3], b[4], 253}); err != nil { - return err - } - // The transceiver does not send the new freq automatically. - return s.getFreq() + return s.st.send([]byte{254, 254, civAddress, 224, 5, b[0], b[1], b[2], b[3], b[4], 253}) } func (s *civControlStruct) incOperatingMode() error { @@ -561,6 +665,7 @@ func (s *civControlStruct) decFilter() error { } func (s *civControlStruct) setOperatingModeAndFilter(modeCode, filterCode byte) error { + s.state.setModeSent = true if err := s.st.send([]byte{254, 254, civAddress, 224, 0x06, modeCode, filterCode, 253}); err != nil { return err } @@ -568,6 +673,7 @@ func (s *civControlStruct) setOperatingModeAndFilter(modeCode, filterCode byte) } func (s *civControlStruct) setPTT(enable bool) error { + s.state.setPTTSent = true var b byte if enable { b = 1 @@ -580,6 +686,7 @@ func (s *civControlStruct) toggleTune() error { return nil } + s.state.setTuneSent = true var b byte if !s.state.tune { b = 2 @@ -590,6 +697,7 @@ func (s *civControlStruct) toggleTune() error { } func (s *civControlStruct) toggleDataMode() error { + s.state.setDataModeSent = true var b byte var f byte if !s.state.dataMode { @@ -629,6 +737,7 @@ func (s *civControlStruct) decBand() error { } func (s *civControlStruct) togglePreamp() error { + s.state.setPreampSent = true b := byte(s.state.preamp + 1) if b > 2 { b = 0 @@ -637,6 +746,7 @@ func (s *civControlStruct) togglePreamp() error { } func (s *civControlStruct) toggleNR() error { + s.state.setNRSent = true var b byte if !s.state.nrEnabled { b = 1 @@ -645,6 +755,7 @@ func (s *civControlStruct) toggleNR() error { } func (s *civControlStruct) incTS() error { + s.state.setTSSent = true var b byte if s.state.tsValue == 13 { b = 0 @@ -655,6 +766,7 @@ func (s *civControlStruct) incTS() error { } func (s *civControlStruct) decTS() error { + s.state.setTSSent = true var b byte if s.state.tsValue == 0 { b = 13 @@ -665,18 +777,22 @@ func (s *civControlStruct) decTS() error { } func (s *civControlStruct) getFreq() error { + s.state.getFreqSent = true return s.st.send([]byte{254, 254, civAddress, 224, 3, 253}) } func (s *civControlStruct) getMode() error { + s.state.getModeSent = true return s.st.send([]byte{254, 254, civAddress, 224, 4, 253}) } func (s *civControlStruct) getDataMode() error { + s.state.getDataModeSent = true return s.st.send([]byte{254, 254, civAddress, 224, 0x1a, 0x06, 253}) } func (s *civControlStruct) getTransmitStatus() error { + s.state.getTransmitStatusSent = true if err := s.st.send([]byte{254, 254, civAddress, 224, 0x1c, 0, 253}); err != nil { return err } @@ -684,6 +800,7 @@ func (s *civControlStruct) getTransmitStatus() error { } func (s *civControlStruct) getVd() error { + s.state.getVdSent = true return s.st.send([]byte{254, 254, civAddress, 224, 0x15, 0x15, 253}) }