Project

General

Profile

Bug #10226 » tg_uv2p.py

Ran Katz, 09/03/2023 12:15 AM

 
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 2 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
# This driver was derived from the:
17
# Quansheng TG-UV2 Utility by Mike Nix <mnix@wanm.com.au>
18
# (So thanks Mike!)
19

    
20
import struct
21
import logging
22
import serial
23
from chirp import chirp_common, directory, bitwise, memmap, errors, util
24
from chirp.settings import RadioSetting, RadioSettingGroup, \
25
                RadioSettingValueBoolean, RadioSettingValueList, \
26
                RadioSettingValueInteger, RadioSettingValueFloat, \
27
                RadioSettingValueMap, RadioSettings
28

    
29
LOG = logging.getLogger(__name__)
30

    
31
mem_format = """
32
struct memory {
33
  bbcd freq[4];
34
  bbcd offset[4];
35
  u8 rxtone;
36
  u8 txtone;
37
  u8 unknown1:2,
38
     txtmode:2,
39
     unknown2:2,
40
     rxtmode:2;
41
  u8 duplex;
42
  u8 unknown3:3,
43
     isnarrow:1,
44
     unknown4:2,
45
     not_scramble:1,
46
     not_revfreq:1;
47
  u8 flag3;
48
  u8 step;
49
  u8 power;
50
};
51

    
52
struct bandflag {
53
    u8 scanadd:1,
54
        unknown1:3,
55
        band:4;
56
};
57

    
58
struct tguv2_config {
59
    u8 unknown1;
60
    u8 squelch;
61
    u8 time_out_timer;
62
    u8 priority_channel;
63

    
64
    u8 unknown2:7,
65
        keyunlocked:1;
66
    u8 busy_lockout;
67
    u8 vox;
68
    u8 unknown3;
69

    
70
    u8 beep_tone_disabled;
71
    u8 display;
72
    u8 step;
73
    u8 unknown4;
74

    
75
    u8 unknown5;
76
    u8 rxmode;
77
    u8 unknown6:7,
78
        not_end_tone_elim:1;
79
    u8 vfo_mode;
80
};
81

    
82
struct vfo {
83
    u8 current;
84
    u8 chan;
85
    u8 memno;
86
};
87

    
88
struct name {
89
  u8 name[6];
90
  u8 unknown1[10];
91
};
92

    
93
#seekto 0x0000;
94
char ident[32];
95
u8 blank[16];
96

    
97
struct memory channels[200];
98
struct memory bands[5];
99

    
100
#seekto 0x0D30;
101
struct bandflag bandflags[200];
102

    
103
#seekto 0x0E30;
104
struct tguv2_config settings;
105
struct vfo vfos[2];
106
u8 unk5;
107
u8 reserved2[9];
108
u8 band_restrict;
109
u8 txen350390;
110

    
111
#seekto 0x0F30;
112
struct name names[200];
113

    
114
"""
115

    
116

    
117
def do_program_mode(radio):
118
    radio.pipe.write(b"\x02PnOGdAM")
119
    for x in range(10):
120
        ack = radio.pipe.read(1)
121
        if ack == b'\x06':
122
            break
123
    else:
124
        raise errors.RadioError("Radio did not ack programming mode")
125

    
126

    
127
def do_ident(radio):
128
    radio.pipe.timeout = 3
129
    radio.pipe.stopbits = serial.STOPBITS_TWO
130
    do_program_mode(radio)
131
    radio.pipe.write(b"\x4D\x02")
132
    ident = radio.pipe.read(8)
133
    LOG.debug(util.hexprint(ident))
134
    if not ident.startswith(b'P5555'):
135
        LOG.debug("First ident attempt (x4D, x02) failed trying 0x40,x02")
136
        do_program_mode(radio)
137
        radio.pipe.write(b"\x40\x02")
138
        ident = radio.pipe.read(8)
