Project

General

Profile

Bug #10904 » ft8100.py

5cb064ba - Dan Smith, 10/19/2023 08:05 PM

 
1
# Copyright 2010 Eric Allen <eric@hackerengineer.net>
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 logging
17
import time
18

    
19
from chirp import chirp_common, directory, bitwise, errors
20
from chirp.drivers import yaesu_clone
21

    
22
LOG = logging.getLogger(__name__)
23

    
24
TONES = chirp_common.OLD_TONES
25
TMODES = ["", "Tone"]
26
MODES = ['FM', 'AM']
27
STEPS = [5.0, 10.0, 12.5, 15.0, 20.0, 25.0, 50.0]
28
DUPLEX = ["", "-", "+", "split"]
29
# "M" for masked memories, which are invisible until un-masked
30
SKIPS = ["", "S", "M"]
31
POWER_LEVELS_VHF = [chirp_common.PowerLevel("Low", watts=5),
32
                    chirp_common.PowerLevel("Mid", watts=20),
33
                    chirp_common.PowerLevel("High", watts=50)]
34
POWER_LEVELS_UHF = [chirp_common.PowerLevel("Low", watts=5),
35
                    chirp_common.PowerLevel("Mid", watts=20),
36
                    chirp_common.PowerLevel("High", watts=35)]
37
SPECIALS = {'1L': -1,
38
            '1U': -2,
39
            '2L': -3,
40
            '2U': -4,
41
            'Home': -5}
42

    
43
MEM_FORMAT = """
44
#seekto 0x{skips:X};
45
u8 skips[13];
46

    
47
#seekto 0x{enables:X};
48
u8 enables[13];
49

    
50
struct mem_struct {{
51
    u8 unknown4:2,
52
       baud9600:1,
53
       am:1,
54
       unknown4b:4;
55
    u8 power:2,
56
       duplex:2,
57
       unknown1b:4;
58
    u8 unknown2:1,
59
       tone_enable:1,
60
       tone:6;
61
    bbcd freq[3];
62
    bbcd offset[3];
63
}};
64

    
65
#seekto 0x{memories:X};
66
struct mem_struct memory[99];
67
"""
68

    
69

    
70
@directory.register
71
class FT8100Radio(yaesu_clone.YaesuCloneModeRadio):
72
    """Implementation for Yaesu FT-8100"""
73
    MODEL = "FT-8100"
74

    
75
    _memstart = 0
76
    _memsize = 2968
77
    _block_lengths = [10, 32, 114, 101, 101, 97, 128, 128, 128, 128, 128, 9, 9,
78
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
79
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
80
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
81
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
82
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
83
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
84
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
85
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
86
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
87
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
88
                      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1]
89

    
90
    @classmethod
91
    def match_model(cls, data, path):
92
        if (len(data) == cls._memsize and
93
                data[1:10] == b'\x01\x01\x07\x08\x02\x01\x01\x00\x01'):
94
            return True
95

    
96
        return False
97

    
98
    def get_features(self):
99
        rf = chirp_common.RadioFeatures()
100
        rf.memory_bounds = (1, 99)
101
        rf.has_ctone = False
102
        rf.has_dtcs = False
103
        rf.has_dtcs_polarity = False
104
        rf.has_bank = False
105
        rf.has_name = False
106

    
107
        rf.valid_tones = list(TONES)
108
        rf.valid_modes = list(MODES)
109
        rf.valid_tmodes = list(TMODES)
110
        rf.valid_duplexes = list(DUPLEX)
111
        # This is not actually implemented, so don't expose it
112
        rf.valid_duplexes.remove('split')
113
        rf.valid_power_levels = POWER_LEVELS_VHF
114
        rf.has_sub_devices = self.VARIANT == ''
115

    
116
        rf.valid_tuning_steps = list(STEPS)
117
        # This is not implemented properly, so don't expose it
118
        rf.valid_tuning_steps.remove(12.5)
119
        rf.can_odd_split = False
120

    
121
        if self.VARIANT == 'VHF':
122
            rf.valid_bands = [(110000000, 550000000)]
123
        else:
124
            # This driver doesn't properly support the upper bound of 1300MHz
125
            # so limit us to 999MHz
126
            rf.valid_bands = [(750000000, 999000000)]
127

    
128
        # This is not actually implemented, so don't expose it
129
        # rf.valid_skips = SKIPS
130
        rf.valid_skips = []
131

    
132
        # TODO
133
        # rf.valid_special_chans = SPECIALS.keys()
134

    
135
        # TODO
136
        # rf.has_tuning_step = False
137
        return rf
138

    
139
    def sync_in(self):
140
        super(FT8100Radio, self).sync_in()
141
        self.pipe.write(bytes([yaesu_clone.CMD_ACK]))
142
        self.pipe.read(1)
143

    
144
    def sync_out(self):
145
        self.update_checksums()
146
        return _clone_out(self)
147

    
148
    def process_mmap(self):
149
        if not self._memstart:
150
            return
151

    
152
        mem_format = MEM_FORMAT.format(memories=self._memstart,
153
                                       skips=self._skipstart,
154
                                       enables=self._enablestart
155
                                       )
156

    
157
        self._memobj = bitwise.parse(mem_format, self._mmap)
158

    
159
    def get_sub_devices(self):
160
        return [FT8100RadioVHF(self._mmap), FT8100RadioUHF(self._mmap)]
161

    
162
    def get_memory(self, number):
163
        bit, byte = self._bit_byte(number)
