Project

General

Profile

New Model #217 » ts2000.py

Charles Stewart, 03/24/2015 09: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
from chirp import chirp_common, directory
17
from chirp.drivers import kenwood_live
18

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

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

    
35
    _upper = 289
36
    _kenwood_split = True
37
    _kenwood_valid_tones = list(TS2000_TONES)
38

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

    
55
    def _cmd_set_memory(self, number, spec):
56
        return "MW0%03i%s;" % (number, spec)
57

    
58
    def _cmd_get_memory(self, number):
59
        return "MR0%03i;" % number
60

    
61
    def _cmd_get_split(self, number):
62
        return "MR1%03i;" % number
63

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

    
67
    def get_memory(self, number):
68
        if number < 0 or number > self._upper:
69
            raise errors.InvalidMemoryLocation( \
70
                "Number must be between 0 and %i" % self._upper)
71
        if self.__memcache.has_key(number) and not NOCACHE:
72
            return self.__memcache[number]
73

    
74
        result = command(self.pipe, *self._cmd_get_memory(number))
75
        if result == "N":
76
            mem = chirp_common.Memory()
77
            mem.number = number
78
            mem.empty = True
79
            self.__memcache[mem.number] = mem
80
            return mem
81

    
82
        spec = result[2:]
83

    
84
        mem = self._parse_mem_spec(result)
85
        self.__memcache[mem.number] = mem
86

    
87
        # FIXME
88
        if mem.duplex == "" and self._kenwood_split:
89
            result = command(self.pipe, *self._cmd_get_split(number))
90
            if " " in result:
91
                value = result.split(" ", 1)[1]
92
                self._parse_split_spec(mem, value.split(","))
93

    
94
        return mem
95
    
96
    def _parse_mem_spec(self, spec):
97
        mem = chirp_common.Memory()
98

    
99
        # pad string so indexes match Kenwood docs
100
        spec = " " + spec
101
        
102
        # use the same variable names as the Kenwood docs
103
        _p1  = spec[3]
104
        _p2  = spec[4]
105
        _p3  = spec[5:7]
106
        _p4  = spec[7:18]
107
        _p5  = spec[18]
108
        _p6  = spec[19]
109
        _p7  = spec[20]
110
        _p8  = spec[21:23]
111
        _p9  = spec[23:25]
112
        _p10 = spec[25:28]
113
        _p11 = spec[28]
114
        _p12 = spec[29]
115
        _p13 = spec[30:39]
116
        _p14 = spec[39:41]
117
        _p15 = spec[41]
118
        _p16 = spec[42:49]
119

    
120
        mem.number = int(_p2 + _p3) # concat bank num and chan num
121
        mem.freq = int(_p4)
122
        mem.mode = TS2000_MODES[int(_p5)]
123
        mem.skip = ["", "S"][int(_p6)]
124
        mem.tmode = TS2000_TMODES[int(_p7)]
125
        mem.rtone = self._kenwood_valid_tones[int(_p8) - 1]
126
        mem.ctone = self._kenwood_valid_tones[int(_p9) - 1]
127
        mem.dtcs = chirp_common.DTCS_CODES[int(_p10) - 1]
128
        mem.duplex = TS2000_DUPLEX[int(_p12)]
129
        mem.offset = int(_p13) # 9-digit
130
        if mem.mode in ["AM", "FM"]:
131
            mem.tuning_step = TS2000_FM_STEPS[int(_p14)]
132
        else:
133
            mem.tuning_step = TS2000_SSB_STEPS[int(_p14)]
134
        mem.name = _p16
135

    
136
        return mem
137

    
138
    def set_memory(self, memory):
139
        if memory.number < 0 or memory.number > self._upper:
140
            raise errors.InvalidMemoryLocation( \
141
                "Number must be between 0 and %i" % self._upper)
142

    
143
        spec = self._make_mem_spec(memory)
144
        spec = "".join(spec)
145
        r1 = command(self.pipe, *self._cmd_set_memory(memory.number, spec))
146
        if not iserr(r1):
147
            time.sleep(0.5)
148
            r2 = command(self.pipe, *self._cmd_set_memory_name(memory.number,
149
                                                               memory.name))
150
            if not iserr(r2):
151
                memory.name = memory.name.rstrip()
152
                self.__memcache[memory.number] = memory
153
            else:
154
                raise errors.InvalidDataError("Radio refused name %i: %s" %\
155
                                                  (memory.number,
156
                                                   repr(memory.name)))
157
        else:
158
            raise errors.InvalidDataError("Radio refused %i" % memory.number)
159

    
160
        # FIXME
161
        if memory.duplex == "split" and self._kenwood_split: 
162
            spec = "".join(self._make_split_spec(memory))
163
            result = command(self.pipe, *self._cmd_set_split(memory.number,
164
                                                             spec))
165
            if iserr(result):
166
                raise errors.InvalidDataError("Radio refused %i" % \
167
                                                  memory.number)
168
    
169
    def _make_mem_spec(self, mem):
170
        if mem.duplex in " +-":
171
            duplex = util.get_dict_rev(TS2000_DUPLEX, mem.duplex)
172
            offset = mem.offset
173
        elif mem.duplex == "split":
174
            duplex = 0
175
            offset = 0
176
        else:
177
            print "Bug: unsupported duplex `%s'" % mem.duplex
178
        if mem.mode in ["AM", "FM"]:
179
            step = TS2000_FM_STEPS.index(mem.tuning_step)
180
        else:
181
            step = TS2000_SSB_STEPS.index(mem.tuning_step)
182
        spec = ( \
183
            "0",
184
            "%03i" % mem.number,
185
            "%011i" % mem.freq,
186
            "%i" % (TS2000_MODES.index(mem.mode)),
187
            "%i" % (mem.skip == "S"),
188
            "%i" % TS2000_TMODES.index(mem.tmode),
189
            "%02i" % (self._kenwood_valid_tones.index(mem.rtone)),
190
            "%02i" % (self._kenwood_valid_tones.index(mem.ctone)),
191
            "%03i" % (chirp_common.DTCS_CODES.index(mem.dtcs)),
192
            "0", # REVERSE status
193
            "%i" % duplex,
194
            "%09i" % offset,
195
            "%i" % step,
196
            "0", # Memory Group number (0-9)
197
            "%s" % mem.name,
198
        )
199

    
200
        return spec
(5-5/10)