139
        LOG.debug(util.hexprint(ident))
140
        if not ident.startswith(b'P5555'):
141
            raise errors.RadioError("Unsupported model")
142
    radio.pipe.write(b"\x06")
143
    ack = radio.pipe.read(1)
144
    if ack != b"\x06":
145
        raise errors.RadioError("Radio did not ack ident")
146

    
147

    
148
def do_status(radio, direction, addr):
149
    status = chirp_common.Status()
150
    status.msg = "Cloning %s radio" % direction
151
    status.cur = addr
152
    status.max = 0x2000
153
    radio.status_fn(status)
154

    
155

    
156
def do_download(radio):
157
    do_ident(radio)
158
    data = b"TG-UV2+ Radio Program Data v1.0\x00"
159
    data += (b"\x00" * 16)
160

    
161
    firstack = None
162
    for i in range(0, 0x2000, 8):
163
        frame = struct.pack(">cHB", b"R", i, 8)
164
        radio.pipe.write(frame)
165
        result = radio.pipe.read(12)
166
        if not (result[0:1] == b"W" and frame[1:4] == result[1:4]):
167
            LOG.debug(util.hexprint(result))
168
            raise errors.RadioError("Invalid response for address 0x%04x" % i)
169
        radio.pipe.write(b"\x06")
170
        ack = radio.pipe.read(1)
171
        if not firstack:
172
            firstack = ack
173
        else:
174
            if not ack == firstack:
175
                LOG.debug("first ack: %s ack received: %s",
176
                          util.hexprint(firstack), util.hexprint(ack))
177
                raise errors.RadioError("Unexpected response")
178
        data += result[4:]
179
        do_status(radio, "from", i)
180

    
181
    return memmap.MemoryMapBytes(data)
182

    
183

    
184
def do_upload(radio):
185
    do_ident(radio)
186
    data = radio._mmap[0x0030:]
187

    
188
    for i in range(0, 0x2000, 8):
189
        frame = struct.pack(">cHB", b"W", i, 8)
190
        frame += data[i:i + 8]
191
        radio.pipe.write(frame)
192
        ack = radio.pipe.read(1)
193
        if ack != b"\x06":
194
            LOG.debug("Radio NAK'd block at address 0x%04x" % i)
195
            raise errors.RadioError(
196
                    "Radio NAK'd block at address 0x%04x" % i)
197
        LOG.debug("Radio ACK'd block at address 0x%04x" % i)
198
        do_status(radio, "to", i)
199

    
200

    
201
DUPLEX = ["", "+", "-"]
202
TGUV2P_STEPS = [5, 6.25, 10, 12.5, 15, 20, 25, 30, 50, 100]
203
CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_|* +-"
204
POWER_LEVELS = [chirp_common.PowerLevel("High", watts=10),
205
                chirp_common.PowerLevel("Med", watts=5),
206
                chirp_common.PowerLevel("Low", watts=1)]
207
POWER_LEVELS_STR = ["High", "Med", "Low"]
208
VALID_BANDS = [(88000000, 108000000),
209
               (136000000, 174000000),
210
               (350000000, 390000000),
211
               (400000000, 470000000),
212
               (470000000, 520000000)]
213

    
214

    
215
@directory.register
216
class QuanshengTGUV2P(chirp_common.CloneModeRadio,
217
                      chirp_common.ExperimentalRadio):
218
    """Quansheng TG-UV2+"""
219
    VENDOR = "Quansheng"
220
    MODEL = "TG-UV2+"
221
    BAUD_RATE = 9600
222
    NEEDS_COMPAT_SERIAL = False
223

    
224
    _memsize = 0x2000
225

    
226
    @classmethod
227
    def get_prompts(cls):
228
        rp = chirp_common.RadioPrompts()
229
        rp.experimental = \
230
            ('Experimental version for TG-UV2/2+ radios '
231
             'Proceed at your own risk!')
