Project

General

Profile

Bug #10248 » ts2000.py

Dan Smith, 01/09/2023 10:34 PM

 
1
# Copyright 2012 Tom Hayward <tom@tomh.us>
2
#
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15

    
16
import re
17
from chirp import chirp_common, directory, util, errors
18
from chirp.drivers import kenwood_live
19
from chirp.drivers.kenwood_live import KenwoodLiveRadio, \
20
    command, iserr, NOCACHE
21

    
22
TS2000_SSB_STEPS = [1.0, 2.5, 5.0, 10.0]
23
TS2000_FM_STEPS = [5.0, 6.25, 10.0, 12.5, 15.0, 20.0, 25.0, 30.0, 50.0, 100.0]
24
TS2000_DUPLEX = dict(kenwood_live.DUPLEX)
25
TS2000_DUPLEX[3] = "="
26
TS2000_DUPLEX[4] = "split"
27
TS2000_MODES = ["?", "LSB", "USB", "CW", "FM", "AM",
28
                "FSK", "CWR", "?", "FSKR"]
29
TS2000_TMODES = ["", "Tone", "TSQL", "DTCS"]
30
TS2000_TONES = list(chirp_common.OLD_TONES)
31
TS2000_TONES.remove(69.3)
32

    
33

    
34
@directory.register
35
class TS2000Radio(KenwoodLiveRadio):
36
    """Kenwood TS-2000"""
37
    MODEL = "TS-2000"
38

    
39
    _upper = 289
40
    _kenwood_split = True
41
    _kenwood_valid_tones = list(TS2000_TONES)
42

    
43
    def get_features(self):
44
        rf = chirp_common.RadioFeatures()
45
        rf.has_dtcs_polarity = False
46
        rf.has_bank = False
47
        rf.can_odd_split = True
48
        rf.valid_modes = ["LSB", "USB", "CW", "FM", "AM"]
49
        rf.valid_tmodes = list(TS2000_TMODES)
50
        rf.valid_tuning_steps = list(TS2000_SSB_STEPS + TS2000_FM_STEPS)
51
        rf.valid_bands = [(1000, 1300000000)]
52
        rf.valid_skips = ["", "S"]
53
        rf.valid_duplexes = list(TS2000_DUPLEX.values())
54

    
55
        # TS-2000 uses ";" as a message separator even though it seems to
56
        # allow you to to use all printable ASCII characters at the manual
57
        # controls.  The radio doesn't send the name after the ";" if you
58
        # input one from the manual controls.
59
        rf.valid_characters = chirp_common.CHARSET_ASCII.replace(';', '')
60
        rf.valid_name_length = 7    # 7 character channel names
61
        rf.memory_bounds = (0, self._upper)
62
        return rf
63

    
64
    def _cmd_set_memory(self, number, spec):
65
        return "MW0%03i%s" % (number, spec)
66

    
67
    def _cmd_set_split(self, number, spec):
68
        return "MW1%03i%s" % (number, spec)
69

    
70
    def _cmd_get_memory(self, number):
71
        return "MR0%03i" % number
72

    
73
    def _cmd_get_split(self, number):
74
        return "MR1%03i" % number
75

    
76
    def _cmd_recall_memory(self, number):
77
        return "MC%03i" % (number)
78

    
79
    def _cmd_cur_memory(self, number):
80
        return "MC"
81

    
82
    def _cmd_erase_memory(self, number):
83
        # write a memory channel that's effectively zeroed except
84
        # for the channel number
85
        return "MW%04i%035i" % (number, 0)
86

    
87
    def erase_memory(self, number):
88
        if number not in self._memcache:
89
            return
90

    
91
        resp = command(self.pipe, *self._cmd_erase_memory(number))
92
        if iserr(resp):
93
            raise errors.RadioError("Radio refused delete of %i" % number)
94
        del self._memcache[number]
95

    
96
    def get_memory(self, number):
97
        if number < 0 or number > self._upper:
98
            raise errors.InvalidMemoryLocation(
99
                "Number must be between 0 and %i" % self._upper)
100
        if number in self._memcache and not NOCACHE:
101
            return self._memcache[number]
102

    
103
        result = command(self.pipe, *self._cmd_get_memory(number))
