Project

General

Profile

Feature #7979 » rh5r_v2_TX_off.py

Jim Unroe, 06/24/2020 10:40 AM

 
1
# Copyright 2017 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
"""Rugged RH5R V2 radio management module"""
17

    
18
import struct
19
import logging
20

    
21
from chirp import chirp_common, bitwise, errors, directory, memmap, util
22
from chirp.settings import RadioSetting, RadioSettingGroup, \
23
    RadioSettingValueInteger, RadioSettingValueList, \
24
    RadioSettingValueBoolean, RadioSettingValueString, \
25
    RadioSettings
26

    
27

    
28
LOG = logging.getLogger(__name__)
29

    
30

    
31
def _identify(radio):
32
    try:
33
        radio.pipe.write("PGM2015")
34
        ack = radio.pipe.read(2)
35
        if ack != "\x06\x30":
36
            raise errors.RadioError("Radio did not ACK first command: %r" %
37
                                    ack)
38
    except:
39
        LOG.exception('')
40
        raise errors.RadioError("Unable to communicate with the radio")
41

    
42

    
43
def _download(radio):
44
    _identify(radio)
45
    data = []
46
    for i in range(0, 0x2000, 0x40):
47
        msg = struct.pack('>cHb', 'R', i, 0x40)
48
        radio.pipe.write(msg)
49
        block = radio.pipe.read(0x40 + 4)
50
        if len(block) != (0x40 + 4):
51
            raise errors.RadioError("Radio sent a short block (%02x/%02x)" % (
52
                len(block), 0x44))
53
        data += block[4:]
54

    
55
        if radio.status_fn:
56
            status = chirp_common.Status()
57
            status.cur = i
58
            status.max = 0x2000
59
            status.msg = "Cloning from radio"
60
            radio.status_fn(status)
61

    
62
    radio.pipe.write("E")
63
    data += 'PGM2015'
64

    
65
    return memmap.MemoryMap(data)
66

    
67

    
68
def _upload(radio):
69
    _identify(radio)
70
    for i in range(0, 0x2000, 0x40):
71
        msg = struct.pack('>cHb', 'W', i, 0x40)
72
        msg += radio._mmap[i:(i + 0x40)]
73
        radio.pipe.write(msg)
74
        ack = radio.pipe.read(1)
75
        if ack != '\x06':
76
            raise errors.RadioError('Radio did not ACK block %i (0x%04x)' % (
77
                i, i))
78

    
79
        if radio.status_fn:
80
            status = chirp_common.Status()
81
            status.cur = i
82
            status.max = 0x2000
83
            status.msg = "Cloning from radio"
84
            radio.status_fn(status)
85

    
86
    radio.pipe.write("E")
87

    
88

    
89
MEM_FORMAT = """
90
struct memory {
91
  bbcd rx_freq[4];
92
  bbcd tx_freq[4];
93
  lbcd rx_tone[2];
94
  lbcd tx_tone[2];
95

    
96
  u8 unknown10:5,
97
     highpower:1,
98
     unknown11:2;
99
  u8 unknown20:4,
100
     narrow:1,
101
     unknown21:3;
102
  u8 unknown31:1,
103
     scanadd:1,
104
     unknown32:6;
105
  u8 unknown4;
106
};
107

    
108
struct name {
109
  char name[7];
110
};
111

    
112
#seekto 0x0010;
113
struct memory channels[128];
114

    
115
#seekto 0x08C0;
116
struct name names[128];
117

    
118
#seekto 0x2020;
119
struct memory vfo1;
120
struct memory vfo2;
121
"""
122

    
123

    
124
POWER_LEVELS = [chirp_common.PowerLevel('Low', watts=1),
125
                chirp_common.PowerLevel('High', watts=5)]
126

    
127

    
128
class TYTTHUVF8_V2(chirp_common.CloneModeRadio):
129
    VENDOR = "TYT"
130
    MODEL = "TH-UVF8F"
131
    BAUD_RATE = 9600
132
    _FILEID = 'OEMOEM \XFF'
133

    
134
    def get_features(self):
135
        rf = chirp_common.RadioFeatures()
136
        rf.memory_bounds = (1, 128)
137
        rf.has_bank = False
138
        rf.has_ctone = True
139
        rf.valid_tuning_steps = [5, 6.25, 10, 12.5]
140
        rf.has_tuning_step = False
141
        rf.has_cross = True
142
        rf.has_rx_dtcs = True
143
        rf.has_settings = False
144
        rf.can_odd_split = False
145
        rf.valid_duplexes = ['', '-', '+', "off"]
146
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
147
        rf.valid_characters = chirp_common.CHARSET_UPPER_NUMERIC + "-"
148
        rf.valid_bands = [(136000000, 174000000),
149
                          (400000000, 480000000)]
150
        rf.valid_skips = ["", "S"]
151
        rf.valid_power_levels = POWER_LEVELS
152
        rf.valid_modes = ["FM", "NFM"]
153
        rf.valid_name_length = 7
154
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
155
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
156
        return rf
157

    
158
    def sync_in(self):
159
        self._mmap = _download(self)
160
        self.process_mmap()
161

    
162
    def sync_out(self):
163
        _upload(self)
164

    
165
    @classmethod
166
    def match_model(cls, filedata, filename):
167
        return (filedata.endswith("PGM2015") and
168
                filedata[0x840:0x848] == cls._FILEID)