232
        rp.pre_download = _(
233
            "1. Turn radio off.\n"
234
            "2. Connect cable to mic/spkr connector.\n"
235
            "3. Make sure connector is firmly connected.\n"
236
            "4. Turn radio on.\n"
237
            "5. Ensure that the radio is tuned to channel with no"
238
            " activity.\n"
239
            "6. Click OK to download image from device.\n")
240
        rp.pre_upload = _(
241
            "1. Turn radio off.\n"
242
            "2. Connect cable to mic/spkr connector.\n"
243
            "3. Make sure connector is firmly connected.\n"
244
            "4. Turn radio on.\n"
245
            "5. Ensure that the radio is tuned to channel with no"
246
            " activity.\n"
247
            "6. Click OK to upload image to device.\n")
248
        return rp
249

    
250
    def get_features(self):
251
        rf = chirp_common.RadioFeatures()
252
        rf.has_settings = True
253
        rf.has_cross = True
254
        rf.has_rx_dtcs = True
255
        rf.has_dtcs_polarity = True
256
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
257
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
258
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
259
        rf.valid_duplexes = DUPLEX
260
        rf.can_odd_split = False
261
        rf.valid_skips = ["", "S"]
262
        rf.valid_characters = CHARSET
263
        rf.valid_name_length = 6
264
        rf.valid_tuning_steps = TGUV2P_STEPS
265
        rf.valid_bands = VALID_BANDS
266

    
267
        rf.valid_modes = ["FM", "NFM"]
268
        rf.valid_power_levels = POWER_LEVELS
269
        rf.has_ctone = True
270
        rf.has_bank = False
271
        rf.has_tuning_step = True
272
        rf.memory_bounds = (0, 199)
273
        return rf
274

    
275
    def sync_in(self):
276
        try:
277
            self._mmap = do_download(self)
278
        except errors.RadioError:
279
            raise
280
        except Exception as e:
281
            raise errors.RadioError("Failed to communicate with radio: %s" % e)
282
        self.process_mmap()
283

    
284
    def sync_out(self):
285
        try:
286
            do_upload(self)
287
        except errors.RadioError:
288
            raise
289
        except Exception as e:
290
            raise errors.RadioError("Failed to communicate with radio: %s" % e)
291

    
292
    def process_mmap(self):
293
        self._memobj = bitwise.parse(mem_format, self._mmap)
294

    
295
    def get_raw_memory(self, number):
296
        return repr(self._memobj.channels[number])
297

    
298
    def _decode_tone(self, _mem, which):
299
        def _get(field):
300
            return getattr(_mem, "%s%s" % (which, field))
301

    
302
        value = _get('tone')
303
        tmode = _get('tmode')
304

    
305
        if (value <= 104) and (tmode <= 3):
306
            if tmode == 0:
307
                mode = val = pol = None
308
            elif tmode == 1:
309
                mode = 'Tone'
310
                val = chirp_common.TONES[value]
311
                pol = None
312
            else:
313
                mode = 'DTCS'
314
                val = chirp_common.DTCS_CODES[value]
315
                pol = "N" if (tmode == 2) else "R"
316
        else:
317
            mode = val = pol = None
318

    
319
        return mode, val, pol
320

    
321
    def _encode_tone(self, _mem, which, mode, val, pol):
322
        def _set(field, value):
323
            setattr(_mem, "%s%s" % (which, field), value)
324

    
325
        if (mode == "Tone"):
326
            _set("tone", chirp_common.TONES.index(val))
327
            _set("tmode", 0x01)
328
        elif mode == "DTCS":
329
            _set("tone", chirp_common.DTCS_CODES.index(val))
330
            if pol == "N":
331
                _set("tmode", 0x02)
332
            else:
333
                _set("tmode", 0x03)
334
        else:
335
            _set("tone", 0)
336
            _set("tmode", 0)