104
        if result == "N":
105
            mem = chirp_common.Memory()
106
            mem.number = number
107
            mem.empty = True
108
            self._memcache[mem.number] = mem
109
            return mem
110

    
111
        mem = self._parse_mem_spec(result)
112
        self._memcache[mem.number] = mem
113

    
114
        # check for split frequency operation
115
        if mem.duplex == "" and self._kenwood_split:
116
            result = command(self.pipe, *self._cmd_get_split(number))
117
            self._parse_split_spec(mem, result)
118

    
119
        return mem
120

    
121
    def _parse_mem_spec(self, spec):
122
        mem = chirp_common.Memory()
123

    
124
        # pad string so indexes match Kenwood docs
125
        spec = " " + spec
126

    
127
        # use the same variable names as the Kenwood docs
128
        # _p1 = spec[3]
129
        _p2 = spec[4]
130
        _p3 = spec[5:7]
131
        _p4 = spec[7:18]
132
        _p5 = spec[18]
133
        _p6 = spec[19]
134
        _p7 = spec[20]
135
        _p8 = spec[21:23]
136
        _p9 = spec[23:25]
137
        _p10 = spec[25:28]
138
        # _p11 = spec[28]
139
        _p12 = spec[29]
140
        _p13 = spec[30:39]
141
        _p14 = spec[39:41]
142
        # _p15 = spec[41]
143
        _p16 = spec[42:49]
144

    
145
        mem.number = int(_p2 + _p3)     # concat bank num and chan num
146

    
147
        if _p5 == '0':
148
            # NOTE(danms): Apparently some TS2000s will return unset
149
            # memory records with all zeroes for the fields instead of
150
            # NAKing the command with an N response. If that happens here,
151
            # return an empty memory.
152
            mem.empty = True
153
            return mem
154

    
155
        mem.freq = int(_p4)
156
        mem.mode = TS2000_MODES[int(_p5)]
157
        mem.skip = ["", "S"][int(_p6)]
158
        mem.tmode = TS2000_TMODES[int(_p7)]
159
        # PL and T-SQL are 1 indexed, DTCS is 0 indexed
160
        mem.rtone = self._kenwood_valid_tones[int(_p8) - 1]
161
        mem.ctone = self._kenwood_valid_tones[int(_p9) - 1]
162
        mem.dtcs = chirp_common.DTCS_CODES[int(_p10)]
163
        mem.duplex = TS2000_DUPLEX[int(_p12)]
164
        mem.offset = int(_p13)      # 9-digit
165
        if mem.mode in ["AM", "FM"]:
166
            mem.tuning_step = TS2000_FM_STEPS[int(_p14)]
167
        else:
168
            mem.tuning_step = TS2000_SSB_STEPS[int(_p14)]
169
        mem.name = _p16
170

    
171
        return mem
172

    
173
    def _parse_split_spec(self, mem, spec):
174

    
175
        # pad string so indexes match Kenwood docs
176
        spec = " " + spec
177

    
178
        # use the same variable names as the Kenwood docs
179
        split_freq = int(spec[7:18])
180
        if mem.freq != split_freq:
181
            mem.duplex = "split"
182
            mem.offset = split_freq
183

    
184
        return mem
185

    
186
    def set_memory(self, memory):
187
        if memory.number < 0 or memory.number > self._upper:
188
            raise errors.InvalidMemoryLocation(
189
                "Number must be between 0 and %i" % self._upper)
190

    
191
        spec = self._make_mem_spec(memory)
192
        spec = "".join(spec)
193
        r1 = command(self.pipe, *self._cmd_set_memory(memory.number, spec))
194
        if not iserr(r1):
195
            memory.name = memory.name.rstrip()
196
            self._memcache[memory.number] = memory
197

    
198
            # if we're tuned to the channel, reload it
199
            r1 = command(self.pipe, *self._cmd_cur_memory(memory.number))
200
            if not iserr(r1):
201
                pattern = re.compile("MC([0-9]{3})")
202
                match = pattern.search(r1)
203
                if match is not None:
204
                    cur_mem = int(match.group(1))
205
                    if cur_mem == memory.number:
206
                        cur_mem = \
