Project

General

Profile

Bug #10226 » tg_uv2p_fixed.py

Alex Young, 08/29/2023 05:47 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_ident(radio):
118
    radio.pipe.timeout = 3
119
    radio.pipe.stopbits = serial.STOPBITS_TWO
120
    radio.pipe.write(b"\x02PnOGdAM")
121
    for x in range(10):
122
        ack = radio.pipe.read(1)
123
        if ack == b'\x06':
124
            break
125
    else:
126
        raise errors.RadioError("Radio did not ack programming mode")
127
    radio.pipe.write(b"\x4D\x02")
128
    ident = radio.pipe.read(8)
129
    LOG.debug(util.hexprint(ident))
130
    if not ident.startswith(b'P5555'):
131
        raise errors.RadioError("Unsupported model")
132
    radio.pipe.write(b"\x06")
133
    ack = radio.pipe.read(1)
134
    if ack != b"\x06":
135
        raise errors.RadioError("Radio did not ack ident")
136

    
137

    
138
def do_status(radio, direction, addr):
139
    status = chirp_common.Status()
140
    status.msg = "Cloning %s radio" % direction
141
    status.cur = addr
142
    status.max = 0x2000
143
    radio.status_fn(status)
144

    
145

    
146
def do_download(radio):
147
    do_ident(radio)
148
    data = b"TG-UV2+ Radio Program Data v1.0\x00"
149
    data += (b"\x00" * 16)
150

    
151
    firstack = None
152
    for i in range(0, 0x2000, 8):
153
        frame = struct.pack(">cHB", b"R", i, 8)
154
        radio.pipe.write(frame)
155
        result = radio.pipe.read(12)
156
        if not (result[0:1] == b"W" and frame[1:4] == result[1:4]):
157
            LOG.debug(util.hexprint(result))
158
            raise errors.RadioError("Invalid response for address 0x%04x" % i)
159
        radio.pipe.write(b"\x06")
160
        ack = radio.pipe.read(1)
161
        if not firstack:
162
            firstack = ack
163
        else:
164
            if not ack == firstack:
165
                LOG.debug("first ack: %s ack received: %s",
166
                          util.hexprint(firstack), util.hexprint(ack))
167
                raise errors.RadioError("Unexpected response")
168
        data += result[4:]
169
        do_status(radio, "from", i)
170

    
171
    return memmap.MemoryMapBytes(data)
172

    
173

    
174
def do_upload(radio):
175
    do_ident(radio)
176
    data = radio._mmap[0x0030:]
177

    
178
    for i in range(0, 0x2000, 8):
179
        frame = struct.pack(">cHB", b"W", i, 8)
180
        frame += data[i:i + 8]
181
        radio.pipe.write(frame)
182
        ack = radio.pipe.read(1)
183
        if ack != b"\x06":
184
            LOG.debug("Radio NAK'd block at address 0x%04x" % i)
185
            raise errors.RadioError(
186
                    "Radio NAK'd block at address 0x%04x" % i)
187
        LOG.debug("Radio ACK'd block at address 0x%04x" % i)
188
        do_status(radio, "to", i)
189

    
190

    
191
DUPLEX = ["", "+", "-"]
192
TGUV2P_STEPS = [5, 6.25, 10, 12.5, 15, 20, 25, 30, 50, 100]
193
CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_|* +-"
194
POWER_LEVELS = [chirp_common.PowerLevel("High", watts=10),
195
                chirp_common.PowerLevel("Med", watts=5),
196
                chirp_common.PowerLevel("Low", watts=1)]
197
POWER_LEVELS_STR = ["High", "Med", "Low"]
198
VALID_BANDS = [(88000000, 108000000),
199
               (136000000, 174000000),
200
               (350000000, 390000000),
201
               (400000000, 470000000),
202
               (470000000, 520000000)]
203

    
204

    
205
@directory.register
206
class QuanshengTGUV2P(chirp_common.CloneModeRadio,
207
                      chirp_common.ExperimentalRadio):
208
    """Quansheng TG-UV2+"""