337

    
338
    def _get_memobjs(self, number):
339
        if isinstance(number, str):
340
            return (getattr(self._memobj, number.lower()), None)
341

    
342
        else:
343
            return (self._memobj.channels[number],
344
                    self._memobj.bandflags[number],
345
                    self._memobj.names[number].name)
346

    
347
    def get_memory(self, number):
348
        _mem, _bf, _nam = self._get_memobjs(number)
349
        mem = chirp_common.Memory()
350
        if isinstance(number, str):
351
            mem.extd_number = number
352
        else:
353
            mem.number = number
354

    
355
        if (_mem.freq.get_raw()[0] == "\xFF") or (_bf.band == "\x0F"):
356
            mem.empty = True
357
            return mem
358

    
359
        mem.freq = int(_mem.freq) * 10
360

    
361
        if _mem.offset.get_raw()[0] == "\xFF":
362
            mem.offset = 0
363
        else:
364
            mem.offset = int(_mem.offset) * 10
365

    
366
        chirp_common.split_tone_decode(
367
            mem,
368
            self._decode_tone(_mem, "tx"),
369
            self._decode_tone(_mem, "rx"))
370

    
371
        if 'step' in _mem and _mem.step > len(TGUV2P_STEPS):
372
            _mem.step = 0x00
373
        mem.tuning_step = TGUV2P_STEPS[_mem.step]
374
        mem.duplex = DUPLEX[_mem.duplex]
375
        mem.mode = _mem.isnarrow and "NFM" or "FM"
376
        mem.skip = "" if bool(_bf.scanadd) else "S"
377
        mem.power = POWER_LEVELS[_mem.power]
378

    
379
        if _nam:
380
            for char in _nam:
381
                try:
382
                    mem.name += CHARSET[char]
383
                except IndexError:
384
                    break
385
            mem.name = mem.name.rstrip()
386

    
387
        mem.extra = RadioSettingGroup("Extra", "extra")
388

    
389
        rs = RadioSetting("not_scramble", "(not)SCRAMBLE",
390
                          RadioSettingValueBoolean(_mem.not_scramble))
391
        mem.extra.append(rs)
392

    
393
        rs = RadioSetting("not_revfreq", "(not)Reverse Duplex",
394
                          RadioSettingValueBoolean(_mem.not_revfreq))
395
        mem.extra.append(rs)
396

    
397
        return mem
398

    
399
    def set_memory(self, mem):
400
        _mem, _bf, _nam = self._get_memobjs(mem.number)
401

    
402
        _bf.set_raw("\xFF")
403

    
404
        if mem.empty:
405
            _mem.set_raw("\xFF" * 16)
406
            return
407

    
408
        _mem.set_raw("\x00" * 12 + "\xFF" * 2 + "\x00"*2)
409

    
410
        _bf.scanadd = int(mem.skip != "S")
411
        _bf.band = 0x0F
412
        for idx, ele in enumerate(VALID_BANDS):
413
            if mem.freq >= ele[0] and mem.freq <= ele[1]:
414
                _bf.band = idx
415

    
416
        _mem.freq = mem.freq / 10
417
        _mem.offset = mem.offset / 10
418

    
419
        tx, rx = chirp_common.split_tone_encode(mem)
420
        self._encode_tone(_mem, 'tx', *tx)
421
        self._encode_tone(_mem, 'rx', *rx)
422

    
423
        _mem.duplex = DUPLEX.index(mem.duplex)
424
        _mem.isnarrow = mem.mode == "NFM"
425
        _mem.step = TGUV2P_STEPS.index(mem.tuning_step)
426

    
427
        if mem.power is None:
428
            _mem.power = 0
429
        else:
430
            _mem.power = POWER_LEVELS.index(mem.power)
431

    
432
        if _nam:
433
            for i in range(0, 6):
434
                try:
435
                    _nam[i] = CHARSET.index(mem.name[i])
