Project

General

Profile

New Model #144 » ftm350.py

Dan Smith, 02/11/2013 05:22 PM

 
1
# Copyright 2013 Dan Smith <dsmith@danplanet.com>
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 time
17
import struct
18
import os
19

    
20
from chirp import chirp_common, yaesu_clone, directory, errors, util
21
from chirp import ft7800
22
from chirp import bitwise, memmap
23
from chirp.settings import RadioSettingGroup, RadioSetting
24
from chirp.settings import RadioSettingValueInteger, RadioSettingValueString
25

    
26
mem_format = """
27
struct mem {
28
  u8 used:1,
29
     unknown1:7;
30
  u8 unknown2:1,
31
     mode:3,
32
     unknown8:2,
33
     duplex:2;
34
  bbcd freq[3];
35
  u8 unknownA:1,
36
     tmode:3,
37
     unknownB:4;
38
  u8 unknown3[3];
39
  u8 unknown4:2,
40
     tone:6;
41
  u8 unknownC:1,
42
     dtcs:7;
43
  u8 showalpha:1,
44
     unknown5:7;
45
  u8 unknown6;
46
  u8 offset;
47
  u8 unknown7[2];
48
};
49

    
50
struct lab {
51
  u8 string[8];
52
};
53

    
54
#seekto 0x0508;
55
struct {
56
  char call[6];
57
  u8 ssid;
58
} aprs_my_callsign;
59

    
60
#seekto 0x0800;
61
struct mem left_memory[500];
62

    
63
#seekto 0x2860;
64
struct mem right_memory[500];
65

    
66
#seekto 0x48C0;
67
struct lab left_label[518];
68
struct lab right_label[518];
69
"""
70

    
71
_TMODES = ["", "Tone", "TSQL", "-RVT", "DTCS", "-PR", "-PAG"]
72
TMODES = ["", "Tone", "TSQL", "", "DTCS", "", ""]
73
MODES = ["FM", "AM", "NFM", "", "WFM"]
74
DUPLEXES = ["", "-", "+"]
75
CHARSET = ('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!"' +
76
           '                                                                ')
77
def aprs_call_to_str(_call):
78
    call = ""
79
    for i in str(_call):
80
        if i == "\xca":
81
            break
82
        call += i
83
    return call
84

    
85
def _safe_read(radio, length):
86
    data = ""
87
    while len(data) < length:
88
        data += radio.pipe.read(length - len(data))
89
    return data
90

    
91
def _clone_in(radio):
92
    data = ""
93

    
94
    radio.pipe.setTimeout(1)
95
    attempts = 30
96

    
97
    data = memmap.MemoryMap("\x00" * (radio._memsize + 128))
98
    length = 0
99
    last_addr = 0
100
    while length < radio._memsize:
101
        frame = radio.pipe.read(131)
102
        if length and not frame:
103
            raise errors.RadioError("Radio not responding")
104

    
105
        if not frame:
106
            attempts -= 1
107
            if attempts <= 0:
108
                raise errors.RadioError("Radio not responding")
109

    
110
        if frame:
111
            addr, = struct.unpack(">H", frame[0:2])
112
            checksum = ord(frame[130])
113
            block = frame[2:130]
114

    
115
            cs = 0
116
            for i in frame[:-1]:
117
                cs = (cs + ord(i)) % 256
118
            if cs != checksum:
119
                print "Calc: %02x Real: %02x Len: %i" % (cs, checksum,
120
                                                         len(block))
121
                raise errors.RadioError("Block failed checksum")
122

    
123
            radio.pipe.write("\x06")
124
            time.sleep(0.05)
125

    
126
            if os.getenv("CHIRP_DEBUG") and (last_addr + 128) != addr:
127
                print "Gap, expecting %04x, got %04x" % (last_addr+128, addr)
128
            last_addr = addr
129
            data[addr] = block
130
            length += len(block)
131

    
132
        status = chirp_common.Status()
133
        status.cur = length
134
        status.max = radio._memsize
135
        status.msg = "Cloning from radio"
136
        radio.status_fn(status)
137

    
138
    return data
139

    
140
def _clone_out(radio):
141
    radio.pipe.setTimeout(1)
142

    
143
    # Seriously, WTF Yaesu?
144
    ranges = [
145
        (0x0000, 0x0000),
146
        (0x0100, 0x0380),
147
        (0x0480, 0xFF80),
148
        (0x0080, 0x0080),
149
        (0xFFFE, 0xFFFE),
150
        ]
151

    
152
    for start, end in ranges:
153
        for i in range(start, end+1, 128):
154
            block = radio._mmap[i:i + 128]
155
            frame = struct.pack(">H", i) + block
156
            cs = 0
157
            for byte in frame:
158
                cs += ord(byte)
159
            frame += chr(cs % 256)
160
            radio.pipe.write(frame)
161
            ack = radio.pipe.read(1)
162
            if ack != "\x06":
163
                raise errors.RadioError("Radio refused block %i" % (i / 128))
164
            time.sleep(0.05)
165

    
166
            status = chirp_common.Status()
167
            status.cur = i + 128
168
            status.max = radio._memsize
169
            status.msg = "Cloning to radio"
170
            radio.status_fn(status)
171

    
172
@directory.register
173
class FTM350Radio(yaesu_clone.YaesuCloneModeRadio):
174
    """Yaesu FTM-350"""
175
    BAUD_RATE = 48000
176
    VENDOR = "Yaesu"
177
    MODEL = "FTM-350"
178

    
179
    _model = ""
180
    _memsize = 65536
181
    _vfo = ""
182

    
183
    def get_features(self):
184
        rf = chirp_common.RadioFeatures()