209
    VENDOR = "Quansheng"
210
    MODEL = "TG-UV2+"
211
    BAUD_RATE = 9600
212
    NEEDS_COMPAT_SERIAL = False
213

    
214
    _memsize = 0x2000
215

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

    
240
    def get_features(self):
241
        rf = chirp_common.RadioFeatures()
242
        rf.has_settings = True
243
        rf.has_cross = True
244
        rf.has_rx_dtcs = True
245
        rf.has_dtcs_polarity = True
246
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
247
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
248
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
249
        rf.valid_duplexes = DUPLEX
250
        rf.can_odd_split = False
251
        rf.valid_skips = ["", "S"]
252
        rf.valid_characters = CHARSET
253
        rf.valid_name_length = 6
254
        rf.valid_tuning_steps = TGUV2P_STEPS
255
        rf.valid_bands = VALID_BANDS
256

    
257
        rf.valid_modes = ["FM", "NFM"]
258
        rf.valid_power_levels = POWER_LEVELS
259
        rf.has_ctone = True
260
        rf.has_bank = False
261
        rf.has_tuning_step = True
262
        rf.memory_bounds = (0, 199)
263
        return rf
264

    
265
    def sync_in(self):
266
        try:
267
            self._mmap = do_download(self)
268
        except errors.RadioError:
269
            raise
270
        except Exception as e:
271
            raise errors.RadioError("Failed to communicate with radio: %s" % e)
272
        self.process_mmap()
273

    
274
    def sync_out(self):
275
        try:
276
            do_upload(self)
277
        except errors.RadioError:
278
            raise
279
        except Exception as e:
280
            raise errors.RadioError("Failed to communicate with radio: %s" % e)
281

    
282
    def process_mmap(self):
283
        self._memobj = bitwise.parse(mem_format, self._mmap)
284

    
285
    def get_raw_memory(self, number):
286
        return repr(self._memobj.channels[number])
287

    
288
    def _decode_tone(self, _mem, which):
289
        def _get(field):
290
            return getattr(_mem, "%s%s" % (which, field))
291

    
292
        value = _get('tone')
293
        tmode = _get('tmode')
294

    
295
        if (value <= 104) and (tmode <= 3):
296
            if tmode == 0:
297
                mode = val = pol = None
298
            elif tmode == 1:
299
                mode = 'Tone'
300
                val = chirp_common.TONES[value]
301
                pol = None
302
            else:
303
                mode = 'DTCS'
304
                val = chirp_common.DTCS_CODES[value]
305
                pol = "N" if (tmode == 2) else "R"
306
        else:
307
            mode = val = pol = None
308

    
309
        return mode, val, pol
310

    
311
    def _encode_tone(self, _mem, which, mode, val, pol):
312
        def _set(field, value):
313
            setattr(_mem, "%s%s" % (which, field), value)
314

    
315
        if (mode == "Tone"):
316
            _set("tone", chirp_common.TONES.index(val))
317
            _set("tmode", 0x01)
318
        elif mode == "DTCS":
319
            _set("tone", chirp_common.DTCS_CODES.index(val))
320
            if pol == "N":
321
                _set("tmode", 0x02)
322
            else:
323
                _set("tmode", 0x03)
324
        else:
325
            _set("tone", 0)
326
            _set("tmode", 0)
327

    
328
    def _get_memobjs(self, number):
329
        if isinstance(number, str):
330
            return (getattr(self._memobj, number.lower()), None)
331

    
332
        else:
333
            return (self._memobj.channels[number],
334
                    self._memobj.bandflags[number],
335
                    self._memobj.names[number].name)
336

    
337
    def get_memory(self, number):
338
        _mem, _bf, _nam = self._get_memobjs(number)
339
        mem = chirp_common.Memory()
340
        if isinstance(number, str):
341
            mem.extd_number = number
342
        else:
343
            mem.number = number
344

    
345
        if (_mem.freq.get_raw()[0] == "\xFF") or (_bf.band == "\x0F"):
346
            mem.empty = True
347
            return mem
