Project

General

Profile

Bug #10904 » ft8100.py

c87402d4 - Dan Smith, 10/19/2023 09:11 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
        # This driver doesn't properly support the upper bound of 1300MHz
122
        # so limit us to 999MHz
123
        rf.valid_bands = [(110000000, 550000000),
124
                          (750000000, 999000000)]
125

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

    
130
        # TODO
131
        # rf.valid_special_chans = SPECIALS.keys()
132

    
133
        # TODO
134
        # rf.has_tuning_step = False
135
        return rf
136

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

    
142
    def sync_out(self):
143
        self.update_checksums()
144
        return _clone_out(self)
145

    
146
    def process_mmap(self):
147
        if not self._memstart:
148
            return
149

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

    
155
        self._memobj = bitwise.parse(mem_format, self._mmap)
156

    
157
    def get_sub_devices(self):
158
        return [FT8100RadioLeft(self._mmap), FT8100RadioRight(self._mmap)]
159

    
160
    def get_memory(self, number):
161
        bit, byte = self._bit_byte(number)
162

    
163
        _mem = self._memobj.memory[number - 1]
164

    
165
        mem = chirp_common.Memory()
166
        mem.number = number
167

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

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

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

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

    
195
        return mem
196

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

    
200
    def set_memory(self, mem):
201
        bit, byte = self._bit_byte(mem.number)
202

    
203
        _mem = self._memobj.memory[mem.number - 1]
204

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

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

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

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

    
227
        # TODO expose these options
228
        _mem.baud9600 = 0
229

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

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

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

    
250
        return bit, byte
251

    
252

    
253
class FT8100RadioLeft(FT8100Radio):
254
    """Yaesu FT-8100 Left subdevice"""
255
    VARIANT = "Left"
256
    _memstart = 0x447
257
    _skipstart = 0x02D
258
    _enablestart = 0x04D
259

    
260

    
261
class FT8100RadioRight(FT8100Radio):
262
    """Yaesu FT-8100 Right subdevice"""
263
    VARIANT = "Right"
264
    _memstart = 0x7E6
265
    _skipstart = 0x03A
266
    _enablestart = 0x05A
267

    
268

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

    
275

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

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

    
288
    start = time.time()
289

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

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

    
300
        ack = pipe.read(1)
301

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

    
305
        total_written += len(out)
306
        _status()
307

    
308
        pos += block
309

    
310
    LOG.debug("Clone completed in %i seconds" % (time.time() - start))
311

    
312
    return True
(10-10/18)