436
                except IndexError:
437
                    _nam[i] = 0xFF
438

    
439
        for setting in mem.extra:
440
            setattr(_mem, setting.get_name(), setting.value)
441

    
442
    def get_settings(self):
443
        _settings = self._memobj.settings
444
        _vfoa = self._memobj.vfos[0]
445
        _vfob = self._memobj.vfos[1]
446
        _bandsettings = self._memobj.bands
447

    
448
        cfg_grp = RadioSettingGroup("cfg_grp", "Configuration")
449
        vfoa_grp = RadioSettingGroup(
450
            "vfoa_grp", "VFO A Settings\n  (Current Status, Read Only)")
451
        vfob_grp = RadioSettingGroup(
452
            "vfob_grp", "VFO B Settings\n  (Current Status, Read Only)")
453

    
454
        group = RadioSettings(cfg_grp, vfoa_grp, vfob_grp)
455
        #
456
        # Configuration Settings
457
        #
458

    
459
        # TX time out timer:
460
        options = ["Off"] + ["%s min" % x for x in range(1, 10)]
461
        rs = RadioSetting("time_out_timer", "TX Time Out Timer",
462
                          RadioSettingValueList(
463
                              options, options[_settings.time_out_timer]))
464
        cfg_grp.append(rs)
465

    
466
        # Display mode
467
        options = ["Frequency", "Channel", "Name"]
468
        rs = RadioSetting("display", "Channel Display Mode",
469
                          RadioSettingValueList(
470
                              options, options[_settings.display]))
471
        cfg_grp.append(rs)
472

    
473
        # Squelch level
474
        rs = RadioSetting("squelch", "Squelch Level",
475
                          RadioSettingValueInteger(0, 9, _settings.squelch))
476
        cfg_grp.append(rs)
477

    
478
        # Vox level
479
        mem_vals = list(range(10))
480
        user_options = [str(x) for x in mem_vals]
481
        user_options[0] = "Off"
482
        options_map = list(zip(user_options, mem_vals))
483

    
484
        rs = RadioSetting("vox", "VOX Level",
485
                          RadioSettingValueMap(options_map, _settings.vox))
486
        cfg_grp.append(rs)
487

    
488
        # Keypad beep
489
        rs = RadioSetting("beep_tone_disabled", "Beep Prompt",
490
                          RadioSettingValueBoolean(
491
                               not _settings.beep_tone_disabled))
492
        cfg_grp.append(rs)
493

    
494
        # Dual watch/crossband
495
        options = ["Dual Watch", "CrossBand", "Normal"]
496
        if _settings.rxmode >= 2:
497
            _rxmode = 2
498
        else:
499
            _rxmode = _settings.rxmode
500
        rs = RadioSetting("rxmode", "Dual Watch/CrossBand Monitor",
501
                          RadioSettingValueList(
502
                            options, options[_rxmode]))
503
        cfg_grp.append(rs)
504

    
505
        # Busy channel lock
506
        rs = RadioSetting("busy_lockout", "Busy Channel Lock",
507
                          RadioSettingValueBoolean(
508
                             not _settings.busy_lockout))
509
        cfg_grp.append(rs)
510

    
511
        # Keypad lock
512
        rs = RadioSetting("keyunlocked", "Keypad Lock",
513
                          RadioSettingValueBoolean(
514
                              not _settings.keyunlocked))
515
        cfg_grp.append(rs)
516

    
517
        # Priority channel
518
        mem_vals = list(range(200))
519
        user_options = [str(x) for x in mem_vals]
520
        mem_vals.insert(0, 0xFF)
521
        user_options.insert(0, "Not Set")
522
        options_map = list(zip(user_options, mem_vals))
523
        if _settings.priority_channel >= 200:
524
            _priority_ch = 0xFF
525
        else:
526
            _priority_ch = _settings.priority_channel