348

    
349
        mem.freq = int(_mem.freq) * 10
350

    
351
        if _mem.offset.get_raw()[0] == "\xFF":
352
            mem.offset = 0
353
        else:
354
            mem.offset = int(_mem.offset) * 10
355

    
356
        chirp_common.split_tone_decode(
357
            mem,
358
            self._decode_tone(_mem, "tx"),
359
            self._decode_tone(_mem, "rx"))
360

    
361
        if 'step' in _mem and _mem.step > len(TGUV2P_STEPS):
362
            _mem.step = 0x00
363
        mem.tuning_step = TGUV2P_STEPS[_mem.step]
364
        mem.duplex = DUPLEX[_mem.duplex]
365
        mem.mode = _mem.isnarrow and "NFM" or "FM"
366
        mem.skip = "" if bool(_bf.scanadd) else "S"
367
        mem.power = POWER_LEVELS[_mem.power]
368

    
369
        if _nam:
370
            for char in _nam:
371
                try:
372
                    mem.name += CHARSET[char]
373
                except IndexError:
374
                    break
375
            mem.name = mem.name.rstrip()
376

    
377
        mem.extra = RadioSettingGroup("Extra", "extra")
378

    
379
        rs = RadioSetting("not_scramble", "(not)SCRAMBLE",
380
                          RadioSettingValueBoolean(_mem.not_scramble))
381
        mem.extra.append(rs)
382

    
383
        rs = RadioSetting("not_revfreq", "(not)Reverse Duplex",
384
                          RadioSettingValueBoolean(_mem.not_revfreq))
385
        mem.extra.append(rs)
386

    
387
        return mem
388

    
389
    def set_memory(self, mem):
390
        _mem, _bf, _nam = self._get_memobjs(mem.number)
391

    
392
        _bf.set_raw("\xFF")
393

    
394
        if mem.empty:
395
            _mem.set_raw("\xFF" * 16)
396
            return
397

    
398
        _mem.set_raw("\x00" * 12 + "\xFF" * 2 + "\x00"*2)
399

    
400
        _bf.scanadd = int(mem.skip != "S")
401
        _bf.band = 0x0F
402
        for idx, ele in enumerate(VALID_BANDS):
403
            if mem.freq >= ele[0] and mem.freq <= ele[1]:
404
                _bf.band = idx
405

    
406
        _mem.freq = mem.freq / 10
407
        _mem.offset = mem.offset / 10
408

    
409
        tx, rx = chirp_common.split_tone_encode(mem)
410
        self._encode_tone(_mem, 'tx', *tx)
411
        self._encode_tone(_mem, 'rx', *rx)
412

    
413
        _mem.duplex = DUPLEX.index(mem.duplex)
414
        _mem.isnarrow = mem.mode == "NFM"
415
        _mem.step = TGUV2P_STEPS.index(mem.tuning_step)
416

    
417
        if mem.power is None:
418
            _mem.power = 0
419
        else:
420
            _mem.power = POWER_LEVELS.index(mem.power)
421

    
422
        if _nam:
423
            for i in range(0, 6):
424
                try:
425
                    _nam[i] = CHARSET.index(mem.name[i])
426
                except IndexError:
427
                    _nam[i] = 0xFF
428

    
429
        for setting in mem.extra:
430
            setattr(_mem, setting.get_name(), setting.value)
431

    
432
    def get_settings(self):
433
        _settings = self._memobj.settings
434
        _vfoa = self._memobj.vfos[0]
435
        _vfob = self._memobj.vfos[1]
436
        _bandsettings = self._memobj.bands
437

    
438
        cfg_grp = RadioSettingGroup("cfg_grp", "Configuration")
439
        vfoa_grp = RadioSettingGroup(
440
            "vfoa_grp", "VFO A Settings\n  (Current Status, Read Only)")
441
        vfob_grp = RadioSettingGroup(
442
            "vfob_grp", "VFO B Settings\n  (Current Status, Read Only)")
443

    
444
        group = RadioSettings(cfg_grp, vfoa_grp, vfob_grp)