185
        rf.has_bank = False
186
        rf.has_ctone = False
187
        rf.has_settings = self._vfo == "left"
188
        rf.has_tuning_step = False
189
        rf.has_dtcs_polarity = False
190
        rf.has_sub_devices = self.VARIANT == ""
191
        rf.valid_skips = [] # FIXME: Finish this
192
        rf.valid_tmodes = [""] + [x for x in TMODES if x]
193
        rf.valid_modes = [x for x in MODES if x]
194
        rf.valid_duplexes = DUPLEXES
195
        rf.memory_bounds = (1, 500)
196
        rf.valid_bands = [(  500000,    1800000),
197
                          (76000000,  250000000),
198
                          (30000000, 1000000000)]
199
        return rf
200

    
201
    def get_sub_devices(self):
202
        return [FTM350RadioLeft(self._mmap), FTM350RadioRight(self._mmap)]
203

    
204
    def sync_in(self):
205
        try:
206
            self._mmap = _clone_in(self)
207
        except errors.RadioError:
208
            raise
209
        except Exception, e:
210
            raise errors.RadioError("Failed to download from radio (%s)" % e)
211
        self.process_mmap()
212

    
213
    def sync_out(self):
214
        try:
215
            _clone_out(self)
216
        except errors.RadioError:
217
            raise
218
        except Exception, e:
219
            raise errors.RadioError("Failed to upload to radio (%s)" % e)
220

    
221
    def process_mmap(self):
222
        self._memobj = bitwise.parse(mem_format, self._mmap)
223

    
224
    def get_raw_memory(self, number):
225
        return (repr(self._memory_obj()[number - 1]) + 
226
                repr(self._label_obj()[number - 1]))
227

    
228
    def _memory_obj(self):
229
        return getattr(self._memobj, "%s_memory" % self._vfo)
230

    
231
    def _label_obj(self):
232
        return getattr(self._memobj, "%s_label" % self._vfo)
233

    
234
    def get_memory(self, number):
235
        _mem = self._memory_obj()[number - 1]
236
        mem = chirp_common.Memory()
237
        mem.number = number
238

    
239
        if not _mem.used:
240
            mem.empty = True
241
            return mem
242

    
243
        mem.freq = ft7800.get_freq(int(_mem.freq) * 10000)
244
        mem.rtone = chirp_common.TONES[_mem.tone]
245
        mem.tmode = TMODES[_mem.tmode]
246
        mem.duplex = DUPLEXES[_mem.duplex - 1]
247
        mem.dtcs = chirp_common.DTCS_CODES[_mem.dtcs]
248
        mem.offset = int(_mem.offset) * 50000
249
        mem.mode = MODES[_mem.mode]
250

    
251
        for char in self._label_obj()[number - 1].string:
252
            if char == 0xCA:
253
                break
254
            try:
255
                mem.name += CHARSET[char]
256
            except IndexError:
257
                mem.name += "?"
258
        mem.name = mem.name.rstrip()
259

    
260
        return mem
261

    
262
    def set_memory(self, mem):
263
        _mem = self._memory_obj()[mem.number - 1]
264
        _mem.used = not mem.empty
265
        if mem.empty:
266
            return
267

    
268
        ft7800.set_freq(mem.freq, _mem, 'freq')
269
        _mem.tone = chirp_common.TONES.index(mem.rtone)
270
        _mem.dtcs = chirp_common.DTCS_CODES.index(mem.dtcs)
271
        _mem.tmode = TMODES.index(mem.tmode)
272
        _mem.duplex = DUPLEXES.index(mem.duplex) + 1
273
        _mem.offset = mem.offset / 50000
274
        _mem.mode = MODES.index(mem.mode)
275

    
276
        for i in range(0, 8):
277
            try:
278
                char = CHARSET.index(mem.name[i])
279
            except IndexError:
280
                char = 0xCA
281
            self._label_obj()[mem.number - 1].string[i] = char
282
        _mem.showalpha = mem.name.strip() != ""
283

    
284
    @classmethod
285
    def match_model(self, filedata, filename):
286
        return filedata.startswith("AH033$")
287

    
288
    def get_settings(self):
289
        top = RadioSettingGroup("all", "All Settings")
290

    
291
        aprs = RadioSettingGroup("aprs", "APRS")
292
        top.append(aprs)
293

    
294
        myc = self._memobj.aprs_my_callsign
295
        rs = RadioSetting("aprs_my_callsign.call", "APRS My Callsign",
296
                          RadioSettingValueString(0, 6,
297
                                                  aprs_call_to_str(myc.call)))
298
        aprs.append(rs)
299

    
300
        rs = RadioSetting("aprs_my_callsign.ssid", "APRS My SSID",
301
                          RadioSettingValueInteger(0, 15, myc.ssid))
302
        aprs.append(rs)
303

    
304
        return top
305

    
306
    def set_settings(self, settings):
307
        for setting in settings:
308
            if not isinstance(setting, RadioSetting):
309
                self.set_settings(setting)
310
                continue
311

    
312
            # Quick hack to make these work
313
            if setting.get_name() == "aprs_my_callsign.call":
314
                self._memobj.aprs_my_callsign.call = \
315
                    setting.value.get_value().upper().replace(" ", "\xCA")
316
            elif setting.get_name() == "aprs_my_callsign.ssid":
317
                self._memobj.aprs_my_callsign.ssid = setting.value
318

    
319

    
320
class FTM350RadioLeft(FTM350Radio):
321
    VARIANT = "Left"
322
    _vfo = "left"
323

    
324
class FTM350RadioRight(FTM350Radio):
325
    VARIANT = "Right"
326
    _vfo = "right"
(1-1/4)