207
                            command(self.pipe,
208
                                    *self._cmd_recall_memory(memory.number))
209
        else:
210
            raise errors.InvalidDataError("Radio refused %i" % memory.number)
211

    
212
        # FIXME
213
        if memory.duplex == "split" and self._kenwood_split:
214
            spec = "".join(self._make_split_spec(memory))
215
            result = command(self.pipe, *self._cmd_set_split(memory.number,
216
                                                             spec))
217
            if iserr(result):
218
                raise errors.InvalidDataError("Radio refused %i" %
219
                                              memory.number)
220

    
221
    def _make_mem_spec(self, mem):
222
        if mem.duplex in " +-":
223
            duplex = util.get_dict_rev(TS2000_DUPLEX, mem.duplex)
224
            offset = mem.offset
225
        elif mem.duplex == "split":
226
            duplex = 0
227
            offset = 0
228
        else:
229
            print("Bug: unsupported duplex `%s'" % mem.duplex)
230
        if mem.mode in ["AM", "FM"]:
231
            step = TS2000_FM_STEPS.index(mem.tuning_step)
232
        else:
233
            step = TS2000_SSB_STEPS.index(mem.tuning_step)
234

    
235
        # TS-2000 won't accept channels with tone mode off if they have
236
        # tone values
237
        if mem.tmode == "":
238
            rtone = 0
239
            ctone = 0
240
            dtcs = 0
241
        else:
242
            # PL and T-SQL are 1 indexed, DTCS is 0 indexed
243
            rtone = (self._kenwood_valid_tones.index(mem.rtone) + 1)
244
            ctone = (self._kenwood_valid_tones.index(mem.ctone) + 1)
245
            dtcs = (chirp_common.DTCS_CODES.index(mem.dtcs))
246

    
247
        spec = (
248
            "%011i" % mem.freq,
249
            "%i" % (TS2000_MODES.index(mem.mode)),
250
            "%i" % (mem.skip == "S"),
251
            "%i" % TS2000_TMODES.index(mem.tmode),
252
            "%02i" % (rtone),
253
            "%02i" % (ctone),
254
            "%03i" % (dtcs),
255
            "0",    # REVERSE status
256
            "%i" % duplex,
257
            "%09i" % offset,
258
            "%02i" % step,
259
            "0",    # Memory Group number (0-9)
260
            "%s" % mem.name,
261
        )
262

    
263
        return spec
264

    
265
    def _make_split_spec(self, mem):
266
        if mem.duplex in " +-":
267
            duplex = util.get_dict_rev(TS2000_DUPLEX, mem.duplex)
268
        elif mem.duplex == "split":
269
            duplex = 0
270
        else:
271
            print("Bug: unsupported duplex `%s'" % mem.duplex)
272
        if mem.mode in ["AM", "FM"]:
273
            step = TS2000_FM_STEPS.index(mem.tuning_step)
274
        else:
275
            step = TS2000_SSB_STEPS.index(mem.tuning_step)
276

    
277
        # TS-2000 won't accept channels with tone mode off if they have
278
        # tone values
279
        if mem.tmode == "":
280
            rtone = 0
281
            ctone = 0
282
            dtcs = 0
283
        else:
284
            # PL and T-SQL are 1 indexed, DTCS is 0 indexed
285
            rtone = (self._kenwood_valid_tones.index(mem.rtone) + 1)
286
            ctone = (self._kenwood_valid_tones.index(mem.ctone) + 1)
287
            dtcs = (chirp_common.DTCS_CODES.index(mem.dtcs))
288

    
289
        spec = (
290
            "%011i" % mem.offset,
291
            "%i" % (TS2000_MODES.index(mem.mode)),
292
            "%i" % (mem.skip == "S"),
293
            "%i" % TS2000_TMODES.index(mem.tmode),
294
            "%02i" % (rtone),
295
            "%02i" % (ctone),
296
            "%03i" % (dtcs),
297
            "0",    # REVERSE status
298
            "%i" % duplex,
299
            "%09i" % 0,
300
            "%02i" % step,
301
            "0",    # Memory Group number (0-9)
302
            "%s" % mem.name,
303
        )
304

    
305
        return spec
(6-6/6)