Skip to content

Commit

Permalink
Hande operating frequency depending on VFO
Browse files Browse the repository at this point in the history
  • Loading branch information
nonoo committed Nov 9, 2020
1 parent e87093e commit 48c18a6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 63 deletions.
132 changes: 70 additions & 62 deletions civcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ type civControlStruct struct {
mutex sync.Mutex
pendingCmds []*civCmd

getFreq civCmd
getPwr civCmd
getS civCmd
getOVF civCmd
Expand All @@ -115,27 +114,28 @@ type civControlStruct struct {
getMainVFOMode civCmd
getSubVFOMode civCmd

lastSReceivedAt time.Time
lastOVFReceivedAt time.Time
lastSWRReceivedAt time.Time

setPwr civCmd
setRFGain civCmd
setSQL civCmd
setNR civCmd
setFreq civCmd
setSubVFOFreq civCmd
setMode civCmd
setSubVFOMode civCmd
setPTT civCmd
setTune civCmd
setDataMode civCmd
setPreamp civCmd
setAGC civCmd
setNREnabled civCmd
setTS civCmd
setVFO civCmd
setSplit civCmd
lastSReceivedAt time.Time
lastOVFReceivedAt time.Time
lastSWRReceivedAt time.Time
lastVFOFreqReceivedAt time.Time

setPwr civCmd
setRFGain civCmd
setSQL civCmd
setNR civCmd
setMainVFOFreq civCmd
setSubVFOFreq civCmd
setMode civCmd
setSubVFOMode civCmd
setPTT civCmd
setTune civCmd
setDataMode civCmd
setPreamp civCmd
setAGC civCmd
setNREnabled civCmd
setTS civCmd
setVFO civCmd
setSplit civCmd

pttTimeoutTimer *time.Timer
tuneTimeoutTimer *time.Timer
Expand Down Expand Up @@ -179,16 +179,16 @@ func (s *civControlStruct) decode(d []byte) bool {
defer s.state.mutex.Unlock()

switch d[4] {
case 0x00:
return s.decodeFreq(payload)
// case 0x00:
// return s.decodeFreq(payload)
case 0x01:
return s.decodeMode(payload)
case 0x03:
return s.decodeFreq(payload)
// case 0x03:
// return s.decodeFreq(payload)
case 0x04:
return s.decodeMode(payload)
case 0x05:
return s.decodeFreq(payload)
// case 0x05:
// return s.decodeFreq(payload)
case 0x06:
return s.decodeMode(payload)
case 0x07:
Expand Down Expand Up @@ -228,33 +228,33 @@ func (s *civControlStruct) decodeFreqData(d []byte) (f uint) {
return
}

func (s *civControlStruct) decodeFreq(d []byte) bool {
if len(d) < 2 {
return !s.state.getFreq.pending && !s.state.setFreq.pending
}

s.state.freq = s.decodeFreqData(d)
statusLog.reportFrequency(s.state.freq)

s.state.bandIdx = len(civBands) - 1 // Set the band idx to GENE by default.
for i := range civBands {
if s.state.freq >= civBands[i].freqFrom && s.state.freq <= civBands[i].freqTo {
s.state.bandIdx = i
civBands[s.state.bandIdx].freq = s.state.freq
break
}
}

if s.state.getFreq.pending {
s.removePendingCmd(&s.state.getFreq)
return false
}
if s.state.setFreq.pending {
s.removePendingCmd(&s.state.setFreq)
return false
}
return true
}
// func (s *civControlStruct) decodeFreq(d []byte) bool {
// if len(d) < 2 {
// return !s.state.getFreq.pending && !s.state.setMainVFOFreq.pending
// }

// s.state.freq = s.decodeFreqData(d)
// statusLog.reportFrequency(s.state.freq)

// s.state.bandIdx = len(civBands) - 1 // Set the band idx to GENE by default.
// for i := range civBands {
// if s.state.freq >= civBands[i].freqFrom && s.state.freq <= civBands[i].freqTo {
// s.state.bandIdx = i
// civBands[s.state.bandIdx].freq = s.state.freq
// break
// }
// }

// if s.state.getFreq.pending {
// s.removePendingCmd(&s.state.getFreq)
// return false
// }
// if s.state.setMainVFOFreq.pending {
// s.removePendingCmd(&s.state.setMainVFOFreq)
// return false
// }
// return true
// }

func (s *civControlStruct) decodeFilterValueToFilterIdx(v byte) int {
for i := range civFilters {
Expand Down Expand Up @@ -705,6 +705,10 @@ func (s *civControlStruct) decodeVFOFreq(d []byte) bool {
s.removePendingCmd(&s.state.getMainVFOFreq)
return false
}
if s.state.setMainVFOFreq.pending {
s.removePendingCmd(&s.state.setMainVFOFreq)
return false
}
case 0x01:
s.state.subFreq = f
statusLog.reportSubFrequency(s.state.subFreq)
Expand Down Expand Up @@ -912,11 +916,11 @@ func (s *civControlStruct) getDigit(v uint, n int) byte {
}

func (s *civControlStruct) incFreq() error {
return s.setFreq(s.state.freq + s.state.ts)
return s.setMainVFOFreq(s.state.freq + s.state.ts)
}

func (s *civControlStruct) decFreq() error {
return s.setFreq(s.state.freq - s.state.ts)
return s.setMainVFOFreq(s.state.freq - s.state.ts)
}

func (s *civControlStruct) encodeFreqData(f uint) (b [5]byte) {
Expand All @@ -938,10 +942,10 @@ func (s *civControlStruct) encodeFreqData(f uint) (b [5]byte) {
return
}

func (s *civControlStruct) setFreq(f uint) error {
func (s *civControlStruct) setMainVFOFreq(f uint) error {
b := s.encodeFreqData(f)
s.initCmd(&s.state.setFreq, "setFreq", []byte{254, 254, civAddress, 224, 5, b[0], b[1], b[2], b[3], b[4], 253})
return s.sendCmd(&s.state.setFreq)
s.initCmd(&s.state.setMainVFOFreq, "setMainVFOFreq", []byte{254, 254, civAddress, 224, 0x25, 0x00, b[0], b[1], b[2], b[3], b[4], 253})
return s.sendCmd(&s.state.setMainVFOFreq)
}

func (s *civControlStruct) setSubVFOFreq(f uint) error {
Expand Down Expand Up @@ -1060,7 +1064,7 @@ func (s *civControlStruct) incBand() error {
if f == 0 {
f = (civBands[i].freqFrom + civBands[i].freqTo) / 2
}
return s.setFreq(f)
return s.setMainVFOFreq(f)
}

func (s *civControlStruct) decBand() error {
Expand All @@ -1072,7 +1076,7 @@ func (s *civControlStruct) decBand() error {
if f == 0 {
f = civBands[i].freqFrom
}
return s.setFreq(f)
return s.setMainVFOFreq(f)
}

func (s *civControlStruct) togglePreamp() error {
Expand Down Expand Up @@ -1314,6 +1318,10 @@ func (s *civControlStruct) loop() {
_ = s.getOVF()
}
}
if !s.state.getMainVFOFreq.pending && !s.state.getSubVFOFreq.pending &&
time.Since(s.state.lastVFOFreqReceivedAt) >= statusPollInterval {
_ = s.getBothVFOFreq()
}
case <-s.resetSReadTimer:
case <-s.newPendingCmdAdded:
case <-time.After(nextPendingCmdTimeout):
Expand Down
2 changes: 1 addition & 1 deletion rigctld.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (s *rigctldStruct) processCmd(cmd string) (close bool, err error) {
_ = s.sendReplyCode(rigctldInvalidParam)
return
}
err = civControl.setFreq(uint(f))
err = civControl.setMainVFOFreq(uint(f))
if err != nil {
_ = s.sendReplyCode(rigctldInvalidParam)
return
Expand Down

0 comments on commit 48c18a6

Please sign in to comment.