527
        rs = RadioSetting(
528
            "priority_channel",
529
            "Priority Channel \n"
530
            "Note: Unused channels,\nor channels "
531
            "in the\nbroadcast FM band,\nwill not be set",
532
            RadioSettingValueMap(options_map, _priority_ch))
533
        cfg_grp.append(rs)
534

    
535
        # Step
536
        mem_vals = list(range(0, len(TGUV2P_STEPS)))
537
        mem_vals.append(0xFF)
538
        user_options = [(str(x) + " kHz") for x in TGUV2P_STEPS]
539
        user_options.append("Unknown")
540
        options_map = list(zip(user_options, mem_vals))
541

    
542
        rs = RadioSetting("step", "Current (VFO?) step size",
543
                          RadioSettingValueMap(options_map, _settings.step))
544
        cfg_grp.append(rs)
545

    
546
        # End (Tail) tone elimination
547
        mem_vals = [0, 1]
548
        user_options = ["Tone Elimination On", "Tone Elimination Off"]
549
        options_map = list(zip(user_options, mem_vals))
550

    
551
        rs = RadioSetting("not_end_tone_elim", "Tx End Tone Elimination",
552
                          RadioSettingValueMap(options_map,
553
                                               _settings.not_end_tone_elim))
554
        cfg_grp.append(rs)
555

    
556
        # VFO mode
557

    
558
        if _settings.vfo_mode >= 1:
559
            _vfo_mode = 0xFF
560
        else:
561
            _vfo_mode = _settings.vfo_mode
562
        mem_vals = [0xFF, 0]
563
        user_options = ["VFO Mode Enabled", "VFO Mode Disabled"]
564
        options_map = list(zip(user_options, mem_vals))
565

    
566
        rs = RadioSetting("vfo_mode", "VFO (CH only) mode",
567
                          RadioSettingValueMap(options_map, _vfo_mode))
568
        cfg_grp.append(rs)
569

    
570
        #
571
        # VFO Settings
572
        #
573

    
574
        vfo_groups = [vfoa_grp, vfob_grp]
575
        vfo_mem = [_vfoa, _vfob]
576
        vfo_lower = ["vfoa", "vfob"]
577
        vfo_upper = ["VFOA", "VFOB"]
578

    
579
        for idx, vfo_group in enumerate(vfo_groups):
580

    
581
            options = ["Channel", "Frequency"]
582
            tempvar = 0 if (vfo_mem[idx].current < 200) else 1
583
            rs = RadioSetting(vfo_lower[idx] + "_mode", vfo_upper[idx]+" Mode",
584
                              RadioSettingValueList(
585
                                  options, options[tempvar]))
586
            vfo_group.append(rs)
587

    
588
            if tempvar == 0:
589
                rs = RadioSetting(vfo_lower[idx] + "_ch",
590
                                  vfo_upper[idx] + " Channel",
591
                                  RadioSettingValueInteger(
592
                                      0, 199, vfo_mem[idx].current))
593
                vfo_group.append(rs)
594
            else:
595
                band_num = vfo_mem[idx].current - 200
596
                freq = int(_bandsettings[band_num].freq) * 10
597
                offset = int(_bandsettings[band_num].offset) * 10
598
                txtmode = _bandsettings[band_num].txtmode
599
                rxtmode = _bandsettings[band_num].rxtmode
600

    
601
                rs = RadioSetting(vfo_lower[idx] + "_freq",
602
                                  vfo_upper[idx] + " Frequency",
603
                                  RadioSettingValueFloat(
604
                                      0.0, 520.0, freq / 1000000.0,
605
                                      precision=6))
606
                vfo_group.append(rs)
607

    
608
                if offset > 70e6:
609
                    offset = 0
610
                rs = RadioSetting(vfo_lower[idx] + "_offset",
611
                                  vfo_upper[idx] + " Offset",
612
                                  RadioSettingValueFloat(
613
                                      0.0, 69.995, offset / 100000.0,
614
                                      resolution=0.005))
