uv6rsplit.py

Split function - Gilles Drumain, 03/31/2020 01:33 pm

Download (38.1 kB)

 
1
# Copyright 2016:
2
# * Jim Unroe KC9HI, <rock.unroe@gmail.com>
3
#
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

    
17
import time
18
import struct
19
import logging
20
import re
21

    
22
LOG = logging.getLogger(__name__)
23

    
24
from chirp import chirp_common, directory, memmap
25
from chirp import bitwise, errors, util
26
from chirp.settings import RadioSettingGroup, RadioSetting, \
27
    RadioSettingValueBoolean, RadioSettingValueList, \
28
    RadioSettingValueString, RadioSettingValueInteger, \
29
    RadioSettingValueFloat, RadioSettings, \
30
    InvalidValueError
31
from textwrap import dedent
32

    
33
##### MAGICS #########################################################
34
                                                                             
35
# Baofeng UV-6R magic string
36
MSTRING_UV6R = "\x50\xBB\xFF\x20\x14\x11\x22"                                
37
                                                                             
38
##### ID strings #####################################################       
39
                                                                             
40
# Baofeng UV-6R                                                              
41
UV6R_fp1 = " BF230#1"
42
UV6R_fp2 = " BF230#2"
43

    
44
DTMF_CHARS = "0123456789 *#ABCD"
45
STEPS = [2.5, 5.0, 6.25, 10.0, 12.5, 20.0, 25.0, 50.0]
46

    
47
LIST_AB = ["A", "B"]
48
LIST_ALMOD = ["Site", "Tone", "Code"]
49
LIST_BANDWIDTH = ["Wide", "Narrow"]
50
LIST_COLOR = ["Off", "Blue", "Orange", "Purple"]
51
LIST_DTMFSPEED = ["%s ms" % x for x in range(50, 2010, 10)]
52
LIST_DTMFST = ["Off", "DT-ST", "ANI-ST", "DT+ANI"]
53
LIST_MODE = ["Channel", "Name", "Frequency"]
54
LIST_OFF1TO9 = ["Off"] + list("123456789")
55
LIST_OFF1TO10 = LIST_OFF1TO9 + ["10"]
56
LIST_OFFAB = ["Off"] + LIST_AB
57
LIST_RESUME = ["TO", "CO", "SE"]
58
LIST_PONMSG = ["Full", "Message"]
59
LIST_PTTID = ["Off", "BOT", "EOT", "Both"]
60
LIST_SCODE = ["%s" % x for x in range(1, 16)]
61
LIST_RPSTE = ["Off"] + ["%s" % x for x in range(1, 11)]
62
LIST_SAVE = ["Off", "1:1", "1:2", "1:3", "1:4"]
63
LIST_SHIFTD = ["Off", "+", "-"]
64
LIST_STEDELAY = ["Off"] + ["%s ms" % x for x in range(100, 1100, 100)]
65
LIST_STEP = [str(x) for x in STEPS]
66
LIST_TCALL = ["Off", "1000 Hz", "1450 Hz", "1750 Hz", "2100 Hz"]
67
LIST_TIMEOUT = ["%s sec" % x for x in range(15, 615, 15)]
68
LIST_TXPOWER = ["High", "Low"]
69
LIST_VOICE = ["Off", "English", "Chinese"]
70
LIST_WORKMODE = ["Frequency", "Channel"]
71

    
72
def model_match(cls, data):
73
    """Match the opened/downloaded image to the correct version"""
74
    match_rid1 = False
75
    match_rid2 = False
76

    
77
    rid1 = data[0x1FF8:0x2000]
78

    
79
    if rid1 in cls._fileid:
80
        match_rid1 = True
81

    
82
    rid2 = data[0x1FD0:0x1FD5]
83

    
84
    if rid2 == cls.MODEL:
85
        match_rid2 = True
86

    
87
    if match_rid1 and match_rid2:
88
        return True
89
    else:
90
        return False
91

    
92

    
93
@directory.register
94
class UV6R(chirp_common.CloneModeRadio,
95
                      chirp_common.ExperimentalRadio):
96
    """Baofeng UV-6R"""
97
    VENDOR = "Baofeng"
98
    MODEL = "UV-6R"
99

    
100
    _fileid = [UV6R_fp2, UV6R_fp1, ]
101

    
102
    _magic = [MSTRING_UV6R, ]
103
    _magic_response_length = 8
104
    _fw_ver_start = 0x1FF0
105
    _recv_block_size = 0x40
106
    _mem_size = 0x2000
107
    _ack_block = False
108

    
109
    _ranges = [(0x0000, 0x1800),
110
               (0x1F40, 0x1F50),
111
               (0x1FC0, 0x1FD0),
112
               (0x1FE0, 0x1FF0)]
113
    _send_block_size = 0x10
114

    
115
    MODES = ["FM", "NFM"]
116
    VALID_CHARS = chirp_common.CHARSET_ALPHANUMERIC + \
117
        "!@#$%^&*()+-=[]:\";'<>?,./"
118
    LENGTH_NAME = 6
119
    SKIP_VALUES = ["", "S"]
120
    DTCS_CODES = sorted(chirp_common.DTCS_CODES + [645])
121
    POWER_LEVELS = [chirp_common.PowerLevel("High", watts=5.00),
122
                    chirp_common.PowerLevel("Low", watts=1.00)]
123
    VALID_BANDS = [(136000000, 174000000),
124
                   (400000000, 520000000)]
125
    PTTID_LIST = LIST_PTTID
126
    SCODE_LIST = LIST_SCODE
