Project

General

Profile

New Model #217 » ts2000.py

Feature complete revision. Split mode VFO and deleting channels working. - Charles Stewart, 03/28/2015 08:15 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 NOCACHE, KenwoodLiveRadio, command, iserr
20

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

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

    
37
    _upper = 289
38
    _kenwood_split = True
39
    _kenwood_valid_tones = list(TS2000_TONES)
40

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

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

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

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

    
68
    def _cmd_get_memory(self, number):
69
        return "MR0%03i" % number
70

    
71
    def _cmd_get_split(self, number):
72
        return "MR1%03i" % number
73

    
74
    def _cmd_recall_memory(self, number):
75
        return "MC%03i" % (number)
76

    
77
    def _cmd_cur_memory(self, number):
78
        return "MC"
79

    
80
    def _cmd_get_split(self, number):
81
        return "MR1%03i" % number
82

    
83
    def _cmd_set_split(self, number, spec):
84
        return "MW1%03i%s" % (number, spec)
85

    
86
    def _cmd_erase_memory(self, number):
87
        # write a memory channel that's effectively zeroed except the channel number
88
        return "MW%04i%035i" % (number, 0)
89

    
90
    def erase_memory(self, number):
91
        if number not in self._memcache:
92
            return
93

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

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

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

    
114
        spec = result[2:]
115

    
116
        mem = self._parse_mem_spec(result)
117
        self._memcache[mem.number] = mem
118

    
119
        # check for split frequency operation
120
        if mem.duplex == "" and self._kenwood_split:
121
            result = command(self.pipe, *self._cmd_get_split(number))
122
            self._parse_split_spec(mem, result)
123

    
124
        return mem
125
    
126
    def _parse_mem_spec(self, spec):
127
        mem = chirp_common.Memory()
128

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

    
150
        mem.number = int(_p2 + _p3) # concat bank num and chan num
151
        mem.freq = int(_p4)
152
        mem.mode = TS2000_MODES[int(_p5)]
153
        mem.skip = ["", "S"][int(_p6)]
154
        mem.tmode = TS2000_TMODES[int(_p7)]
155
        mem.rtone = self._kenwood_valid_tones[int(_p8) - 1] # PL is 1 indexed
156
        mem.ctone = self._kenwood_valid_tones[int(_p9) - 1] # CTCSS is 1 indexed
157
        mem.dtcs = chirp_common.DTCS_CODES[int(_p10)]       # DCS is 0 indexed
158
        mem.duplex = TS2000_DUPLEX[int(_p12)]
159
        mem.offset = int(_p13) # 9-digit
160
        if mem.mode in ["AM", "FM"]:
161
            mem.tuning_step = TS2000_FM_STEPS[int(_p14)]
162
        else:
163
            mem.tuning_step = TS2000_SSB_STEPS[int(_p14)]
164
        mem.name = _p16
165

    
166
        return mem
167
    
168
    def _parse_split_spec(self, mem, spec):
169

    
170
        # pad string so indexes match Kenwood docs
171
        spec = " " + spec
172
        
173
        # use the same variable names as the Kenwood docs
174
        split_freq = int(spec[7:18])
175
        if mem.freq != split_freq:
176
            mem.duplex = "split"
177
            mem.offset = split_freq
178

    
179
        return mem
180

    
181
    def set_memory(self, memory):
182
        if memory.number < 0 or memory.number > self._upper:
183
            raise errors.InvalidMemoryLocation( \
184
                "Number must be between 0 and %i" % self._upper)
185

    
186
        spec = self._make_mem_spec(memory)
187
        spec = "".join(spec)
188
        r1 = command(self.pipe, *self._cmd_set_memory(memory.number, spec))
189
        if not iserr(r1):
190
            memory.name = memory.name.rstrip()
191
            self._memcache[memory.number] = memory
192

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

    
205
        # FIXME
206
        if memory.duplex == "split" and self._kenwood_split: 
207
            spec = "".join(self._make_split_spec(memory))