164

    
165
        _mem = self._memobj.memory[number - 1]
166

    
167
        mem = chirp_common.Memory()
168
        mem.number = number
169

    
170
        mem.freq = int(_mem.freq) * 1000
171
        if _mem.tone >= len(TONES) or _mem.duplex >= len(DUPLEX):
172
            mem.empty = True
173
            return mem
174
        else:
175
            mem.rtone = TONES[_mem.tone]
176
        mem.tmode = TMODES[_mem.tone_enable]
177
        mem.mode = MODES[_mem.am]
178
        mem.duplex = DUPLEX[_mem.duplex]
179

    
180
        if _mem.duplex == DUPLEX.index("split"):
181
            tx_freq = int(_mem.offset) * 1000
182
            mem.offset = tx_freq - mem.freq
183
        else:
184
            mem.offset = int(_mem.offset) * 1000
185

    
186
        if mem.freq // 100 == 4:
187
            mem.power = POWER_LEVELS_UHF[_mem.power]
188
        else:
189
            mem.power = POWER_LEVELS_VHF[_mem.power]
190

    
191
        # M01 can't be disabled
192
        if not self._memobj.enables[byte] & bit and number != 1:
193
            mem.empty = True
194
        elif number == 1:
195
            mem.immutable = ['empty']
196

    
197
        return mem
198

    
199
    def get_raw_memory(self, number):
200
        return repr(self._memobj.memory[number - 1])
201

    
202
    def set_memory(self, mem):
203
        bit, byte = self._bit_byte(mem.number)
204

    
205
        _mem = self._memobj.memory[mem.number - 1]
206

    
207
        _mem.freq = mem.freq // 1000
208
        _mem.tone = TONES.index(mem.rtone)
209
        _mem.tone_enable = TMODES.index(mem.tmode)
210
        _mem.am = MODES.index(mem.mode)
211
        _mem.duplex = DUPLEX.index(mem.duplex)
212

    
213
        if mem.duplex == "split":
214
            tx_freq = mem.freq + mem.offset
215
            _mem.offset = (tx_freq % 10000000) // 1000
216
        else:
217
            _mem.offset = mem.offset // 1000
218

    
219
        if mem.power:
220
            _mem.power = POWER_LEVELS_VHF.index(mem.power)
221
        else:
222
            _mem.power = 0
223

    
224
        if mem.empty:
225
            self._memobj.enables[byte] &= ~bit
226
        else:
227
            self._memobj.enables[byte] |= bit
228

    
229
        # TODO expose these options
230
        _mem.baud9600 = 0
231

    
232
        # These need to be cleared, otherwise strange things happen
233
        _mem.unknown4 = 0
234
        _mem.unknown4b = 0
235
        _mem.unknown1b = 0
236
        _mem.unknown2 = 0
237

    
238
    def _checksums(self):
239
        return [yaesu_clone.YaesuChecksum(0x0000, 0x0B96)]
240

    
241
    # I didn't believe this myself, but it seems that there's a bit for
242
    # enabling VHF M01, but no bit for UHF01, and the enables are shifted down,
243
    # so that the first bit is for M02
244
    def _bit_byte(self, number):
245
        if self.VARIANT == 'VHF':
246
            bit = 1 << ((number - 1) % 8)
247
            byte = (number - 1) // 8
248
        else:
249
            bit = 1 << ((number - 2) % 8)
250
            byte = (number - 2) // 8
251

    
252
        return bit, byte
253

    
254

    
255
class FT8100RadioVHF(FT8100Radio):
256
    """Yaesu FT-8100 VHF subdevice"""
257
    VARIANT = "VHF"
258
    _memstart = 0x447
259
    _skipstart = 0x02D
260
    _enablestart = 0x04D
261

    
262

    
263
class FT8100RadioUHF(FT8100Radio):
264
    """Yaesu FT-8100 UHF subdevice"""
265
    VARIANT = "UHF"
266
    _memstart = 0x7E6
267
    _skipstart = 0x03A
268
    _enablestart = 0x05A
269

    
270

    
271
def _clone_out(radio):
272
    try:
273
        return __clone_out(radio)
274
    except Exception as e:
275
        raise errors.RadioError("Failed to communicate with the radio: %s" % e)
276

    
277

    
278
def __clone_out(radio):
279
    pipe = radio.pipe
280
    block_lengths = radio._block_lengths
281
    total_written = 0
282

    
283
    def _status():
284
        status = chirp_common.Status()
285
        status.msg = "Cloning to radio"
286
        status.max = sum(block_lengths)
287
        status.cur = total_written
288
        radio.status_fn(status)
289

    
290
    start = time.time()
291

    
292
    pos = 0
293
    for block in radio._block_lengths:
294
        LOG.debug("\nSending %i-%i" % (pos, pos + block))
295
        out = radio.get_mmap()[pos:pos + block]
296

    
297
        # need to chew byte-by-byte here or else we lose the ACK...not sure why
298
        for b in out:
299
            pipe.write(bytes([b]))
300
            pipe.read(1)  # chew the echo
301

    
302
        ack = pipe.read(1)
303

    
304
        if ack[0] != yaesu_clone.CMD_ACK:
305
            raise Exception("block not ack'ed: %s" % repr(ack))
306

    
307
        total_written += len(out)
308
        _status()
309

    
310
        pos += block
311

    
312
    LOG.debug("Clone completed in %i seconds" % (time.time() - start))
313

    
314
    return True
(7-7/18)