445
        #
446
        # Configuration Settings
447
        #
448

    
449
        # TX time out timer:
450
        options = ["Off"] + ["%s min" % x for x in range(1, 10)]
451
        rs = RadioSetting("time_out_timer", "TX Time Out Timer",
452
                          RadioSettingValueList(
453
                              options, options[_settings.time_out_timer]))
454
        cfg_grp.append(rs)
455

    
456
        # Display mode
457
        options = ["Frequency", "Channel", "Name"]
458
        rs = RadioSetting("display", "Channel Display Mode",
459
                          RadioSettingValueList(
460
                              options, options[_settings.display]))
461
        cfg_grp.append(rs)
462

    
463
        # Squelch level
464
        rs = RadioSetting("squelch", "Squelch Level",
465
                          RadioSettingValueInteger(0, 9, _settings.squelch))
466
        cfg_grp.append(rs)
467

    
468
        # Vox level
469
        mem_vals = list(range(10))
470
        user_options = [str(x) for x in mem_vals]
471
        user_options[0] = "Off"
472
        options_map = list(zip(user_options, mem_vals))
473

    
474
        rs = RadioSetting("vox", "VOX Level",
475
                          RadioSettingValueMap(options_map, _settings.vox))
476
        cfg_grp.append(rs)
477

    
478
        # Keypad beep
479
        rs = RadioSetting("beep_tone_disabled", "Beep Prompt",
480
                          RadioSettingValueBoolean(
481
                               not _settings.beep_tone_disabled))
482
        cfg_grp.append(rs)
483

    
484
        # Dual watch/crossband
485
        options = ["Dual Watch", "CrossBand", "Normal"]
486
        if _settings.rxmode >= 2:
487
            _rxmode = 2
488
        else:
489
            _rxmode = _settings.rxmode
490
        rs = RadioSetting("rxmode", "Dual Watch/CrossBand Monitor",
491
                          RadioSettingValueList(
492
                            options, options[_rxmode]))
493
        cfg_grp.append(rs)
494

    
495
        # Busy channel lock
496
        rs = RadioSetting("busy_lockout", "Busy Channel Lock",
497
                          RadioSettingValueBoolean(
498
                             not _settings.busy_lockout))
499
        cfg_grp.append(rs)
500

    
501
        # Keypad lock
502
        rs = RadioSetting("keyunlocked", "Keypad Lock",
503
                          RadioSettingValueBoolean(
504
                              not _settings.keyunlocked))
505
        cfg_grp.append(rs)
506

    
507
        # Priority channel
508
        mem_vals = list(range(200))
509
        user_options = [str(x) for x in mem_vals]
510
        mem_vals.insert(0, 0xFF)
511
        user_options.insert(0, "Not Set")
512
        options_map = list(zip(user_options, mem_vals))
513
        if _settings.priority_channel >= 200:
514
            _priority_ch = 0xFF
515
        else:
516
            _priority_ch = _settings.priority_channel
517
        rs = RadioSetting(
518
            "priority_channel",
519
            "Priority Channel \n"
520
            "Note: Unused channels,\nor channels "
521
            "in the\nbroadcast FM band,\nwill not be set",
522
            RadioSettingValueMap(options_map, _priority_ch))
523
        cfg_grp.append(rs)
524

    
525
        # Step
526
        mem_vals = list(range(0, len(TGUV2P_STEPS)))
527
        mem_vals.append(0xFF)
528
        user_options = [(str(x) + " kHz") for x in TGUV2P_STEPS]
529
        user_options.append("Unknown")
530
        options_map = list(zip(user_options, mem_vals))
531

    
532
        rs = RadioSetting("step", "Current (VFO?) step size",
533
                          RadioSettingValueMap(options_map, _settings.step))
534
        cfg_grp.append(rs)
535

    
536
        # End (Tail) tone elimination
537
        mem_vals = [0, 1]
538
        user_options = ["Tone Elimination On", "Tone Elimination Off"]
539
        options_map = list(zip(user_options, mem_vals))