208
            result = command(self.pipe, *self._cmd_set_split(memory.number,
209
                                                             spec))
210
            if iserr(result):
211
                raise errors.InvalidDataError("Radio refused %i" % \
212
                                                  memory.number)
213
    
214
    def _make_mem_spec(self, mem):
215
        if mem.duplex in " +-":
216
            duplex = util.get_dict_rev(TS2000_DUPLEX, mem.duplex)
217
            offset = mem.offset
218
        elif mem.duplex == "split":
219
            duplex = 0
220
            offset = 0
221
        else:
222
            print "Bug: unsupported duplex `%s'" % mem.duplex
223
        if mem.mode in ["AM", "FM"]:
224
            step = TS2000_FM_STEPS.index(mem.tuning_step)
225
        else:
226
            step = TS2000_SSB_STEPS.index(mem.tuning_step)
227

    
228
        # TS-2000 won't accept channels with tone mode off if they have
229
        # tone values
230
        if mem.tmode == "":
231
            rtone = 0
232
            ctone = 0
233
            dtcs = 0
234
        else:
235
            rtone = (self._kenwood_valid_tones.index(mem.rtone) + 1)    # PL is 1 indexed
236
            ctone = (self._kenwood_valid_tones.index(mem.ctone) + 1)    # CTCSS is 1 indexed
237
            dtcs = (chirp_common.DTCS_CODES.index(mem.dtcs))            # DCS is 0 indexed
238

    
239
        spec = ( \
240
            "%011i" % mem.freq,
241
            "%i" % (TS2000_MODES.index(mem.mode)),
242
            "%i" % (mem.skip == "S"),
243
            "%i" % TS2000_TMODES.index(mem.tmode),
244
            "%02i" % (rtone),
245
            "%02i" % (ctone),
246
            "%03i" % (dtcs),
247
            "0", # REVERSE status
248
            "%i" % duplex,
249
            "%09i" % offset,
250
            "%02i" % step,
251
            "0", # Memory Group number (0-9)
252
            "%s" % mem.name,
253
        )
254

    
255
        return spec
256
    
257
    def _make_split_spec(self, mem):
258
        if mem.duplex in " +-":
259
            duplex = util.get_dict_rev(TS2000_DUPLEX, mem.duplex)
260
            offset = mem.offset
261
        elif mem.duplex == "split":
262
            duplex = 0
263
            offset = 0
264
        else:
265
            print "Bug: unsupported duplex `%s'" % mem.duplex
266
        if mem.mode in ["AM", "FM"]:
267
            step = TS2000_FM_STEPS.index(mem.tuning_step)
268
        else:
269
            step = TS2000_SSB_STEPS.index(mem.tuning_step)
270

    
271
        # TS-2000 won't accept channels with tone mode off if they have
272
        # tone values
273
        if mem.tmode == "":
274
            rtone = 0
275
            ctone = 0
276
            dtcs = 0
277
        else:
278
            rtone = (self._kenwood_valid_tones.index(mem.rtone) + 1)    # PL is 1 indexed
279
            ctone = (self._kenwood_valid_tones.index(mem.ctone) + 1)    # CTCSS is 1 indexed
280
            dtcs = (chirp_common.DTCS_CODES.index(mem.dtcs))            # DCS is 0 indexed
281

    
282
        spec = ( \
283
            "%011i" % mem.offset,
284
            "%i" % (TS2000_MODES.index(mem.mode)),
285
            "%i" % (mem.skip == "S"),
286
            "%i" % TS2000_TMODES.index(mem.tmode),
287
            "%02i" % (rtone),
288
            "%02i" % (ctone),
289
            "%03i" % (dtcs),
290
            "0", # REVERSE status
291
            "%i" % duplex,
292
            "%09i" % 0,
293
            "%02i" % step,
294
            "0", # Memory Group number (0-9)
295
            "%s" % mem.name,
296
        )
297

    
298
        return spec
(10-10/10)