gmrsuv1_US32412.py

Kurt Bantle, 07/13/2018 07:50 pm

Download (34.4 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.drivers import baofeng_common
25
from chirp import chirp_common, directory, memmap
26
from chirp import bitwise, errors, util
27
from chirp.settings import RadioSettingGroup, RadioSetting, \
28
    RadioSettingValueBoolean, RadioSettingValueList, \
29
    RadioSettingValueString, RadioSettingValueInteger, \
30
    RadioSettingValueFloat, RadioSettings, \
31
    InvalidValueError
32
from textwrap import dedent
33

    
34
##### MAGICS #########################################################
35

    
36
# BTECH GMRS-V1 magic string
37
MSTRING_GMRSV1 = "\x50\x5F\x20\x15\x12\x15\x4D"
38

    
39
##### ID strings #####################################################
40

    
41
# BTECH GMRS-V1
42
GMRSV1_fp1 = "US32411" # original
43
GMRSV1_fp2 = "US32416" # original
44
GMRSV1_fp3 = "US32418" # new rules
45
GMRSV1_fp4 = "US32412" # original
46

    
47
DTMF_CHARS = "0123456789 *#ABCD"
48
STEPS = [2.5, 5.0, 6.25, 10.0, 12.5, 20.0, 25.0, 50.0]
49

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

    
75
GMRS_FREQS1 = [462.5625, 462.5875, 462.6125, 462.6375, 462.6625,
76
               462.6875, 462.7125]
77
GMRS_FREQS2 = [467.5625, 467.5875, 467.6125, 467.6375, 467.6625,
78
               467.6875, 467.7125]
79
GMRS_FREQS3 = [462.5500, 462.5750, 462.6000, 462.6250, 462.6500,
80
               462.6750, 462.7000, 462.7250]
81
GMRS_FREQS_ORIG = GMRS_FREQS1 + GMRS_FREQS3 * 2
82
GMRS_FREQS_2017 = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
83

    
84
def model_match(cls, data):
85
    """Match the opened/downloaded image to the correct version"""
86
    rid = data[0x1EF0:0x1EF7]
87

    
88
    if rid in cls._fileid:
89
        return True
90

    
91
    return False
92

    
93

    
94
@directory.register
95
class GMRSV1(baofeng_common.BaofengCommonHT):
96
    """BTech GMRS-V1"""
97
    VENDOR = "BTECH"
98
    MODEL = "GMRS-V1"
99

    
100
    _fileid = [GMRSV1_fp3, GMRSV1_fp2, GMRSV1_fp1, GMRSV1_fp4, ]
101
    _is_orig = [GMRSV1_fp2, GMRSV1_fp1, GMRSV1_fp4, ]
102

    
103
    _magic = [MSTRING_GMRSV1, ]
104
    _magic_response_length = 8
105
    _fw_ver_start = 0x1EF0
106
    _recv_block_size = 0x40
107
    _mem_size = 0x2000
108
    _ack_block = True
109

    
110
    _ranges = [(0x0000, 0x0DF0),
111
               (0x0E00, 0x1800),
112
               (0x1EE0, 0x1EF0),
113
               (0x1F60, 0x1F70),
114
               (0x1F80, 0x1F90),
115
               (0x1FC0, 0x1FD0)]
116
    _send_block_size = 0x10
117

    
118
    MODES = ["NFM", "FM"]
119
    VALID_CHARS = chirp_common.CHARSET_ALPHANUMERIC + \
120
        "!@#$%^&*()+-=[]:\";'<>?,./"
121
    LENGTH_NAME = 7
122
    SKIP_VALUES = ["", "S"]
123
    DTCS_CODES = sorted(chirp_common.DTCS_CODES + [645])
124
    POWER_LEVELS = [chirp_common.PowerLevel("High", watts=5.00),
125
                    chirp_common.PowerLevel("Low", watts=2.00)]
126
    VALID_BANDS = [(130000000, 180000000),
127
                   (400000000, 521000000)]
128
    PTTID_LIST = LIST_PTTID
129
    SCODE_LIST = LIST_SCODE
130

    
131

    
132
    def get_features(self):
133
        """Get the radio's features"""
134

    
135
        rf = chirp_common.RadioFeatures()
136
        rf.has_settings = True
137
        rf.has_bank = False
138
        rf.has_tuning_step = False
139
        rf.can_odd_split = False
140
        rf.has_name = True
141
        rf.has_offset = False
142
        rf.has_mode = True
143
        rf.has_dtcs = True
144
        rf.has_rx_dtcs = True
145
        rf.has_dtcs_polarity = True
146
        rf.has_ctone = True
147
        rf.has_cross = True
148
        rf.valid_modes = self.MODES
149
        rf.valid_characters = self.VALID_CHARS
150
        rf.valid_name_length = self.LENGTH_NAME
151
        rf.valid_duplexes = ["", "-", "+"]
152
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
153
        rf.valid_cross_modes = [
154
            "Tone->Tone",
155
            "DTCS->",
156
            "->DTCS",
157
            "Tone->DTCS",
158
            "DTCS->Tone",
159
            "->Tone",
160
            "DTCS->DTCS"]
161
        rf.valid_skips = self.SKIP_VALUES
162
        rf.valid_dtcs_codes = self.DTCS_CODES
163
        rf.memory_bounds = (0, 127)
164
        rf.valid_power_levels = self.POWER_LEVELS
165
        rf.valid_bands = self.VALID_BANDS
166

    
167
        return rf
168

    
169

    
170
    MEM_FORMAT = """
171
    #seekto 0x0000;
172
    struct {
173
      lbcd rxfreq[4];
174
      lbcd txfreq[4];
175
      ul16 rxtone;
176
      ul16 txtone;
177
      u8 unknown0:4,
178
         scode:4;
179
      u8 unknown1;
180
      u8 unknown2:7,
181
         lowpower:1;
182
      u8 unknown3:1,
183
         wide:1,
184
         unknown4:2,
185
         bcl:1,
186
         scan:1,
187
         pttid:2;
188
    } memory[128];
189

    
190
    #seekto 0x0B00;
191
    struct {
192
      u8 code[5];
193
      u8 unused[11];
194
    } pttid[15];
195
    
196
    #seekto 0x0CAA;
197
    struct {
198
      u8 code[5];      
199
      u8 unused1:6,    
200
         aniid:2;      
201
      u8 unknown[2];   
202
      u8 dtmfon;       
203
      u8 dtmfoff;      
204
    } ani;             
205
                       
206
    #seekto 0x0E20;    
207
    struct {           
208
      u8 unused01:4,   
209
         squelch:4;    
210
      u8 unused02;     
211
      u8 unused03;     
212
      u8 unused04:5,   
213
         save:3;       
214
      u8 unused05:4,   
215
         vox:4;        
216
      u8 unused06;     
217
      u8 unused07:4,   
218
         abr:4;        
219
      u8 unused08:7,   
220
         tdr:1;        
221
      u8 unused09:7,   
222
         beep:1;       
223
      u8 unused10:2,   
224
         timeout:6;    
225
      u8 unused11[4];  
226
      u8 unused12:6,   
227
         voice:2;      
228
      u8 unused13;     
229
      u8 unused14:6,   
230
         dtmfst:2;     
231
      u8 unused15;     
232
      u8 unused16:6,   
233
         screv:2;      
234
      u8 unused17:6,   
235
         pttid:2;      
236
      u8 unused18:2,   
237
         pttlt:6;      
238
      u8 unused19:6,   
239
         mdfa:2;       
240
      u8 unused20:6,   
241
         mdfb:2;       
242
      u8 unused21;     
243
      u8 unused22:7,   
244
         sync:1;       
245
      u8 unused23[4];  
246
      u8 unused24:6,   
247
         wtled:2;      
248
      u8 unused25:6,   
249
         rxled:2;      
250
      u8 unused26:6,   
251
         txled:2;      
252
      u8 unused27:6,   
253
         almod:2;      
254
      u8 unused28:7,   
255
         dbptt:1;      
256
      u8 unused29:6,   
257
         tdrab:2;      
258
      u8 unused30:7,   
259
         ste:1;        
260
      u8 unused31:4,   
261
         rpste:4;      
262
      u8 unused32:4,   
263
         rptrl:4;      
264
      u8 unused33:7,   
265
         ponmsg:1;     
266
      u8 unused34:7,   
267
         roger:1;      
268
      u8 unused35:6,   
269
         rtone:2;      
270
      u8 unused36;     
271
      u8 unused37:6,   
272
         rogerrx:2;    
273
      u8 unused38;     
274
      u8 displayab:1,  
275
         unknown1:2,   
276
         fmradio:1,    
277
         alarm:1,      
278
         unknown2:1,   
279
         reset:1,      
280
         menu:1;       
281
      u8 unused39;     
282
      u8 workmode;     
283
      u8 keylock;      
284
      u8 cht;          
285
    } settings;        
286
                       
287
    #seekto 0x0E76;    
288
    struct {           
289
      u8 unused1:1,    
290
         mrcha:7;      
291
      u8 unused2:1,    
292
         mrchb:7;      
293
    } wmchannel;       
294
                       
295
    struct vfo {       
296
      u8 unknown0[8];  
297
      u8 freq[8];      
298
      u8 unknown1;     
299
      u8 offset[4];    
300
      u8 unknown2;     
301
      ul16 rxtone;     
302
      ul16 txtone;     
303
      u8 unused1:7,    
304
         band:1;       
305
      u8 unknown3;     
306
      u8 unused2:2,    
307
         sftd:2,       
308
         scode:4;      
309
      u8 unknown4;     
310
      u8 unused3:1     
311
         step:3,       
312
         unused4:4;    
313
      u8 txpower:1,    
314
         widenarr:1,   
315
         unknown5:4,   
316
         txpower3:2;   
317
    };                 
318
                       
319
    #seekto 0x0F00;    
320
    struct {           
321
      struct vfo a;
322
      struct vfo b;
323
    } vfo;
324
    
325
    #seekto 0x0F4E;
326
    u16 fm_presets;
327
    
328
    #seekto 0x1000;
329
    struct {
330
      char name[7];
331
      u8 unknown1[9];
332
    } names[128];
333
    
334
    #seekto 0x1ED0;
335
    struct {
336
      char line1[7];
337
      char line2[7];
338
    } sixpoweron_msg;
339
    
340
    #seekto 0x1EE0;
341
    struct {
342
      char line1[7];
343
      char line2[7];
344
    } poweron_msg;
345
    
346
    #seekto 0x1EF0;
347
    struct {
348
      char line1[7];
349
      char line2[7];
350
    } firmware_msg;
351

    
352
    struct squelch {
353
      u8 sql0;
354
      u8 sql1;
355
      u8 sql2;
356
      u8 sql3;
357
      u8 sql4;
358
      u8 sql5;
359
      u8 sql6;
360
      u8 sql7;
361
      u8 sql8;
362
      u8 sql9;
363
    };
364
    
365
    #seekto 0x1F60;
366
    struct {
367
      struct squelch vhf;
368
      u8 unknown1[6];
369
      u8 unknown2[16];
370
      struct squelch uhf;
371
    } squelch;
372

    
373
    """
374

    
375
    @classmethod
376
    def get_prompts(cls):
377
        rp = chirp_common.RadioPrompts()
378
        rp.experimental = \
379
            ('The BTech GMRS-V1 driver is a beta version.\n'
380
             '\n'
381
             'Please save an unedited copy of your first successful\n'
382
             'download to a CHIRP Radio Images(*.img) file.'
383
             )
384
        rp.pre_download = _(dedent("""\
385
            Follow these instructions to download your info:
386

    
387
            1 - Turn off your radio
388
            2 - Connect your interface cable
389
            3 - Turn on your radio
390
            4 - Do the download of your radio data
391
            """))
392
        rp.pre_upload = _(dedent("""\
393
            Follow this instructions to upload your info:
394

    
395
            1 - Turn off your radio
396
            2 - Connect your interface cable
397
            3 - Turn on your radio
398
            4 - Do the upload of your radio data
399
            """))
400
        return rp
401

    
402
    def process_mmap(self):
403
        """Process the mem map into the mem object"""
404
        self._memobj = bitwise.parse(self.MEM_FORMAT, self._mmap)
405

    
406
    def validate_memory(self, mem):
407
        msgs = baofeng_common.BaofengCommonHT.validate_memory(self, mem)
408

    
409
        _mem = self._memobj.memory[mem.number]
410
        _msg_freq = 'Memory location cannot change frequency'
411
        _msg_nfm = 'Memory location only supports NFM'
412
        _msg_txp = 'Memory location only supports Low'
413

    
414
        # Original GMRS-V1 models
415
        if str(self._memobj.firmware_msg.line1) in self._is_orig:
416
            # range of memories with values permanently set by FCC rules
417
            if mem.number <= 22:
418
                if mem.freq != int(GMRS_FREQS_ORIG[mem.number] * 1000000):
419
                    # warn user can't change frequency
420
                    msgs.append(chirp_common.ValidationError(_msg_freq))
421

    
422
                if mem.number <= 6:
423
                    if mem.mode == "FM":
424
                        # warn user can't change mode
425
                        msgs.append(chirp_common.ValidationError(_msg_nfm))
426

    
427
        # GMRS-V1 models supporting 2017 GMRS rules
428
        else:
429
            # range of memories with values permanently set by FCC rules
430
            if mem.number >= 1 and mem.number <= 30:
431
                if mem.freq != int(GMRS_FREQS_2017[mem.number - 1] * 1000000):
432
                    # warn user can't change frequency
433
                    msgs.append(chirp_common.ValidationError(_msg_freq))
434

    
435
                if mem.number >= 8 and mem.number <= 14:
436
                    if mem.mode == "FM":
437
                        # warn user can't change mode
438
                        msgs.append(chirp_common.ValidationError(_msg_nfm))
439

    
440
                    if str(mem.power) == "High":
441
                        # warn user can't change power level
442
                        msgs.append(chirp_common.ValidationError(_msg_txp))
443

    
444
        return msgs
445

    
446
    def get_memory(self, number):
447
        _mem = self._memobj.memory[number]
448
        _nam = self._memobj.names[number]
449

    
450
        mem = chirp_common.Memory()
451
        mem.number = number
452

    
453
        if _mem.get_raw()[0] == "\xff":
454
            mem.empty = True
455
            return mem
456

    
457
        mem.freq = int(_mem.rxfreq) * 10
458

    
459
        # TX freq set
460
        offset = (int(_mem.txfreq) * 10) - mem.freq
461
        if offset != 0:
462
            if offset > 0:
463
                mem.offset = offset
464
                mem.duplex = "+"
465
        else:
466
            mem.offset = 0
467

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

    
474
        dtcs_pol = ["N", "N"]
475

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

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

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

    
518
        mem.dtcs_polarity = "".join(dtcs_pol)
519

    
520
        if not _mem.scan:
521
            mem.skip = "S"
522

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

    
531
        mem.mode = _mem.wide and "FM" or "NFM"
532

    
533
        mem.extra = RadioSettingGroup("Extra", "extra")
534

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

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

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

    
549
        return mem
550

    
551
    def set_memory(self, mem):
552
        _mem = self._memobj.memory[mem.number]
553
        _nam = self._memobj.names[mem.number]
554

    
555
        if mem.empty:
556
            _mem.set_raw("\xff" * 16)
557
            _nam.set_raw("\xff" * 16)
558
            return
559

    
560
        _mem.set_raw("\x00" * 16)
561

    
562
        _mem.rxfreq = mem.freq / 10
563

    
564
        if str(self._memobj.firmware_msg.line1) in self._is_orig:
565
            if mem.number > 22:
566
                _mem.txfreq = mem.freq / 10
567
        else:
568
            if mem.number < 1 or mem.number > 22:
569
                _mem.txfreq = mem.freq / 10
570

    
571
        _namelength = self.get_features().valid_name_length
572
        for i in range(_namelength):
573
            try:
574
                _nam.name[i] = mem.name[i]
575
            except IndexError:
576
                _nam.name[i] = "\xFF"
577

    
578
        rxmode = txmode = ""
579
        if mem.tmode == "Tone":
580
            _mem.txtone = int(mem.rtone * 10)
581
            _mem.rxtone = 0
582
        elif mem.tmode == "TSQL":
583
            _mem.txtone = int(mem.ctone * 10)
584
            _mem.rxtone = int(mem.ctone * 10)
585
        elif mem.tmode == "DTCS":
586
            rxmode = txmode = "DTCS"
587
            _mem.txtone = self.DTCS_CODES.index(mem.dtcs) + 1
588
            _mem.rxtone = self.DTCS_CODES.index(mem.dtcs) + 1
589
        elif mem.tmode == "Cross":
590
            txmode, rxmode = mem.cross_mode.split("->", 1)
591
            if txmode == "Tone":
592
                _mem.txtone = int(mem.rtone * 10)
593
            elif txmode == "DTCS":
594
                _mem.txtone = self.DTCS_CODES.index(mem.dtcs) + 1
595
            else:
596
                _mem.txtone = 0
597
            if rxmode == "Tone":
598
                _mem.rxtone = int(mem.ctone * 10)
599
            elif rxmode == "DTCS":
600
                _mem.rxtone = self.DTCS_CODES.index(mem.rx_dtcs) + 1
601
            else:
602
                _mem.rxtone = 0
603
        else:
604
            _mem.rxtone = 0
605
            _mem.txtone = 0
606

    
607
        if txmode == "DTCS" and mem.dtcs_polarity[0] == "R":
608
            _mem.txtone += 0x69
609
        if rxmode == "DTCS" and mem.dtcs_polarity[1] == "R":
610
            _mem.rxtone += 0x69
611

    
612
        _mem.scan = mem.skip != "S"
613
        _mem.wide = mem.mode == "FM"
614

    
615
        if mem.power:
616
            _mem.lowpower = self.POWER_LEVELS.index(mem.power)
617
        else:
618
            _mem.lowpower = 0
619

    
620
        # extra settings
621
        if len(mem.extra) > 0:
622
            # there are setting, parse
623
            for setting in mem.extra:
624
                setattr(_mem, setting.get_name(), setting.value)
625

    
626
    def get_settings(self):
627
        """Translate the bit in the mem_struct into settings in the UI"""
628
        _mem = self._memobj
629
        basic = RadioSettingGroup("basic", "Basic Settings")
630
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
631
        other = RadioSettingGroup("other", "Other Settings")
632
        work = RadioSettingGroup("work", "Work Mode Settings")
633
        fm_preset = RadioSettingGroup("fm_preset", "FM Preset")
634
        dtmfe = RadioSettingGroup("dtmfe", "DTMF Encode Settings")
635
        service = RadioSettingGroup("service", "Service Settings")
636
        top = RadioSettings(basic, advanced, other, work, fm_preset, dtmfe,
637
                            service)
638

    
639
        # Basic settings
640
        if _mem.settings.squelch > 0x09:
641
            val = 0x00
642
        else:
643
            val = _mem.settings.squelch
644
        rs = RadioSetting("settings.squelch", "Squelch",
645
                          RadioSettingValueList(
646
                              LIST_OFF1TO9, LIST_OFF1TO9[val]))
647
        basic.append(rs)
648

    
649
        if _mem.settings.save > 0x04:
650
            val = 0x00
651
        else:
652
            val = _mem.settings.save
653
        rs = RadioSetting("settings.save", "Battery Saver",
654
                          RadioSettingValueList(
655
                              LIST_SAVE, LIST_SAVE[val]))
656
        basic.append(rs)
657

    
658
        if _mem.settings.vox > 0x0A:
659
            val = 0x00
660
        else:
661
            val = _mem.settings.vox
662
        rs = RadioSetting("settings.vox", "Vox",
663
                          RadioSettingValueList(
664
                              LIST_OFF1TO10, LIST_OFF1TO10[val]))
665
        basic.append(rs)
666

    
667
        if _mem.settings.abr > 0x0A:
668
            val = 0x00
669
        else:
670
            val = _mem.settings.abr
671
        rs = RadioSetting("settings.abr", "Backlight Timeout",
672
                          RadioSettingValueList(
673
                              LIST_OFF1TO10, LIST_OFF1TO10[val]))
674
        basic.append(rs)
675

    
676
        rs = RadioSetting("settings.tdr", "Dual Watch",
677
                          RadioSettingValueBoolean(_mem.settings.tdr))
678
        basic.append(rs)
679

    
680
        rs = RadioSetting("settings.beep", "Beep",
681
                           RadioSettingValueBoolean(_mem.settings.beep))
682
        basic.append(rs)
683

    
684
        if _mem.settings.timeout > 0x27:
685
            val = 0x03
686
        else:
687
            val = _mem.settings.timeout
688
        rs = RadioSetting("settings.timeout", "Timeout Timer",
689
                          RadioSettingValueList(
690
                              LIST_TIMEOUT, LIST_TIMEOUT[val]))
691
        basic.append(rs)
692

    
693
        if _mem.settings.voice > 0x02:
694
            val = 0x01
695
        else:
696
            val = _mem.settings.voice
697
        rs = RadioSetting("settings.voice", "Voice Prompt",
698
                          RadioSettingValueList(
699
                              LIST_VOICE, LIST_VOICE[val]))
700
        basic.append(rs)
701

    
702
        rs = RadioSetting("settings.dtmfst", "DTMF Sidetone",
703
                          RadioSettingValueList(LIST_DTMFST, LIST_DTMFST[
704
                              _mem.settings.dtmfst]))
705
        basic.append(rs)
706

    
707
        if _mem.settings.screv > 0x02:
708
            val = 0x01
709
        else:
710
            val = _mem.settings.screv
711
        rs = RadioSetting("settings.screv", "Scan Resume",
712
                          RadioSettingValueList(
713
                              LIST_RESUME, LIST_RESUME[val]))
714
        basic.append(rs)
715

    
716
        rs = RadioSetting("settings.pttid", "When to send PTT ID",
717
                          RadioSettingValueList(LIST_PTTID, LIST_PTTID[
718
                              _mem.settings.pttid]))
719
        basic.append(rs)
720

    
721
        if _mem.settings.pttlt > 0x1E:
722
            val = 0x05
723
        else:
724
            val = _mem.settings.pttlt
725
        rs = RadioSetting("pttlt", "PTT ID Delay",
726
                          RadioSettingValueInteger(0, 50, val))
727
        basic.append(rs)
728

    
729
        rs = RadioSetting("settings.mdfa", "Display Mode (A)",
730
                          RadioSettingValueList(LIST_MODE, LIST_MODE[
731
                              _mem.settings.mdfa]))
732
        basic.append(rs)
733

    
734
        rs = RadioSetting("settings.mdfb", "Display Mode (B)",
735
                          RadioSettingValueList(LIST_MODE, LIST_MODE[
736
                              _mem.settings.mdfb]))
737
        basic.append(rs)
738

    
739
        rs = RadioSetting("settings.sync", "Sync A & B",
740
                          RadioSettingValueBoolean(_mem.settings.sync))
741
        basic.append(rs)
742

    
743
        rs = RadioSetting("settings.wtled", "Standby LED Color",
744
                          RadioSettingValueList(
745
                              LIST_COLOR, LIST_COLOR[_mem.settings.wtled]))
746
        basic.append(rs)
747

    
748
        rs = RadioSetting("settings.rxled", "RX LED Color",
749
                          RadioSettingValueList(
750
                              LIST_COLOR, LIST_COLOR[_mem.settings.rxled]))
751
        basic.append(rs)
752

    
753
        rs = RadioSetting("settings.txled", "TX LED Color",
754
                          RadioSettingValueList(
755
                              LIST_COLOR, LIST_COLOR[_mem.settings.txled]))
756
        basic.append(rs)
757

    
758
        val = _mem.settings.almod
759
        rs = RadioSetting("settings.almod", "Alarm Mode",
760
                          RadioSettingValueList(
761
                              LIST_ALMOD, LIST_ALMOD[val]))
762
        basic.append(rs)
763

    
764
        rs = RadioSetting("settings.dbptt", "Double PTT",
765
                          RadioSettingValueBoolean(_mem.settings.dbptt))
766
        basic.append(rs)
767

    
768
        if _mem.settings.tdrab > 0x02:
769
            val = 0x00
770
        else:
771
            val = _mem.settings.tdrab
772
        rs = RadioSetting("settings.tdrab", "Dual Watch TX Priority",
773
                          RadioSettingValueList(
774
                              LIST_OFFAB, LIST_OFFAB[val]))
775
        basic.append(rs)
776

    
777
        rs = RadioSetting("settings.ste", "Squelch Tail Eliminate (HT to HT)",
778
                          RadioSettingValueBoolean(_mem.settings.ste))
779
        basic.append(rs)
780

    
781
        if _mem.settings.rpste > 0x0A:
782
            val = 0x00
783
        else:
784
            val = _mem.settings.rpste
785
        rs = RadioSetting("settings.rpste",
786
                          "Squelch Tail Eliminate (repeater)",
787
                              RadioSettingValueList(
788
                              LIST_RPSTE, LIST_RPSTE[val]))
789
        basic.append(rs)
790

    
791
        if _mem.settings.rptrl > 0x0A:
792
            val = 0x00
793
        else:
794
            val = _mem.settings.rptrl
795
        rs = RadioSetting("settings.rptrl", "STE Repeater Delay",
796
                          RadioSettingValueList(
797
                              LIST_STEDELAY, LIST_STEDELAY[val]))
798
        basic.append(rs)
799

    
800
        rs = RadioSetting("settings.ponmsg", "Power-On Message",
801
                          RadioSettingValueList(LIST_PONMSG, LIST_PONMSG[
802
                              _mem.settings.ponmsg]))
803
        basic.append(rs)
804

    
805
        rs = RadioSetting("settings.roger", "Roger Beep",
806
                          RadioSettingValueBoolean(_mem.settings.roger))
807
        basic.append(rs)
808

    
809
        rs = RadioSetting("settings.rtone", "Tone Burst Frequency",
810
                          RadioSettingValueList(LIST_RTONE, LIST_RTONE[
811
                              _mem.settings.rtone]))
812
        basic.append(rs)
813

    
814
        rs = RadioSetting("settings.rogerrx", "Roger Beep (RX)",
815
                          RadioSettingValueList(
816
                             LIST_OFFAB, LIST_OFFAB[
817
                             _mem.settings.rogerrx]))
818
        basic.append(rs)
819

    
820
        # Advanced settings
821
        rs = RadioSetting("settings.reset", "RESET Menu",
822
                          RadioSettingValueBoolean(_mem.settings.reset))
823
        advanced.append(rs)
824

    
825
        rs = RadioSetting("settings.menu", "All Menus",
826
                          RadioSettingValueBoolean(_mem.settings.menu))
827
        advanced.append(rs)
828

    
829
        rs = RadioSetting("settings.fmradio", "Broadcast FM Radio",
830
                          RadioSettingValueBoolean(_mem.settings.fmradio))
831
        advanced.append(rs)
832

    
833
        rs = RadioSetting("settings.alarm", "Alarm Sound",
834
                          RadioSettingValueBoolean(_mem.settings.alarm))
835
        advanced.append(rs)
836

    
837
        # Other settings
838
        def _filter(name):
839
            filtered = ""
840
            for char in str(name):
841
                if char in chirp_common.CHARSET_ASCII:
842
                    filtered += char
843
                else:
844
                    filtered += " "
845
            return filtered
846

    
847
        _msg = _mem.firmware_msg
848
        val = RadioSettingValueString(0, 7, _filter(_msg.line1))
849
        val.set_mutable(False)
850
        rs = RadioSetting("firmware_msg.line1", "Firmware Message 1", val)
851
        other.append(rs)
852

    
853
        val = RadioSettingValueString(0, 7, _filter(_msg.line2))
854
        val.set_mutable(False)
855
        rs = RadioSetting("firmware_msg.line2", "Firmware Message 2", val)
856
        other.append(rs)
857

    
858
        _msg = _mem.sixpoweron_msg
859
        val = RadioSettingValueString(0, 7, _filter(_msg.line1))
860
        val.set_mutable(False)
861
        rs = RadioSetting("sixpoweron_msg.line1", "6+Power-On Message 1", val)
862
        other.append(rs)
863
        val = RadioSettingValueString(0, 7, _filter(_msg.line2))
864
        val.set_mutable(False)
865
        rs = RadioSetting("sixpoweron_msg.line2", "6+Power-On Message 2", val)
866
        other.append(rs)
867

    
868
        _msg = _mem.poweron_msg
869
        rs = RadioSetting("poweron_msg.line1", "Power-On Message 1",
870
                          RadioSettingValueString(
871
                              0, 7, _filter(_msg.line1)))
872
        other.append(rs)
873
        rs = RadioSetting("poweron_msg.line2", "Power-On Message 2",
874
                          RadioSettingValueString(
875
                              0, 7, _filter(_msg.line2)))
876
        other.append(rs)
877

    
878
        # Work mode settings
879
        rs = RadioSetting("settings.displayab", "Display",
880
                          RadioSettingValueList(
881
                              LIST_AB, LIST_AB[_mem.settings.displayab]))
882
        work.append(rs)
883

    
884
        rs = RadioSetting("settings.workmode", "VFO/MR Mode",
885
                          RadioSettingValueList(
886
                              LIST_WORKMODE,
887
                              LIST_WORKMODE[_mem.settings.workmode]))
888
        work.append(rs)
889

    
890
        rs = RadioSetting("settings.keylock", "Keypad Lock",
891
                          RadioSettingValueBoolean(_mem.settings.keylock))
892
        work.append(rs)
893

    
894
        rs = RadioSetting("wmchannel.mrcha", "MR A Channel",
895
                          RadioSettingValueInteger(0, 127,
896
                                                      _mem.wmchannel.mrcha))
897
        work.append(rs)
898

    
899
        rs = RadioSetting("wmchannel.mrchb", "MR B Channel",
900
                          RadioSettingValueInteger(0, 127,
901
                                                      _mem.wmchannel.mrchb))
902
        work.append(rs)
903

    
904
        def convert_bytes_to_freq(bytes):
905
            real_freq = 0
906
            for byte in bytes:
907
                real_freq = (real_freq * 10) + byte
908
            return chirp_common.format_freq(real_freq * 10)
909

    
910
        def my_validate(value):
911
            value = chirp_common.parse_freq(value)
912
            msg = ("Can't be less than %i.0000")
913
            if value > 99000000 and value < 130 * 1000000:
914
                raise InvalidValueError(msg % (130))
915
            msg = ("Can't be between %i.9975-%i.0000")
916
            if (179 + 1) * 1000000 <= value and value < 400 * 1000000:
917
                raise InvalidValueError(msg % (179, 400))
918
            msg = ("Can't be greater than %i.9975")
919
            if value > 99000000 and value > (520 + 1) * 1000000:
920
                raise InvalidValueError(msg % (520))
921
            return chirp_common.format_freq(value)
922

    
923
        def apply_freq(setting, obj):
924
            value = chirp_common.parse_freq(str(setting.value)) / 10
925
            for i in range(7, -1, -1):
926
                obj.freq[i] = value % 10
927
                value /= 10
928

    
929
        val1a = RadioSettingValueString(0, 10,
930
                                        convert_bytes_to_freq(_mem.vfo.a.freq))
931
        val1a.set_validate_callback(my_validate)
932
        rs = RadioSetting("vfo.a.freq", "VFO A Frequency", val1a)
933
        rs.set_apply_callback(apply_freq, _mem.vfo.a)
934
        work.append(rs)
935

    
936
        val1b = RadioSettingValueString(0, 10,
937
                                        convert_bytes_to_freq(_mem.vfo.b.freq))
938
        val1b.set_validate_callback(my_validate)
939
        rs = RadioSetting("vfo.b.freq", "VFO B Frequency", val1b)
940
        rs.set_apply_callback(apply_freq, _mem.vfo.b)
941
        work.append(rs)
942

    
943
        rs = RadioSetting("vfo.a.step", "VFO A Tuning Step",
944
                          RadioSettingValueList(
945
                              LIST_STEP, LIST_STEP[_mem.vfo.a.step]))
946
        work.append(rs)
947
        rs = RadioSetting("vfo.b.step", "VFO B Tuning Step",
948
                          RadioSettingValueList(
949
                              LIST_STEP, LIST_STEP[_mem.vfo.b.step]))
950
        work.append(rs)
951

    
952
        # broadcast FM settings
953
        _fm_presets = self._memobj.fm_presets
954
        if _fm_presets <= 108.0 * 10 - 650:
955
            preset = _fm_presets / 10.0 + 65
956
        elif _fm_presets >= 65.0 * 10 and _fm_presets <= 108.0 * 10:
957
            preset = _fm_presets / 10.0
958
        else:
959
            preset = 76.0
960
        rs = RadioSetting("fm_presets", "FM Preset(MHz)",
961
                          RadioSettingValueFloat(65, 108.0, preset, 0.1, 1))
962
        fm_preset.append(rs)
963

    
964
        # DTMF settings
965
        def apply_code(setting, obj, length):
966
            code = []
967
            for j in range(0, length):
968
                try:
969
                    code.append(DTMF_CHARS.index(str(setting.value)[j]))
970
                except IndexError:
971
                    code.append(0xFF)
972
            obj.code = code
973

    
974
        for i in range(0, 15):
975
            _codeobj = self._memobj.pttid[i].code
976
            _code = "".join([DTMF_CHARS[x] for x in _codeobj if int(x) < 0x1F])
977
            val = RadioSettingValueString(0, 5, _code, False)
978
            val.set_charset(DTMF_CHARS)
979
            pttid = RadioSetting("pttid/%i.code" % i,
980
                                 "Signal Code %i" % (i + 1), val)
981
            pttid.set_apply_callback(apply_code, self._memobj.pttid[i], 5)
982
            dtmfe.append(pttid)
983

    
984
        if _mem.ani.dtmfon > 0xC3:
985
            val = 0x03
986
        else:
987
            val = _mem.ani.dtmfon
988
        rs = RadioSetting("ani.dtmfon", "DTMF Speed (on)",
989
                          RadioSettingValueList(LIST_DTMFSPEED,
990
                                                LIST_DTMFSPEED[val]))
991
        dtmfe.append(rs)
992

    
993
        if _mem.ani.dtmfoff > 0xC3:
994
            val = 0x03
995
        else:
996
            val = _mem.ani.dtmfoff
997
        rs = RadioSetting("ani.dtmfoff", "DTMF Speed (off)",
998
                          RadioSettingValueList(LIST_DTMFSPEED,
999
                                                LIST_DTMFSPEED[val]))
1000
        dtmfe.append(rs)
1001

    
1002
        _codeobj = self._memobj.ani.code
1003
        _code = "".join([DTMF_CHARS[x] for x in _codeobj if int(x) < 0x1F])
1004
        val = RadioSettingValueString(0, 5, _code, False)
1005
        val.set_charset(DTMF_CHARS)
1006
        rs = RadioSetting("ani.code", "ANI Code", val)
1007
        rs.set_apply_callback(apply_code, self._memobj.ani, 5)
1008
        dtmfe.append(rs)
1009

    
1010
        rs = RadioSetting("ani.aniid", "When to send ANI ID",
1011
                          RadioSettingValueList(LIST_PTTID,
1012
                                                LIST_PTTID[_mem.ani.aniid]))
1013
        dtmfe.append(rs)
1014

    
1015
        # Service settings
1016
        for band in ["vhf", "uhf"]:
1017
            for index in range(0, 10):
1018
                key = "squelch.%s.sql%i" % (band, index)
1019
                if band == "vhf":
1020
                    _obj = self._memobj.squelch.vhf
1021
                elif band == "uhf":
1022
                    _obj = self._memobj.squelch.uhf
1023
                val = RadioSettingValueInteger(0, 123,
1024
                          getattr(_obj, "sql%i" % (index)))
1025
                if index == 0:
1026
                    val.set_mutable(False)
1027
                name = "%s Squelch %i" % (band.upper(), index)
1028
                rs = RadioSetting(key, name, val)
1029
                service.append(rs)
1030

    
1031
        return top
1032

    
1033
    @classmethod
1034
    def match_model(cls, filedata, filename):
1035
        match_size = False
1036
        match_model = False
1037

    
1038
        # testing the file data size
1039
        if len(filedata) == 0x2008:
1040
            match_size = True
1041

    
1042
        # testing the firmware model fingerprint
1043
        match_model = model_match(cls, filedata)
1044

    
1045
        if match_size and match_model:
1046
            return True
1047
        else:
1048
            return False