169

    
170
    def process_mmap(self):
171
        print MEM_FORMAT
172
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
173

    
174
    def _is_txinh(self, _mem):
175
        raw_tx = ""
176
        for i in range(0, 4):
177
            raw_tx += _mem.tx_freq[i].get_raw()
178
        return raw_tx == "\x00\x00\x00\x00"
179

    
180
    def get_raw_memory(self, number):
181
        return (repr(self._memobj.channels[number - 1]) +
182
                repr(self._memobj.names[number - 1]))
183

    
184
    def _get_memobjs(self, number):
185
        return (self._memobj.channels[number - 1],
186
                self._memobj.names[number - 1])
187

    
188
    def _decode_tone(self, toneval):
189
        pol = "N"
190
        rawval = (toneval[1].get_bits(0xFF) << 8) | toneval[0].get_bits(0xFF)
191

    
192
        if toneval[0].get_bits(0xFF) == 0xFF:
193
            mode = ""
194
            val = 0
195
        elif toneval[1].get_bits(0xC0) == 0xC0:
196
            mode = "DTCS"
197
            val = int("%x" % (rawval & 0x3FFF))
198
            pol = "R"
199
        elif toneval[1].get_bits(0x80):
200
            mode = "DTCS"
201
            val = int("%x" % (rawval & 0x3FFF))
202
        else:
203
            mode = "Tone"
204
            val = int(toneval) / 10.0
205

    
206
        return mode, val, pol
207

    
208
    def _encode_tone(self, _toneval, mode, val, pol):
209
        toneval = 0
210
        if mode == "Tone":
211
            toneval = int("%i" % (val * 10), 16)
212
        elif mode == "DTCS":
213
            toneval = int("%i" % val, 16)
214
            toneval |= 0x8000
215
            if pol == "R":
216
                toneval |= 0x4000
217
        else:
218
            toneval = 0xFFFF
219

    
220
        _toneval[0].set_raw(toneval & 0xFF)
221
        _toneval[1].set_raw((toneval >> 8) & 0xFF)
222

    
223
    def get_memory(self, number):
224
        _mem, _name = self._get_memobjs(number)
225

    
226
        mem = chirp_common.Memory()
227

    
228
        if isinstance(number, str):
229
            mem.number = SPECIALS[number]
230
            mem.extd_number = number
231
        else:
232
            mem.number = number
233

    
234
        if _mem.get_raw().startswith("\xFF\xFF\xFF\xFF"):
235
            mem.empty = True
236
            return mem
237

    
238
        mem.freq = int(_mem.rx_freq) * 10
239
        offset = (int(_mem.tx_freq) - int(_mem.rx_freq)) * 10
240
        if self._is_txinh(_mem):
241
            mem.duplex = "off"
242
            mem.offset = 0
243
        elif not offset:
244
            mem.offset = 0
245
            mem.duplex = ''
246
        elif offset < 0:
247
            mem.offset = abs(offset)
248
            mem.duplex = '-'
249
        else:
250
            mem.offset = offset
251
            mem.duplex = '+'
252

    
253
        txmode, txval, txpol = self._decode_tone(_mem.tx_tone)
254
        rxmode, rxval, rxpol = self._decode_tone(_mem.rx_tone)
255

    
256
        chirp_common.split_tone_decode(mem,
257
                                       (txmode, txval, txpol),
258
                                       (rxmode, rxval, rxpol))
259

    
260
        mem.mode = 'NFM' if _mem.narrow else 'FM'
261
        mem.skip = '' if _mem.scanadd else 'S'
262
        mem.power = POWER_LEVELS[int(_mem.highpower)]
263
        mem.name = str(_name.name).rstrip('\xFF ')
264

    
265
        return mem
266

    
267
    def set_memory(self, mem):
268
        _mem, _name = self._get_memobjs(mem.number)
269
        if mem.empty:
270
            _mem.set_raw('\xFF' * 16)
271
            _name.set_raw('\xFF' * 7)
272
            return
273
        _mem.set_raw('\x00' * 16)
274

    
275
        _mem.rx_freq = mem.freq / 10
276
        if mem.duplex == "off":
277
            for i in range(0, 4):
278
                _mem.tx_freq[i].set_raw("\xFF")
279
        elif mem.duplex == '-':
280
            mult = -1
281
        elif not mem.duplex:
282
            mult = 0
283
        else:
284
            mult = 1
285
        _mem.tx_freq = (mem.freq + (mem.offset * mult)) / 10
286

    
287
        (txmode, txval, txpol), (rxmode, rxval, rxpol) = \
288
                chirp_common.split_tone_encode(mem)
289

    
290
        self._encode_tone(_mem.tx_tone, txmode, txval, txpol)
291
        self._encode_tone(_mem.rx_tone, rxmode, rxval, rxpol)
292

    
293
        _mem.narrow = mem.mode == 'NFM'
294
        _mem.scanadd = mem.skip != 'S'
295
        _mem.highpower = POWER_LEVELS.index(mem.power) if mem.power else 1
296
        _name.name = mem.name.rstrip(' ').ljust(7, '\xFF')
297

    
298

    
299
@directory.register
300
class RH5RV2(TYTTHUVF8_V2):
301
    VENDOR = "Rugged"
302
    MODEL = "RH5R-V2"
303
    _FILEID = 'RUGGED \xFF'
(2-2/2)