127

    
128

    
129
    MEM_FORMAT = """
130
    #seekto 0x0000;
131
    struct {
132
      lbcd rxfreq[4];
133
      lbcd txfreq[4];
134
      ul16 rxtone;
135
      ul16 txtone;
136
      u8 unknown0:4,
137
         scode:4;
138
      u8 unknown1;
139
      u8 unknown2:7,
140
         lowpower:1;
141
      u8 unknown3:1,
142
         wide:1,
143
         unknown4:2,
144
         bcl:1,
145
         scan:1,
146
         pttid:2;
147
    } memory[128];
148

    
149
    #seekto 0x0B00;
150
    struct {
151
      u8 code[5];
152
      u8 unused[11];
153
    } pttid[15];
154

    
155
    #seekto 0x0CAA;
156
    struct {
157
      u8 code[5];
158
      u8 unused:6,
159
         aniid:2;
160
      u8 unknown[2];
161
      u8 dtmfon;
162
      u8 dtmfoff;
163
    } ani;
164

    
165
    #seekto 0x0E20;
166
    struct {
167
      u8 unused00:4,
168
         squelch:4;
169
      u8 unused01:5,
170
         step:3;
171
      u8 unknown00;
172
      u8 unused02:5,
173
         save:3;
174
      u8 unused03:4,
175
         vox:4;
176
      u8 unknown01;
177
      u8 unused04:4,
178
         abr:4;
179
      u8 unused05:7,
180
         tdr:1;
181
      u8 unused06:7,
182
         beep:1;
183
      u8 unused07:2,
184
         timeout:6;
185
      u8 unused08:6,
186
         tcall:2;
187
      u8 unknown02[3];
188
      u8 unused09:6,
189
         voice:2;
190
      u8 unknown03;
191
      u8 unused10:6,
192
         dtmfst:2;
193
      u8 unknown04;
194
      u8 unused11:6,
195
         screv:2;
196
      u8 unused12:6,
197
         pttid:2;
198
      u8 unused13:2,
199
         pttlt:6;
200
      u8 unused14:6,
201
         mdfa:2;
202
      u8 unused15:6,
203
         mdfb:2;
204
      u8 unknown05;
205
      u8 unused16:7,
206
         autolk:1;
207
      u8 unknown06[4];
208
      u8 unused17:6,
209
         wtled:2;
210
      u8 unused18:6,
211
         rxled:2;
212
      u8 unused19:6,
213
         txled:2;
214
      u8 unused20:6,
215
         almod:2;
216
      u8 unknown07[2];
217
      u8 unused22:7,
218
         ste:1;
219
      u8 unused23:4,
220
         rpste:4;
221
      u8 unused24:4,
222
         rptrl:4;
223
      u8 unused25:7,
224
         ponmsg:1;
225
      u8 unused26:7,
226
         roger:1;
227
      u8 unused27:7,
228
         reset:1;
229
      u8 unknown08;
230
      u8 displayab:1,
231
         unknown09:2,
232
         fmradio:1,
233
         alarm:1,
234
         unknown10:1,
235
         menu:1;
236
      u8 unknown11;
237
      u8 unused29:7,
238
         workmode:1;
239
      u8 unused30:7,
240
         keylock:1;
241
      u8 cht;
242
    } settings;
243

    
244
    #seekto 0x0E76;
245
    struct {
246
      u8 unused0:1,
247
         mrcha:7;
248
      u8 unused1:1,
249
         mrchb:7;
250
    } wmchannel;
251

    
252
    struct vfo {
253
      u8 unknown0[8];
254
      u8 freq[8];
255
      u8 offset[6];
256
      ul16 rxtone;
257
      ul16 txtone;
258
      u8 unused0:7,
259
         band:1;
260
      u8 unknown3;
261
      u8 unknown4:2,
262
         sftd:2,
263
         scode:4;
264
      u8 unknown5;
265
      u8 unknown6:1,
266
         step:3,
267
         unknown7:4;
268
      u8 txpower:1,
269
         widenarr:1,
270
         unknown8:6;
271
    };
272

    
273
    #seekto 0x0F00;
274
    struct {
275
      struct vfo a;
276
      struct vfo b;
277
    } vfo;
278

    
279
    #seekto 0x0F4E;
280
    u16 fm_presets;
281

    
282
    #seekto 0x1000;
283
    struct {
284
      char name[6];
285
      u8 unknown[10];
286
    } names[128];
287

    
288
    #seekto 0x1F40;
289
    struct {
290
      u8 sql0;
291
      u8 sql1;
292
      u8 sql2;
293
      u8 sql3;
294
      u8 sql4;
295
      u8 sql5;
296
      u8 sql6;
297
      u8 sql7;
298
      u8 sql8;
299
      u8 sql9;
300
    } squelch;
301

    
302
    struct limit {
303
      u8 enable;
304
      bbcd lower[2];
305
      bbcd upper[2];
306
    };
307

    
308
    #seekto 0x1FC0;
309
    struct {
310
      struct limit vhf;
311
      struct limit uhf;
312
    } limits;
313

    
314
    #seekto 0x1FD0;
315
    struct {
316
      char line1[8];
317
      char line2[8];
318
    } sixpoweron_msg;
319

    
320
    #seekto 0x1FE0;
321
    struct {
322
      char line1[7];
323
      char line2[7];
324
    } poweron_msg;
325

    
326
    #seekto 0x1FF0;
327
    struct {
328
      char line1[8];
329
      char line2[8];
330
    } firmware_msg;
331

    
332
    """
333

    
334
    @classmethod
335
    def get_prompts(cls):
336
        rp = chirp_common.RadioPrompts()
337
        rp.experimental = \
338
            ('The BTech UV-6R driver is a beta version.\n'
339
             '\n'
340
             'Please save an unedited copy of your first successful\n'
341
             'download to a CHIRP Radio Images(*.img) file.'
342
             )
343
        rp.pre_download = _(dedent("""\
344
            Follow these instructions to download your info:
345

    
346
            1 - Turn off your radio
347
            2 - Connect your interface cable
348
            3 - Turn on your radio
349
            4 - Do the download of your radio data
350
            """))
351
        rp.pre_upload = _(dedent("""\
352
            Follow this instructions to upload your info:
353

    
354
            1 - Turn off your radio
355
            2 - Connect your interface cable
356
            3 - Turn on your radio
357
            4 - Do the upload of your radio data
358
            """))