540

    
541
        rs = RadioSetting("not_end_tone_elim", "Tx End Tone Elimination",
542
                          RadioSettingValueMap(options_map,
543
                                               _settings.not_end_tone_elim))
544
        cfg_grp.append(rs)
545

    
546
        # VFO mode
547

    
548
        if _settings.vfo_mode >= 1:
549
            _vfo_mode = 0xFF
550
        else:
551
            _vfo_mode = _settings.vfo_mode
552
        mem_vals = [0xFF, 0]
553
        user_options = ["VFO Mode Enabled", "VFO Mode Disabled"]
554
        options_map = list(zip(user_options, mem_vals))
555

    
556
        rs = RadioSetting("vfo_mode", "VFO (CH only) mode",
557
                          RadioSettingValueMap(options_map, _vfo_mode))
558
        cfg_grp.append(rs)
559

    
560
        #
561
        # VFO Settings
562
        #
563

    
564
        vfo_groups = [vfoa_grp, vfob_grp]
565
        vfo_mem = [_vfoa, _vfob]
566
        vfo_lower = ["vfoa", "vfob"]
567
        vfo_upper = ["VFOA", "VFOB"]
568

    
569
        for idx, vfo_group in enumerate(vfo_groups):
570

    
571
            options = ["Channel", "Frequency"]
572
            tempvar = 0 if (vfo_mem[idx].current < 200) else 1
573
            rs = RadioSetting(vfo_lower[idx] + "_mode", vfo_upper[idx]+" Mode",
574
                              RadioSettingValueList(
575
                                  options, options[tempvar]))
576
            vfo_group.append(rs)
577

    
578
            if tempvar == 0:
579
                rs = RadioSetting(vfo_lower[idx] + "_ch",
580
                                  vfo_upper[idx] + " Channel",
581
                                  RadioSettingValueInteger(
582
                                      0, 199, vfo_mem[idx].current))
583
                vfo_group.append(rs)
584
            else:
585
                band_num = vfo_mem[idx].current - 200
586
                freq = int(_bandsettings[band_num].freq) * 10
587
                offset = int(_bandsettings[band_num].offset) * 10
588
                txtmode = _bandsettings[band_num].txtmode
589
                rxtmode = _bandsettings[band_num].rxtmode
590

    
591
                rs = RadioSetting(vfo_lower[idx] + "_freq",
592
                                  vfo_upper[idx] + " Frequency",
593
                                  RadioSettingValueFloat(
594
                                      0.0, 520.0, freq / 1000000.0,
595
                                      precision=6))
596
                vfo_group.append(rs)
597

    
598
                if offset > 70e6:
599
                    offset = 0
600
                rs = RadioSetting(vfo_lower[idx] + "_offset",
601
                                  vfo_upper[idx] + " Offset",
602
                                  RadioSettingValueFloat(
603
                                      0.0, 69.995, offset / 100000.0,
604
                                      resolution=0.005))
605
                vfo_group.append(rs)
606

    
607
                rs = RadioSetting(vfo_lower[idx] + "_duplex",
608
                                  vfo_upper[idx] + " Shift",
609
                                  RadioSettingValueList(
610
                                      DUPLEX,
611
                                      DUPLEX[_bandsettings[band_num].duplex]))
612
                vfo_group.append(rs)
613

    
614
                rs = RadioSetting(
615
                    vfo_lower[idx] + "_step",
616
                    vfo_upper[idx] + " Step",
617
                    RadioSettingValueFloat(
618
                        0.0, 1000.0,
619
                        TGUV2P_STEPS[_bandsettings[band_num].step],
620
                        resolution=0.25))
621
                vfo_group.append(rs)
622

    
623
                rs = RadioSetting(
624
                    vfo_lower[idx] + "_pwr",
625
                    vfo_upper[idx] + " Power",
626
                    RadioSettingValueList(
627
                        POWER_LEVELS_STR,
628
                        POWER_LEVELS_STR[_bandsettings[band_num].power]))
629
                vfo_group.append(rs)
630

    
631
                options = ["None", "Tone", "DTCS-N", "DTCS-I"]