615
                vfo_group.append(rs)
616

    
617
                rs = RadioSetting(vfo_lower[idx] + "_duplex",
618
                                  vfo_upper[idx] + " Shift",
619
                                  RadioSettingValueList(
620
                                      DUPLEX,
621
                                      DUPLEX[_bandsettings[band_num].duplex]))
622
                vfo_group.append(rs)
623

    
624
                rs = RadioSetting(
625
                    vfo_lower[idx] + "_step",
626
                    vfo_upper[idx] + " Step",
627
                    RadioSettingValueFloat(
628
                        0.0, 1000.0,
629
                        TGUV2P_STEPS[_bandsettings[band_num].step],
630
                        resolution=0.25))
631
                vfo_group.append(rs)
632

    
633
                rs = RadioSetting(
634
                    vfo_lower[idx] + "_pwr",
635
                    vfo_upper[idx] + " Power",
636
                    RadioSettingValueList(
637
                        POWER_LEVELS_STR,
638
                        POWER_LEVELS_STR[_bandsettings[band_num].power]))
639
                vfo_group.append(rs)
640

    
641
                options = ["None", "Tone", "DTCS-N", "DTCS-I"]
642
                rs = RadioSetting(vfo_lower[idx] + "_ttmode",
643
                                  vfo_upper[idx]+" TX tone mode",
644
                                  RadioSettingValueList(
645
                                      options, options[txtmode]))
646
                vfo_group.append(rs)
647
                if txtmode == 1:
648
                    rs = RadioSetting(
649
                        vfo_lower[idx] + "_ttone",
650
                        vfo_upper[idx] + " TX tone",
651
                        RadioSettingValueFloat(
652
                            0.0, 1000.0,
653
                            chirp_common.TONES[_bandsettings[band_num].txtone],
654
                            resolution=0.1))
655
                    vfo_group.append(rs)
656
                elif txtmode >= 2:
657
                    txtone = _bandsettings[band_num].txtone
658
                    rs = RadioSetting(
659
                        vfo_lower[idx] + "_tdtcs",
660
                        vfo_upper[idx] + " TX DTCS",
661
                        RadioSettingValueInteger(
662
                            0, 1000, chirp_common.DTCS_CODES[txtone]))
663
                    vfo_group.append(rs)
664

    
665
                options = ["None", "Tone", "DTCS-N", "DTCS-I"]
666
                rs = RadioSetting(vfo_lower[idx] + "_rtmode",
667
                                  vfo_upper[idx] + " RX tone mode",
668
                                  RadioSettingValueList(options,
669
                                                        options[rxtmode]))
670
                vfo_group.append(rs)
671

    
672
                if rxtmode == 1:
673
                    rs = RadioSetting(
674
                        vfo_lower[idx] + "_rtone",
675
                        vfo_upper[idx] + " RX tone",
676
                        RadioSettingValueFloat(
677
                            0.0, 1000.0,
678
                            chirp_common.TONES[_bandsettings[band_num].rxtone],
679
                            resolution=0.1))
680
                    vfo_group.append(rs)
681
                elif rxtmode >= 2:
682
                    rxtone = _bandsettings[band_num].rxtone
683
                    rs = RadioSetting(vfo_lower[idx] + "_rdtcs",
684
                                      vfo_upper[idx] + " TX rTCS",
685
                                      RadioSettingValueInteger(
686
                                          0, 1000,
687
                                          chirp_common.DTCS_CODES[rxtone]))
688
                    vfo_group.append(rs)
689

    
690
                options = ["FM", "NFM"]
691
                rs = RadioSetting(
692
                    vfo_lower[idx] + "_fm",
693
                    vfo_upper[idx] + " FM BW ",
694
                    RadioSettingValueList(
695
                        options, options[_bandsettings[band_num].isnarrow]))