359
        return rp
360

    
361
    def process_mmap(self):
362
        """Process the mem map into the mem object"""
363
        self._memobj = bitwise.parse(self.MEM_FORMAT, self._mmap)
364

    
365
    def sync_in(self):
366
        """Download from radio"""
367
        try:
368
            data = _download(self)
369
        except errors.RadioError:
370
            # Pass through any real errors we raise
371
            raise
372
        except:
373
            # If anything unexpected happens, make sure we raise
374
            # a RadioError and log the problem
375
            LOG.exception('Unexpected error during download')
376
            raise errors.RadioError('Unexpected error communicating '
377
                                    'with the radio')
378
        self._mmap = memmap.MemoryMap(data)
379
        self.process_mmap()
380

    
381
    def sync_out(self):
382
        """Upload to radio"""
383
        try:
384
            _upload(self)
385
        except errors.RadioError:
386
            raise
387
        except Exception, e:
388
            # If anything unexpected happens, make sure we raise
389
            # a RadioError and log the problem
390
            LOG.exception('Unexpected error during upload')
391
            raise errors.RadioError('Unexpected error communicating '
392
                                    'with the radio')
393
                                    
394
    def get_features(self):
395
        """Get the radio's features"""
396

    
397
        rf = chirp_common.RadioFeatures()
398
        rf.has_settings = True
399
        rf.has_bank = False
400
        rf.has_tuning_step = False
401
        rf.can_odd_split = True
402
        rf.has_name = True
403
        rf.has_offset = True
404
        rf.has_mode = True
405
        rf.has_dtcs = True
406
        rf.has_rx_dtcs = True
407
        rf.has_dtcs_polarity = True
408
        rf.has_ctone = True
409
        rf.has_cross = True
410
        rf.valid_modes = ["FM", "NFM"]
411
        rf.valid_characters = self.VALID_CHARS
412
        rf.valid_name_length = 6
413
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
414
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
415
        rf.valid_cross_modes = [
416
            "Tone->Tone",
417
            "DTCS->",
418
            "->DTCS",
419
            "Tone->DTCS",
420
            "DTCS->Tone",
421
            "->Tone",
422
            "DTCS->DTCS"]
423
        rf.valid_skips = ["", "S"]
424
        rf.valid_dtcs_codes = self.DTCS_CODES
425
        rf.memory_bounds = (0, 127)
426
        rf.valid_power_levels = self.POWER_LEVELS
427
        rf.valid_bands = self.VALID_BANDS
428
        rf.valid_tuning_steps = STEPS
429

    
430
        return rf
431

    
432
    def get_raw_memory(self, number):
433
        return repr(self._memobj.memory[number])
434
        
435
    def _is_txinh(self, _mem):
436
        raw_tx = ""
437
        for i in range(0, 4):
438
            raw_tx += _mem.txfreq[i].get_raw()
439
        return raw_tx == "\xFF\xFF\xFF\xFF"
440

    
441
    def get_memory(self, number):
442
        _mem = self._memobj.memory[number]
443
        _nam = self._memobj.names[number]
444

    
445
        mem = chirp_common.Memory()
446
        mem.number = number
447

    
448
        if _mem.get_raw()[0] == "\xff":
449
            mem.empty = True
450
            return mem
451

    
452
        mem.freq = int(_mem.rxfreq) * 10
453

    
454
        if self._is_txinh(_mem):
455
            mem.duplex = "off"
456
            mem.offset = 0
457
        elif abs(int(_mem.rxfreq) - int(_mem.txfreq)) < 7000000:    
458
            mem.duplex = "split"
459
            mem.offset = int(_mem.txfreq) * 10
460
        elif int(_mem.rxfreq) < int(_mem.txfreq):    
461
            mem.duplex = "+"
462
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
463
        elif int(_mem.rxfreq) > int(_mem.txfreq):    
464
            mem.duplex = "-"
465
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
466
        else:
467
            int(_mem.rxfreq) == int(_mem.txfreq) and ""
468
            mem.offset = 0
469

    
470
        for char in _nam.name:
471
            if str(char) == "\xFF":
472
                char = " "  # The OEM software may have 0xFF mid-name
473
            mem.name += str(char)
474
        mem.name = mem.name.rstrip()
475

    
476
        dtcs_pol = ["N", "N"]
477

    
478
        if _mem.txtone in [0, 0xFFFF]:
479
            txmode = ""
480
        elif _mem.txtone >= 0x0258:
481
            txmode = "Tone"
482
            mem.rtone = int(_mem.txtone) / 10.0
483
        elif _mem.txtone <= 0x0258:
484
            txmode = "DTCS"
485
            if _mem.txtone > 0x69:
486
                index = _mem.txtone - 0x6A
487
                dtcs_pol[0] = "R"
488
            else:
489
                index = _mem.txtone - 1
490
            mem.dtcs = self.DTCS_CODES[index]
491
        else:
492
            LOG.warn("Bug: txtone is %04x" % _mem.txtone)
493

    
494
        if _mem.rxtone in [0, 0xFFFF]:
495
            rxmode = ""
496
        elif _mem.rxtone >= 0x0258:
497
            rxmode = "Tone"
498
            mem.ctone = int(_mem.rxtone) / 10.0
499
        elif _mem.rxtone <= 0x0258:
500
            rxmode = "DTCS"
501
            if _mem.rxtone >= 0x6A:
502
                index = _mem.rxtone - 0x6A
503
                dtcs_pol[1] = "R"
504
            else:
505
                index = _mem.rxtone - 1
506
            mem.rx_dtcs = self.DTCS_CODES[index]
507
        else:
508
            LOG.warn("Bug: rxtone is %04x" % _mem.rxtone)
509

    
510
        if txmode == "Tone" and not rxmode:
511
            mem.tmode = "Tone"