632
                rs = RadioSetting(vfo_lower[idx] + "_ttmode",
633
                                  vfo_upper[idx]+" TX tone mode",
634
                                  RadioSettingValueList(
635
                                      options, options[txtmode]))
636
                vfo_group.append(rs)
637
                if txtmode == 1:
638
                    rs = RadioSetting(
639
                        vfo_lower[idx] + "_ttone",
640
                        vfo_upper[idx] + " TX tone",
641
                        RadioSettingValueFloat(
642
                            0.0, 1000.0,
643
                            chirp_common.TONES[_bandsettings[band_num].txtone],
644
                            resolution=0.1))
645
                    vfo_group.append(rs)
646
                elif txtmode >= 2:
647
                    txtone = _bandsettings[band_num].txtone
648
                    rs = RadioSetting(
649
                        vfo_lower[idx] + "_tdtcs",
650
                        vfo_upper[idx] + " TX DTCS",
651
                        RadioSettingValueInteger(
652
                            0, 1000, chirp_common.DTCS_CODES[txtone]))
653
                    vfo_group.append(rs)
654

    
655
                options = ["None", "Tone", "DTCS-N", "DTCS-I"]
656
                rs = RadioSetting(vfo_lower[idx] + "_rtmode",
657
                                  vfo_upper[idx] + " RX tone mode",
658
                                  RadioSettingValueList(options,
659
                                                        options[rxtmode]))
660
                vfo_group.append(rs)
661

    
662
                if rxtmode == 1:
663
                    rs = RadioSetting(
664
                        vfo_lower[idx] + "_rtone",
665
                        vfo_upper[idx] + " RX tone",
666
                        RadioSettingValueFloat(
667
                            0.0, 1000.0,
668
                            chirp_common.TONES[_bandsettings[band_num].rxtone],
669
                            resolution=0.1))
670
                    vfo_group.append(rs)
671
                elif rxtmode >= 2:
672
                    rxtone = _bandsettings[band_num].rxtone
673
                    rs = RadioSetting(vfo_lower[idx] + "_rdtcs",
674
                                      vfo_upper[idx] + " TX rTCS",
675
                                      RadioSettingValueInteger(
676
                                          0, 1000,
677
                                          chirp_common.DTCS_CODES[rxtone]))
678
                    vfo_group.append(rs)
679

    
680
                options = ["FM", "NFM"]
681
                rs = RadioSetting(
682
                    vfo_lower[idx] + "_fm",
683
                    vfo_upper[idx] + " FM BW ",
684
                    RadioSettingValueList(
685
                        options, options[_bandsettings[band_num].isnarrow]))
686
                vfo_group.append(rs)
687

    
688
        return group
689

    
690
    def _validate_priority_ch(self, ch_num):
691
        if ch_num == 0xFF:
692
            return True
693
        _mem, _bf, _nam = self._get_memobjs(ch_num)
694
        if (_mem.freq.get_raw()[0] == "\xFF") or (_bf.band == "\x0F"):
695
            return False
696
        elif _bf.band == 0x00:
697
            return False
698
        else:
699
            return True
700

    
701
    def set_settings(self, settings):
702
        for element in settings:
703
            if not isinstance(element, RadioSetting):
704
                self.set_settings(element)
705
                continue
706
            else:
707
                try:
708
                    if "vfoa" in element.get_name():
709
                        continue
710
                    if "vfob" in element.get_name():
711
                        continue
712
                    elif "." in element.get_name():
713
                        bits = element.get_name().split(".")
714
                        obj = self._memobj
715
                        for bit in bits[:-1]:
716
                            obj = getattr(obj, bit)
717
                        setting = bits[-1]
718
                    else:
719
                        obj = self._memobj.settings
720
                        setting = element.get_name()
721

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

    
774
    @classmethod
775
    def match_model(cls, filedata, filename):
776
        return (filedata.startswith(b"TG-UV2+ Radio Program Data") and
777
                len(filedata) == (cls._memsize + 0x30))
(4-4/6)