696
                vfo_group.append(rs)
697

    
698
        return group
699

    
700
    def _validate_priority_ch(self, ch_num):
701
        if ch_num == 0xFF:
702
            return True
703
        _mem, _bf, _nam = self._get_memobjs(ch_num)
704
        if (_mem.freq.get_raw()[0] == "\xFF") or (_bf.band == "\x0F"):
705
            return False
706
        elif _bf.band == 0x00:
707
            return False
708
        else:
709
            return True
710

    
711
    def set_settings(self, settings):
712
        for element in settings:
713
            if not isinstance(element, RadioSetting):
714
                self.set_settings(element)
715
                continue
716
            else:
717
                try:
718
                    if "vfoa" in element.get_name():
719
                        continue
720
                    if "vfob" in element.get_name():
721
                        continue
722
                    elif "." in element.get_name():
723
                        bits = element.get_name().split(".")
724
                        obj = self._memobj
725
                        for bit in bits[:-1]:
726
                            obj = getattr(obj, bit)
727
                        setting = bits[-1]
728
                    else:
729
                        obj = self._memobj.settings
730
                        setting = element.get_name()
731

    
732
                    if element.has_apply_callback():
733
                        LOG.debug("using apply callback")
734
                        element.run_apply_callback()
735
                    elif setting == "beep_tone_disabled":
736
                        LOG.debug("Setting %s = %s" % (setting,
737
                                                       not int(element.value)))
738
                        setattr(obj, setting, not int(element.value))
739
                    elif setting == "busy_lockout":
740
                        LOG.debug("Setting %s = %s" % (setting,
741
                                                       not int(element.value)))
742
                        setattr(obj, setting, not int(element.value))
743
                    elif setting == "keyunlocked":
744
                        # keypad currently unlocked being set to locked
745
                        # and rx_mode is currently not "Normal":
746
                        if getattr(obj, "keyunlocked") and int(element.value) \
747
                                and (getattr(obj, "rxmode") != 0x02):
748
                            raise errors.InvalidValueError(
749
                                "Keypad lock not allowed in "
750
                                "Dual-Watch or CrossBand")
751
                        LOG.debug("Setting %s = %s" % (setting,
752
                                                       not int(element.value)))
753
                        setattr(obj, setting, not int(element.value))
754
                    elif setting == "rxmode":
755
                        # rx_mode was normal, now being set otherwise
756
                        # and keypad is locked:
757
                        if (getattr(obj, "rxmode") == 0x02) \
758
                                and (int(element.value) != 2) \
759
                                and not (getattr(obj, "keyunlocked")):
760
                            raise errors.InvalidValueError(
761
                                "Dual-Watch or CrossBand can not be set "
762
                                "when keypad is locked")
763
                        LOG.debug("Setting %s = %s" % (setting, element.value))
764
                        setattr(obj, setting, element.value)
765
                    elif setting == "priority_channel":
766
                        _check = self._validate_priority_ch(int(element.value))
767
                        if _check:
768
                            LOG.debug("Setting %s = %s" % (setting,
769
                                                           element.value))
770
                            setattr(obj, setting, element.value)
771
                        else:
772
                            raise errors.InvalidValueError(
773
                                "Please select a valid priority channel:\n"
774
                                "A used memory channel which is not "
775
                                "in the Broadcast FM band (88-108 MHz),\n"
776
                                "Or select 'Not Used'")
777
                    elif element.value.get_mutable():
778
                        LOG.debug("Setting %s = %s" % (setting, element.value))
779
                        setattr(obj, setting, element.value)
780
                except Exception:
781
                    LOG.debug(element.get_name())
782
                    raise
783

    
784
    @classmethod
785
    def match_model(cls, filedata, filename):
786
        return (filedata.startswith(b"TG-UV2+ Radio Program Data") and
787
                len(filedata) == (cls._memsize + 0x30))
(5-5/6)