512
        elif txmode == rxmode and txmode == "Tone" and mem.rtone == mem.ctone:
513
            mem.tmode = "TSQL"
514
        elif txmode == rxmode and txmode == "DTCS" and mem.dtcs == mem.rx_dtcs:
515
            mem.tmode = "DTCS"
516
        elif rxmode or txmode:
517
            mem.tmode = "Cross"
518
            mem.cross_mode = "%s->%s" % (txmode, rxmode)
519

    
520
        mem.dtcs_polarity = "".join(dtcs_pol)
521

    
522
        if not _mem.scan:
523
            mem.skip = "S"
524

    
525
        levels = self.POWER_LEVELS
526
        try:
527
            mem.power = levels[_mem.lowpower]
528
        except IndexError:
529
            LOG.error("Radio reported invalid power level %s (in %s)" %
530
                      (_mem.power, levels))
531
            mem.power = levels[0]
532

    
533
        mem.mode = _mem.wide and "FM" or "NFM"
534

    
535
        mem.extra = RadioSettingGroup("Extra", "extra")
536

    
537
        rs = RadioSetting("bcl", "BCL",
538
                          RadioSettingValueBoolean(_mem.bcl))
539
        mem.extra.append(rs)
540

    
541
        rs = RadioSetting("pttid", "PTT ID",
542
                          RadioSettingValueList(self.PTTID_LIST,
543
                                                self.PTTID_LIST[_mem.pttid]))
544
        mem.extra.append(rs)
545

    
546
        rs = RadioSetting("scode", "S-CODE",
547
                          RadioSettingValueList(self.SCODE_LIST,
548
                                                self.SCODE_LIST[_mem.scode]))
549
        mem.extra.append(rs)
550

    
551
        return mem
552

    
553
    def _set_mem(self, number):
554
        return self._memobj.memory[number]
555

    
556
    def _set_nam(self, number):
557
        return self._memobj.names[number]
558

    
559
    def set_memory(self, mem):
560
        _mem = self._memobj.memory[mem.number]
561
        _nam = self._memobj.names[mem.number]
562

    
563
        if mem.empty:
564
            _mem.set_raw("\xff" * 16)
565
            _nam.set_raw("\xff" * 16)
566
            return
567

    
568
        was_empty = False
569
        # same method as used in get_memory to find
570
        # out whether a raw memory is empty
571
        if _mem.get_raw()[0] == "\xff":
572
            was_empty = True
573
            LOG.debug
574
        else:
575
            # memorize old extra-values before erasing the whole memory
576
            # used to solve issue 4121
577
            LOG.debug("mem was not empty, memorize extra-settings")
578
            prev_bcl = _mem.bcl.get_value()
579
            prev_scode = _mem.scode.get_value()
580
            prev_pttid = _mem.pttid.get_value()
581

    
582
        _mem.set_raw("\x00" * 16)
583

    
584
        _mem.rxfreq = mem.freq / 10
585

    
586
        if mem.duplex == "off":
587
            for i in range(0, 4):
588
                _mem.txfreq[i].set_raw("\xFF")
589
        elif mem.duplex == "split":
590
            _mem.txfreq = mem.offset / 10
591
        elif mem.duplex == "+":
592
            _mem.txfreq = (mem.freq + mem.offset) / 10
593
        elif mem.duplex == "-":
594
            _mem.txfreq = (mem.freq - mem.offset) / 10
595
        else:
596
            _mem.txfreq = mem.freq / 10
597

    
598
        _namelength = self.get_features().valid_name_length
599
        for i in range(_namelength):
600
            try:
601
                _nam.name[i] = mem.name[i]
602
            except IndexError:
603
                _nam.name[i] = "\xFF"
604

    
605
        rxmode = txmode = ""
606
        if mem.tmode == "Tone":
607
            _mem.txtone = int(mem.rtone * 10)
608
            _mem.rxtone = 0
609
        elif mem.tmode == "TSQL":
610
            _mem.txtone = int(mem.ctone * 10)
611
            _mem.rxtone = int(mem.ctone * 10)
612
        elif mem.tmode == "DTCS":
613
            rxmode = txmode = "DTCS"
614
            _mem.txtone = self.DTCS_CODES.index(mem.dtcs) + 1
615
            _mem.rxtone = self.DTCS_CODES.index(mem.dtcs) + 1
616
        elif mem.tmode == "Cross":
617
            txmode, rxmode = mem.cross_mode.split("->", 1)
618
            if txmode == "Tone":
619
                _mem.txtone = int(mem.rtone * 10)
620
            elif txmode == "DTCS":
621
                _mem.txtone = self.DTCS_CODES.index(mem.dtcs) + 1
622
            else:
623
                _mem.txtone = 0
624
            if rxmode == "Tone":
625
                _mem.rxtone = int(mem.ctone * 10)
626
            elif rxmode == "DTCS":
627
                _mem.rxtone = self.DTCS_CODES.index(mem.rx_dtcs) + 1
628
            else:
629
                _mem.rxtone = 0
630
        else:
631
            _mem.rxtone = 0
632
            _mem.txtone = 0
633

    
634
        if txmode == "DTCS" and mem.dtcs_polarity[0] == "R":
635
            _mem.txtone += 0x69
636
        if rxmode == "DTCS" and mem.dtcs_polarity[1] == "R":
637
            _mem.rxtone += 0x69
638

    
639
        _mem.scan = mem.skip != "S"
640
        _mem.wide = mem.mode == "FM"
641

    
642
        if mem.power:
643
            _mem.lowpower = self.POWER_LEVELS.index(mem.power)
644
        else:
645
            _mem.lowpower = 0
646

    
647
        if not was_empty:
648
            # restoring old extra-settings (issue 4121
649
            _mem.bcl.set_value(prev_bcl)
650
            _mem.scode.set_value(prev_scode)
651
            _mem.pttid.set_value(prev_pttid)
652

    
653
        for setting in mem.extra:
654
            setattr(_mem, setting.get_name(), setting.value)   
655

    
656
    def get_settings(self):
657
        """Translate the bit in the mem_struct into settings in the UI"""
658
        _mem = self._memobj
659
        basic = RadioSettingGroup("basic", "Basic Settings")
660
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
661
        other = RadioSettingGroup("other", "Other Settings")
662
        work = RadioSettingGroup("work", "Work Mode Settings")
663
        fm_preset = RadioSettingGroup("fm_preset", "FM Preset")
664
        dtmfe = RadioSettingGroup("dtmfe", "DTMF Encode Settings")
665
        service = RadioSettingGroup("service", "Service Settings")
666
        top = RadioSettings(basic, advanced, other, work, fm_preset, dtmfe,
667
                            service)
668

    
669
        # Basic settings
670
        if _mem.settings.squelch > 0x09:
671
            val = 0x00
672
        else:
673
            val = _mem.settings.squelch
674
        rs = RadioSetting("settings.squelch", "Squelch",
675
                          RadioSettingValueList(
676
                              LIST_OFF1TO9, LIST_OFF1TO9[val]))
677
        basic.append(rs)
678

    
679
        if _mem.settings.save > 0x04:
680
            val = 0x00
681
        else:
682
            val = _mem.settings.save
683
        rs = RadioSetting("settings.save", "Battery Saver",
684
                          RadioSettingValueList(
685
                              LIST_SAVE, LIST_SAVE[val]))
686
        basic.append(rs)
687

    
688
        if _mem.settings.vox > 0x0A:
689
            val = 0x00
690
        else:
691
            val = _mem.settings.vox
692
        rs = RadioSetting("settings.vox", "Vox",
693
                          RadioSettingValueList(
694
                              LIST_OFF1TO10, LIST_OFF1TO10[val]))
695
        basic.append(rs)
696

    
697
        if _mem.settings.abr > 0x0A:
698
            val = 0x00
699
        else:
700
            val = _mem.settings.abr
701
        rs = RadioSetting("settings.abr", "Backlight Timeout",
702
                          RadioSettingValueList(
703
                              LIST_OFF1TO10, LIST_OFF1TO10[val]))
704
        basic.append(rs)
705

    
706
        rs = RadioSetting("settings.tdr", "Dual Watch",
707
                          RadioSettingValueBoolean(_mem.settings.tdr))
708
        basic.append(rs)
709

    
710
        rs = RadioSetting("settings.beep", "Beep",
711
                           RadioSettingValueBoolean(_mem.settings.beep))
712
        basic.append(rs)
713

    
714
        if _mem.settings.timeout > 0x27:
715
            val = 0x03
716
        else:
717
            val = _mem.settings.timeout
718
        rs = RadioSetting("settings.timeout", "Timeout Timer",
719
                          RadioSettingValueList(
720
                              LIST_TIMEOUT, LIST_TIMEOUT[val]))
721
        basic.append(rs)
722

    
723
        if _mem.settings.voice > 0x02:
724
            val = 0x01
725
        else:
726
            val = _mem.settings.voice
727
        rs = RadioSetting("settings.voice", "Voice Prompt",
728
                          RadioSettingValueList(
729
                              LIST_VOICE, LIST_VOICE[val]))
730
        basic.append(rs)
731

    
732
        rs = RadioSetting("settings.dtmfst", "DTMF Sidetone",
733
                          RadioSettingValueList(LIST_DTMFST, LIST_DTMFST[
734
                              _mem.settings.dtmfst]))
735
        basic.append(rs)
736

    
737
        if _mem.settings.screv > 0x02:
738
            val = 0x01
739
        else:
740
            val = _mem.settings.screv
741
        rs = RadioSetting("settings.screv", "Scan Resume",
742
                          RadioSettingValueList(
743
                              LIST_RESUME, LIST_RESUME[val]))
744
        basic.append(rs)
745

    
746
        rs = RadioSetting("settings.pttid", "When to send PTT ID",
747
                          RadioSettingValueList(LIST_PTTID, LIST_PTTID[
748
                              _mem.settings.pttid]))
749
        basic.append(rs)
750

    
751
        if _mem.settings.pttlt > 0x1E:
752
            val = 0x05
753
        else:
754
            val = _mem.settings.pttlt
755
        rs = RadioSetting("pttlt", "PTT ID Delay",
756
                          RadioSettingValueInteger(0, 50, val))
757
        basic.append(rs)
758

    
759
        rs = RadioSetting("settings.mdfa", "Display Mode (A)",
760
                          RadioSettingValueList(LIST_MODE, LIST_MODE[
761
                              _mem.settings.mdfa]))
762
        basic.append(rs)
763

    
764
        rs = RadioSetting("settings.mdfb", "Display Mode (B)",
765
                          RadioSettingValueList(LIST_MODE, LIST_MODE[
766
                              _mem.settings.mdfb]))
767
        basic.append(rs)
768

    
769
        rs = RadioSetting("settings.autolk", "Auto Lock Keypad",
770
                          RadioSettingValueBoolean(_mem.settings.autolk))
771
        basic.append(rs)
772

    
773
        rs = RadioSetting("settings.wtled", "Standby LED Color",
774
                          RadioSettingValueList(
775
                              LIST_COLOR, LIST_COLOR[_mem.settings.wtled]))
776
        basic.append(rs)
777

    
778
        rs = RadioSetting("settings.rxled", "RX LED Color",
779
                          RadioSettingValueList(
780
                              LIST_COLOR, LIST_COLOR[_mem.settings.rxled]))
781
        basic.append(rs)
782

    
783
        rs = RadioSetting("settings.txled", "TX LED Color",
784
                          RadioSettingValueList(
785
                              LIST_COLOR, LIST_COLOR[_mem.settings.txled]))
786
        basic.append(rs)
787

    
788
        if _mem.settings.almod > 0x02:
789
            val = 0x00
790
        else:
