uv6r.py

Gilles Drumain, 03/31/2020 01:35 pm

Download (28.2 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
# Baofeng UV-6R magic string
37
MSTRING_UV6R = "\x50\xBB\xFF\x20\x14\x11\x22"                                
38
                                                                             
39
##### ID strings #####################################################       
40
                                                                             
41
# Baofeng UV-6R                                                              
42
UV6R_fp1 = " BF230#1"
43
UV6R_fp2 = " BF230#2"
44

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

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

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

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

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

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

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

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

    
93

    
94
@directory.register
95
class UV6R(baofeng_common.BaofengCommonHT):
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
         reset:1,
236
         menu:1;
237
      u8 unknown11;
238
      u8 unused29:7,
239
         workmode:1;
240
      u8 unused30:7,
241
         keylock:1;
242
      u8 cht;
243
    } settings;
244

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

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

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

    
280
    #seekto 0x0F4E;
281
    u16 fm_presets;
282

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

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

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

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

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

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

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

    
333
    """
334

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

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

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

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

    
366
    def get_settings(self):
367
        """Translate the bit in the mem_struct into settings in the UI"""
368
        _mem = self._memobj
369
        basic = RadioSettingGroup("basic", "Basic Settings")
370
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
371
        other = RadioSettingGroup("other", "Other Settings")
372
        work = RadioSettingGroup("work", "Work Mode Settings")
373
        fm_preset = RadioSettingGroup("fm_preset", "FM Preset")
374
        dtmfe = RadioSettingGroup("dtmfe", "DTMF Encode Settings")
375
        service = RadioSettingGroup("service", "Service Settings")
376
        top = RadioSettings(basic, advanced, other, work, fm_preset, dtmfe,
377
                            service)
378

    
379
        # Basic settings
380
        if _mem.settings.squelch > 0x09:
381
            val = 0x00
382
        else:
383
            val = _mem.settings.squelch
384
        rs = RadioSetting("settings.squelch", "Squelch",
385
                          RadioSettingValueList(
386
                              LIST_OFF1TO9, LIST_OFF1TO9[val]))
387
        basic.append(rs)
388

    
389
        if _mem.settings.save > 0x04:
390
            val = 0x00
391
        else:
392
            val = _mem.settings.save
393
        rs = RadioSetting("settings.save", "Battery Saver",
394
                          RadioSettingValueList(
395
                              LIST_SAVE, LIST_SAVE[val]))
396
        basic.append(rs)
397

    
398
        if _mem.settings.vox > 0x0A:
399
            val = 0x00
400
        else:
401
            val = _mem.settings.vox
402
        rs = RadioSetting("settings.vox", "Vox",
403
                          RadioSettingValueList(
404
                              LIST_OFF1TO10, LIST_OFF1TO10[val]))
405
        basic.append(rs)
406

    
407
        if _mem.settings.abr > 0x0A:
408
            val = 0x00
409
        else:
410
            val = _mem.settings.abr
411
        rs = RadioSetting("settings.abr", "Backlight Timeout",
412
                          RadioSettingValueList(
413
                              LIST_OFF1TO10, LIST_OFF1TO10[val]))
414
        basic.append(rs)
415

    
416
        rs = RadioSetting("settings.tdr", "Dual Watch",
417
                          RadioSettingValueBoolean(_mem.settings.tdr))
418
        basic.append(rs)
419

    
420
        rs = RadioSetting("settings.beep", "Beep",
421
                           RadioSettingValueBoolean(_mem.settings.beep))
422
        basic.append(rs)
423

    
424
        if _mem.settings.timeout > 0x27:
425
            val = 0x03
426
        else:
427
            val = _mem.settings.timeout
428
        rs = RadioSetting("settings.timeout", "Timeout Timer",
429
                          RadioSettingValueList(
430
                              LIST_TIMEOUT, LIST_TIMEOUT[val]))
431
        basic.append(rs)
432

    
433
        if _mem.settings.voice > 0x02:
434
            val = 0x01
435
        else:
436
            val = _mem.settings.voice
437
        rs = RadioSetting("settings.voice", "Voice Prompt",
438
                          RadioSettingValueList(
439
                              LIST_VOICE, LIST_VOICE[val]))
440
        basic.append(rs)
441

    
442
        rs = RadioSetting("settings.dtmfst", "DTMF Sidetone",
443
                          RadioSettingValueList(LIST_DTMFST, LIST_DTMFST[
444
                              _mem.settings.dtmfst]))
445
        basic.append(rs)
446

    
447
        if _mem.settings.screv > 0x02:
448
            val = 0x01
449
        else:
450
            val = _mem.settings.screv
451
        rs = RadioSetting("settings.screv", "Scan Resume",
452
                          RadioSettingValueList(
453
                              LIST_RESUME, LIST_RESUME[val]))
454
        basic.append(rs)
455

    
456
        rs = RadioSetting("settings.pttid", "When to send PTT ID",
457
                          RadioSettingValueList(LIST_PTTID, LIST_PTTID[
458
                              _mem.settings.pttid]))
459
        basic.append(rs)
460

    
461
        if _mem.settings.pttlt > 0x1E:
462
            val = 0x05
463
        else:
464
            val = _mem.settings.pttlt
465
        rs = RadioSetting("pttlt", "PTT ID Delay",
466
                          RadioSettingValueInteger(0, 50, val))
467
        basic.append(rs)
468

    
469
        rs = RadioSetting("settings.mdfa", "Display Mode (A)",
470
                          RadioSettingValueList(LIST_MODE, LIST_MODE[
471
                              _mem.settings.mdfa]))
472
        basic.append(rs)
473

    
474
        rs = RadioSetting("settings.mdfb", "Display Mode (B)",
475
                          RadioSettingValueList(LIST_MODE, LIST_MODE[
476
                              _mem.settings.mdfb]))
477
        basic.append(rs)
478

    
479
        rs = RadioSetting("settings.autolk", "Auto Lock Keypad",
480
                          RadioSettingValueBoolean(_mem.settings.autolk))
481
        basic.append(rs)
482

    
483
        rs = RadioSetting("settings.wtled", "Standby LED Color",
484
                          RadioSettingValueList(
485
                              LIST_COLOR, LIST_COLOR[_mem.settings.wtled]))
486
        basic.append(rs)
487

    
488
        rs = RadioSetting("settings.rxled", "RX LED Color",
489
                          RadioSettingValueList(
490
                              LIST_COLOR, LIST_COLOR[_mem.settings.rxled]))
491
        basic.append(rs)
492

    
493
        rs = RadioSetting("settings.txled", "TX LED Color",
494
                          RadioSettingValueList(
495
                              LIST_COLOR, LIST_COLOR[_mem.settings.txled]))
496
        basic.append(rs)
497

    
498
        if _mem.settings.almod > 0x02:
499
            val = 0x00
500
        else:
501
            val = _mem.settings.almod
502
        rs = RadioSetting("settings.almod", "Alarm Mode",
503
                          RadioSettingValueList(
504
                              LIST_ALMOD, LIST_ALMOD[val]))
505
        basic.append(rs)
506

    
507
        if _mem.settings.tcall > 0x05:
508
            val = 0x00
509
        else:
510
            val = _mem.settings.tcall
511
        rs = RadioSetting("settings.tcall", "Tone Burst Frequency",
512
                          RadioSettingValueList(
513
                              LIST_TCALL, LIST_TCALL[val]))
514
        basic.append(rs)
515

    
516
        rs = RadioSetting("settings.ste", "Squelch Tail Eliminate (HT to HT)",
517
                          RadioSettingValueBoolean(_mem.settings.ste))
518
        basic.append(rs)
519

    
520
        if _mem.settings.rpste > 0x0A:
521
            val = 0x00
522
        else:
523
            val = _mem.settings.rpste
524
        rs = RadioSetting("settings.rpste",
525
                          "Squelch Tail Eliminate (repeater)",
526
                              RadioSettingValueList(
527
                              LIST_RPSTE, LIST_RPSTE[val]))
528
        basic.append(rs)
529

    
530
        if _mem.settings.rptrl > 0x0A:
531
            val = 0x00
532
        else:
533
            val = _mem.settings.rptrl
534
        rs = RadioSetting("settings.rptrl", "STE Repeater Delay",
535
                          RadioSettingValueList(
536
                              LIST_STEDELAY, LIST_STEDELAY[val]))
537
        basic.append(rs)
538

    
539
        rs = RadioSetting("settings.ponmsg", "Power-On Message",
540
                          RadioSettingValueList(LIST_PONMSG, LIST_PONMSG[
541
                              _mem.settings.ponmsg]))
542
        basic.append(rs)
543

    
544
        rs = RadioSetting("settings.roger", "Roger Beep",
545
                          RadioSettingValueBoolean(_mem.settings.roger))
546
        basic.append(rs)
547

    
548
        # Advanced settings
549
        rs = RadioSetting("settings.reset", "RESET Menu",
550
                          RadioSettingValueBoolean(_mem.settings.reset))
551
        advanced.append(rs)
552

    
553
        rs = RadioSetting("settings.menu", "All Menus",
554
                          RadioSettingValueBoolean(_mem.settings.menu))
555
        advanced.append(rs)
556

    
557
        rs = RadioSetting("settings.fmradio", "Broadcast FM Radio",
558
                          RadioSettingValueBoolean(_mem.settings.fmradio))
559
        advanced.append(rs)
560

    
561
        rs = RadioSetting("settings.alarm", "Alarm Sound",
562
                          RadioSettingValueBoolean(_mem.settings.alarm))
563
        advanced.append(rs)
564

    
565
        # Other settings
566
        def _filter(name):
567
            filtered = ""
568
            for char in str(name):
569
                if char in chirp_common.CHARSET_ASCII:
570
                    filtered += char
571
                else:
572
                    filtered += " "
573
            return filtered
574

    
575
        _msg = _mem.firmware_msg
576
        val = RadioSettingValueString(0, 8, _filter(_msg.line1))
577
        val.set_mutable(False)
578
        rs = RadioSetting("firmware_msg.line1", "Firmware Message 1", val)
579
        other.append(rs)
580

    
581
        val = RadioSettingValueString(0, 8, _filter(_msg.line2))
582
        val.set_mutable(False)
583
        rs = RadioSetting("firmware_msg.line2", "Firmware Message 2", val)
584
        other.append(rs)
585

    
586
        _msg = _mem.sixpoweron_msg
587
        val = RadioSettingValueString(0, 8, _filter(_msg.line1))
588
        val.set_mutable(False)
589
        rs = RadioSetting("sixpoweron_msg.line1", "6+Power-On Message 1", val)
590
        other.append(rs)
591
        val = RadioSettingValueString(0, 8, _filter(_msg.line2))
592
        val.set_mutable(False)
593
        rs = RadioSetting("sixpoweron_msg.line2", "6+Power-On Message 2", val)
594
        other.append(rs)
595

    
596
        _msg = _mem.poweron_msg
597
        rs = RadioSetting("poweron_msg.line1", "Power-On Message 1",
598
                          RadioSettingValueString(
599
                              0, 7, _filter(_msg.line1)))
600
        other.append(rs)
601
        rs = RadioSetting("poweron_msg.line2", "Power-On Message 2",
602
                          RadioSettingValueString(
603
                              0, 7, _filter(_msg.line2)))
604
        other.append(rs)
605

    
606
        lower = 136
607
        upper = 174
608
        rs = RadioSetting("limits.vhf.lower", "VHF Lower Limit (MHz)",
609
                          RadioSettingValueInteger(
610
                              lower, upper, _mem.limits.vhf.lower))
611
        other.append(rs)
612

    
613
        rs = RadioSetting("limits.vhf.upper", "VHF Upper Limit (MHz)",
614
                          RadioSettingValueInteger(
615
                              lower, upper, _mem.limits.vhf.upper))
616
        other.append(rs)
617

    
618
        lower = 400
619
        upper = 520
620
        rs = RadioSetting("limits.uhf.lower", "UHF Lower Limit (MHz)",
621
                          RadioSettingValueInteger(
622
                              lower, upper, _mem.limits.uhf.lower))
623
        other.append(rs)
624

    
625
        rs = RadioSetting("limits.uhf.upper", "UHF Upper Limit (MHz)",
626
                          RadioSettingValueInteger(
627
                              lower, upper, _mem.limits.uhf.upper))
628
        other.append(rs)
629

    
630
        # Work mode settings
631
        rs = RadioSetting("settings.displayab", "Display",
632
                          RadioSettingValueList(
633
                              LIST_AB, LIST_AB[_mem.settings.displayab]))
634
        work.append(rs)
635

    
636
        rs = RadioSetting("settings.workmode", "VFO/MR Mode",
637
                          RadioSettingValueList(
638
                              LIST_WORKMODE,
639
                              LIST_WORKMODE[_mem.settings.workmode]))
640
        work.append(rs)
641

    
642
        rs = RadioSetting("settings.keylock", "Keypad Lock",
643
                          RadioSettingValueBoolean(_mem.settings.keylock))
644
        work.append(rs)
645

    
646
        rs = RadioSetting("wmchannel.mrcha", "MR A Channel",
647
                          RadioSettingValueInteger(0, 127,
648
                                                      _mem.wmchannel.mrcha))
649
        work.append(rs)
650

    
651
        rs = RadioSetting("wmchannel.mrchb", "MR B Channel",
652
                          RadioSettingValueInteger(0, 127,
653
                                                      _mem.wmchannel.mrchb))
654
        work.append(rs)
655

    
656
        def convert_bytes_to_freq(bytes):
657
            real_freq = 0
658
            for byte in bytes:
659
                real_freq = (real_freq * 10) + byte
660
            return chirp_common.format_freq(real_freq * 10)
661

    
662
        def my_validate(value):
663
            _vhf_lower = int(_mem.limits.vhf.lower)
664
            _vhf_upper = int(_mem.limits.vhf.upper)
665
            _uhf_lower = int(_mem.limits.uhf.lower)
666
            _uhf_upper = int(_mem.limits.uhf.upper)
667
            value = chirp_common.parse_freq(value)
668
            msg = ("Can't be less than %i.0000")
669
            if value > 99000000 and value < _vhf_lower * 1000000:
670
                raise InvalidValueError(msg % _vhf_lower)
671
            msg = ("Can't be between %i.9975-%i.0000")
672
            if _vhf_upper * 1000000 <= value and value < _uhf_lower * 1000000:
673
                raise InvalidValueError(msg % (_vhf_upper - 1, _uhf_lower))
674
            msg = ("Can't be greater than %i.9975")
675
            if value > 99000000 and value >= _uhf_upper * 1000000:
676
                raise InvalidValueError(msg % (_uhf_upper - 1))
677
            return chirp_common.format_freq(value)
678

    
679
        def apply_freq(setting, obj):
680
            value = chirp_common.parse_freq(str(setting.value)) / 10
681
            for i in range(7, -1, -1):
682
                obj.freq[i] = value % 10
683
                value /= 10
684

    
685
        val1a = RadioSettingValueString(0, 10,
686
                                        convert_bytes_to_freq(_mem.vfo.a.freq))
687
        val1a.set_validate_callback(my_validate)
688
        rs = RadioSetting("vfo.a.freq", "VFO A Frequency", val1a)
689
        rs.set_apply_callback(apply_freq, _mem.vfo.a)
690
        work.append(rs)
691

    
692
        val1b = RadioSettingValueString(0, 10,
693
                                        convert_bytes_to_freq(_mem.vfo.b.freq))
694
        val1b.set_validate_callback(my_validate)
695
        rs = RadioSetting("vfo.b.freq", "VFO B Frequency", val1b)
696
        rs.set_apply_callback(apply_freq, _mem.vfo.b)
697
        work.append(rs)
698

    
699
        rs = RadioSetting("vfo.a.sftd", "VFO A Shift",
700
                          RadioSettingValueList(
701
                              LIST_SHIFTD, LIST_SHIFTD[_mem.vfo.a.sftd]))
702
        work.append(rs)
703

    
704
        rs = RadioSetting("vfo.b.sftd", "VFO B Shift",
705
                          RadioSettingValueList(
706
                              LIST_SHIFTD, LIST_SHIFTD[_mem.vfo.b.sftd]))
707
        work.append(rs)
708

    
709
        def convert_bytes_to_offset(bytes):
710
            real_offset = 0
711
            for byte in bytes:
712
                real_offset = (real_offset * 10) + byte
713
            return chirp_common.format_freq(real_offset * 1000)
714

    
715
        def apply_offset(setting, obj):
716
            value = chirp_common.parse_freq(str(setting.value)) / 1000
717
            for i in range(5, -1, -1):
718
                obj.offset[i] = value % 10
719
                value /= 10
720

    
721
        val1a = RadioSettingValueString(
722
                    0, 10, convert_bytes_to_offset(_mem.vfo.a.offset))
723
        rs = RadioSetting("vfo.a.offset",
724
                          "VFO A Offset", val1a)
725
        rs.set_apply_callback(apply_offset, _mem.vfo.a)
726
        work.append(rs)
727

    
728
        val1b = RadioSettingValueString(
729
                    0, 10, convert_bytes_to_offset(_mem.vfo.b.offset))
730
        rs = RadioSetting("vfo.b.offset",
731
                          "VFO B Offset", val1b)
732
        rs.set_apply_callback(apply_offset, _mem.vfo.b)
733
        work.append(rs)
734

    
735
        rs = RadioSetting("vfo.a.txpower", "VFO A Power",
736
                          RadioSettingValueList(
737
                              LIST_TXPOWER,
738
                              LIST_TXPOWER[_mem.vfo.a.txpower]))
739
        work.append(rs)
740

    
741
        rs = RadioSetting("vfo.b.txpower", "VFO B Power",
742
                          RadioSettingValueList(
743
                              LIST_TXPOWER,
744
                              LIST_TXPOWER[_mem.vfo.b.txpower]))
745
        work.append(rs)
746

    
747
        rs = RadioSetting("vfo.a.widenarr", "VFO A Bandwidth",
748
                          RadioSettingValueList(
749
                              LIST_BANDWIDTH,
750
                              LIST_BANDWIDTH[_mem.vfo.a.widenarr]))
751
        work.append(rs)
752

    
753
        rs = RadioSetting("vfo.b.widenarr", "VFO B Bandwidth",
754
                          RadioSettingValueList(
755
                              LIST_BANDWIDTH,
756
                              LIST_BANDWIDTH[_mem.vfo.b.widenarr]))
757
        work.append(rs)
758

    
759
        rs = RadioSetting("vfo.a.scode", "VFO A S-CODE",
760
                          RadioSettingValueList(
761
                              LIST_SCODE,
762
                              LIST_SCODE[_mem.vfo.a.scode]))
763
        work.append(rs)
764

    
765
        rs = RadioSetting("vfo.b.scode", "VFO B S-CODE",
766
                          RadioSettingValueList(
767
                              LIST_SCODE,
768
                              LIST_SCODE[_mem.vfo.b.scode]))
769
        work.append(rs)
770

    
771
        rs = RadioSetting("vfo.a.step", "VFO A Tuning Step",
772
                          RadioSettingValueList(
773
                              LIST_STEP, LIST_STEP[_mem.vfo.a.step]))
774
        work.append(rs)
775
        rs = RadioSetting("vfo.b.step", "VFO B Tuning Step",
776
                          RadioSettingValueList(
777
                              LIST_STEP, LIST_STEP[_mem.vfo.b.step]))
778
        work.append(rs)
779

    
780
        # broadcast FM settings
781
        _fm_presets = self._memobj.fm_presets
782
        if _fm_presets <= 108.0 * 10 - 650:
783
            preset = _fm_presets / 10.0 + 65
784
        elif _fm_presets >= 65.0 * 10 and _fm_presets <= 108.0 * 10:
785
            preset = _fm_presets / 10.0
786
        else:
787
            preset = 76.0
788
        rs = RadioSetting("fm_presets", "FM Preset(MHz)",
789
                          RadioSettingValueFloat(65, 108.0, preset, 0.1, 1))
790
        fm_preset.append(rs)
791

    
792
        # DTMF settings
793
        def apply_code(setting, obj, length):
794
            code = []
795
            for j in range(0, length):
796
                try:
797
                    code.append(DTMF_CHARS.index(str(setting.value)[j]))
798
                except IndexError:
799
                    code.append(0xFF)
800
            obj.code = code
801

    
802
        for i in range(0, 15):
803
            _codeobj = self._memobj.pttid[i].code
804
            _code = "".join([DTMF_CHARS[x] for x in _codeobj if int(x) < 0x1F])
805
            val = RadioSettingValueString(0, 5, _code, False)
806
            val.set_charset(DTMF_CHARS)
807
            pttid = RadioSetting("pttid/%i.code" % i,
808
                                 "Signal Code %i" % (i + 1), val)
809
            pttid.set_apply_callback(apply_code, self._memobj.pttid[i], 5)
810
            dtmfe.append(pttid)
811

    
812
        if _mem.ani.dtmfon > 0xC3:
813
            val = 0x03
814
        else:
815
            val = _mem.ani.dtmfon
816
        rs = RadioSetting("ani.dtmfon", "DTMF Speed (on)",
817
                          RadioSettingValueList(LIST_DTMFSPEED,
818
                                                LIST_DTMFSPEED[val]))
819
        dtmfe.append(rs)
820

    
821
        if _mem.ani.dtmfoff > 0xC3:
822
            val = 0x03
823
        else:
824
            val = _mem.ani.dtmfoff
825
        rs = RadioSetting("ani.dtmfoff", "DTMF Speed (off)",
826
                          RadioSettingValueList(LIST_DTMFSPEED,
827
                                                LIST_DTMFSPEED[val]))
828
        dtmfe.append(rs)
829

    
830
        _codeobj = self._memobj.ani.code
831
        _code = "".join([DTMF_CHARS[x] for x in _codeobj if int(x) < 0x1F])
832
        val = RadioSettingValueString(0, 5, _code, False)
833
        val.set_charset(DTMF_CHARS)
834
        rs = RadioSetting("ani.code", "ANI Code", val)
835
        rs.set_apply_callback(apply_code, self._memobj.ani, 5)
836
        dtmfe.append(rs)
837

    
838
        rs = RadioSetting("ani.aniid", "When to send ANI ID",
839
                          RadioSettingValueList(LIST_PTTID,
840
                                                LIST_PTTID[_mem.ani.aniid]))
841
        dtmfe.append(rs)
842

    
843
        # Service settings
844
        for index in range(0, 10):
845
            key = "squelch.sql%i" % (index)
846
            _obj = self._memobj.squelch
847
            val = RadioSettingValueInteger(0, 123,
848
                      getattr(_obj, "sql%i" % (index)))
849
            if index == 0:
850
                val.set_mutable(False)
851
            name = "Squelch %i" % (index)
852
            rs = RadioSetting(key, name, val)
853
            service.append(rs)
854

    
855
        return top
856

    
857
    @classmethod
858
    def match_model(cls, filedata, filename):
859
        match_size = False
860
        match_model = False
861

    
862
        # testing the file data size
863
        if len(filedata) == 0x2008 or 0x2010:
864
            match_size = True
865

    
866
        # testing the firmware model fingerprint
867
        match_model = model_match(cls, filedata)
868

    
869
        if match_size and match_model:
870
            return True
871
        else:
872
            return False