791
            val = _mem.settings.almod
792
        rs = RadioSetting("settings.almod", "Alarm Mode",
793
                          RadioSettingValueList(
794
                              LIST_ALMOD, LIST_ALMOD[val]))
795
        basic.append(rs)
796

    
797
        if _mem.settings.tcall > 0x05:
798
            val = 0x00
799
        else:
800
            val = _mem.settings.tcall
801
        rs = RadioSetting("settings.tcall", "Tone Burst Frequency",
802
                          RadioSettingValueList(
803
                              LIST_TCALL, LIST_TCALL[val]))
804
        basic.append(rs)
805

    
806
        rs = RadioSetting("settings.ste", "Squelch Tail Eliminate (HT to HT)",
807
                          RadioSettingValueBoolean(_mem.settings.ste))
808
        basic.append(rs)
809

    
810
        if _mem.settings.rpste > 0x0A:
811
            val = 0x00
812
        else:
813
            val = _mem.settings.rpste
814
        rs = RadioSetting("settings.rpste",
815
                          "Squelch Tail Eliminate (repeater)",
816
                              RadioSettingValueList(
817
                              LIST_RPSTE, LIST_RPSTE[val]))
818
        basic.append(rs)
819

    
820
        if _mem.settings.rptrl > 0x0A:
821
            val = 0x00
822
        else:
823
            val = _mem.settings.rptrl
824
        rs = RadioSetting("settings.rptrl", "STE Repeater Delay",
825
                          RadioSettingValueList(
826
                              LIST_STEDELAY, LIST_STEDELAY[val]))
827
        basic.append(rs)
828

    
829
        rs = RadioSetting("settings.ponmsg", "Power-On Message",
830
                          RadioSettingValueList(LIST_PONMSG, LIST_PONMSG[
831
                              _mem.settings.ponmsg]))
832
        basic.append(rs)
833

    
834
        rs = RadioSetting("settings.roger", "Roger Beep",
835
                          RadioSettingValueBoolean(_mem.settings.roger))
836
        basic.append(rs)
837

    
838
        # Advanced settings
839
        rs = RadioSetting("settings.reset", "RESET Menu",
840
                          RadioSettingValueBoolean(_mem.settings.reset))
841
        advanced.append(rs)
842

    
843
        rs = RadioSetting("settings.menu", "All Menus",
844
                          RadioSettingValueBoolean(_mem.settings.menu))
845
        advanced.append(rs)
846

    
847
        rs = RadioSetting("settings.fmradio", "Broadcast FM Radio",
848
                          RadioSettingValueBoolean(_mem.settings.fmradio))
849
        advanced.append(rs)
850

    
851
        rs = RadioSetting("settings.alarm", "Alarm Sound",
852
                          RadioSettingValueBoolean(_mem.settings.alarm))
853
        advanced.append(rs)
854

    
855
        # Other settings
856
        def _filter(name):
857
            filtered = ""
858
            for char in str(name):
859
                if char in chirp_common.CHARSET_ASCII:
860
                    filtered += char
861
                else:
862
                    filtered += " "
863
            return filtered
864

    
865
        _msg = _mem.firmware_msg
866
        val = RadioSettingValueString(0, 8, _filter(_msg.line1))
867
        val.set_mutable(False)
868
        rs = RadioSetting("firmware_msg.line1", "Firmware Message 1", val)
869
        other.append(rs)
870

    
871
        val = RadioSettingValueString(0, 8, _filter(_msg.line2))
872
        val.set_mutable(False)
873
        rs = RadioSetting("firmware_msg.line2", "Firmware Message 2", val)
874
        other.append(rs)
875

    
876
        _msg = _mem.sixpoweron_msg
877
        val = RadioSettingValueString(0, 8, _filter(_msg.line1))
878
        val.set_mutable(False)
879
        rs = RadioSetting("sixpoweron_msg.line1", "6+Power-On Message 1", val)
880
        other.append(rs)
881
        val = RadioSettingValueString(0, 8, _filter(_msg.line2))
882
        val.set_mutable(False)
883
        rs = RadioSetting("sixpoweron_msg.line2", "6+Power-On Message 2", val)
884
        other.append(rs)
885

    
886
        _msg = _mem.poweron_msg
887
        rs = RadioSetting("poweron_msg.line1", "Power-On Message 1",
888
                          RadioSettingValueString(
889
                              0, 7, _filter(_msg.line1)))
890
        other.append(rs)
891
        rs = RadioSetting("poweron_msg.line2", "Power-On Message 2",
892
                          RadioSettingValueString(
893
                              0, 7, _filter(_msg.line2)))
894
        other.append(rs)
895

    
896
        lower = 136
897
        upper = 174
898
        rs = RadioSetting("limits.vhf.lower", "VHF Lower Limit (MHz)",
899
                          RadioSettingValueInteger(
900
                              lower, upper, _mem.limits.vhf.lower))
901
        other.append(rs)
902

    
903
        rs = RadioSetting("limits.vhf.upper", "VHF Upper Limit (MHz)",
904
                          RadioSettingValueInteger(
905
                              lower, upper, _mem.limits.vhf.upper))
906
        other.append(rs)
907

    
908
        lower = 400
909
        upper = 520
910
        rs = RadioSetting("limits.uhf.lower", "UHF Lower Limit (MHz)",
911
                          RadioSettingValueInteger(
912
                              lower, upper, _mem.limits.uhf.lower))
913
        other.append(rs)
914

    
915
        rs = RadioSetting("limits.uhf.upper", "UHF Upper Limit (MHz)",
916
                          RadioSettingValueInteger(
917
                              lower, upper, _mem.limits.uhf.upper))
918
        other.append(rs)
919

    
920
        # Work mode settings
921
        rs = RadioSetting("settings.displayab", "Display",
922
                          RadioSettingValueList(
923
                              LIST_AB, LIST_AB[_mem.settings.displayab]))
924
        work.append(rs)
925

    
926
        rs = RadioSetting("settings.workmode", "VFO/MR Mode",
927
                          RadioSettingValueList(
928
                              LIST_WORKMODE,
929
                              LIST_WORKMODE[_mem.settings.workmode]))
930
        work.append(rs)
931

    
932
        rs = RadioSetting("settings.keylock", "Keypad Lock",
933
                          RadioSettingValueBoolean(_mem.settings.keylock))
934
        work.append(rs)
935

    
936
        if _mem.settings.cht > 0x80:
937
            val = 0x80
938
        else:
939
            val = _mem.settings.cht
940
        rs = RadioSetting("settings.cht", "End of channel list (CHT 1 to 128) ",
941
                          RadioSettingValueInteger(1, 128,
942
                                                      _mem.settings.cht))
943
        work.append(rs)
944

    
945
        rs = RadioSetting("wmchannel.mrcha", "MR A Channel",
946
                          RadioSettingValueInteger(0, 127,
947
                                                      _mem.wmchannel.mrcha))
948
        work.append(rs)
949

    
950
        rs = RadioSetting("wmchannel.mrchb", "MR B Channel",
951
                          RadioSettingValueInteger(0, 127,
952
                                                      _mem.wmchannel.mrchb))
953
        work.append(rs)
954

    
955
        def convert_bytes_to_freq(bytes):
956
            real_freq = 0
957
            for byte in bytes:
958
                real_freq = (real_freq * 10) + byte
959
            return chirp_common.format_freq(real_freq * 10)
960

    
961
        def my_validate(value):
962
            _vhf_lower = int(_mem.limits.vhf.lower)
963
            _vhf_upper = int(_mem.limits.vhf.upper)
964
            _uhf_lower = int(_mem.limits.uhf.lower)
965
            _uhf_upper = int(_mem.limits.uhf.upper)
966
            value = chirp_common.parse_freq(value)
967
            msg = ("Can't be less than %i.0000")
968
            if value > 99000000 and value < _vhf_lower * 1000000:
969
                raise InvalidValueError(msg % _vhf_lower)
970
            msg = ("Can't be between %i.9975-%i.0000")
971
            if _vhf_upper * 1000000 <= value and value < _uhf_lower * 1000000:
972
                raise InvalidValueError(msg % (_vhf_upper - 1, _uhf_lower))
973
            msg = ("Can't be greater than %i.9975")
974
            if value > 99000000 and value >= _uhf_upper * 1000000:
975
                raise InvalidValueError(msg % (_uhf_upper - 1))
976
            return chirp_common.format_freq(value)
977

    
978
        def apply_freq(setting, obj):
979
            value = chirp_common.parse_freq(str(setting.value)) / 10
980
            for i in range(7, -1, -1):
981
                obj.freq[i] = value % 10
982
                value /= 10
983

    
984
        val1a = RadioSettingValueString(0, 10,
985
                                        convert_bytes_to_freq(_mem.vfo.a.freq))
986
        val1a.set_validate_callback(my_validate)
987
        rs = RadioSetting("vfo.a.freq", "VFO A Frequency", val1a)
988
        rs.set_apply_callback(apply_freq, _mem.vfo.a)
989
        work.append(rs)
990

    
991
        val1b = RadioSettingValueString(0, 10,
992
                                        convert_bytes_to_freq(_mem.vfo.b.freq))
993
        val1b.set_validate_callback(my_validate)
994
        rs = RadioSetting("vfo.b.freq", "VFO B Frequency", val1b)
995
        rs.set_apply_callback(apply_freq, _mem.vfo.b)
996
        work.append(rs)
997

    
998
        rs = RadioSetting("vfo.a.sftd", "VFO A Shift",
999
                          RadioSettingValueList(
1000
                              LIST_SHIFTD, LIST_SHIFTD[_mem.vfo.a.sftd]))
1001
        work.append(rs)
1002

    
1003
        rs = RadioSetting("vfo.b.sftd", "VFO B Shift",
1004
                          RadioSettingValueList(
1005
                              LIST_SHIFTD, LIST_SHIFTD[_mem.vfo.b.sftd]))
1006
        work.append(rs)
1007

    
1008
        def convert_bytes_to_offset(bytes):
1009
            real_offset = 0
1010
            for byte in bytes:
1011
                real_offset = (real_offset * 10) + byte
1012
            return chirp_common.format_freq(real_offset * 1000)
1013

    
1014
        def apply_offset(setting, obj):
1015
            value = chirp_common.parse_freq(str(setting.value)) / 1000
1016
            for i in range(5, -1, -1):
1017
                obj.offset[i] = value % 10
1018
                value /= 10
1019

    
1020
        val1a = RadioSettingValueString(
1021
                    0, 10, convert_bytes_to_offset(_mem.vfo.a.offset))
1022
        rs = RadioSetting("vfo.a.offset",
1023
                          "VFO A Offset", val1a)
1024
        rs.set_apply_callback(apply_offset, _mem.vfo.a)
1025
        work.append(rs)
1026

    
1027
        val1b = RadioSettingValueString(
1028
                    0, 10, convert_bytes_to_offset(_mem.vfo.b.offset))
1029
        rs = RadioSetting("vfo.b.offset",
1030
                          "VFO B Offset", val1b)
1031
        rs.set_apply_callback(apply_offset, _mem.vfo.b)
1032
        work.append(rs)
1033

    
1034
        rs = RadioSetting("vfo.a.txpower", "VFO A Power",
1035
                          RadioSettingValueList(
1036
                              LIST_TXPOWER,
1037
                              LIST_TXPOWER[_mem.vfo.a.txpower]))
1038
        work.append(rs)
1039

    
1040
        rs = RadioSetting("vfo.b.txpower", "VFO B Power",
1041
                          RadioSettingValueList(
1042
                              LIST_TXPOWER,
1043
                              LIST_TXPOWER[_mem.vfo.b.txpower]))
1044
        work.append(rs)
1045

    
1046
        rs = RadioSetting("vfo.a.widenarr", "VFO A Bandwidth",
1047
                          RadioSettingValueList(
1048
                              LIST_BANDWIDTH,
1049
                              LIST_BANDWIDTH[_mem.vfo.a.widenarr]))
1050
        work.append(rs)
1051

    
1052
        rs = RadioSetting("vfo.b.widenarr", "VFO B Bandwidth",
1053
                          RadioSettingValueList(
1054
                              LIST_BANDWIDTH,
1055
                              LIST_BANDWIDTH[_mem.vfo.b.widenarr]))
1056
        work.append(rs)
1057

    
1058
        rs = RadioSetting("vfo.a.scode", "VFO A S-CODE",
1059
                          RadioSettingValueList(
1060
                              LIST_SCODE,
1061
                              LIST_SCODE[_mem.vfo.a.scode]))
1062
        work.append(rs)
1063

    
1064
        rs = RadioSetting("vfo.b.scode", "VFO B S-CODE",
1065
                          RadioSettingValueList(
1066
                              LIST_SCODE,
1067
                              LIST_SCODE[_mem.vfo.b.scode]))
1068
        work.append(rs)
1069

    
1070
        rs = RadioSetting("vfo.a.step", "VFO A Tuning Step",
1071
                          RadioSettingValueList(
1072
                              LIST_STEP, LIST_STEP[_mem.vfo.a.step]))
1073
        work.append(rs)
1074
        rs = RadioSetting("vfo.b.step", "VFO B Tuning Step",
1075
                          RadioSettingValueList(
1076
                              LIST_STEP, LIST_STEP[_mem.vfo.b.step]))
1077
        work.append(rs)
1078

    
1079
        # broadcast FM settings
1080
        _fm_presets = self._memobj.fm_presets
1081
        if _fm_presets <= 108.0 * 10 - 650:
1082
            preset = _fm_presets / 10.0 + 65
1083
        elif _fm_presets >= 65.0 * 10 and _fm_presets <= 108.0 * 10:
1084
            preset = _fm_presets / 10.0
1085
        else:
1086
            preset = 76.0
1087
        rs = RadioSetting("fm_presets", "FM Preset(MHz)",
1088
                          RadioSettingValueFloat(65, 108.0, preset, 0.1, 1))
1089
        fm_preset.append(rs)
1090

    
1091
        # DTMF settings
1092
        def apply_code(setting, obj, length):
1093
            code = []
1094
            for j in range(0, length):
1095
                try:
1096
                    code.append(DTMF_CHARS.index(str(setting.value)[j]))
1097
                except IndexError:
1098
                    code.append(0xFF)
1099
            obj.code = code
1100

    
1101
        for i in range(0, 15):
1102
            _codeobj = self._memobj.pttid[i].code
1103
            _code = "".join([DTMF_CHARS[x] for x in _codeobj if int(x) < 0x1F])
1104
            val = RadioSettingValueString(0, 5, _code, False)
1105
            val.set_charset(DTMF_CHARS)
1106
            pttid = RadioSetting("pttid/%i.code" % i,
1107
                                 "Signal Code %i" % (i + 1), val)
1108
            pttid.set_apply_callback(apply_code, self._memobj.pttid[i], 5)
1109
            dtmfe.append(pttid)
1110

    
1111
        if _mem.ani.dtmfon > 0xC3:
1112
            val = 0x03
1113
        else:
1114
            val = _mem.ani.dtmfon
1115
        rs = RadioSetting("ani.dtmfon", "DTMF Speed (on)",
1116
                          RadioSettingValueList(LIST_DTMFSPEED,
1117
                                                LIST_DTMFSPEED[val]))
1118
        dtmfe.append(rs)
1119

    
1120
        if _mem.ani.dtmfoff > 0xC3:
1121
            val = 0x03
1122
        else:
1123
            val = _mem.ani.dtmfoff
1124
        rs = RadioSetting("ani.dtmfoff", "DTMF Speed (off)",
1125
                          RadioSettingValueList(LIST_DTMFSPEED,
1126
                                                LIST_DTMFSPEED[val]))
1127
        dtmfe.append(rs)
1128

    
1129
        _codeobj = self._memobj.ani.code
1130
        _code = "".join([DTMF_CHARS[x] for x in _codeobj if int(x) < 0x1F])
1131
        val = RadioSettingValueString(0, 5, _code, False)
1132
        val.set_charset(DTMF_CHARS)
1133
        rs = RadioSetting("ani.code", "ANI Code", val)
1134
        rs.set_apply_callback(apply_code, self._memobj.ani, 5)
1135
        dtmfe.append(rs)
1136

    
1137
        rs = RadioSetting("ani.aniid", "When to send ANI ID",
1138
                          RadioSettingValueList(LIST_PTTID,
1139
                                                LIST_PTTID[_mem.ani.aniid]))
1140
        dtmfe.append(rs)
1141

    
1142
        # Service settings
1143
        for index in range(0, 10):
1144
            key = "squelch.sql%i" % (index)
1145
            _obj = self._memobj.squelch
1146
            val = RadioSettingValueInteger(0, 123,
1147
                      getattr(_obj, "sql%i" % (index)))
1148
            if index == 0:
1149
                val.set_mutable(False)
1150
            name = "Squelch %i" % (index)
1151
            rs = RadioSetting(key, name, val)
1152
            service.append(rs)
1153

    
1154
        return top
1155

    
1156
    @classmethod
1157
    def match_model(cls, filedata, filename):
1158
        match_size = False
1159
        match_model = False
1160

    
1161
        # testing the file data size
1162
        if len(filedata) == 0x2008 or 0x2010:
1163
            match_size = True
1164

    
1165
        # testing the firmware model fingerprint
1166
        match_model = model_match(cls, filedata)
1167

    
1168
        if match_size and match_model:
1169
            return True
1170
        else:
1171
            return False