Project

General

Profile

Bug #9701 ยป btech_vc7062.py

Jim Unroe, 01/13/2022 05:48 PM

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

    
18
import time
19
import struct
20
import logging
21

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

    
31
LOG = logging.getLogger(__name__)
32

    
33
# A note about the memmory in these radios
34
#
35
# The real memory of these radios extends to 0x4000
36
# On read the factory software only uses up to 0x3200
37
# On write it just uploads the contents up to 0x3100
38
#
39
# The mem beyond 0x3200 holds the ID data
40

    
41
MEM_SIZE = 0x4000
42
BLOCK_SIZE = 0x40
43
TX_BLOCK_SIZE = 0x10
44
ACK_CMD = "\x06"
45
MODES = ["FM", "NFM"]
46
SKIP_VALUES = ["S", ""]
47
TONES = chirp_common.TONES
48
DTCS = sorted(chirp_common.DTCS_CODES + [645])
49

    
50
# lists related to "extra" settings
51
PTTID_LIST = ["OFF", "BOT", "EOT", "BOTH"]
52
PTTIDCODE_LIST = ["%s" % x for x in range(1, 16)]
53
OPTSIG_LIST = ["OFF", "DTMF", "2TONE", "5TONE"]
54
SPMUTE_LIST = ["Tone/DTCS", "Tone/DTCS and Optsig", "Tone/DTCS or Optsig"]
55

    
56
# lists
57
LIST_AB = ["A", "B"]
58
LIST_ABCD = LIST_AB + ["C", "D"]
59
LIST_ANIL = ["3", "4", "5"]
60
LIST_APO = ["Off"] + ["%s minutes" % x for x in range(30, 330, 30)]
61
LIST_COLOR4 = ["Off", "Blue", "Orange", "Purple"]
62
LIST_COLOR8 = ["White", "Red", "Blue", "Green", "Yellow", "Indego",
63
               "Purple", "Gray"]
64
LIST_COLOR9 = ["Black"] + LIST_COLOR8
65
LIST_DTMFST = ["OFF", "Keyboard", "ANI", "Keyboad + ANI"]
66
LIST_EMCTP = ["TX alarm sound", "TX ANI", "Both"]
67
LIST_EMCTPX = ["Off"] + LIST_EMCTP
68
LIST_LANGUA = ["English", "Chinese"]
69
LIST_MDF = ["Frequency", "Channel", "Name"]
70
LIST_OFF1TO9 = ["Off"] + ["%s seconds" % x for x in range(1, 10)]
71
LIST_OFF1TO10 = ["Off"] + ["%s seconds" % x for x in range(1, 11)]
72
LIST_OFF1TO50 = ["Off"] + ["%s seconds" % x for x in range(1, 51)]
73
LIST_PONMSG = ["Full", "Message", "Battery voltage"]
74
LIST_REPM = ["Off", "Carrier", "CTCSS or DCS", "Tone", "DTMF"]
75
LIST_REPS = ["1000 Hz", "1450 Hz", "1750 Hz", "2100Hz"]
76
LIST_RPTDL = ["Off"] + ["%s ms" % x for x in range(1, 11)]
77
LIST_SCMODE = ["Off", "PTT-SC", "MEM-SC", "PON-SC"]
78
LIST_SHIFT = ["Off", "+", "-"]
79
LIST_SKIPTX = ["Off", "Skip 1", "Skip 2"]
80
STEPS = [2.5, 5.0, 6.25, 10.0, 12.5, 25.0]
81
LIST_STEP = [str(x) for x in STEPS]
82
LIST_SYNC = ["Off", "AB", "CD", "AB+CD"]
83
# the first 12 TMR choices common to all color display mobile radios
84
LIST_TMR12 = ["OFF", "M+A", "M+B", "M+C", "M+D", "M+A+B", "M+A+C", "M+A+D",
85
              "M+B+C", "M+B+D", "M+C+D", "M+A+B+C"]
86
# the 16 choice list for color display mobile radios that correctly implement
87
# the full 16 TMR choices
88
LIST_TMR16 = LIST_TMR12 + ["M+A+B+D", "M+A+C+D", "M+B+C+D", "A+B+C+D"]
89
# the 15 choice list for color mobile radios that are missing the M+A+B+D
90
# choice in the TMR menu
91
LIST_TMR15 = LIST_TMR12 + ["M+A+C+D", "M+B+C+D", "A+B+C+D"]
92
LIST_TMRTX = ["Track", "Fixed"]
93
LIST_TOT = ["%s sec" % x for x in range(15, 615, 15)]
94
LIST_TXDISP = ["Power", "Mic Volume"]
95
LIST_TXP = ["High", "Low"]
96
LIST_TXP3 = ["High", "Mid", "Low"]
97
LIST_SCREV = ["TO (timeout)", "CO (carrier operated)", "SE (search)"]
98
LIST_VFOMR = ["Frequency", "Channel"]
99
LIST_VOICE = ["Off"] + LIST_LANGUA
100
LIST_VOX = ["Off"] + ["%s" % x for x in range(1, 11)]
101
LIST_VOXT = ["%s seconds" % x for x in range(0, 21)]
102
LIST_WIDE = ["Wide", "Narrow"]
103

    
104
# lists related to DTMF, 2TONE and 5TONE settings
105
LIST_5TONE_STANDARDS = ["CCIR1", "CCIR2", "PCCIR", "ZVEI1", "ZVEI2", "ZVEI3",
106
                        "PZVEI", "DZVEI", "PDZVEI", "EEA", "EIA", "EURO",
107
                        "CCITT", "NATEL", "MODAT", "none"]
108
LIST_5TONE_STANDARDS_without_none = ["CCIR1", "CCIR2", "PCCIR", "ZVEI1",
109
                                     "ZVEI2", "ZVEI3",
110
                                     "PZVEI", "DZVEI", "PDZVEI", "EEA", "EIA",
111
                                     "EURO", "CCITT", "NATEL", "MODAT"]
112
LIST_5TONE_STANDARD_PERIODS = ["20", "30", "40", "50", "60", "70", "80", "90",
113
                               "100", "110", "120", "130", "140", "150", "160",
114
                               "170", "180", "190", "200"]
115
LIST_5TONE_DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
116
                     "B", "C", "D", "E", "F"]
117
LIST_5TONE_DELAY = ["%s ms" % x for x in range(0, 1010, 10)]
118
LIST_5TONE_RESET = ["%s ms" % x for x in range(100, 8100, 100)]
119
LIST_5TONE_RESET_COLOR = ["%s ms" % x for x in range(100, 20100, 100)]
120
LIST_DTMF_SPEED = ["%s ms" % x for x in range(50, 2010, 10)]
121
LIST_DTMF_DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B",
122
                    "C", "D", "#", "*"]
123
LIST_DTMF_VALUES = [0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
124
                    0x0D, 0x0E, 0x0F, 0x00, 0x0C, 0x0B]
125
LIST_DTMF_SPECIAL_DIGITS = ["*", "#", "A", "B", "C", "D"]
126
LIST_DTMF_SPECIAL_VALUES = [0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00]
127
LIST_DTMF_DELAY = ["%s ms" % x for x in range(100, 4100, 100)]
128
CHARSET_DTMF_DIGITS = "0123456789AaBbCcDd#*"
129
LIST_2TONE_DEC = ["A-B", "A-C", "A-D",
130
                  "B-A", "B-C", "B-D",
131
                  "C-A", "C-B", "C-D",
132
                  "D-A", "D-B", "D-C"]
133
LIST_2TONE_RESPONSE = ["None", "Alert", "Transpond", "Alert+Transpond"]
134

    
135
# This is a general serial timeout for all serial read functions.
136
# Practice has show that about 0.7 sec will be enough to cover all radios.
137
STIMEOUT = 0.7
138

    
139
# this var controls the verbosity in the debug and by default it's low (False)
140
# make it True and you will to get a very verbose debug.log
141
debug = False
142

    
143
# valid chars on the LCD, Note that " " (space) is stored as "\xFF"
144
VALID_CHARS = chirp_common.CHARSET_ALPHANUMERIC + \
145
    "`{|}!\"#$%&'()*+,-./:;<=>?@[]^_"
146

    
147
GMRS_FREQS1 = [462.5625, 462.5875, 462.6125, 462.6375, 462.6625,
148
               462.6875, 462.7125]
149
GMRS_FREQS2 = [467.5625, 467.5875, 467.6125, 467.6375, 467.6625,
150
               467.6875, 467.7125]
151
GMRS_FREQS3 = [462.5500, 462.5750, 462.6000, 462.6250, 462.6500,
152
               462.6750, 462.7000, 462.7250]
153
GMRS_FREQS = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
154

    
155

    
156
# #### ID strings #####################################################
157

    
158
# BTECH UV2501 pre-production units
159
UV2501pp_fp = "M2C294"
160
# BTECH UV2501 pre-production units 2 + and 1st Gen radios
161
UV2501pp2_fp = "M29204"
162
# B-TECH UV-2501 second generation (2G) radios
163
UV2501G2_fp = "BTG214"
164
# B-TECH UV-2501 third generation (3G) radios
165
UV2501G3_fp = "BTG324"
166

    
167
# B-TECH UV-2501+220 pre-production units
168
UV2501_220pp_fp = "M3C281"
169
# extra block read for the 2501+220 pre-production units
170
# the same for all of this radios so far
171
UV2501_220pp_id = "      280528"
172
# B-TECH UV-2501+220
173
UV2501_220_fp = "M3G201"
174
# new variant, let's call it Generation 2
175
UV2501_220G2_fp = "BTG211"
176
# B-TECH UV-2501+220 third generation (3G)
177
UV2501_220G3_fp = "BTG311"
178

    
179
# B-TECH UV-5001 pre-production units + 1st Gen radios
180
UV5001pp_fp = "V19204"
181
# B-TECH UV-5001 alpha units
182
UV5001alpha_fp = "V28204"
183
# B-TECH UV-5001 second generation (2G) radios
184
UV5001G2_fp = "BTG214"
185
# B-TECH UV-5001 second generation (2G2)
186
UV5001G22_fp = "V2G204"
187
# B-TECH UV-5001 third generation (3G)
188
UV5001G3_fp = "BTG304"
189

    
190
# B-TECH UV-25X2
191
UV25X2_fp = "UC2012"
192

    
193
# B-TECH UV-25X4
194
UV25X4_fp = "UC4014"
195

    
196
# B-TECH UV-50X2
197
UV50X2_fp = "UC2M12"
198

    
199
# B-TECH GMRS-50X1
200
GMRS50X1_fp = "NC1802"
201
GMRS50X1_fp1 = "NC1932"
202

    
203
# special var to know when we found a BTECH Gen 3
204
BTECH3 = [UV2501G3_fp, UV2501_220G3_fp, UV5001G3_fp]
205

    
206

    
207
# WACCOM Mini-8900
208
MINI8900_fp = "M28854"
209

    
210

    
211
# QYT KT-UV980
212
KTUV980_fp = "H28854"
213

    
214
# QYT KT8900
215
KT8900_fp = "M29154"
216
# New generations KT8900
217
KT8900_fp1 = "M2C234"
218
KT8900_fp2 = "M2G1F4"
219
KT8900_fp3 = "M2G2F4"
220
KT8900_fp4 = "M2G304"
221
KT8900_fp5 = "M2G314"
222
KT8900_fp6 = "M2G424"
223
KT8900_fp7 = "M27184"
224
# this radio has an extra ID
225
KT8900_id = "303688"
226
# another extra ID in sep/2021
227
KT8900_id2 = "\x05\x58\x3d\xf0\x10"
228

    
229
# KT8900R
230
KT8900R_fp = "M3G1F4"
231
# Second Generation
232
KT8900R_fp1 = "M3G214"
233
# another model
234
KT8900R_fp2 = "M3C234"
235
# another model G4?
236
KT8900R_fp3 = "M39164"
237
# another model
238
KT8900R_fp4 = "M3G314"
239
# this radio has an extra ID
240
KT8900R_id = "280528"
241
# another extra ID in dec/2018
242
KT8900R_id2 = "\x05\x58\x3d\xf0\x10"
243

    
244
# KT7900D (quad band)
245
KT7900D_fp = "VC4004"
246
KT7900D_fp1 = "VC4284"
247
KT7900D_fp2 = "VC4264"
248
KT7900D_fp3 = "VC4114"
249
KT7900D_fp4 = "VC4104"
250
KT7900D_fp5 = "VC4254"
251
KT7900D_fp6 = "VC5264"
252
KT7900D_fp7 = "VC9204"
253

    
254
# QB25 (quad band) - a clone of KT7900D
255
QB25_fp = "QB-25"
256

    
257
# KT8900D (dual band)
258
KT8900D_fp = "VC2002"
259
KT8900D_fp1 = "VC8632"
260
KT8900D_fp2 = "VC3402"
261
KT8900D_fp3 = "VC7062"
262

    
263
# LUITON LT-588UV
264
LT588UV_fp = "V2G1F4"
265
# Added by rstrickoff gen 2 id
266
LT588UV_fp1 = "V2G214"
267

    
268
# QYT KT-8R (quad band ht)
269
KT8R_fp = "MCB264"
270
KT8R_fp1 = "MCB284"
271
KT8R_fp2 = "MC5264"
272

    
273
# QYT KT5800 (dual band)
274
KT5800_fp = "VCB222"
275

    
276
# QYT KT980Plus (dual band)
277
KT980PLUS_fp = "VC2002"
278
KT980PLUS_fp1 = "VC6042"
279

    
280
# Radioddity DB25-G (gmrs)
281
DB25G_fp = "VC6182"
282
DB25G_fp1 = "VC7062"
283

    
284

    
285
# ### MAGICS
286
# for the Waccom Mini-8900
287
MSTRING_MINI8900 = "\x55\xA5\xB5\x45\x55\x45\x4d\x02"
288
# for the B-TECH UV-2501+220 (including pre production ones)
289
MSTRING_220 = "\x55\x20\x15\x12\x12\x01\x4d\x02"
290
# for the QYT KT8900 & R
291
MSTRING_KT8900 = "\x55\x20\x15\x09\x16\x45\x4D\x02"
292
MSTRING_KT8900R = "\x55\x20\x15\x09\x25\x01\x4D\x02"
293
# magic string for all other models
294
MSTRING = "\x55\x20\x15\x09\x20\x45\x4d\x02"
295
# for the QYT KT7900D & KT8900D
296
MSTRING_KT8900D = "\x55\x20\x16\x08\x01\xFF\xDC\x02"
297
# for the BTECH UV-25X2 and UV-50X2
298
MSTRING_UV25X2 = "\x55\x20\x16\x12\x28\xFF\xDC\x02"
299
# for the BTECH UV-25X4
300
MSTRING_UV25X4 = "\x55\x20\x16\x11\x18\xFF\xDC\x02"
301
# for the BTECH GMRS-50X1
302
MSTRING_GMRS50X1 = "\x55\x20\x18\x10\x18\xFF\xDC\x02"
303
# for the QYT KT-8R
304
MSTRING_KT8R = "\x55\x20\x17\x07\x03\xFF\xDC\x02"
305

    
306

    
307
def _clean_buffer(radio):
308
    """Cleaning the read serial buffer, hard timeout to survive an infinite
309
    data stream"""
310

    
311
    # touching the serial timeout to optimize the flushing
312
    # restored at the end to the default value
313
    radio.pipe.timeout = 0.1
314
    dump = "1"
315
    datacount = 0
316

    
317
    try:
318
        while len(dump) > 0:
319
            dump = radio.pipe.read(100)
320
            datacount += len(dump)
321
            # hard limit to survive a infinite serial data stream
322
            # 5 times bigger than a normal rx block (69 bytes)
323
            if datacount > 345:
324
                seriale = "Please check your serial port selection."
325
                raise errors.RadioError(seriale)
326

    
327
        # restore the default serial timeout
328
        radio.pipe.timeout = STIMEOUT
329

    
330
    except Exception:
331
        raise errors.RadioError("Unknown error cleaning the serial buffer")
332

    
333

    
334
def _rawrecv(radio, amount):
335
    """Raw read from the radio device, less intensive way"""
336

    
337
    data = ""
338

    
339
    try:
340
        data = radio.pipe.read(amount)
341

    
342
        # DEBUG
343
        if debug is True:
344
            LOG.debug("<== (%d) bytes:\n\n%s" %
345
                      (len(data), util.hexprint(data)))
346

    
347
        # fail if no data is received
348
        if len(data) == 0:
349
            raise errors.RadioError("No data received from radio")
350

    
351
        # notice on the logs if short
352
        if len(data) < amount:
353
            LOG.warn("Short reading %d bytes from the %d requested." %
354
                     (len(data), amount))
355

    
356
    except:
357
        raise errors.RadioError("Error reading data from radio")
358

    
359
    return data
360

    
361

    
362
def _send(radio, data):
363
    """Send data to the radio device"""
364

    
365
    try:
366
        for byte in data:
367
            radio.pipe.write(byte)
368
            # Some OS (mainly Linux ones) are too fast on the serial and
369
            # get the MCU inside the radio stuck in the early stages, this
370
            # hits some models more than others.
371
            #
372
            # To cope with that we introduce a delay on the writes.
373
            # Many option have been tested (delaying only after error occures,
374
            # after short reads, only for linux, ...)
375
            # Finally, a static delay was chosen as simplest of all solutions
376
            # (Michael Wagner, OE4AMW)
377
            # (for details, see issue 3993)
378
            sleep(0.002)
379

    
380
        # DEBUG
381
        if debug is True:
382
            LOG.debug("==> (%d) bytes:\n\n%s" %
383
                      (len(data), util.hexprint(data)))
384
    except:
385
        raise errors.RadioError("Error sending data to radio")
386

    
387

    
388
def _make_frame(cmd, addr, length, data=""):
389
    """Pack the info in the headder format"""
390
    frame = "\x06" + struct.pack(">BHB", ord(cmd), addr, length)
391
    # add the data if set
392
    if len(data) != 0:
393
        frame += data
394

    
395
    return frame
396

    
397

    
398
def _recv(radio, addr):
399
    """Get data from the radio all at once to lower syscalls load"""
400

    
401
    # Get the full 69 bytes at a time to reduce load
402
    # 1 byte ACK + 4 bytes header + 64 bytes of data (BLOCK_SIZE)
403

    
404
    # get the whole block
405
    block = _rawrecv(radio, BLOCK_SIZE + 5)
406

    
407
    # basic check
408
    if len(block) < (BLOCK_SIZE + 5):
409
        raise errors.RadioError("Short read of the block 0x%04x" % addr)
410

    
411
    # checking for the ack
412
    if block[0] != ACK_CMD:
413
        raise errors.RadioError("Bad ack from radio in block 0x%04x" % addr)
414

    
415
    # header validation
416
    c, a, l = struct.unpack(">BHB", block[1:5])
417
    if a != addr or l != BLOCK_SIZE or c != ord("X"):
418
        LOG.debug("Invalid header for block 0x%04x" % addr)
419
        LOG.debug("CMD: %s  ADDR: %04x  SIZE: %02x" % (c, a, l))
420
        raise errors.RadioError("Invalid header for block 0x%04x:" % addr)
421

    
422
    # return the data
423
    return block[5:]
424

    
425

    
426
def _start_clone_mode(radio, status):
427
    """Put the radio in clone mode and get the ident string, 3 tries"""
428

    
429
    # cleaning the serial buffer
430
    _clean_buffer(radio)
431

    
432
    # prep the data to show in the UI
433
    status.cur = 0
434
    status.msg = "Identifying the radio..."
435
    status.max = 3
436
    radio.status_fn(status)
437

    
438
    try:
439
        for a in range(0, status.max):
440
            # Update the UI
441
            status.cur = a + 1
442
            radio.status_fn(status)
443

    
444
            # send the magic word
445
            _send(radio, radio._magic)
446

    
447
            # Now you get a x06 of ACK if all goes well
448
            ack = radio.pipe.read(1)
449

    
450
            if ack == "\x06":
451
                # DEBUG
452
                LOG.info("Magic ACK received")
453
                status.cur = status.max
454
                radio.status_fn(status)
455

    
456
                return True
457

    
458
        return False
459

    
460
    except errors.RadioError:
461
        raise
462
    except Exception, e:
463
        raise errors.RadioError("Error sending Magic to radio:\n%s" % e)
464

    
465

    
466
def _do_ident(radio, status, upload=False):
467
    """Put the radio in PROGRAM mode & identify it"""
468
    #  set the serial discipline
469
    radio.pipe.baudrate = 9600
470
    radio.pipe.parity = "N"
471

    
472
    # open the radio into program mode
473
    if _start_clone_mode(radio, status) is False:
474
        msg = "Radio did not enter clone mode"
475
        # warning about old versions of QYT KT8900
476
        if radio.MODEL == "KT8900":
477
            msg += ". You may want to try it as a WACCOM MINI-8900, there is a"
478
            msg += " known variant of this radios that is a clone of it."
479
        raise errors.RadioError(msg)
480

    
481
    # Ok, get the ident string
482
    ident = _rawrecv(radio, 49)
483

    
484
    # basic check for the ident
485
    if len(ident) != 49:
486
        raise errors.RadioError("Radio send a short ident block.")
487

    
488
    # check if ident is OK
489
    itis = False
490
    for fp in radio._fileid:
491
        if fp in ident:
492
            # got it!
493
            itis = True
494
            # checking if we are dealing with a Gen 3 BTECH
495
            if radio.VENDOR == "BTECH" and fp in BTECH3:
496
                radio.btech3 = True
497

    
498
            break
499

    
500
    if itis is False:
501
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
502
        raise errors.RadioError("Radio identification failed.")
503

    
504
    # some radios needs a extra read and check for a code on it, this ones
505
    # has the check value in the _id2 var, others simply False
506
    if radio._id2 is not False:
507
        # lower the timeout here as this radios are reseting due to timeout
508
        radio.pipe.timeout = 0.05
509

    
510
        # query & receive the extra ID
511
        _send(radio, _make_frame("S", 0x3DF0, 16))
512
        id2 = _rawrecv(radio, 21)
513

    
514
        # WARNING !!!!!!
515
        # different radios send a response with a different amount of data
516
        # it seems that it's padded with \xff, \x20 and some times with \x00
517
        # we just care about the first 16, our magic string is in there
518
        if len(id2) < 16:
519
            raise errors.RadioError("The extra ID is short, aborting.")
520

    
521
        # ok, the correct string must be in the received data
522
        # the radio._id2 var will be always a list
523
        flag2 = False
524
        for _id2 in radio._id2:
525
            if _id2 in id2:
526
                flag2 = True
527

    
528
        if not flag2:
529
            LOG.debug("Full *BAD* extra ID on the %s is: \n%s" %
530
                      (radio.MODEL, util.hexprint(id2)))
531
            raise errors.RadioError("The extra ID is wrong, aborting.")
532

    
533
        # this radios need a extra request/answer here on the upload
534
        # the amount of data received depends of the radio type
535
        #
536
        # also the first block of TX must no have the ACK at the beginning
537
        # see _upload for this.
538
        if upload is True:
539
            # send an ACK
540
            _send(radio, ACK_CMD)
541

    
542
            # the amount of data depend on the radio, so far we have two radios
543
            # reading two bytes with an ACK at the end and just ONE with just
544
            # one byte (QYT KT8900)
545
            # the JT-6188 appears a clone of the last, but reads TWO bytes.
546
            #
547
            # we will read two bytes with a custom timeout to not penalize the
548
            # users for this.
549
            #
550
            # we just check for a response and last byte being a ACK, that is
551
            # the common stone for all radios (3 so far)
552
            ack = _rawrecv(radio, 2)
553

    
554
            # checking
555
            if len(ack) == 0 or ack[-1:] != ACK_CMD:
556
                raise errors.RadioError("Radio didn't ACK the upload")
557

    
558
            # restore the default serial timeout
559
            radio.pipe.timeout = STIMEOUT
560

    
561
    # DEBUG
562
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
563

    
564
    return True
565

    
566

    
567
def _download(radio):
568
    """Get the memory map"""
569

    
570
    # UI progress
571
    status = chirp_common.Status()
572

    
573
    # put radio in program mode and identify it
574
    _do_ident(radio, status)
575

    
576
    # the models that doesn't have the extra ID have to make a dummy read here
577
    if radio._id2 is False:
578
        _send(radio, _make_frame("S", 0, BLOCK_SIZE))
579
        discard = _rawrecv(radio, BLOCK_SIZE + 5)
580

    
581
        if debug is True:
582
            LOG.info("Dummy first block read done, got this:\n\n %s",
583
                     util.hexprint(discard))
584

    
585
    # reset the progress bar in the UI
586
    status.max = MEM_SIZE / BLOCK_SIZE
587
    status.msg = "Cloning from radio..."
588
    status.cur = 0
589
    radio.status_fn(status)
590

    
591
    # cleaning the serial buffer
592
    _clean_buffer(radio)
593

    
594
    data = ""
595
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
596
        # sending the read request
597
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
598

    
599
        # read
600
        d = _recv(radio, addr)
601

    
602
        # aggregate the data
603
        data += d
604

    
605
        # UI Update
606
        status.cur = addr / BLOCK_SIZE
607
        status.msg = "Cloning from radio..."
608
        radio.status_fn(status)
609

    
610
    return data
611

    
612

    
613
def _upload(radio):
614
    """Upload procedure"""
615

    
616
    # The UPLOAD mem is restricted to lower than 0x3100,
617
    # so we will overide that here localy
618
    MEM_SIZE = radio.UPLOAD_MEM_SIZE
619

    
620
    # UI progress
621
    status = chirp_common.Status()
622

    
623
    # put radio in program mode and identify it
624
    _do_ident(radio, status, True)
625

    
626
    # get the data to upload to radio
627
    data = radio.get_mmap()
628

    
629
    # Reset the UI progress
630
    status.max = MEM_SIZE / TX_BLOCK_SIZE
631
    status.cur = 0
632
    status.msg = "Cloning to radio..."
633
    radio.status_fn(status)
634

    
635
    # the radios that doesn't have the extra ID 'may' do a dummy write, I found
636
    # that leveraging the bad ACK and NOT doing the dummy write is ok, as the
637
    # dummy write is accepted (it actually writes to the mem!) by the radio.
638

    
639
    # cleaning the serial buffer
640
    _clean_buffer(radio)
641

    
642
    # the fun start here
643
    for addr in range(0, MEM_SIZE, TX_BLOCK_SIZE):
644
        # getting the block of data to send
645
        d = data[addr:addr + TX_BLOCK_SIZE]
646

    
647
        # build the frame to send
648
        frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
649

    
650
        # first block must not send the ACK at the beginning for the
651
        # ones that has the extra id, since this have to do a extra step
652
        if addr == 0 and radio._id2 is not False:
653
            frame = frame[1:]
654

    
655
        # send the frame
656
        _send(radio, frame)
657

    
658
        # receiving the response
659
        ack = _rawrecv(radio, 1)
660

    
661
        # basic check
662
        if len(ack) != 1:
663
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
664

    
665
        if ack not in "\x06\x05":
666
            raise errors.RadioError("Bad ACK writing block 0x%04x:" % addr)
667

    
668
        # UI Update
669
        status.cur = addr / TX_BLOCK_SIZE
670
        status.msg = "Cloning to radio..."
671
        radio.status_fn(status)
672

    
673

    
674
def model_match(cls, data):
675
    """Match the opened/downloaded image to the correct version"""
676
    rid = data[0x3f70:0x3f76]
677

    
678
    if rid in cls._fileid:
679
        return True
680

    
681
    return False
682

    
683

    
684
def _decode_ranges(low, high):
685
    """Unpack the data in the ranges zones in the memmap and return
686
    a tuple with the integer corresponding to the Mhz it means"""
687
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
688
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
689
    ilow *= 1000000
690
    ihigh *= 1000000
691

    
692
    return (ilow, ihigh)
693

    
694

    
695
def _split(rf, f1, f2):
696
    """Returns False if the two freqs are in the same band (no split)
697
    or True otherwise"""
698

    
699
    # determine if the two freqs are in the same band
700
    for low, high in rf.valid_bands:
701
        if f1 >= low and f1 <= high and \
702
                f2 >= low and f2 <= high:
703
            # if the two freqs are on the same Band this is not a split
704
            return False
705

    
706
    # if you get here is because the freq pairs are split
707
    return True
708

    
709

    
710
class BTechMobileCommon(chirp_common.CloneModeRadio,
711
                        chirp_common.ExperimentalRadio):
712
    """BTECH's UV-5001 and alike radios"""
713
    VENDOR = "BTECH"
714
    MODEL = ""
715
    IDENT = ""
716
    BANDS = 2
717
    COLOR_LCD = False
718
    COLOR_LCD2 = False
719
    COLOR_LCD3 = False
720
    NAME_LENGTH = 6
721
    UPLOAD_MEM_SIZE = 0X3100
722
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
723
                     chirp_common.PowerLevel("Low", watts=10)]
724
    _vhf_range = (130000000, 180000000)
725
    _220_range = (200000000, 271000000)
726
    _uhf_range = (400000000, 521000000)
727
    _350_range = (350000000, 391000000)
728
    _upper = 199
729
    _magic = MSTRING
730
    _fileid = None
731
    _id2 = False
732
    btech3 = False
733
    _gmrs = False
734

    
735
    @classmethod
736
    def get_prompts(cls):
737
        rp = chirp_common.RadioPrompts()
738
        rp.experimental = \
739
            ('This driver is experimental.\n'
740
             '\n'
741
             'Please keep a copy of your memories with the original software '
742
             'if you treasure them, this driver is new and may contain'
743
             ' bugs.\n'
744
             '\n'
745
             )
746
        rp.pre_download = _(dedent("""\
747
            Follow these instructions to download your info:
748

    
749
            1 - Turn off your radio
750
            2 - Connect your interface cable
751
            3 - Turn on your radio
752
            4 - Do the download of your radio data
753

    
754
            """))
755
        rp.pre_upload = _(dedent("""\
756
            Follow these instructions to upload your info:
757

    
758
            1 - Turn off your radio
759
            2 - Connect your interface cable
760
            3 - Turn on your radio
761
            4 - Do the upload of your radio data
762

    
763
            """))
764
        return rp
765

    
766
    def get_features(self):
767
        """Get the radio's features"""
768

    
769
        # we will use the following var as global
770
        global POWER_LEVELS
771

    
772
        rf = chirp_common.RadioFeatures()
773
        rf.has_settings = True
774
        rf.has_bank = False
775
        rf.has_tuning_step = False
776
        rf.can_odd_split = True
777
        rf.has_name = True
778
        rf.has_offset = True
779
        rf.has_mode = True
780
        rf.has_dtcs = True
781
        rf.has_rx_dtcs = True
782
        rf.has_dtcs_polarity = True
783
        rf.has_ctone = True
784
        rf.has_cross = True
785
        rf.valid_modes = MODES
786
        rf.valid_characters = VALID_CHARS
787
        rf.valid_name_length = self.NAME_LENGTH
788
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
789
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
790
        rf.valid_cross_modes = [
791
            "Tone->Tone",
792
            "DTCS->",
793
            "->DTCS",
794
            "Tone->DTCS",
795
            "DTCS->Tone",
796
            "->Tone",
797
            "DTCS->DTCS"]
798
        rf.valid_skips = SKIP_VALUES
799
        rf.valid_dtcs_codes = DTCS
800
        rf.valid_tuning_steps = STEPS
801
        rf.memory_bounds = (0, self._upper)
802

    
803
        # power levels
804
        POWER_LEVELS = self._power_levels
805
        rf.valid_power_levels = POWER_LEVELS
806

    
807
        # normal dual bands
808
        rf.valid_bands = [self._vhf_range, self._uhf_range]
809

    
810
        # 220 band
811
        if self.BANDS == 3 or self.BANDS == 4:
812
            rf.valid_bands.append(self._220_range)
813

    
814
        # 350 band
815
        if self.BANDS == 4:
816
            rf.valid_bands.append(self._350_range)
817

    
818
        return rf
819

    
820
    def validate_memory(self, mem):
821
        msgs = chirp_common.CloneModeRadio.validate_memory(self, mem)
822

    
823
        _msg_duplex1 = 'Memory location only supports "Low"'
824
        _msg_duplex2 = 'Memory location only supports "off"'
825
        _msg_duplex3 = 'Memory location only supports "(None)", "+" or "off"'
826

    
827
        if self._gmrs:
828
            if mem.number < 1 or mem.number > 30:
829
                if float(mem.freq) / 1000000 in GMRS_FREQS1:
830
                    if mem.duplex not in ['', 'off']:
831
                        # warn user wrong Duplex
832
                        msgs.append(chirp_common.ValidationError(_msg_duplex2))
833
                    if mem.power != self._power_levels[2]:
834
                        # warn user wrong Duplex
835
                        msgs.append(chirp_common.ValidationError(_msg_duplex1))
836

    
837
                if float(mem.freq) / 1000000 in GMRS_FREQS2:
838
                    if mem.duplex not in ['off', ]:
839
                        # warn user wrong Duplex
840
                        msgs.append(chirp_common.ValidationError(_msg_duplex2))
841

    
842
                if float(mem.freq) / 1000000 in GMRS_FREQS3:
843
                    if mem.duplex not in ['', '+', 'off']:
844
                        # warn user wrong Duplex
845
                        msgs.append(chirp_common.ValidationError(_msg_duplex3))
846

    
847
        return msgs
848

    
849
    def sync_in(self):
850
        """Download from radio"""
851
        data = _download(self)
852
        self._mmap = memmap.MemoryMap(data)
853
        self.process_mmap()
854

    
855
    def sync_out(self):
856
        """Upload to radio"""
857
        try:
858
            _upload(self)
859
        except errors.RadioError:
860
            raise
861
        except Exception, e:
862
            raise errors.RadioError("Error: %s" % e)
863

    
864
    def get_raw_memory(self, number):
865
        return repr(self._memobj.memory[number])
866

    
867
    def _decode_tone(self, val):
868
        """Parse the tone data to decode from mem, it returns:
869
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
870
        pol = None
871

    
872
        if val in [0, 65535]:
873
            return '', None, None
874
        elif val > 0x0258:
875
            a = val / 10.0
876
            return 'Tone', a, pol
877
        else:
878
            if val > 0x69:
879
                index = val - 0x6A
880
                pol = "R"
881
            else:
882
                index = val - 1
883
                pol = "N"
884

    
885
            tone = DTCS[index]
886
            return 'DTCS', tone, pol
887

    
888
    def _encode_tone(self, memval, mode, val, pol):
889
        """Parse the tone data to encode from UI to mem"""
890
        if mode == '' or mode is None:
891
            memval.set_raw("\x00\x00")
892
        elif mode == 'Tone':
893
            memval.set_value(val * 10)
894
        elif mode == 'DTCS':
895
            # detect the index in the DTCS list
896
            try:
897
                index = DTCS.index(val)
898
                if pol == "N":
899
                    index += 1
900
                else:
901
                    index += 0x6A
902
                memval.set_value(index)
903
            except:
904
                msg = "Digital Tone '%d' is not supported" % value
905
                LOG.error(msg)
906
                raise errors.RadioError(msg)
907
        else:
908
            msg = "Internal error: invalid mode '%s'" % mode
909
            LOG.error(msg)
910
            raise errors.InvalidDataError(msg)
911

    
912
    def get_memory(self, number):
913
        """Get the mem representation from the radio image"""
914
        _mem = self._memobj.memory[number]
915
        _names = self._memobj.names[number]
916

    
917
        # Create a high-level memory object to return to the UI
918
        mem = chirp_common.Memory()
919

    
920
        # Memory number
921
        mem.number = number
922

    
923
        if _mem.get_raw()[0] == "\xFF":
924
            mem.empty = True
925
            return mem
926

    
927
        # Freq and offset
928
        mem.freq = int(_mem.rxfreq) * 10
929
        # tx freq can be blank
930
        if _mem.get_raw()[4] == "\xFF":
931
            # TX freq not set
932
            mem.offset = 0
933
            mem.duplex = "off"
934
        else:
935
            # TX freq set
936
            offset = (int(_mem.txfreq) * 10) - mem.freq
937
            if offset != 0:
938
                if _split(self.get_features(), mem.freq, int(
939
                          _mem.txfreq) * 10):
940
                    mem.duplex = "split"
941
                    mem.offset = int(_mem.txfreq) * 10
942
                elif offset < 0:
943
                    mem.offset = abs(offset)
944
                    mem.duplex = "-"
945
                elif offset > 0:
946
                    mem.offset = offset
947
                    mem.duplex = "+"
948
            else:
949
                mem.offset = 0
950

    
951
        # name TAG of the channel
952
        mem.name = str(_names.name).rstrip("\xFF").replace("\xFF", " ")
953

    
954
        # power
955
        mem.power = POWER_LEVELS[int(_mem.power)]
956

    
957
        # wide/narrow
958
        mem.mode = MODES[int(_mem.wide)]
959

    
960
        # skip
961
        mem.skip = SKIP_VALUES[_mem.add]
962

    
963
        # tone data
964
        rxtone = txtone = None
965
        txtone = self._decode_tone(_mem.txtone)
966
        rxtone = self._decode_tone(_mem.rxtone)
967
        chirp_common.split_tone_decode(mem, txtone, rxtone)
968

    
969
        # Extra
970
        mem.extra = RadioSettingGroup("extra", "Extra")
971

    
972
        if not self.COLOR_LCD or \
973
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
974
            scramble = RadioSetting("scramble", "Scramble",
975
                                    RadioSettingValueBoolean(bool(
976
                                        _mem.scramble)))
977
            mem.extra.append(scramble)
978

    
979
        bcl = RadioSetting("bcl", "Busy channel lockout",
980
                           RadioSettingValueBoolean(bool(_mem.bcl)))
981
        mem.extra.append(bcl)
982

    
983
        pttid = RadioSetting("pttid", "PTT ID",
984
                             RadioSettingValueList(PTTID_LIST,
985
                                                   PTTID_LIST[_mem.pttid]))
986
        mem.extra.append(pttid)
987

    
988
        # validating scode
989
        scode = _mem.scode if _mem.scode != 15 else 0
990
        pttidcode = RadioSetting("scode", "PTT ID signal code",
991
                                 RadioSettingValueList(
992
                                     PTTIDCODE_LIST,
993
                                     PTTIDCODE_LIST[scode]))
994
        mem.extra.append(pttidcode)
995

    
996
        optsig = RadioSetting("optsig", "Optional signaling",
997
                              RadioSettingValueList(
998
                                  OPTSIG_LIST,
999
                                  OPTSIG_LIST[_mem.optsig]))
1000
        mem.extra.append(optsig)
1001

    
1002
        spmute = RadioSetting("spmute", "Speaker mute",
1003
                              RadioSettingValueList(
1004
                                  SPMUTE_LIST,
1005
                                  SPMUTE_LIST[_mem.spmute]))
1006
        mem.extra.append(spmute)
1007

    
1008
        return mem
1009

    
1010
    def set_memory(self, mem):
1011
        """Set the memory data in the eeprom img from the UI"""
1012
        # get the eprom representation of this channel
1013
        _mem = self._memobj.memory[mem.number]
1014
        _names = self._memobj.names[mem.number]
1015

    
1016
        mem_was_empty = False
1017
        # same method as used in get_memory for determining if mem is empty
1018
        # doing this BEFORE overwriting it with new values ...
1019
        if _mem.get_raw()[0] == "\xFF":
1020
            LOG.debug("This mem was empty before")
1021
            mem_was_empty = True
1022

    
1023
        # if empty memmory
1024
        if mem.empty:
1025
            # the channel itself
1026
            _mem.set_raw("\xFF" * 16)
1027
            # the name tag
1028
            _names.set_raw("\xFF" * 16)
1029
            return
1030

    
1031
        if mem_was_empty:
1032
            # Zero the whole memory if we're making it unempty for
1033
            # the first time
1034
            LOG.debug('Zeroing new memory')
1035
            _mem.set_raw('\x00' * 16)
1036

    
1037
        if self._gmrs:
1038
            if mem.number >= 1 and mem.number <= 30:
1039
                GMRS_FREQ = int(GMRS_FREQS[mem.number - 1] * 1000000)
1040
                mem.freq = GMRS_FREQ
1041
                if mem.number <= 22:
1042
                    mem.duplex = ''
1043
                    mem.offset = 0
1044
                    if mem.number <= 7:
1045
                        mem.power = self._power_levels[2]
1046
                    if mem.number >= 8 and mem.number <= 14:
1047
                        mem.duplex = 'off'
1048
                        mem.mode = "NFM"
1049
                        mem.power = self._power_levels[2]
1050
                if mem.number > 22:
1051
                    mem.duplex = '+'
1052
                    mem.offset = 5000000
1053
            elif float(mem.freq) / 1000000 in GMRS_FREQS:
1054
                if float(mem.freq) / 1000000 in GMRS_FREQS1:
1055
                    mem.duplex = ''
1056
                    mem.offset = 0
1057
                    mem.power = self._power_levels[2]
1058
                if float(mem.freq) / 1000000 in GMRS_FREQS2:
1059
                    mem.duplex = 'off'
1060
                    mem.offset = 0
1061
                    mem.mode = "NFM"
1062
                    mem.power = self._power_levels[2]
1063
                if float(mem.freq) / 1000000 in GMRS_FREQS3:
1064
                    if mem.duplex == '+':
1065
                        mem.offset = 5000000
1066
                    else:
1067
                        mem.offset = 0
1068
            else:
1069
                mem.duplex = 'off'
1070
                mem.offset = 0
1071

    
1072
        # frequency
1073
        _mem.rxfreq = mem.freq / 10
1074

    
1075
        # duplex
1076
        if mem.duplex == "+":
1077
            _mem.txfreq = (mem.freq + mem.offset) / 10
1078
        elif mem.duplex == "-":
1079
            _mem.txfreq = (mem.freq - mem.offset) / 10
1080
        elif mem.duplex == "off":
1081
            for i in _mem.txfreq:
1082
                i.set_raw("\xFF")
1083
        elif mem.duplex == "split":
1084
            _mem.txfreq = mem.offset / 10
1085
        else:
1086
            _mem.txfreq = mem.freq / 10
1087

    
1088
        # tone data
1089
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
1090
            chirp_common.split_tone_encode(mem)
1091
        self._encode_tone(_mem.txtone, txmode, txtone, txpol)
1092
        self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)
1093

    
1094
        # name TAG of the channel
1095
        if len(mem.name) < self.NAME_LENGTH:
1096
            # we must pad to self.NAME_LENGTH chars, " " = "\xFF"
1097
            mem.name = str(mem.name).ljust(self.NAME_LENGTH, " ")
1098
        _names.name = str(mem.name).replace(" ", "\xFF")
1099

    
1100
        # power, # default power level is high
1101
        _mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)
1102

    
1103
        # wide/narrow
1104
        _mem.wide = MODES.index(mem.mode)
1105

    
1106
        # scan add property
1107
        _mem.add = SKIP_VALUES.index(mem.skip)
1108

    
1109
        # reseting unknowns, this have to be set by hand
1110
        _mem.unknown0 = 0
1111
        _mem.unknown1 = 0
1112
        _mem.unknown2 = 0
1113
        _mem.unknown3 = 0
1114
        _mem.unknown4 = 0
1115
        _mem.unknown5 = 0
1116
        _mem.unknown6 = 0
1117

    
1118
        def _zero_settings():
1119
            _mem.spmute = 0
1120
            _mem.optsig = 0
1121
            _mem.scramble = 0
1122
            _mem.bcl = 0
1123
            _mem.pttid = 0
1124
            _mem.scode = 0
1125

    
1126
        if self.COLOR_LCD and _mem.scramble:
1127
            LOG.info('Resetting scramble bit for BTECH COLOR_LCD variant')
1128
            _mem.scramble = 0
1129

    
1130
        # extra settings
1131
        if len(mem.extra) > 0:
1132
            # there are setting, parse
1133
            LOG.debug("Extra-Setting supplied. Setting them.")
1134
            # Zero them all first so any not provided by model don't
1135
            # stay set
1136
            _zero_settings()
1137
            for setting in mem.extra:
1138
                setattr(_mem, setting.get_name(), setting.value)
1139
        else:
1140
            if mem.empty:
1141
                LOG.debug("New mem is empty.")
1142
            else:
1143
                LOG.debug("New mem is NOT empty")
1144
                # set extra-settings to default ONLY when apreviously empty or
1145
                # deleted memory was edited to prevent errors such as #4121
1146
                if mem_was_empty:
1147
                    LOG.debug("old mem was empty. Setting default for extras.")
1148
                    _zero_settings()
1149

    
1150
        return mem
1151

    
1152
    def get_settings(self):
1153
        """Translate the bit in the mem_struct into settings in the UI"""
1154
        _mem = self._memobj
1155
        basic = RadioSettingGroup("basic", "Basic Settings")
1156
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
1157
        other = RadioSettingGroup("other", "Other Settings")
1158
        work = RadioSettingGroup("work", "Work Mode Settings")
1159
        top = RadioSettings(basic, advanced, other, work)
1160

    
1161
        # Basic
1162
        if self.COLOR_LCD:
1163
            tmr = RadioSetting("settings.tmr", "Transceiver multi-receive",
1164
                               RadioSettingValueList(
1165
                                   self.LIST_TMR,
1166
                                   self.LIST_TMR[_mem.settings.tmr]))
1167
            basic.append(tmr)
1168
        else:
1169
            tdr = RadioSetting("settings.tdr", "Transceiver dual receive",
1170
                               RadioSettingValueBoolean(_mem.settings.tdr))
1171
            basic.append(tdr)
1172

    
1173
        sql = RadioSetting("settings.sql", "Squelch level",
1174
                           RadioSettingValueInteger(0, 9, _mem.settings.sql))
1175
        basic.append(sql)
1176

    
1177
        if self.MODEL == "GMRS-50X1":
1178
            autolk = RadioSetting("settings.autolk", "Auto keylock",
1179
                                  RadioSettingValueBoolean(
1180
                                      _mem.settings.autolk))
1181
            basic.append(autolk)
1182

    
1183
        if self.MODEL == "DB25-G":
1184
            rs = RadioSettingValueInteger(0, 127, _mem.settings.mgain2)
1185
            mgain2 = RadioSetting("settings.mgain2", "Mic gain", rs)
1186
            basic.append(mgain2)
1187

    
1188
        tot = RadioSetting("settings.tot", "Time out timer",
1189
                           RadioSettingValueList(
1190
                               LIST_TOT,
1191
                               LIST_TOT[_mem.settings.tot]))
1192
        basic.append(tot)
1193

    
1194
        if self.MODEL == "KT-8R":
1195
                save = RadioSetting("settings.save", "Battery Save",
1196
                                    RadioSettingValueBoolean(
1197
                                        _mem.settings.save))
1198
                basic.append(save)
1199

    
1200
        if not self.MODEL == "KT-8R":
1201
            if self.VENDOR == "BTECH" or self.COLOR_LCD:
1202
                apo = RadioSetting("settings.apo", "Auto power off timer",
1203
                                   RadioSettingValueList(
1204
                                       LIST_APO,
1205
                                       LIST_APO[_mem.settings.apo]))
1206
                basic.append(apo)
1207
            else:
1208
                toa = RadioSetting("settings.apo", "Time out alert timer",
1209
                                   RadioSettingValueList(
1210
                                       LIST_OFF1TO10,
1211
                                       LIST_OFF1TO10[_mem.settings.apo]))
1212
                basic.append(toa)
1213

    
1214
        abr = RadioSetting("settings.abr", "Backlight timer",
1215
                           RadioSettingValueList(
1216
                               LIST_OFF1TO50,
1217
                               LIST_OFF1TO50[_mem.settings.abr]))
1218
        basic.append(abr)
1219

    
1220
        beep = RadioSetting("settings.beep", "Key beep",
1221
                            RadioSettingValueBoolean(_mem.settings.beep))
1222
        basic.append(beep)
1223

    
1224
        if self.MODEL == "KT-8R":
1225
                dsub = RadioSetting("settings.dsub", "CTCSS/DCS code display",
1226
                                    RadioSettingValueBoolean(
1227
                                        _mem.settings.dsub))
1228
                basic.append(dsub)
1229

    
1230
        if self.MODEL == "KT-8R":
1231
                dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1232
                                      RadioSettingValueBoolean(
1233
                                          _mem.settings.dtmfst))
1234
                basic.append(dtmfst)
1235
        else:
1236
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1237
                                  RadioSettingValueList(
1238
                                      LIST_DTMFST,
1239
                                      LIST_DTMFST[_mem.settings.dtmfst]))
1240
            basic.append(dtmfst)
1241

    
1242
        if not self.COLOR_LCD:
1243
            prisc = RadioSetting("settings.prisc", "Priority scan",
1244
                                 RadioSettingValueBoolean(
1245
                                     _mem.settings.prisc))
1246
            basic.append(prisc)
1247

    
1248
            prich = RadioSetting("settings.prich", "Priority channel",
1249
                                 RadioSettingValueInteger(0, self._upper,
1250
                                                          _mem.settings.prich))
1251
            basic.append(prich)
1252

    
1253
        screv = RadioSetting("settings.screv", "Scan resume method",
1254
                             RadioSettingValueList(
1255
                                 LIST_SCREV,
1256
                                 LIST_SCREV[_mem.settings.screv]))
1257
        basic.append(screv)
1258

    
1259
        pttlt = RadioSetting("settings.pttlt", "PTT transmit delay",
1260
                             RadioSettingValueInteger(0, 30,
1261
                                                      _mem.settings.pttlt))
1262
        basic.append(pttlt)
1263

    
1264
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1265
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1266
                                 RadioSettingValueList(
1267
                                     LIST_EMCTPX,
1268
                                     LIST_EMCTPX[_mem.settings.emctp]))
1269
            basic.append(emctp)
1270
        else:
1271
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1272
                                 RadioSettingValueList(
1273
                                     LIST_EMCTP,
1274
                                     LIST_EMCTP[_mem.settings.emctp]))
1275
            basic.append(emctp)
1276

    
1277
        emcch = RadioSetting("settings.emcch", "Alarm channel",
1278
                             RadioSettingValueInteger(0, self._upper,
1279
                                                      _mem.settings.emcch))
1280
        basic.append(emcch)
1281

    
1282
        if self.COLOR_LCD:
1283
            if _mem.settings.sigbp > 0x01:
1284
                val = 0x00
1285
            else:
1286
                val = _mem.settings.sigbp
1287
            sigbp = RadioSetting("settings.sigbp", "Signal beep",
1288
                                 RadioSettingValueBoolean(val))
1289
            basic.append(sigbp)
1290
        else:
1291
            ringt = RadioSetting("settings.ringt", "Ring time",
1292
                                 RadioSettingValueList(
1293
                                     LIST_OFF1TO9,
1294
                                     LIST_OFF1TO9[_mem.settings.ringt]))
1295
            basic.append(ringt)
1296

    
1297
        camdf = RadioSetting("settings.camdf", "Display mode A",
1298
                             RadioSettingValueList(
1299
                                 LIST_MDF,
1300
                                 LIST_MDF[_mem.settings.camdf]))
1301
        basic.append(camdf)
1302

    
1303
        cbmdf = RadioSetting("settings.cbmdf", "Display mode B",
1304
                             RadioSettingValueList(
1305
                                 LIST_MDF,
1306
                                 LIST_MDF[_mem.settings.cbmdf]))
1307
        basic.append(cbmdf)
1308

    
1309
        if self.COLOR_LCD:
1310
            ccmdf = RadioSetting("settings.ccmdf", "Display mode C",
1311
                                 RadioSettingValueList(
1312
                                     LIST_MDF,
1313
                                     LIST_MDF[_mem.settings.ccmdf]))
1314
            basic.append(ccmdf)
1315

    
1316
            cdmdf = RadioSetting("settings.cdmdf", "Display mode D",
1317
                                 RadioSettingValueList(
1318
                                     LIST_MDF,
1319
                                     LIST_MDF[_mem.settings.cdmdf]))
1320
            basic.append(cdmdf)
1321

    
1322
            langua = RadioSetting("settings.langua", "Language",
1323
                                  RadioSettingValueList(
1324
                                      LIST_LANGUA,
1325
                                      LIST_LANGUA[_mem.settings.langua]))
1326
            basic.append(langua)
1327

    
1328
        if self.MODEL == "KT-8R":
1329
            voice = RadioSetting("settings.voice", "Voice prompt",
1330
                                 RadioSettingValueList(
1331
                                     LIST_VOICE,
1332
                                     LIST_VOICE[_mem.settings.voice]))
1333
            basic.append(voice)
1334

    
1335
            vox = RadioSetting("settings.vox", "VOX",
1336
                               RadioSettingValueList(
1337
                                   LIST_VOX,
1338
                                   LIST_VOX[_mem.settings.vox]))
1339
            basic.append(vox)
1340

    
1341
            voxt = RadioSetting("settings.voxt", "VOX delay time",
1342
                                RadioSettingValueList(
1343
                                    LIST_VOXT,
1344
                                    LIST_VOXT[_mem.settings.voxt]))
1345
            basic.append(voxt)
1346

    
1347
        if self.VENDOR == "BTECH":
1348
            if self.COLOR_LCD:
1349
                sync = RadioSetting("settings.sync", "Channel display sync",
1350
                                    RadioSettingValueList(
1351
                                        LIST_SYNC,
1352
                                        LIST_SYNC[_mem.settings.sync]))
1353
                basic.append(sync)
1354
            else:
1355
                sync = RadioSetting("settings.sync", "A/B channel sync",
1356
                                    RadioSettingValueBoolean(
1357
                                        _mem.settings.sync))
1358
                basic.append(sync)
1359
        else:
1360
            autolk = RadioSetting("settings.sync", "Auto keylock",
1361
                                  RadioSettingValueBoolean(
1362
                                      _mem.settings.sync))
1363
            basic.append(autolk)
1364

    
1365
        if not self.COLOR_LCD:
1366
            ponmsg = RadioSetting("settings.ponmsg", "Power-on message",
1367
                                  RadioSettingValueList(
1368
                                      LIST_PONMSG,
1369
                                      LIST_PONMSG[_mem.settings.ponmsg]))
1370
            basic.append(ponmsg)
1371

    
1372
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3):
1373
            mainfc = RadioSetting("settings.mainfc",
1374
                                  "Main LCD foreground color",
1375
                                  RadioSettingValueList(
1376
                                      LIST_COLOR9,
1377
                                      LIST_COLOR9[_mem.settings.mainfc]))
1378
            basic.append(mainfc)
1379

    
1380
            mainbc = RadioSetting("settings.mainbc",
1381
                                  "Main LCD background color",
1382
                                  RadioSettingValueList(
1383
                                      LIST_COLOR9,
1384
                                      LIST_COLOR9[_mem.settings.mainbc]))
1385
            basic.append(mainbc)
1386

    
1387
            menufc = RadioSetting("settings.menufc", "Menu foreground color",
1388
                                  RadioSettingValueList(
1389
                                      LIST_COLOR9,
1390
                                      LIST_COLOR9[_mem.settings.menufc]))
1391
            basic.append(menufc)
1392

    
1393
            menubc = RadioSetting("settings.menubc", "Menu background color",
1394
                                  RadioSettingValueList(
1395
                                      LIST_COLOR9,
1396
                                      LIST_COLOR9[_mem.settings.menubc]))
1397
            basic.append(menubc)
1398

    
1399
            stafc = RadioSetting("settings.stafc",
1400
                                 "Top status foreground color",
1401
                                 RadioSettingValueList(
1402
                                     LIST_COLOR9,
1403
                                     LIST_COLOR9[_mem.settings.stafc]))
1404
            basic.append(stafc)
1405

    
1406
            stabc = RadioSetting("settings.stabc",
1407
                                 "Top status background color",
1408
                                 RadioSettingValueList(
1409
                                     LIST_COLOR9,
1410
                                     LIST_COLOR9[_mem.settings.stabc]))
1411
            basic.append(stabc)
1412

    
1413
            sigfc = RadioSetting("settings.sigfc",
1414
                                 "Bottom status foreground color",
1415
                                 RadioSettingValueList(
1416
                                     LIST_COLOR9,
1417
                                     LIST_COLOR9[_mem.settings.sigfc]))
1418
            basic.append(sigfc)
1419

    
1420
            sigbc = RadioSetting("settings.sigbc",
1421
                                 "Bottom status background color",
1422
                                 RadioSettingValueList(
1423
                                     LIST_COLOR9,
1424
                                     LIST_COLOR9[_mem.settings.sigbc]))
1425
            basic.append(sigbc)
1426

    
1427
            rxfc = RadioSetting("settings.rxfc", "Receiving character color",
1428
                                RadioSettingValueList(
1429
                                    LIST_COLOR9,
1430
                                    LIST_COLOR9[_mem.settings.rxfc]))
1431
            basic.append(rxfc)
1432

    
1433
            txfc = RadioSetting("settings.txfc",
1434
                                "Transmitting character color",
1435
                                RadioSettingValueList(
1436
                                    LIST_COLOR9,
1437
                                    LIST_COLOR9[_mem.settings.txfc]))
1438
            basic.append(txfc)
1439

    
1440
            txdisp = RadioSetting("settings.txdisp",
1441
                                  "Transmitting status display",
1442
                                  RadioSettingValueList(
1443
                                      LIST_TXDISP,
1444
                                      LIST_TXDISP[_mem.settings.txdisp]))
1445
            basic.append(txdisp)
1446
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1447
            stfc = RadioSetting("settings.stfc",
1448
                                "ST-FC",
1449
                                RadioSettingValueList(
1450
                                    LIST_COLOR8,
1451
                                    LIST_COLOR8[_mem.settings.stfc]))
1452
            basic.append(stfc)
1453

    
1454
            mffc = RadioSetting("settings.mffc",
1455
                                "MF-FC",
1456
                                RadioSettingValueList(
1457
                                    LIST_COLOR8,
1458
                                    LIST_COLOR8[_mem.settings.mffc]))
1459
            basic.append(mffc)
1460

    
1461
            sfafc = RadioSetting("settings.sfafc",
1462
                                 "SFA-FC",
1463
                                 RadioSettingValueList(
1464
                                     LIST_COLOR8,
1465
                                     LIST_COLOR8[_mem.settings.sfafc]))
1466
            basic.append(sfafc)
1467

    
1468
            sfbfc = RadioSetting("settings.sfbfc",
1469
                                 "SFB-FC",
1470
                                 RadioSettingValueList(
1471
                                     LIST_COLOR8,
1472
                                     LIST_COLOR8[_mem.settings.sfbfc]))
1473
            basic.append(sfbfc)
1474

    
1475
            sfcfc = RadioSetting("settings.sfcfc",
1476
                                 "SFC-FC",
1477
                                 RadioSettingValueList(
1478
                                     LIST_COLOR8,
1479
                                     LIST_COLOR8[_mem.settings.sfcfc]))
1480
            basic.append(sfcfc)
1481

    
1482
            sfdfc = RadioSetting("settings.sfdfc",
1483
                                 "SFD-FC",
1484
                                 RadioSettingValueList(
1485
                                     LIST_COLOR8,
1486
                                     LIST_COLOR8[_mem.settings.sfdfc]))
1487
            basic.append(sfdfc)
1488

    
1489
            subfc = RadioSetting("settings.subfc",
1490
                                 "SUB-FC",
1491
                                 RadioSettingValueList(
1492
                                     LIST_COLOR8,
1493
                                     LIST_COLOR8[_mem.settings.subfc]))
1494
            basic.append(subfc)
1495

    
1496
            fmfc = RadioSetting("settings.fmfc",
1497
                                "FM-FC",
1498
                                RadioSettingValueList(
1499
                                    LIST_COLOR8,
1500
                                    LIST_COLOR8[_mem.settings.fmfc]))
1501
            basic.append(fmfc)
1502

    
1503
            sigfc = RadioSetting("settings.sigfc",
1504
                                 "SIG-FC",
1505
                                 RadioSettingValueList(
1506
                                     LIST_COLOR8,
1507
                                     LIST_COLOR8[_mem.settings.sigfc]))
1508
            basic.append(sigfc)
1509

    
1510
            if not self.MODEL == "KT-8R":
1511
                modfc = RadioSetting("settings.modfc",
1512
                                     "MOD-FC",
1513
                                     RadioSettingValueList(
1514
                                         LIST_COLOR8,
1515
                                         LIST_COLOR8[_mem.settings.modfc]))
1516
                basic.append(modfc)
1517

    
1518
            menufc = RadioSetting("settings.menufc",
1519
                                  "MENUFC",
1520
                                  RadioSettingValueList(
1521
                                      LIST_COLOR8,
1522
                                      LIST_COLOR8[_mem.settings.menufc]))
1523
            basic.append(menufc)
1524

    
1525
            txfc = RadioSetting("settings.txfc",
1526
                                "TX-FC",
1527
                                RadioSettingValueList(
1528
                                    LIST_COLOR8,
1529
                                    LIST_COLOR8[_mem.settings.txfc]))
1530
            basic.append(txfc)
1531

    
1532
            if self.MODEL == "KT-8R":
1533
                rxfc = RadioSetting("settings.rxfc",
1534
                                    "RX-FC",
1535
                                    RadioSettingValueList(
1536
                                        LIST_COLOR8,
1537
                                        LIST_COLOR8[_mem.settings.rxfc]))
1538
                basic.append(rxfc)
1539

    
1540
            if not self.MODEL == "KT-8R":
1541
                txdisp = RadioSetting("settings.txdisp",
1542
                                      "Transmitting status display",
1543
                                      RadioSettingValueList(
1544
                                          LIST_TXDISP,
1545
                                          LIST_TXDISP[_mem.settings.txdisp]))
1546
                basic.append(txdisp)
1547
        else:
1548
            wtled = RadioSetting("settings.wtled", "Standby backlight Color",
1549
                                 RadioSettingValueList(
1550
                                     LIST_COLOR4,
1551
                                     LIST_COLOR4[_mem.settings.wtled]))
1552
            basic.append(wtled)
1553

    
1554
            rxled = RadioSetting("settings.rxled", "RX backlight Color",
1555
                                 RadioSettingValueList(
1556
                                     LIST_COLOR4,
1557
                                     LIST_COLOR4[_mem.settings.rxled]))
1558
            basic.append(rxled)
1559

    
1560
            txled = RadioSetting("settings.txled", "TX backlight Color",
1561
                                 RadioSettingValueList(
1562
                                     LIST_COLOR4,
1563
                                     LIST_COLOR4[_mem.settings.txled]))
1564
            basic.append(txled)
1565

    
1566
        anil = RadioSetting("settings.anil", "ANI length",
1567
                            RadioSettingValueList(
1568
                                LIST_ANIL,
1569
                                LIST_ANIL[_mem.settings.anil]))
1570
        basic.append(anil)
1571

    
1572
        reps = RadioSetting("settings.reps", "Relay signal (tone burst)",
1573
                            RadioSettingValueList(
1574
                                LIST_REPS,
1575
                                LIST_REPS[_mem.settings.reps]))
1576
        basic.append(reps)
1577

    
1578
        if not self.MODEL == "GMRS-50X1" and not self.MODEL == "KT-8R":
1579
            repm = RadioSetting("settings.repm", "Relay condition",
1580
                                RadioSettingValueList(
1581
                                    LIST_REPM,
1582
                                    LIST_REPM[_mem.settings.repm]))
1583
            basic.append(repm)
1584

    
1585
        if self.VENDOR == "BTECH" or self.COLOR_LCD:
1586
            if self.COLOR_LCD:
1587
                tmrmr = RadioSetting("settings.tmrmr", "TMR return time",
1588
                                     RadioSettingValueList(
1589
                                         LIST_OFF1TO50,
1590
                                         LIST_OFF1TO50[_mem.settings.tmrmr]))
1591
                basic.append(tmrmr)
1592
            else:
1593
                tdrab = RadioSetting("settings.tdrab", "TDR return time",
1594
                                     RadioSettingValueList(
1595
                                         LIST_OFF1TO50,
1596
                                         LIST_OFF1TO50[_mem.settings.tdrab]))
1597
                basic.append(tdrab)
1598

    
1599
            ste = RadioSetting("settings.ste", "Squelch tail eliminate",
1600
                               RadioSettingValueBoolean(_mem.settings.ste))
1601
            basic.append(ste)
1602

    
1603
            rpste = RadioSetting("settings.rpste", "Repeater STE",
1604
                                 RadioSettingValueList(
1605
                                     LIST_OFF1TO9,
1606
                                     LIST_OFF1TO9[_mem.settings.rpste]))
1607
            basic.append(rpste)
1608

    
1609
            rptdl = RadioSetting("settings.rptdl", "Repeater STE delay",
1610
                                 RadioSettingValueList(
1611
                                     LIST_RPTDL,
1612
                                     LIST_RPTDL[_mem.settings.rptdl]))
1613
            basic.append(rptdl)
1614

    
1615
        if self.MODEL == "DB25-G":
1616
            mgain = RadioSetting("settings.mgain", "Auto power-on",
1617
                                 RadioSettingValueBoolean(_mem.settings.mgain))
1618
            basic.append(mgain)
1619

    
1620
        if str(_mem.fingerprint.fp) in BTECH3:
1621
            mgain = RadioSetting("settings.mgain", "Mic gain",
1622
                                 RadioSettingValueInteger(0, 120,
1623
                                                          _mem.settings.mgain))
1624
            basic.append(mgain)
1625

    
1626
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1627
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain",
1628
                                 RadioSettingValueInteger(0, 60,
1629
                                                          _mem.settings.dtmfg))
1630
            basic.append(dtmfg)
1631

    
1632
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1633
            mgain = RadioSetting("settings.mgain", "Mic gain",
1634
                                 RadioSettingValueInteger(0, 120,
1635
                                                          _mem.settings.mgain))
1636
            basic.append(mgain)
1637

    
1638
            skiptx = RadioSetting("settings.skiptx", "Skip TX",
1639
                                  RadioSettingValueList(
1640
                                      LIST_SKIPTX,
1641
                                      LIST_SKIPTX[_mem.settings.skiptx]))
1642
            basic.append(skiptx)
1643

    
1644
            scmode = RadioSetting("settings.scmode", "Scan mode",
1645
                                  RadioSettingValueList(
1646
                                      LIST_SCMODE,
1647
                                      LIST_SCMODE[_mem.settings.scmode]))
1648
            basic.append(scmode)
1649

    
1650
        if self.MODEL == "KT-8R":
1651
            tmrtx = RadioSetting("settings.tmrtx", "TX in multi-standby",
1652
                                 RadioSettingValueList(
1653
                                     LIST_TMRTX,
1654
                                     LIST_TMRTX[_mem.settings.tmrtx]))
1655
            basic.append(tmrtx)
1656

    
1657
        # Advanced
1658
        def _filter(name):
1659
            filtered = ""
1660
            for char in str(name):
1661
                if char in VALID_CHARS:
1662
                    filtered += char
1663
                else:
1664
                    filtered += " "
1665
            return filtered
1666

    
1667
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3):
1668
            _msg = self._memobj.poweron_msg
1669
            line1 = RadioSetting("poweron_msg.line1",
1670
                                 "Power-on message line 1",
1671
                                 RadioSettingValueString(0, 8, _filter(
1672
                                                         _msg.line1)))
1673
            advanced.append(line1)
1674
            line2 = RadioSetting("poweron_msg.line2",
1675
                                 "Power-on message line 2",
1676
                                 RadioSettingValueString(0, 8, _filter(
1677
                                                         _msg.line2)))
1678
            advanced.append(line2)
1679
            line3 = RadioSetting("poweron_msg.line3",
1680
                                 "Power-on message line 3",
1681
                                 RadioSettingValueString(0, 8, _filter(
1682
                                                         _msg.line3)))
1683
            advanced.append(line3)
1684
            line4 = RadioSetting("poweron_msg.line4",
1685
                                 "Power-on message line 4",
1686
                                 RadioSettingValueString(0, 8, _filter(
1687
                                                         _msg.line4)))
1688
            advanced.append(line4)
1689
            line5 = RadioSetting("poweron_msg.line5",
1690
                                 "Power-on message line 5",
1691
                                 RadioSettingValueString(0, 8, _filter(
1692
                                                         _msg.line5)))
1693
            advanced.append(line5)
1694
            line6 = RadioSetting("poweron_msg.line6",
1695
                                 "Power-on message line 6",
1696
                                 RadioSettingValueString(0, 8, _filter(
1697
                                                         _msg.line6)))
1698
            advanced.append(line6)
1699
            line7 = RadioSetting("poweron_msg.line7",
1700
                                 "Power-on message line 7",
1701
                                 RadioSettingValueString(0, 8, _filter(
1702
                                                         _msg.line7)))
1703
            advanced.append(line7)
1704
            line8 = RadioSetting("poweron_msg.line8", "Static message",
1705
                                 RadioSettingValueString(0, 8, _filter(
1706
                                                         _msg.line8)))
1707
            advanced.append(line8)
1708
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1709
            _msg = self._memobj.static_msg
1710
            line = RadioSetting("static_msg.line", "Static message",
1711
                                RadioSettingValueString(0, 16, _filter(
1712
                                    _msg.line)))
1713
            advanced.append(line)
1714
        else:
1715
            _msg = self._memobj.poweron_msg
1716
            line1 = RadioSetting("poweron_msg.line1",
1717
                                 "Power-on message line 1",
1718
                                 RadioSettingValueString(0, 6, _filter(
1719
                                                         _msg.line1)))
1720
            advanced.append(line1)
1721
            line2 = RadioSetting("poweron_msg.line2",
1722
                                 "Power-on message line 2",
1723
                                 RadioSettingValueString(0, 6, _filter(
1724
                                                         _msg.line2)))
1725
            advanced.append(line2)
1726

    
1727
        if self.MODEL in ("UV-2501", "UV-5001"):
1728
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1729
                                   RadioSettingValueBoolean(
1730
                                       _mem.settings2.vfomren))
1731
            advanced.append(vfomren)
1732

    
1733
            reseten = RadioSetting("settings2.reseten", "RESET",
1734
                                   RadioSettingValueBoolean(
1735
                                       _mem.settings2.reseten))
1736
            advanced.append(reseten)
1737

    
1738
            menuen = RadioSetting("settings2.menuen", "Menu",
1739
                                  RadioSettingValueBoolean(
1740
                                      _mem.settings2.menuen))
1741
            advanced.append(menuen)
1742

    
1743
        # Other
1744
        def convert_bytes_to_limit(bytes):
1745
            limit = ""
1746
            for byte in bytes:
1747
                if byte < 10:
1748
                    limit += chr(byte + 0x30)
1749
                else:
1750
                    break
1751
            return limit
1752

    
1753
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1754
            _ranges = self._memobj.ranges220
1755
            ranges = "ranges220"
1756
        else:
1757
            _ranges = self._memobj.ranges
1758
            ranges = "ranges"
1759

    
1760
        _limit = convert_bytes_to_limit(_ranges.vhf_low)
1761
        val = RadioSettingValueString(0, 3, _limit)
1762
        val.set_mutable(False)
1763
        vhf_low = RadioSetting("%s.vhf_low" % ranges, "VHF low", val)
1764
        other.append(vhf_low)
1765

    
1766
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1767
        val = RadioSettingValueString(0, 3, _limit)
1768
        val.set_mutable(False)
1769
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1770
        other.append(vhf_high)
1771

    
1772
        if self.BANDS == 3 or self.BANDS == 4:
1773
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1774
            val = RadioSettingValueString(0, 3, _limit)
1775
            val.set_mutable(False)
1776
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1777
            other.append(vhf2_low)
1778

    
1779
            _limit = convert_bytes_to_limit(_ranges.vhf2_high)
1780
            val = RadioSettingValueString(0, 3, _limit)
1781
            val.set_mutable(False)
1782
            vhf2_high = RadioSetting("%s.vhf2_high" % ranges, "VHF2 high", val)
1783
            other.append(vhf2_high)
1784

    
1785
        _limit = convert_bytes_to_limit(_ranges.uhf_low)
1786
        val = RadioSettingValueString(0, 3, _limit)
1787
        val.set_mutable(False)
1788
        uhf_low = RadioSetting("%s.uhf_low" % ranges, "UHF low", val)
1789
        other.append(uhf_low)
1790

    
1791
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1792
        val = RadioSettingValueString(0, 3, _limit)
1793
        val.set_mutable(False)
1794
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1795
        other.append(uhf_high)
1796

    
1797
        if self.BANDS == 4:
1798
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1799
            val = RadioSettingValueString(0, 3, _limit)
1800
            val.set_mutable(False)
1801
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1802
            other.append(uhf2_low)
1803

    
1804
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1805
            val = RadioSettingValueString(0, 3, _limit)
1806
            val.set_mutable(False)
1807
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1808
            other.append(uhf2_high)
1809

    
1810
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1811
        val.set_mutable(False)
1812
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1813
        other.append(fp)
1814

    
1815
        # Work
1816
        if self.COLOR_LCD:
1817
            dispab = RadioSetting("settings2.dispab", "Display",
1818
                                  RadioSettingValueList(
1819
                                      LIST_ABCD,
1820
                                      LIST_ABCD[_mem.settings2.dispab]))
1821
            work.append(dispab)
1822
        else:
1823
            dispab = RadioSetting("settings2.dispab", "Display",
1824
                                  RadioSettingValueList(
1825
                                      LIST_AB,
1826
                                      LIST_AB[_mem.settings2.dispab]))
1827
            work.append(dispab)
1828

    
1829
        if self.COLOR_LCD:
1830
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode",
1831
                                  RadioSettingValueList(
1832
                                      LIST_VFOMR,
1833
                                      LIST_VFOMR[_mem.settings2.vfomra]))
1834
            work.append(vfomra)
1835

    
1836
            vfomrb = RadioSetting("settings2.vfomrb", "VFO/MR B mode",
1837
                                  RadioSettingValueList(
1838
                                      LIST_VFOMR,
1839
                                      LIST_VFOMR[_mem.settings2.vfomrb]))
1840
            work.append(vfomrb)
1841

    
1842
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode",
1843
                                  RadioSettingValueList(
1844
                                      LIST_VFOMR,
1845
                                      LIST_VFOMR[_mem.settings2.vfomrc]))
1846
            work.append(vfomrc)
1847

    
1848
            vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode",
1849
                                  RadioSettingValueList(
1850
                                      LIST_VFOMR,
1851
                                      LIST_VFOMR[_mem.settings2.vfomrd]))
1852
            work.append(vfomrd)
1853
        else:
1854
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode",
1855
                                 RadioSettingValueList(
1856
                                     LIST_VFOMR,
1857
                                     LIST_VFOMR[_mem.settings2.vfomr]))
1858
            work.append(vfomr)
1859

    
1860
        keylock = RadioSetting("settings2.keylock", "Keypad lock",
1861
                               RadioSettingValueBoolean(
1862
                                   _mem.settings2.keylock))
1863
        work.append(keylock)
1864

    
1865
        mrcha = RadioSetting("settings2.mrcha", "MR A channel",
1866
                             RadioSettingValueInteger(0, self._upper,
1867
                                                      _mem.settings2.mrcha))
1868
        work.append(mrcha)
1869

    
1870
        mrchb = RadioSetting("settings2.mrchb", "MR B channel",
1871
                             RadioSettingValueInteger(0, self._upper,
1872
                                                      _mem.settings2.mrchb))
1873
        work.append(mrchb)
1874

    
1875
        if self.COLOR_LCD:
1876
            mrchc = RadioSetting("settings2.mrchc", "MR C channel",
1877
                                 RadioSettingValueInteger(
1878
                                     0, self._upper, _mem.settings2.mrchc))
1879
            work.append(mrchc)
1880

    
1881
            mrchd = RadioSetting("settings2.mrchd", "MR D channel",
1882
                                 RadioSettingValueInteger(
1883
                                     0, self._upper, _mem.settings2.mrchd))
1884
            work.append(mrchd)
1885

    
1886
        def convert_bytes_to_freq(bytes):
1887
            real_freq = 0
1888
            for byte in bytes:
1889
                real_freq = (real_freq * 10) + byte
1890
            return chirp_common.format_freq(real_freq * 10)
1891

    
1892
        def my_validate(value):
1893
            _vhf_lower = int(convert_bytes_to_limit(_ranges.vhf_low))
1894
            _vhf_upper = int(convert_bytes_to_limit(_ranges.vhf_high))
1895
            _uhf_lower = int(convert_bytes_to_limit(_ranges.uhf_low))
1896
            _uhf_upper = int(convert_bytes_to_limit(_ranges.uhf_high))
1897
            if self.BANDS == 3 or self.BANDS == 4:
1898
                _vhf2_lower = int(convert_bytes_to_limit(_ranges.vhf2_low))
1899
                _vhf2_upper = int(convert_bytes_to_limit(_ranges.vhf2_high))
1900
            if self.BANDS == 4:
1901
                _uhf2_lower = int(convert_bytes_to_limit(_ranges.uhf2_low))
1902
                _uhf2_upper = int(convert_bytes_to_limit(_ranges.uhf2_high))
1903

    
1904
            value = chirp_common.parse_freq(value)
1905
            msg = ("Can't be less then %i.0000")
1906
            if value > 99000000 and value < _vhf_lower * 1000000:
1907
                raise InvalidValueError(msg % (_vhf_lower))
1908
            msg = ("Can't be betweeb %i.9975-%i.0000")
1909
            if self.BANDS == 2:
1910
                if (_vhf_upper + 1) * 1000000 <= value and \
1911
                        value < _uhf_lower * 1000000:
1912
                    raise InvalidValueError(msg % (_vhf_upper, _uhf_lower))
1913
            if self.BANDS == 3:
1914
                if (_vhf_upper + 1) * 1000000 <= value and \
1915
                        value < _vhf2_lower * 1000000:
1916
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1917
                if (_vhf2_upper + 1) * 1000000 <= value and \
1918
                        value < _uhf_lower * 1000000:
1919
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf_lower))
1920
            if self.BANDS == 4:
1921
                if (_vhf_upper + 1) * 1000000 <= value and \
1922
                        value < _vhf2_lower * 1000000:
1923
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1924
                if (_vhf2_upper + 1) * 1000000 <= value and \
1925
                        value < _uhf2_lower * 1000000:
1926
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf2_lower))
1927
                if (_uhf2_upper + 1) * 1000000 <= value and \
1928
                        value < _uhf_lower * 1000000:
1929
                    raise InvalidValueError(msg % (_uhf2_upper, _uhf_lower))
1930
            msg = ("Can't be greater then %i.9975")
1931
            if value > 99000000 and value >= _uhf_upper * 1000000:
1932
                raise InvalidValueError(msg % (_uhf_upper))
1933
            return chirp_common.format_freq(value)
1934

    
1935
        def apply_freq(setting, obj):
1936
            value = chirp_common.parse_freq(str(setting.value)) / 10
1937
            for i in range(7, -1, -1):
1938
                obj.freq[i] = value % 10
1939
                value /= 10
1940

    
1941
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1942
                                        _mem.vfo.a.freq))
1943
        val1a.set_validate_callback(my_validate)
1944
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
1945
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
1946
        work.append(vfoafreq)
1947

    
1948
        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1949
                                        _mem.vfo.b.freq))
1950
        val1b.set_validate_callback(my_validate)
1951
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
1952
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
1953
        work.append(vfobfreq)
1954

    
1955
        if self.COLOR_LCD:
1956
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1957
                                            _mem.vfo.c.freq))
1958
            val1c.set_validate_callback(my_validate)
1959
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
1960
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
1961
            work.append(vfocfreq)
1962

    
1963
            val1d = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1964
                                            _mem.vfo.d.freq))
1965
            val1d.set_validate_callback(my_validate)
1966
            vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
1967
            vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
1968
            work.append(vfodfreq)
1969

    
1970
        if not self.MODEL == "GMRS-50X1":
1971
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift",
1972
                                      RadioSettingValueList(
1973
                                          LIST_SHIFT,
1974
                                          LIST_SHIFT[_mem.vfo.a.shiftd]))
1975
            work.append(vfoashiftd)
1976

    
1977
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift",
1978
                                      RadioSettingValueList(
1979
                                          LIST_SHIFT,
1980
                                          LIST_SHIFT[_mem.vfo.b.shiftd]))
1981
            work.append(vfobshiftd)
1982

    
1983
            if self.COLOR_LCD:
1984
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift",
1985
                                          RadioSettingValueList(
1986
                                              LIST_SHIFT,
1987
                                              LIST_SHIFT[_mem.vfo.c.shiftd]))
1988
                work.append(vfocshiftd)
1989

    
1990
                vfodshiftd = RadioSetting("vfo.d.shiftd", "VFO D shift",
1991
                                          RadioSettingValueList(
1992
                                              LIST_SHIFT,
1993
                                              LIST_SHIFT[_mem.vfo.d.shiftd]))
1994
                work.append(vfodshiftd)
1995

    
1996
        def convert_bytes_to_offset(bytes):
1997
            real_offset = 0
1998
            for byte in bytes:
1999
                real_offset = (real_offset * 10) + byte
2000
            return chirp_common.format_freq(real_offset * 1000)
2001

    
2002
        def apply_offset(setting, obj):
2003
            value = chirp_common.parse_freq(str(setting.value)) / 1000
2004
            for i in range(5, -1, -1):
2005
                obj.offset[i] = value % 10
2006
                value /= 10
2007

    
2008
        if not self.MODEL == "GMRS-50X1":
2009
            if self.COLOR_LCD:
2010
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2011
                                                _mem.vfo.a.offset))
2012
                vfoaoffset = RadioSetting("vfo.a.offset",
2013
                                          "VFO A offset (0.000-999.999)",
2014
                                          val1a)
2015
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
2016
                work.append(vfoaoffset)
2017

    
2018
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2019
                                                _mem.vfo.b.offset))
2020
                vfoboffset = RadioSetting("vfo.b.offset",
2021
                                          "VFO B offset (0.000-999.999)",
2022
                                          val1b)
2023
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
2024
                work.append(vfoboffset)
2025

    
2026
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2027
                                                _mem.vfo.c.offset))
2028
                vfocoffset = RadioSetting("vfo.c.offset",
2029
                                          "VFO C offset (0.000-999.999)",
2030
                                          val1c)
2031
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
2032
                work.append(vfocoffset)
2033

    
2034
                val1d = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2035
                                                _mem.vfo.d.offset))
2036
                vfodoffset = RadioSetting("vfo.d.offset",
2037
                                          "VFO D offset (0.000-999.999)",
2038
                                          val1d)
2039
                vfodoffset.set_apply_callback(apply_offset, _mem.vfo.d)
2040
                work.append(vfodoffset)
2041
            else:
2042
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2043
                                                _mem.vfo.a.offset))
2044
                vfoaoffset = RadioSetting("vfo.a.offset",
2045
                                          "VFO A offset (0.000-99.999)", val1a)
2046
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
2047
                work.append(vfoaoffset)
2048

    
2049
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2050
                                                _mem.vfo.b.offset))
2051
                vfoboffset = RadioSetting("vfo.b.offset",
2052
                                          "VFO B offset (0.000-99.999)", val1b)
2053
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
2054
                work.append(vfoboffset)
2055

    
2056
        if not self.MODEL == "GMRS-50X1":
2057
            vfoatxp = RadioSetting("vfo.a.power", "VFO A power",
2058
                                   RadioSettingValueList(
2059
                                       LIST_TXP,
2060
                                       LIST_TXP[_mem.vfo.a.power]))
2061
            work.append(vfoatxp)
2062

    
2063
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power",
2064
                                   RadioSettingValueList(
2065
                                       LIST_TXP,
2066
                                       LIST_TXP[_mem.vfo.b.power]))
2067
            work.append(vfobtxp)
2068

    
2069
            if self.COLOR_LCD:
2070
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power",
2071
                                       RadioSettingValueList(
2072
                                           LIST_TXP,
2073
                                           LIST_TXP[_mem.vfo.c.power]))
2074
                work.append(vfoctxp)
2075

    
2076
                vfodtxp = RadioSetting("vfo.d.power", "VFO D power",
2077
                                       RadioSettingValueList(
2078
                                           LIST_TXP,
2079
                                           LIST_TXP[_mem.vfo.d.power]))
2080
                work.append(vfodtxp)
2081

    
2082
        if not self.MODEL == "GMRS-50X1":
2083
            vfoawide = RadioSetting("vfo.a.wide", "VFO A bandwidth",
2084
                                    RadioSettingValueList(
2085
                                        LIST_WIDE,
2086
                                        LIST_WIDE[_mem.vfo.a.wide]))
2087
            work.append(vfoawide)
2088

    
2089
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth",
2090
                                    RadioSettingValueList(
2091
                                        LIST_WIDE,
2092
                                        LIST_WIDE[_mem.vfo.b.wide]))
2093
            work.append(vfobwide)
2094

    
2095
            if self.COLOR_LCD:
2096
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth",
2097
                                        RadioSettingValueList(
2098
                                            LIST_WIDE,
2099
                                            LIST_WIDE[_mem.vfo.c.wide]))
2100
                work.append(vfocwide)
2101

    
2102
                vfodwide = RadioSetting("vfo.d.wide", "VFO D bandwidth",
2103
                                        RadioSettingValueList(
2104
                                            LIST_WIDE,
2105
                                            LIST_WIDE[_mem.vfo.d.wide]))
2106
                work.append(vfodwide)
2107

    
2108
        vfoastep = RadioSetting("vfo.a.step", "VFO A step",
2109
                                RadioSettingValueList(
2110
                                    LIST_STEP,
2111
                                    LIST_STEP[_mem.vfo.a.step]))
2112
        work.append(vfoastep)
2113

    
2114
        vfobstep = RadioSetting("vfo.b.step", "VFO B step",
2115
                                RadioSettingValueList(
2116
                                    LIST_STEP,
2117
                                    LIST_STEP[_mem.vfo.b.step]))
2118
        work.append(vfobstep)
2119

    
2120
        if self.COLOR_LCD:
2121
            vfocstep = RadioSetting("vfo.c.step", "VFO C step",
2122
                                    RadioSettingValueList(
2123
                                        LIST_STEP,
2124
                                        LIST_STEP[_mem.vfo.c.step]))
2125
            work.append(vfocstep)
2126

    
2127
            vfodstep = RadioSetting("vfo.d.step", "VFO D step",
2128
                                    RadioSettingValueList(
2129
                                        LIST_STEP,
2130
                                        LIST_STEP[_mem.vfo.d.step]))
2131
            work.append(vfodstep)
2132

    
2133
        vfoaoptsig = RadioSetting("vfo.a.optsig", "VFO A optional signal",
2134
                                  RadioSettingValueList(
2135
                                      OPTSIG_LIST,
2136
                                      OPTSIG_LIST[_mem.vfo.a.optsig]))
2137
        work.append(vfoaoptsig)
2138

    
2139
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal",
2140
                                  RadioSettingValueList(
2141
                                      OPTSIG_LIST,
2142
                                      OPTSIG_LIST[_mem.vfo.b.optsig]))
2143
        work.append(vfoboptsig)
2144

    
2145
        if self.COLOR_LCD:
2146
            vfocoptsig = RadioSetting("vfo.c.optsig", "VFO C optional signal",
2147
                                      RadioSettingValueList(
2148
                                          OPTSIG_LIST,
2149
                                          OPTSIG_LIST[_mem.vfo.c.optsig]))
2150
            work.append(vfocoptsig)
2151

    
2152
            vfodoptsig = RadioSetting("vfo.d.optsig", "VFO D optional signal",
2153
                                      RadioSettingValueList(
2154
                                          OPTSIG_LIST,
2155
                                          OPTSIG_LIST[_mem.vfo.d.optsig]))
2156
            work.append(vfodoptsig)
2157

    
2158
        vfoaspmute = RadioSetting("vfo.a.spmute", "VFO A speaker mute",
2159
                                  RadioSettingValueList(
2160
                                      SPMUTE_LIST,
2161
                                      SPMUTE_LIST[_mem.vfo.a.spmute]))
2162
        work.append(vfoaspmute)
2163

    
2164
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute",
2165
                                  RadioSettingValueList(
2166
                                      SPMUTE_LIST,
2167
                                      SPMUTE_LIST[_mem.vfo.b.spmute]))
2168
        work.append(vfobspmute)
2169

    
2170
        if self.COLOR_LCD:
2171
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute",
2172
                                      RadioSettingValueList(
2173
                                          SPMUTE_LIST,
2174
                                          SPMUTE_LIST[_mem.vfo.c.spmute]))
2175
            work.append(vfocspmute)
2176

    
2177
            vfodspmute = RadioSetting("vfo.d.spmute", "VFO D speaker mute",
2178
                                      RadioSettingValueList(
2179
                                          SPMUTE_LIST,
2180
                                          SPMUTE_LIST[_mem.vfo.d.spmute]))
2181
            work.append(vfodspmute)
2182

    
2183
        if not self.COLOR_LCD or \
2184
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2185
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble",
2186
                                   RadioSettingValueBoolean(
2187
                                       _mem.vfo.a.scramble))
2188
            work.append(vfoascr)
2189

    
2190
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble",
2191
                                   RadioSettingValueBoolean(
2192
                                       _mem.vfo.b.scramble))
2193
            work.append(vfobscr)
2194

    
2195
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2196
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble",
2197
                                   RadioSettingValueBoolean(
2198
                                       _mem.vfo.c.scramble))
2199
            work.append(vfocscr)
2200

    
2201
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble",
2202
                                   RadioSettingValueBoolean(
2203
                                       _mem.vfo.d.scramble))
2204
            work.append(vfodscr)
2205

    
2206
        if not self.MODEL == "GMRS-50X1":
2207
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID",
2208
                                     RadioSettingValueList(
2209
                                         PTTIDCODE_LIST,
2210
                                         PTTIDCODE_LIST[_mem.vfo.a.scode]))
2211
            work.append(vfoascode)
2212

    
2213
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID",
2214
                                     RadioSettingValueList(
2215
                                         PTTIDCODE_LIST,
2216
                                         PTTIDCODE_LIST[_mem.vfo.b.scode]))
2217
            work.append(vfobscode)
2218

    
2219
            if self.COLOR_LCD:
2220
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID",
2221
                                         RadioSettingValueList(
2222
                                             PTTIDCODE_LIST,
2223
                                             PTTIDCODE_LIST[_mem.vfo.c.scode]))
2224
                work.append(vfocscode)
2225

    
2226
                vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID",
2227
                                         RadioSettingValueList(
2228
                                             PTTIDCODE_LIST,
2229
                                             PTTIDCODE_LIST[_mem.vfo.d.scode]))
2230
                work.append(vfodscode)
2231

    
2232
        if not self.MODEL == "GMRS-50X1":
2233
            pttid = RadioSetting("settings.pttid", "PTT ID",
2234
                                 RadioSettingValueList(
2235
                                     PTTID_LIST,
2236
                                     PTTID_LIST[_mem.settings.pttid]))
2237
            work.append(pttid)
2238

    
2239
        if not self.COLOR_LCD:
2240
            # FM presets
2241
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2242
            top.append(fm_presets)
2243

    
2244
            def fm_validate(value):
2245
                if value == 0:
2246
                    return chirp_common.format_freq(value)
2247
                if not (87.5 <= value and value <= 108.0):  # 87.5-108MHz
2248
                    msg = ("FM-Preset-Frequency: " +
2249
                           "Must be between 87.5 and 108 MHz")
2250
                    raise InvalidValueError(msg)
2251
                return value
2252

    
2253
            def apply_fm_preset_name(setting, obj):
2254
                valstring = str(setting.value)
2255
                for i in range(0, 6):
2256
                    if valstring[i] in VALID_CHARS:
2257
                        obj[i] = valstring[i]
2258
                    else:
2259
                        obj[i] = '0xff'
2260

    
2261
            def apply_fm_freq(setting, obj):
2262
                value = chirp_common.parse_freq(str(setting.value)) / 10
2263
                for i in range(7, -1, -1):
2264
                    obj.freq[i] = value % 10
2265
                    value /= 10
2266

    
2267
            _presets = self._memobj.fm_radio_preset
2268
            i = 1
2269
            for preset in _presets:
2270
                line = RadioSetting("fm_presets_" + str(i),
2271
                                    "Station name " + str(i),
2272
                                    RadioSettingValueString(0, 6, _filter(
2273
                                        preset.broadcast_station_name)))
2274
                line.set_apply_callback(apply_fm_preset_name,
2275
                                        preset.broadcast_station_name)
2276

    
2277
                val = RadioSettingValueFloat(0, 108,
2278
                                             convert_bytes_to_freq(
2279
                                                 preset.freq))
2280
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2281
                                      "Frequency " + str(i), val)
2282
                val.set_validate_callback(fm_validate)
2283
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2284
                fm_presets.append(line)
2285
                fm_presets.append(fmfreq)
2286

    
2287
                i = i + 1
2288

    
2289
        # DTMF-Setting
2290
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2291
                                              "DTMF Encoding Settings")
2292
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2293
                                              "DTMF Decoding Settings")
2294
        top.append(dtmf_enc_settings)
2295
        top.append(dtmf_dec_settings)
2296
        txdisable = RadioSetting("dtmf_settings.txdisable",
2297
                                 "TX-Disable",
2298
                                 RadioSettingValueBoolean(
2299
                                     _mem.dtmf_settings.txdisable))
2300
        dtmf_enc_settings.append(txdisable)
2301

    
2302
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2303
                                 "RX-Disable",
2304
                                 RadioSettingValueBoolean(
2305
                                     _mem.dtmf_settings.rxdisable))
2306
        dtmf_enc_settings.append(rxdisable)
2307

    
2308
        if _mem.dtmf_settings.dtmfspeed_on > 0x0F:
2309
            val = 0x03
2310
        else:
2311
            val = _mem.dtmf_settings.dtmfspeed_on
2312
        dtmfspeed_on = RadioSetting(
2313
            "dtmf_settings.dtmfspeed_on",
2314
            "DTMF Speed (On Time)",
2315
            RadioSettingValueList(LIST_DTMF_SPEED,
2316
                                  LIST_DTMF_SPEED[
2317
                                      val]))
2318
        dtmf_enc_settings.append(dtmfspeed_on)
2319

    
2320
        if _mem.dtmf_settings.dtmfspeed_off > 0x0F:
2321
            val = 0x03
2322
        else:
2323
            val = _mem.dtmf_settings.dtmfspeed_off
2324
        dtmfspeed_off = RadioSetting(
2325
            "dtmf_settings.dtmfspeed_off",
2326
            "DTMF Speed (Off Time)",
2327
            RadioSettingValueList(LIST_DTMF_SPEED,
2328
                                  LIST_DTMF_SPEED[
2329
                                      val]))
2330
        dtmf_enc_settings.append(dtmfspeed_off)
2331

    
2332
        def memory2string(dmtf_mem):
2333
            dtmf_string = ""
2334
            for digit in dmtf_mem:
2335
                if digit != 255:
2336
                    index = LIST_DTMF_VALUES.index(digit)
2337
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2338
            return dtmf_string
2339

    
2340
        def apply_dmtf_frame(setting, obj):
2341
            LOG.debug("Setting DTMF-Code: " + str(setting.value))
2342
            val_string = str(setting.value)
2343
            for i in range(0, 16):
2344
                obj[i] = 255
2345
            i = 0
2346
            for current_char in val_string:
2347
                current_char = current_char.upper()
2348
                index = LIST_DTMF_DIGITS.index(current_char)
2349
                obj[i] = LIST_DTMF_VALUES[index]
2350
                i = i + 1
2351

    
2352
        codes = self._memobj.dtmf_codes
2353
        i = 1
2354
        for dtmfcode in codes:
2355
            val = RadioSettingValueString(0, 16, memory2string(
2356
                                              dtmfcode.code),
2357
                                          False, CHARSET_DTMF_DIGITS)
2358
            line = RadioSetting("dtmf_code_" + str(i) + "_code",
2359
                                "DMTF Code " + str(i), val)
2360
            line.set_apply_callback(apply_dmtf_frame, dtmfcode.code)
2361
            dtmf_enc_settings.append(line)
2362
            i = i + 1
2363

    
2364
        line = RadioSetting("dtmf_settings.mastervice",
2365
                            "Master and Vice ID",
2366
                            RadioSettingValueBoolean(
2367
                                _mem.dtmf_settings.mastervice))
2368
        dtmf_dec_settings.append(line)
2369

    
2370
        val = RadioSettingValueString(0, 16, memory2string(
2371
                                          _mem.dtmf_settings.masterid),
2372
                                      False, CHARSET_DTMF_DIGITS)
2373
        line = RadioSetting("dtmf_settings.masterid",
2374
                            "Master Control ID ", val)
2375
        line.set_apply_callback(apply_dmtf_frame,
2376
                                _mem.dtmf_settings.masterid)
2377
        dtmf_dec_settings.append(line)
2378

    
2379
        line = RadioSetting("dtmf_settings.minspection",
2380
                            "Master Inspection",
2381
                            RadioSettingValueBoolean(
2382
                                _mem.dtmf_settings.minspection))
2383
        dtmf_dec_settings.append(line)
2384

    
2385
        line = RadioSetting("dtmf_settings.mmonitor",
2386
                            "Master Monitor",
2387
                            RadioSettingValueBoolean(
2388
                                _mem.dtmf_settings.mmonitor))
2389
        dtmf_dec_settings.append(line)
2390

    
2391
        line = RadioSetting("dtmf_settings.mstun",
2392
                            "Master Stun",
2393
                            RadioSettingValueBoolean(
2394
                                _mem.dtmf_settings.mstun))
2395
        dtmf_dec_settings.append(line)
2396

    
2397
        line = RadioSetting("dtmf_settings.mkill",
2398
                            "Master Kill",
2399
                            RadioSettingValueBoolean(
2400
                                _mem.dtmf_settings.mkill))
2401
        dtmf_dec_settings.append(line)
2402

    
2403
        line = RadioSetting("dtmf_settings.mrevive",
2404
                            "Master Revive",
2405
                            RadioSettingValueBoolean(
2406
                                _mem.dtmf_settings.mrevive))
2407
        dtmf_dec_settings.append(line)
2408

    
2409
        val = RadioSettingValueString(0, 16, memory2string(
2410
                                          _mem.dtmf_settings.viceid),
2411
                                      False, CHARSET_DTMF_DIGITS)
2412
        line = RadioSetting("dtmf_settings.viceid",
2413
                            "Vice Control ID ", val)
2414
        line.set_apply_callback(apply_dmtf_frame,
2415
                                _mem.dtmf_settings.viceid)
2416
        dtmf_dec_settings.append(line)
2417

    
2418
        line = RadioSetting("dtmf_settings.vinspection",
2419
                            "Vice Inspection",
2420
                            RadioSettingValueBoolean(
2421
                                _mem.dtmf_settings.vinspection))
2422
        dtmf_dec_settings.append(line)
2423

    
2424
        line = RadioSetting("dtmf_settings.vmonitor",
2425
                            "Vice Monitor",
2426
                            RadioSettingValueBoolean(
2427
                                _mem.dtmf_settings.vmonitor))
2428
        dtmf_dec_settings.append(line)
2429

    
2430
        line = RadioSetting("dtmf_settings.vstun",
2431
                            "Vice Stun",
2432
                            RadioSettingValueBoolean(
2433
                                _mem.dtmf_settings.vstun))
2434
        dtmf_dec_settings.append(line)
2435

    
2436
        line = RadioSetting("dtmf_settings.vkill",
2437
                            "Vice Kill",
2438
                            RadioSettingValueBoolean(
2439
                                _mem.dtmf_settings.vkill))
2440
        dtmf_dec_settings.append(line)
2441

    
2442
        line = RadioSetting("dtmf_settings.vrevive",
2443
                            "Vice Revive",
2444
                            RadioSettingValueBoolean(
2445
                                _mem.dtmf_settings.vrevive))
2446
        dtmf_dec_settings.append(line)
2447

    
2448
        val = RadioSettingValueString(0, 16, memory2string(
2449
                                          _mem.dtmf_settings.inspection),
2450
                                      False, CHARSET_DTMF_DIGITS)
2451
        line = RadioSetting("dtmf_settings.inspection",
2452
                            "Inspection", val)
2453
        line.set_apply_callback(apply_dmtf_frame,
2454
                                _mem.dtmf_settings.inspection)
2455
        dtmf_dec_settings.append(line)
2456

    
2457
        val = RadioSettingValueString(0, 16, memory2string(
2458
                                          _mem.dtmf_settings.alarmcode),
2459
                                      False, CHARSET_DTMF_DIGITS)
2460
        line = RadioSetting("dtmf_settings.alarmcode",
2461
                            "Alarm", val)
2462
        line.set_apply_callback(apply_dmtf_frame,
2463
                                _mem.dtmf_settings.alarmcode)
2464
        dtmf_dec_settings.append(line)
2465

    
2466
        val = RadioSettingValueString(0, 16, memory2string(
2467
                                          _mem.dtmf_settings.kill),
2468
                                      False, CHARSET_DTMF_DIGITS)
2469
        line = RadioSetting("dtmf_settings.kill",
2470
                            "Kill", val)
2471
        line.set_apply_callback(apply_dmtf_frame,
2472
                                _mem.dtmf_settings.kill)
2473
        dtmf_dec_settings.append(line)
2474

    
2475
        val = RadioSettingValueString(0, 16, memory2string(
2476
                                          _mem.dtmf_settings.monitor),
2477
                                      False, CHARSET_DTMF_DIGITS)
2478
        line = RadioSetting("dtmf_settings.monitor",
2479
                            "Monitor", val)
2480
        line.set_apply_callback(apply_dmtf_frame,
2481
                                _mem.dtmf_settings.monitor)
2482
        dtmf_dec_settings.append(line)
2483

    
2484
        val = RadioSettingValueString(0, 16, memory2string(
2485
                                          _mem.dtmf_settings.stun),
2486
                                      False, CHARSET_DTMF_DIGITS)
2487
        line = RadioSetting("dtmf_settings.stun",
2488
                            "Stun", val)
2489
        line.set_apply_callback(apply_dmtf_frame,
2490
                                _mem.dtmf_settings.stun)
2491
        dtmf_dec_settings.append(line)
2492

    
2493
        val = RadioSettingValueString(0, 16, memory2string(
2494
                                          _mem.dtmf_settings.revive),
2495
                                      False, CHARSET_DTMF_DIGITS)
2496
        line = RadioSetting("dtmf_settings.revive",
2497
                            "Revive", val)
2498
        line.set_apply_callback(apply_dmtf_frame,
2499
                                _mem.dtmf_settings.revive)
2500
        dtmf_dec_settings.append(line)
2501

    
2502
        def apply_dmtf_listvalue(setting, obj):
2503
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2504
            val = str(setting.value)
2505
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2506
            val = LIST_DTMF_SPECIAL_VALUES[index]
2507
            obj.set_value(val)
2508

    
2509
        if _mem.dtmf_settings.groupcode not in LIST_DTMF_SPECIAL_VALUES:
2510
            val = 0x0B
2511
        else:
2512
            val = _mem.dtmf_settings.groupcode
2513
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2514
        line = RadioSetting(
2515
            "dtmf_settings.groupcode",
2516
            "Group Code",
2517
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2518
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2519
        line.set_apply_callback(apply_dmtf_listvalue,
2520
                                _mem.dtmf_settings.groupcode)
2521
        dtmf_dec_settings.append(line)
2522

    
2523
        if _mem.dtmf_settings.spacecode not in LIST_DTMF_SPECIAL_VALUES:
2524
            val = 0x0C
2525
        else:
2526
            val = _mem.dtmf_settings.spacecode
2527
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2528
        line = RadioSetting(
2529
            "dtmf_settings.spacecode",
2530
            "Space Code",
2531
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2532
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2533
        line.set_apply_callback(apply_dmtf_listvalue,
2534
                                _mem.dtmf_settings.spacecode)
2535
        dtmf_dec_settings.append(line)
2536

    
2537
        if self.COLOR_LCD:
2538
            if _mem.dtmf_settings.resettime > 0x63:
2539
                val = 0x4F
2540
            else:
2541
                val = _mem.dtmf_settings.resettime
2542
            line = RadioSetting(
2543
                "dtmf_settings.resettime",
2544
                "Reset time",
2545
                RadioSettingValueList(LIST_5TONE_RESET_COLOR,
2546
                                      LIST_5TONE_RESET_COLOR[
2547
                                          val]))
2548
            dtmf_dec_settings.append(line)
2549
        else:
2550
            line = RadioSetting(
2551
                "dtmf_settings.resettime",
2552
                "Reset time",
2553
                RadioSettingValueList(LIST_5TONE_RESET,
2554
                                      LIST_5TONE_RESET[
2555
                                          _mem.dtmf_settings.resettime]))
2556
            dtmf_dec_settings.append(line)
2557

    
2558
        if _mem.dtmf_settings.delayproctime > 0x27:
2559
            val = 0x04
2560
        else:
2561
            val = _mem.dtmf_settings.delayproctime
2562
        line = RadioSetting(
2563
            "dtmf_settings.delayproctime",
2564
            "Delay processing time",
2565
            RadioSettingValueList(LIST_DTMF_DELAY,
2566
                                  LIST_DTMF_DELAY[
2567
                                      val]))
2568
        dtmf_dec_settings.append(line)
2569

    
2570
        # 5 Tone Settings
2571
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2572
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2573

    
2574
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2575
        group_5tone.append(stds_5tone)
2576
        group_5tone.append(codes_5tone)
2577

    
2578
        top.append(group_5tone)
2579

    
2580
        def apply_list_value(setting, obj):
2581
            options = setting.value.get_options()
2582
            obj.set_value(options.index(str(setting.value)))
2583

    
2584
        _5tone_standards = self._memobj._5tone_std_settings
2585
        i = 0
2586
        for standard in _5tone_standards:
2587
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2588
                                          LIST_5TONE_STANDARDS[i])
2589
            stds_5tone.append(std_5tone)
2590

    
2591
            period = standard.period
2592
            if period == 255:
2593
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2594
                          " is not yet configured. Setting to 70ms.")
2595
                period = 5
2596

    
2597
            if period <= len(LIST_5TONE_STANDARD_PERIODS):
2598
                line = RadioSetting(
2599
                    "_5tone_std_settings_" + str(i) + "_period",
2600
                    "Period (ms)", RadioSettingValueList
2601
                    (LIST_5TONE_STANDARD_PERIODS,
2602
                     LIST_5TONE_STANDARD_PERIODS[period]))
2603
                line.set_apply_callback(apply_list_value, standard.period)
2604
                std_5tone.append(line)
2605
            else:
2606
                LOG.debug("Invalid value for 5tone period! Disabling.")
2607

    
2608
            group_tone = standard.group_tone
2609
            if group_tone == 255:
2610
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2611
                          " is not yet configured. Setting to A.")
2612
                group_tone = 10
2613

    
2614
            if group_tone <= len(LIST_5TONE_DIGITS):
2615
                line = RadioSetting(
2616
                    "_5tone_std_settings_" + str(i) + "_grouptone",
2617
                    "Group Tone",
2618
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2619
                                          LIST_5TONE_DIGITS[
2620
                                              group_tone]))
2621
                line.set_apply_callback(apply_list_value,
2622
                                        standard.group_tone)
2623
                std_5tone.append(line)
2624
            else:
2625
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2626

    
2627
            repeat_tone = standard.repeat_tone
2628
            if repeat_tone == 255:
2629
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2630
                          " is not yet configured. Setting to E.")
2631
                repeat_tone = 14
2632

    
2633
            if repeat_tone <= len(LIST_5TONE_DIGITS):
2634
                line = RadioSetting(
2635
                    "_5tone_std_settings_" + str(i) + "_repttone",
2636
                    "Repeat Tone",
2637
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2638
                                          LIST_5TONE_DIGITS[
2639
                                              repeat_tone]))
2640
                line.set_apply_callback(apply_list_value,
2641
                                        standard.repeat_tone)
2642
                std_5tone.append(line)
2643
            else:
2644
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2645
            i = i + 1
2646

    
2647
        def my_apply_5tonestdlist_value(setting, obj):
2648
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2649
                obj.set_value(0xFF)
2650
            else:
2651
                obj.set_value(LIST_5TONE_STANDARDS.
2652
                              index(str(setting.value)))
2653

    
2654
        def apply_5tone_frame(setting, obj):
2655
            LOG.debug("Setting 5 Tone: " + str(setting.value))
2656
            valstring = str(setting.value)
2657
            if len(valstring) == 0:
2658
                for i in range(0, 5):
2659
                    obj[i] = 255
2660
            else:
2661
                validFrame = True
2662
                for i in range(0, 5):
2663
                    currentChar = valstring[i].upper()
2664
                    if currentChar in LIST_5TONE_DIGITS:
2665
                        obj[i] = LIST_5TONE_DIGITS.index(currentChar)
2666
                    else:
2667
                        validFrame = False
2668
                        LOG.debug("invalid char: " + str(currentChar))
2669
                if not validFrame:
2670
                    LOG.debug("setting whole frame to FF")
2671
                    for i in range(0, 5):
2672
                        obj[i] = 255
2673

    
2674
        def validate_5tone_frame(value):
2675
            if (len(str(value)) != 5) and (len(str(value)) != 0):
2676
                msg = ("5 Tone must have 5 digits or 0 digits")
2677
                raise InvalidValueError(msg)
2678
            for digit in str(value):
2679
                if digit.upper() not in LIST_5TONE_DIGITS:
2680
                    msg = (str(digit) + " is not a valid digit for 5tones")
2681
                    raise InvalidValueError(msg)
2682
            return value
2683

    
2684
        def frame2string(frame):
2685
            frameString = ""
2686
            for digit in frame:
2687
                if digit != 255:
2688
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2689
            return frameString
2690

    
2691
        _5tone_codes = self._memobj._5tone_codes
2692
        i = 1
2693
        for code in _5tone_codes:
2694
            code_5tone = RadioSettingGroup("code_5tone_" + str(i),
2695
                                           "5 Tone code " + str(i))
2696
            codes_5tone.append(code_5tone)
2697
            if (code.standard == 255):
2698
                currentVal = 15
2699
            else:
2700
                currentVal = code.standard
2701
            line = RadioSetting("_5tone_code_" + str(i) + "_std",
2702
                                " Standard",
2703
                                RadioSettingValueList(LIST_5TONE_STANDARDS,
2704
                                                      LIST_5TONE_STANDARDS[
2705
                                                          currentVal]))
2706
            line.set_apply_callback(my_apply_5tonestdlist_value,
2707
                                    code.standard)
2708
            code_5tone.append(line)
2709

    
2710
            val = RadioSettingValueString(0, 6,
2711
                                          frame2string(code.frame1), False)
2712
            line = RadioSetting("_5tone_code_" + str(i) + "_frame1",
2713
                                " Frame 1", val)
2714
            val.set_validate_callback(validate_5tone_frame)
2715
            line.set_apply_callback(apply_5tone_frame, code.frame1)
2716
            code_5tone.append(line)
2717

    
2718
            val = RadioSettingValueString(0, 6,
2719
                                          frame2string(code.frame2), False)
2720
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2721
                                " Frame 2", val)
2722
            val.set_validate_callback(validate_5tone_frame)
2723
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2724
            code_5tone.append(line)
2725

    
2726
            val = RadioSettingValueString(0, 6,
2727
                                          frame2string(code.frame3), False)
2728
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2729
                                " Frame 3", val)
2730
            val.set_validate_callback(validate_5tone_frame)
2731
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2732
            code_5tone.append(line)
2733
            i = i + 1
2734

    
2735
        _5_tone_decode1 = RadioSetting(
2736
            "_5tone_settings._5tone_decode_call_frame1",
2737
            "5 Tone decode call Frame 1",
2738
            RadioSettingValueBoolean(
2739
                _mem._5tone_settings._5tone_decode_call_frame1))
2740
        group_5tone.append(_5_tone_decode1)
2741

    
2742
        _5_tone_decode2 = RadioSetting(
2743
            "_5tone_settings._5tone_decode_call_frame2",
2744
            "5 Tone decode call Frame 2",
2745
            RadioSettingValueBoolean(
2746
                _mem._5tone_settings._5tone_decode_call_frame2))
2747
        group_5tone.append(_5_tone_decode2)
2748

    
2749
        _5_tone_decode3 = RadioSetting(
2750
            "_5tone_settings._5tone_decode_call_frame3",
2751
            "5 Tone decode call Frame 3",
2752
            RadioSettingValueBoolean(
2753
                _mem._5tone_settings._5tone_decode_call_frame3))
2754
        group_5tone.append(_5_tone_decode3)
2755

    
2756
        _5_tone_decode_disp1 = RadioSetting(
2757
            "_5tone_settings._5tone_decode_disp_frame1",
2758
            "5 Tone decode disp Frame 1",
2759
            RadioSettingValueBoolean(
2760
                _mem._5tone_settings._5tone_decode_disp_frame1))
2761
        group_5tone.append(_5_tone_decode_disp1)
2762

    
2763
        _5_tone_decode_disp2 = RadioSetting(
2764
            "_5tone_settings._5tone_decode_disp_frame2",
2765
            "5 Tone decode disp Frame 2",
2766
            RadioSettingValueBoolean(
2767
                _mem._5tone_settings._5tone_decode_disp_frame2))
2768
        group_5tone.append(_5_tone_decode_disp2)
2769

    
2770
        _5_tone_decode_disp3 = RadioSetting(
2771
            "_5tone_settings._5tone_decode_disp_frame3",
2772
            "5 Tone decode disp Frame 3",
2773
            RadioSettingValueBoolean(
2774
                _mem._5tone_settings._5tone_decode_disp_frame3))
2775
        group_5tone.append(_5_tone_decode_disp3)
2776

    
2777
        decode_standard = _mem._5tone_settings.decode_standard
2778
        if decode_standard == 255:
2779
            decode_standard = 0
2780
        if decode_standard <= len(LIST_5TONE_STANDARDS_without_none):
2781
            line = RadioSetting("_5tone_settings.decode_standard",
2782
                                "5 Tone-decode Standard",
2783
                                RadioSettingValueList(
2784
                                    LIST_5TONE_STANDARDS_without_none,
2785
                                    LIST_5TONE_STANDARDS_without_none[
2786
                                        decode_standard]))
2787
            group_5tone.append(line)
2788
        else:
2789
            LOG.debug("Invalid decode std...")
2790

    
2791
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2792
        if _5tone_delay1 == 255:
2793
            _5tone_delay1 = 20
2794

    
2795
        if _5tone_delay1 <= len(LIST_5TONE_DELAY):
2796
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2797
                                         LIST_5TONE_DELAY[
2798
                                             _5tone_delay1])
2799
            line = RadioSetting("_5tone_settings._5tone_delay1",
2800
                                "5 Tone Delay Frame 1", list)
2801
            group_5tone.append(line)
2802
        else:
2803
            LOG.debug("Invalid value for 5tone delay (frame1) ! Disabling.")
2804

    
2805
        _5tone_delay2 = _mem._5tone_settings._5tone_delay2
2806
        if _5tone_delay2 == 255:
2807
            _5tone_delay2 = 20
2808
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2809

    
2810
        if _5tone_delay2 <= len(LIST_5TONE_DELAY):
2811
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2812
                                         LIST_5TONE_DELAY[
2813
                                             _5tone_delay2])
2814
            line = RadioSetting("_5tone_settings._5tone_delay2",
2815
                                "5 Tone Delay Frame 2", list)
2816
            group_5tone.append(line)
2817
        else:
2818
            LOG.debug("Invalid value for 5tone delay (frame2)! Disabling.")
2819

    
2820
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2821
        if _5tone_delay3 == 255:
2822
            _5tone_delay3 = 20
2823
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2824

    
2825
        if _5tone_delay3 <= len(LIST_5TONE_DELAY):
2826
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2827
                                         LIST_5TONE_DELAY[
2828
                                             _5tone_delay3])
2829
            line = RadioSetting("_5tone_settings._5tone_delay3",
2830
                                "5 Tone Delay Frame 3", list)
2831
            group_5tone.append(line)
2832
        else:
2833
            LOG.debug("Invalid value for 5tone delay (frame3)! Disabling.")
2834

    
2835
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2836
        if ext_length == 255:
2837
            ext_length = 0
2838
            LOG.debug("1st Tone ext lenght unconfigured! Resetting to 0")
2839

    
2840
        if ext_length <= len(LIST_5TONE_DELAY):
2841
            list = RadioSettingValueList(
2842
                LIST_5TONE_DELAY,
2843
                LIST_5TONE_DELAY[
2844
                    ext_length])
2845
            line = RadioSetting(
2846
                "_5tone_settings._5tone_first_digit_ext_length",
2847
                "First digit extend length", list)
2848
            group_5tone.append(line)
2849
        else:
2850
            LOG.debug("Invalid value for 5tone ext length! Disabling.")
2851

    
2852
        decode_reset_time = _mem._5tone_settings.decode_reset_time
2853
        if decode_reset_time == 255:
2854
            decode_reset_time = 59
2855
            LOG.debug("Decode reset time unconfigured. resetting.")
2856
        if decode_reset_time <= len(LIST_5TONE_RESET):
2857
            list = RadioSettingValueList(
2858
                LIST_5TONE_RESET,
2859
                LIST_5TONE_RESET[
2860
                    decode_reset_time])
2861
            line = RadioSetting("_5tone_settings.decode_reset_time",
2862
                                "Decode reset time", list)
2863
            group_5tone.append(line)
2864
        else:
2865
            LOG.debug("Invalid value decode reset time! Disabling.")
2866

    
2867
        # 2 Tone
2868
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2869
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2870

    
2871
        top.append(encode_2tone)
2872
        top.append(decode_2tone)
2873

    
2874
        duration_1st_tone = self._memobj._2tone.duration_1st_tone
2875
        if duration_1st_tone == 255:
2876
            LOG.debug("Duration of first 2 Tone digit is not yet " +
2877
                      "configured. Setting to 600ms")
2878
            duration_1st_tone = 60
2879

    
2880
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2881
            line = RadioSetting("_2tone.duration_1st_tone",
2882
                                "Duration 1st Tone",
2883
                                RadioSettingValueList(LIST_5TONE_DELAY,
2884
                                                      LIST_5TONE_DELAY[
2885
                                                          duration_1st_tone]))
2886
            encode_2tone.append(line)
2887

    
2888
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2889
        if duration_2nd_tone == 255:
2890
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2891
                      "configured. Setting to 600ms")
2892
            duration_2nd_tone = 60
2893

    
2894
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2895
            line = RadioSetting("_2tone.duration_2nd_tone",
2896
                                "Duration 2nd Tone",
2897
                                RadioSettingValueList(LIST_5TONE_DELAY,
2898
                                                      LIST_5TONE_DELAY[
2899
                                                          duration_2nd_tone]))
2900
            encode_2tone.append(line)
2901

    
2902
        duration_gap = self._memobj._2tone.duration_gap
2903
        if duration_gap == 255:
2904
            LOG.debug("Duration of gap is not yet " +
2905
                      "configured. Setting to 300ms")
2906
            duration_gap = 30
2907

    
2908
        if duration_gap <= len(LIST_5TONE_DELAY):
2909
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2910
                                RadioSettingValueList(LIST_5TONE_DELAY,
2911
                                                      LIST_5TONE_DELAY[
2912
                                                          duration_gap]))
2913
            encode_2tone.append(line)
2914

    
2915
        def _2tone_validate(value):
2916
            if value == 0:
2917
                return 65535
2918
            if value == 65535:
2919
                return value
2920
            if not (300 <= value and value <= 3000):
2921
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
2922
                raise InvalidValueError(msg)
2923
            return value
2924

    
2925
        def apply_2tone_freq(setting, obj):
2926
            val = int(setting.value)
2927
            if (val == 0) or (val == 65535):
2928
                obj.set_value(65535)
2929
            else:
2930
                obj.set_value(val)
2931

    
2932
        i = 1
2933
        for code in self._memobj._2tone._2tone_encode:
2934
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
2935
                                           "Encode Code " + str(i))
2936
            encode_2tone.append(code_2tone)
2937

    
2938
            tmp = code.freq1
2939
            if tmp == 65535:
2940
                tmp = 0
2941
            val1 = RadioSettingValueInteger(0, 65535, tmp)
2942
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
2943
                                 "Frequency 1", val1)
2944
            val1.set_validate_callback(_2tone_validate)
2945
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
2946
            code_2tone.append(freq1)
2947

    
2948
            tmp = code.freq2
2949
            if tmp == 65535:
2950
                tmp = 0
2951
            val2 = RadioSettingValueInteger(0, 65535, tmp)
2952
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
2953
                                 "Frequency 2", val2)
2954
            val2.set_validate_callback(_2tone_validate)
2955
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
2956
            code_2tone.append(freq2)
2957

    
2958
            i = i + 1
2959

    
2960
        decode_reset_time = _mem._2tone.reset_time
2961
        if decode_reset_time == 255:
2962
            decode_reset_time = 59
2963
            LOG.debug("Decode reset time unconfigured. resetting.")
2964
        if decode_reset_time <= len(LIST_5TONE_RESET):
2965
            list = RadioSettingValueList(
2966
                LIST_5TONE_RESET,
2967
                LIST_5TONE_RESET[
2968
                    decode_reset_time])
2969
            line = RadioSetting("_2tone.reset_time",
2970
                                "Decode reset time", list)
2971
            decode_2tone.append(line)
2972
        else:
2973
            LOG.debug("Invalid value decode reset time! Disabling.")
2974

    
2975
        def apply_2tone_freq_pair(setting, obj):
2976
            val = int(setting.value)
2977
            derived_val = 65535
2978
            frqname = str(setting._name[-5:])
2979
            derivedname = "derived_from_" + frqname
2980

    
2981
            if (val == 0):
2982
                val = 65535
2983
                derived_val = 65535
2984
            else:
2985
                derived_val = int(round(2304000.0/val))
2986

    
2987
            obj[frqname].set_value(val)
2988
            obj[derivedname].set_value(derived_val)
2989

    
2990
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
2991
                      derivedname + ": " + str(derived_val))
2992

    
2993
        i = 1
2994
        for decode_code in self._memobj._2tone._2tone_decode:
2995
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
2996
                                                "Decode Code " + str(i))
2997
            decode_2tone.append(_2tone_dec_code)
2998

    
2999
            j = 1
3000
            for dec in decode_code.decs:
3001
                val = dec.dec
3002
                if val == 255:
3003
                    LOG.debug("Dec for Code " + str(i) + " Dec " + str(j) +
3004
                              " is not yet configured. Setting to 0.")
3005
                    val = 0
3006

    
3007
                if val <= len(LIST_2TONE_DEC):
3008
                    line = RadioSetting(
3009
                        "_2tone_dec_settings_" + str(i) + "_dec_" + str(j),
3010
                        "Dec " + str(j), RadioSettingValueList
3011
                        (LIST_2TONE_DEC,
3012
                         LIST_2TONE_DEC[val]))
3013
                    line.set_apply_callback(apply_list_value, dec.dec)
3014
                    _2tone_dec_code.append(line)
3015
                else:
3016
                    LOG.debug("Invalid value for 2tone dec! Disabling.")
3017

    
3018
                val = dec.response
3019
                if val == 255:
3020
                    LOG.debug("Response for Code " + str(i) + " Dec " +
3021
                              str(j) + " is not yet configured. Setting to 0.")
3022
                    val = 0
3023

    
3024
                if val <= len(LIST_2TONE_RESPONSE):
3025
                    line = RadioSetting(
3026
                        "_2tone_dec_settings_" + str(i) + "_resp_" + str(j),
3027
                        "Response " + str(j), RadioSettingValueList
3028
                        (LIST_2TONE_RESPONSE,
3029
                         LIST_2TONE_RESPONSE[val]))
3030
                    line.set_apply_callback(apply_list_value, dec.response)
3031
                    _2tone_dec_code.append(line)
3032
                else:
3033
                    LOG.debug("Invalid value for 2tone response! Disabling.")
3034

    
3035
                val = dec.alert
3036
                if val == 255:
3037
                    LOG.debug("Alert for Code " + str(i) + " Dec " + str(j) +
3038
                              " is not yet configured. Setting to 0.")
3039
                    val = 0
3040

    
3041
                if val <= len(PTTIDCODE_LIST):
3042
                    line = RadioSetting(
3043
                        "_2tone_dec_settings_" + str(i) + "_alert_" + str(j),
3044
                        "Alert " + str(j), RadioSettingValueList
3045
                        (PTTIDCODE_LIST,
3046
                         PTTIDCODE_LIST[val]))
3047
                    line.set_apply_callback(apply_list_value, dec.alert)
3048
                    _2tone_dec_code.append(line)
3049
                else:
3050
                    LOG.debug("Invalid value for 2tone alert! Disabling.")
3051
                j = j + 1
3052

    
3053
            freq = self._memobj._2tone.freqs[i-1]
3054
            for char in ['A', 'B', 'C', 'D']:
3055
                setting_name = "freq" + str(char)
3056

    
3057
                tmp = freq[setting_name]
3058
                if tmp == 65535:
3059
                    tmp = 0
3060
                if tmp != 0:
3061
                    expected = int(round(2304000.0/tmp))
3062
                    from_mem = freq["derived_from_" + setting_name]
3063
                    if expected != from_mem:
3064
                        LOG.error("Expected " + str(expected) +
3065
                                  " but read " + str(from_mem) +
3066
                                  ". Disabling 2Tone Decode Freqs!")
3067
                        break
3068
                val = RadioSettingValueInteger(0, 65535, tmp)
3069
                frq = RadioSetting("2tone_dec_" + str(i) + "_freq" + str(char),
3070
                                   ("Decode Frequency " + str(char)), val)
3071
                val.set_validate_callback(_2tone_validate)
3072
                frq.set_apply_callback(apply_2tone_freq_pair, freq)
3073
                _2tone_dec_code.append(frq)
3074

    
3075
            i = i + 1
3076

    
3077
        return top
3078

    
3079
    def set_settings(self, settings):
3080
        _settings = self._memobj.settings
3081
        for element in settings:
3082
            if not isinstance(element, RadioSetting):
3083
                if element.get_name() == "fm_preset":
3084
                    self._set_fm_preset(element)
3085
                else:
3086
                    self.set_settings(element)
3087
                    continue
3088
            else:
3089
                try:
3090
                    name = element.get_name()
3091
                    if "." in name:
3092
                        bits = name.split(".")
3093
                        obj = self._memobj
3094
                        for bit in bits[:-1]:
3095
                            if "/" in bit:
3096
                                bit, index = bit.split("/", 1)
3097
                                index = int(index)
3098
                                obj = getattr(obj, bit)[index]
3099
                            else:
3100
                                obj = getattr(obj, bit)
3101
                        setting = bits[-1]
3102
                    else:
3103
                        obj = _settings
3104
                        setting = element.get_name()
3105

    
3106
                    if element.has_apply_callback():
3107
                        LOG.debug("Using apply callback")
3108
                        element.run_apply_callback()
3109
                    elif element.value.get_mutable():
3110
                        LOG.debug("Setting %s = %s" % (setting, element.value))
3111
                        setattr(obj, setting, element.value)
3112
                except Exception, e:
3113
                    LOG.debug(element.get_name())
3114
                    raise
3115

    
3116
    @classmethod
3117
    def match_model(cls, filedata, filename):
3118
        match_size = False
3119
        match_model = False
3120

    
3121
        # testing the file data size
3122
        if len(filedata) == MEM_SIZE:
3123
            match_size = True
3124

    
3125
        # testing the firmware model fingerprint
3126
        match_model = model_match(cls, filedata)
3127

    
3128
        if match_size and match_model:
3129
            return True
3130
        else:
3131
            return False
3132

    
3133

    
3134
MEM_FORMAT = """
3135
#seekto 0x0000;
3136
struct {
3137
  lbcd rxfreq[4];
3138
  lbcd txfreq[4];
3139
  ul16 rxtone;
3140
  ul16 txtone;
3141
  u8 unknown0:4,
3142
     scode:4;
3143
  u8 unknown1:2,
3144
     spmute:2,
3145
     unknown2:2,
3146
     optsig:2;
3147
  u8 unknown3:3,
3148
     scramble:1,
3149
     unknown4:3,
3150
     power:1;
3151
  u8 unknown5:1,
3152
     wide:1,
3153
     unknown6:2,
3154
     bcl:1,
3155
     add:1,
3156
     pttid:2;
3157
} memory[200];
3158

    
3159
#seekto 0x0E00;
3160
struct {
3161
  u8 tdr;
3162
  u8 unknown1;
3163
  u8 sql;
3164
  u8 unknown2[2];
3165
  u8 tot;
3166
  u8 apo;           // BTech radios use this as the Auto Power Off time
3167
                    // other radios use this as pre-Time Out Alert
3168
  u8 unknown3;
3169
  u8 abr;
3170
  u8 beep;
3171
  u8 unknown4[4];
3172
  u8 dtmfst;
3173
  u8 unknown5[2];
3174
  u8 prisc;
3175
  u8 prich;
3176
  u8 screv;
3177
  u8 unknown6[2];
3178
  u8 pttid;
3179
  u8 pttlt;
3180
  u8 unknown7;
3181
  u8 emctp;
3182
  u8 emcch;
3183
  u8 ringt;
3184
  u8 unknown8;
3185
  u8 camdf;
3186
  u8 cbmdf;
3187
  u8 sync;          // BTech radios use this as the display sync setting
3188
                    // other radios use this as the auto keypad lock setting
3189
  u8 ponmsg;
3190
  u8 wtled;
3191
  u8 rxled;
3192
  u8 txled;
3193
  u8 unknown9[5];
3194
  u8 anil;
3195
  u8 reps;
3196
  u8 repm;
3197
  u8 tdrab;
3198
  u8 ste;
3199
  u8 rpste;
3200
  u8 rptdl;
3201
  u8 mgain;
3202
  u8 dtmfg;
3203
} settings;
3204

    
3205
#seekto 0x0E80;
3206
struct {
3207
  u8 unknown1;
3208
  u8 vfomr;
3209
  u8 keylock;
3210
  u8 unknown2;
3211
  u8 unknown3:4,
3212
     vfomren:1,
3213
     unknown4:1,
3214
     reseten:1,
3215
     menuen:1;
3216
  u8 unknown5[11];
3217
  u8 dispab;
3218
  u8 mrcha;
3219
  u8 mrchb;
3220
  u8 menu;
3221
} settings2;
3222

    
3223
#seekto 0x0EC0;
3224
struct {
3225
  char line1[6];
3226
  char line2[6];
3227
} poweron_msg;
3228

    
3229
struct settings_vfo {
3230
  u8 freq[8];
3231
  u8 offset[6];
3232
  u8 unknown2[2];
3233
  ul16 rxtone;
3234
  ul16 txtone;
3235
  u8 scode;
3236
  u8 spmute;
3237
  u8 optsig;
3238
  u8 scramble;
3239
  u8 wide;
3240
  u8 power;
3241
  u8 shiftd;
3242
  u8 step;
3243
  u8 unknown3[4];
3244
};
3245

    
3246
#seekto 0x0F00;
3247
struct {
3248
  struct settings_vfo a;
3249
  struct settings_vfo b;
3250
} vfo;
3251

    
3252
#seekto 0x1000;
3253
struct {
3254
  char name[6];
3255
  u8 unknown1[10];
3256
} names[200];
3257

    
3258
#seekto 0x2400;
3259
struct {
3260
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3261
  u8 group_tone;
3262
  u8 repeat_tone;
3263
  u8 unused[13];
3264
} _5tone_std_settings[15];
3265

    
3266
#seekto 0x2500;
3267
struct {
3268
  u8 frame1[5];
3269
  u8 frame2[5];
3270
  u8 frame3[5];
3271
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3272
} _5tone_codes[15];
3273

    
3274
#seekto 0x25F0;
3275
struct {
3276
  u8 _5tone_delay1; // * 10ms
3277
  u8 _5tone_delay2; // * 10ms
3278
  u8 _5tone_delay3; // * 10ms
3279
  u8 _5tone_first_digit_ext_length;
3280
  u8 unknown1;
3281
  u8 unknown2;
3282
  u8 unknown3;
3283
  u8 unknown4;
3284
  u8 decode_standard;
3285
  u8 unknown5:5,
3286
     _5tone_decode_call_frame3:1,
3287
     _5tone_decode_call_frame2:1,
3288
     _5tone_decode_call_frame1:1;
3289
  u8 unknown6:5,
3290
     _5tone_decode_disp_frame3:1,
3291
     _5tone_decode_disp_frame2:1,
3292
     _5tone_decode_disp_frame1:1;
3293
  u8 decode_reset_time; // * 100 + 100ms
3294
} _5tone_settings;
3295

    
3296
#seekto 0x2900;
3297
struct {
3298
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3299
} dtmf_codes[15];
3300

    
3301
#seekto 0x29F0;
3302
struct {
3303
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3304
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3305
  u8 unknown0[14];
3306
  u8 inspection[16];
3307
  u8 monitor[16];
3308
  u8 alarmcode[16];
3309
  u8 stun[16];
3310
  u8 kill[16];
3311
  u8 revive[16];
3312
  u8 unknown1[16];
3313
  u8 unknown2[16];
3314
  u8 unknown3[16];
3315
  u8 unknown4[16];
3316
  u8 unknown5[16];
3317
  u8 unknown6[16];
3318
  u8 unknown7[16];
3319
  u8 masterid[16];
3320
  u8 viceid[16];
3321
  u8 unused01:7,
3322
     mastervice:1;
3323
  u8 unused02:3,
3324
     mrevive:1,
3325
     mkill:1,
3326
     mstun:1,
3327
     mmonitor:1,
3328
     minspection:1;
3329
  u8 unused03:3,
3330
     vrevive:1,
3331
     vkill:1,
3332
     vstun:1,
3333
     vmonitor:1,
3334
     vinspection:1;
3335
  u8 unused04:6,
3336
     txdisable:1,
3337
     rxdisable:1;
3338
  u8 groupcode;
3339
  u8 spacecode;
3340
  u8 delayproctime; // * 100 + 100ms
3341
  u8 resettime;     // * 100 + 100ms
3342
} dtmf_settings;
3343

    
3344
#seekto 0x2D00;
3345
struct {
3346
  struct {
3347
    ul16 freq1;
3348
    u8 unused01[6];
3349
    ul16 freq2;
3350
    u8 unused02[6];
3351
  } _2tone_encode[15];
3352
  u8 duration_1st_tone; // *10ms
3353
  u8 duration_2nd_tone; // *10ms
3354
  u8 duration_gap;      // *10ms
3355
  u8 unused03[13];
3356
  struct {
3357
    struct {
3358
      u8 dec;      // one out of LIST_2TONE_DEC
3359
      u8 response; // one out of LIST_2TONE_RESPONSE
3360
      u8 alert;    // 1-16
3361
    } decs[4];
3362
    u8 unused04[4];
3363
  } _2tone_decode[15];
3364
  u8 unused05[16];
3365

    
3366
  struct {
3367
    ul16 freqA;
3368
    ul16 freqB;
3369
    ul16 freqC;
3370
    ul16 freqD;
3371
    // unknown what those values mean, but they are
3372
    // derived from configured frequencies
3373
    ul16 derived_from_freqA; // 2304000/freqA
3374
    ul16 derived_from_freqB; // 2304000/freqB
3375
    ul16 derived_from_freqC; // 2304000/freqC
3376
    ul16 derived_from_freqD; // 2304000/freqD
3377
  }freqs[15];
3378
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3379
} _2tone;
3380

    
3381
#seekto 0x3000;
3382
struct {
3383
  u8 freq[8];
3384
  char broadcast_station_name[6];
3385
  u8 unknown[2];
3386
} fm_radio_preset[16];
3387

    
3388
#seekto 0x3C90;
3389
struct {
3390
  u8 vhf_low[3];
3391
  u8 vhf_high[3];
3392
  u8 uhf_low[3];
3393
  u8 uhf_high[3];
3394
} ranges;
3395

    
3396
// the UV-2501+220 & KT8900R has different zones for storing ranges
3397

    
3398
#seekto 0x3CD0;
3399
struct {
3400
  u8 vhf_low[3];
3401
  u8 vhf_high[3];
3402
  u8 unknown1[4];
3403
  u8 unknown2[6];
3404
  u8 vhf2_low[3];
3405
  u8 vhf2_high[3];
3406
  u8 unknown3[4];
3407
  u8 unknown4[6];
3408
  u8 uhf_low[3];
3409
  u8 uhf_high[3];
3410
} ranges220;
3411

    
3412
#seekto 0x3F70;
3413
struct {
3414
  char fp[6];
3415
} fingerprint;
3416

    
3417
"""
3418

    
3419

    
3420
class BTech(BTechMobileCommon):
3421
    """BTECH's UV-5001 and alike radios"""
3422
    BANDS = 2
3423
    COLOR_LCD = False
3424
    NAME_LENGTH = 6
3425

    
3426
    def set_options(self):
3427
        """This is to read the options from the image and set it in the
3428
        environment, for now just the limits of the freqs in the VHF/UHF
3429
        ranges"""
3430

    
3431
        # setting the correct ranges for each radio type
3432
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3433
            # the model 2501+220 has a segment in 220
3434
            # and a different position in the memmap
3435
            # also the QYT KT8900R
3436
            ranges = self._memobj.ranges220
3437
        else:
3438
            ranges = self._memobj.ranges
3439

    
3440
        # the normal dual bands
3441
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3442
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3443

    
3444
        # DEBUG
3445
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3446
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3447

    
3448
        # 220Mhz radios case
3449
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3450
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3451
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3452
            self._220_range = vhf2
3453

    
3454
        # set the class with the real data
3455
        self._vhf_range = vhf
3456
        self._uhf_range = uhf
3457

    
3458
    def process_mmap(self):
3459
        """Process the mem map into the mem object"""
3460

    
3461
        # Get it
3462
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3463

    
3464
        # load specific parameters from the radio image
3465
        self.set_options()
3466

    
3467

    
3468
# Declaring Aliases (Clones of the real radios)
3469
class JT2705M(chirp_common.Alias):
3470
    VENDOR = "Jetstream"
3471
    MODEL = "JT2705M"
3472

    
3473

    
3474
class JT6188Mini(chirp_common.Alias):
3475
    VENDOR = "Juentai"
3476
    MODEL = "JT-6188 Mini"
3477

    
3478

    
3479
class JT6188Plus(chirp_common.Alias):
3480
    VENDOR = "Juentai"
3481
    MODEL = "JT-6188 Plus"
3482

    
3483

    
3484
class SSGT890(chirp_common.Alias):
3485
    VENDOR = "Sainsonic"
3486
    MODEL = "GT-890"
3487

    
3488

    
3489
class ZastoneMP300(chirp_common.Alias):
3490
    VENDOR = "Zastone"
3491
    MODEL = "MP-300"
3492

    
3493

    
3494
# real radios
3495
@directory.register
3496
class UV2501(BTech):
3497
    """Baofeng Tech UV2501"""
3498
    MODEL = "UV-2501"
3499
    _fileid = [UV2501G3_fp,
3500
               UV2501G2_fp,
3501
               UV2501pp2_fp,
3502
               UV2501pp_fp]
3503

    
3504

    
3505
@directory.register
3506
class UV2501_220(BTech):
3507
    """Baofeng Tech UV2501+220"""
3508
    MODEL = "UV-2501+220"
3509
    BANDS = 3
3510
    _magic = MSTRING_220
3511
    _id2 = [UV2501_220pp_id, ]
3512
    _fileid = [UV2501_220G3_fp,
3513
               UV2501_220G2_fp,
3514
               UV2501_220_fp,
3515
               UV2501_220pp_fp]
3516

    
3517

    
3518
@directory.register
3519
class UV5001(BTech):
3520
    """Baofeng Tech UV5001"""
3521
    MODEL = "UV-5001"
3522
    _fileid = [UV5001G3_fp,
3523
               UV5001G22_fp,
3524
               UV5001G2_fp,
3525
               UV5001alpha_fp,
3526
               UV5001pp_fp]
3527
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3528
                     chirp_common.PowerLevel("Low", watts=10)]
3529

    
3530

    
3531
@directory.register
3532
class MINI8900(BTech):
3533
    """WACCOM MINI-8900"""
3534
    VENDOR = "WACCOM"
3535
    MODEL = "MINI-8900"
3536
    _magic = MSTRING_MINI8900
3537
    _fileid = [MINI8900_fp, ]
3538
    # Clones
3539
    ALIASES = [JT6188Plus, ]
3540

    
3541

    
3542
@directory.register
3543
class KTUV980(BTech):
3544
    """QYT KT-UV980"""
3545
    VENDOR = "QYT"
3546
    MODEL = "KT-UV980"
3547
    _vhf_range = (136000000, 175000000)
3548
    _uhf_range = (400000000, 481000000)
3549
    _magic = MSTRING_MINI8900
3550
    _fileid = [KTUV980_fp, ]
3551
    # Clones
3552
    ALIASES = [JT2705M, ]
3553

    
3554
# Please note that there is a version of this radios that is a clone of the
3555
# Waccom Mini8900, maybe an early version?
3556

    
3557

    
3558
class OTGRadioV1(chirp_common.Alias):
3559
    VENDOR = 'OTGSTUFF'
3560
    MODEL = 'OTG Radio v1'
3561

    
3562

    
3563
@directory.register
3564
class KT9800(BTech):
3565
    """QYT KT8900"""
3566
    VENDOR = "QYT"
3567
    MODEL = "KT8900"
3568
    _vhf_range = (136000000, 175000000)
3569
    _uhf_range = (400000000, 481000000)
3570
    _magic = MSTRING_KT8900
3571
    _fileid = [KT8900_fp,
3572
               KT8900_fp1,
3573
               KT8900_fp2,
3574
               KT8900_fp3,
3575
               KT8900_fp4,
3576
               KT8900_fp5,
3577
               KT8900_fp6,
3578
               KT8900_fp7]
3579
    _id2 = [KT8900_id, KT8900_id2]
3580
    # Clones
3581
    ALIASES = [JT6188Mini, SSGT890, ZastoneMP300]
3582

    
3583

    
3584
@directory.register
3585
class KT9800R(BTech):
3586
    """QYT KT8900R"""
3587
    VENDOR = "QYT"
3588
    MODEL = "KT8900R"
3589
    BANDS = 3
3590
    _vhf_range = (136000000, 175000000)
3591
    _220_range = (240000000, 271000000)
3592
    _uhf_range = (400000000, 481000000)
3593
    _magic = MSTRING_KT8900R
3594
    _fileid = [KT8900R_fp,
3595
               KT8900R_fp1,
3596
               KT8900R_fp2,
3597
               KT8900R_fp3,
3598
               KT8900R_fp4]
3599
    _id2 = [KT8900R_id, KT8900R_id2]
3600

    
3601

    
3602
@directory.register
3603
class LT588UV(BTech):
3604
    """LUITON LT-588UV"""
3605
    VENDOR = "LUITON"
3606
    MODEL = "LT-588UV"
3607
    _vhf_range = (136000000, 175000000)
3608
    _uhf_range = (400000000, 481000000)
3609
    _magic = MSTRING_KT8900
3610
    _fileid = [LT588UV_fp,
3611
               LT588UV_fp1]
3612
    _power_levels = [chirp_common.PowerLevel("High", watts=60),
3613
                     chirp_common.PowerLevel("Low", watts=10)]
3614

    
3615

    
3616
COLOR_MEM_FORMAT = """
3617
#seekto 0x0000;
3618
struct {
3619
  lbcd rxfreq[4];
3620
  lbcd txfreq[4];
3621
  ul16 rxtone;
3622
  ul16 txtone;
3623
  u8 unknown0:4,
3624
     scode:4;
3625
  u8 unknown1:2,
3626
     spmute:2,
3627
     unknown2:2,
3628
     optsig:2;
3629
  u8 unknown3:3,
3630
     scramble:1,
3631
     unknown4:2,
3632
     power:2;
3633
  u8 unknown5:1,
3634
     wide:1,
3635
     unknown6:2,
3636
     bcl:1,
3637
     add:1,
3638
     pttid:2;
3639
} memory[200];
3640

    
3641
#seekto 0x0E00;
3642
struct {
3643
  u8 tmr;
3644
  u8 unknown1;
3645
  u8 sql;
3646
  u8 unknown2;
3647
  u8 mgain2;
3648
  u8 tot;
3649
  u8 apo;
3650
  u8 unknown3;
3651
  u8 abr;
3652
  u8 beep;
3653
  u8 unknown4[4];
3654
  u8 dtmfst;
3655
  u8 unknown5[2];
3656
  u8 screv;
3657
  u8 unknown6[2];
3658
  u8 pttid;
3659
  u8 pttlt;
3660
  u8 unknown7;
3661
  u8 emctp;
3662
  u8 emcch;
3663
  u8 sigbp;
3664
  u8 unknown8;
3665
  u8 camdf;
3666
  u8 cbmdf;
3667
  u8 ccmdf;
3668
  u8 cdmdf;
3669
  u8 langua;
3670
  u8 sync;          // BTech radios use this as the display sync
3671
                    // setting, other radios use this as the auto
3672
                    // keypad lock setting
3673
  u8 mainfc;
3674
  u8 mainbc;
3675
  u8 menufc;
3676
  u8 menubc;
3677
  u8 stafc;
3678
  u8 stabc;
3679
  u8 sigfc;
3680
  u8 sigbc;
3681
  u8 rxfc;
3682
  u8 txfc;
3683
  u8 txdisp;
3684
  u8 unknown9[5];
3685
  u8 anil;
3686
  u8 reps;
3687
  u8 repm;
3688
  u8 tmrmr;
3689
  u8 ste;
3690
  u8 rpste;
3691
  u8 rptdl;
3692
  u8 dtmfg;
3693
  u8 mgain;         // used by db25-g for ponyey
3694
  u8 skiptx;
3695
  u8 scmode;
3696
} settings;
3697

    
3698
#seekto 0x0E80;
3699
struct {
3700
  u8 unknown1;
3701
  u8 vfomr;
3702
  u8 keylock;
3703
  u8 unknown2;
3704
  u8 unknown3:4,
3705
     vfomren:1,
3706
     unknown4:1,
3707
     reseten:1,
3708
     menuen:1;
3709
  u8 unknown5[11];
3710
  u8 dispab;
3711
  u8 unknown6[2];
3712
  u8 menu;
3713
  u8 unknown7[7];
3714
  u8 vfomra;
3715
  u8 vfomrb;
3716
  u8 vfomrc;
3717
  u8 vfomrd;
3718
  u8 mrcha;
3719
  u8 mrchb;
3720
  u8 mrchc;
3721
  u8 mrchd;
3722
} settings2;
3723

    
3724
struct settings_vfo {
3725
  u8 freq[8];
3726
  u8 offset[6];
3727
  u8 unknown2[2];
3728
  ul16 rxtone;
3729
  ul16 txtone;
3730
  u8 scode;
3731
  u8 spmute;
3732
  u8 optsig;
3733
  u8 scramble;
3734
  u8 wide;
3735
  u8 power;
3736
  u8 shiftd;
3737
  u8 step;
3738
  u8 unknown3[4];
3739
};
3740

    
3741
#seekto 0x0F00;
3742
struct {
3743
  struct settings_vfo a;
3744
  struct settings_vfo b;
3745
  struct settings_vfo c;
3746
  struct settings_vfo d;
3747
} vfo;
3748

    
3749
#seekto 0x0F80;
3750
struct {
3751
  char line1[8];
3752
  char line2[8];
3753
  char line3[8];
3754
  char line4[8];
3755
  char line5[8];
3756
  char line6[8];
3757
  char line7[8];
3758
  char line8[8];
3759
} poweron_msg;
3760

    
3761
#seekto 0x1000;
3762
struct {
3763
  char name[8];
3764
  u8 unknown1[8];
3765
} names[200];
3766

    
3767
#seekto 0x2400;
3768
struct {
3769
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3770
  u8 group_tone;
3771
  u8 repeat_tone;
3772
  u8 unused[13];
3773
} _5tone_std_settings[15];
3774

    
3775
#seekto 0x2500;
3776
struct {
3777
  u8 frame1[5];
3778
  u8 frame2[5];
3779
  u8 frame3[5];
3780
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3781
} _5tone_codes[15];
3782

    
3783
#seekto 0x25F0;
3784
struct {
3785
  u8 _5tone_delay1; // * 10ms
3786
  u8 _5tone_delay2; // * 10ms
3787
  u8 _5tone_delay3; // * 10ms
3788
  u8 _5tone_first_digit_ext_length;
3789
  u8 unknown1;
3790
  u8 unknown2;
3791
  u8 unknown3;
3792
  u8 unknown4;
3793
  u8 decode_standard;
3794
  u8 unknown5:5,
3795
     _5tone_decode_call_frame3:1,
3796
     _5tone_decode_call_frame2:1,
3797
     _5tone_decode_call_frame1:1;
3798
  u8 unknown6:5,
3799
     _5tone_decode_disp_frame3:1,
3800
     _5tone_decode_disp_frame2:1,
3801
     _5tone_decode_disp_frame1:1;
3802
  u8 decode_reset_time; // * 100 + 100ms
3803
} _5tone_settings;
3804

    
3805
#seekto 0x2900;
3806
struct {
3807
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3808
} dtmf_codes[15];
3809

    
3810
#seekto 0x29F0;
3811
struct {
3812
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3813
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3814
  u8 unknown0[14];
3815
  u8 inspection[16];
3816
  u8 monitor[16];
3817
  u8 alarmcode[16];
3818
  u8 stun[16];
3819
  u8 kill[16];
3820
  u8 revive[16];
3821
  u8 unknown1[16];
3822
  u8 unknown2[16];
3823
  u8 unknown3[16];
3824
  u8 unknown4[16];
3825
  u8 unknown5[16];
3826
  u8 unknown6[16];
3827
  u8 unknown7[16];
3828
  u8 masterid[16];
3829
  u8 viceid[16];
3830
  u8 unused01:7,
3831
     mastervice:1;
3832
  u8 unused02:3,
3833
     mrevive:1,
3834
     mkill:1,
3835
     mstun:1,
3836
     mmonitor:1,
3837
     minspection:1;
3838
  u8 unused03:3,
3839
     vrevive:1,
3840
     vkill:1,
3841
     vstun:1,
3842
     vmonitor:1,
3843
     vinspection:1;
3844
  u8 unused04:6,
3845
     txdisable:1,
3846
     rxdisable:1;
3847
  u8 groupcode;
3848
  u8 spacecode;
3849
  u8 delayproctime; // * 100 + 100ms
3850
  u8 resettime;     // * 100 + 100ms
3851
} dtmf_settings;
3852

    
3853
#seekto 0x2D00;
3854
struct {
3855
  struct {
3856
    ul16 freq1;
3857
    u8 unused01[6];
3858
    ul16 freq2;
3859
    u8 unused02[6];
3860
  } _2tone_encode[15];
3861
  u8 duration_1st_tone; // *10ms
3862
  u8 duration_2nd_tone; // *10ms
3863
  u8 duration_gap;      // *10ms
3864
  u8 unused03[13];
3865
  struct {
3866
    struct {
3867
      u8 dec;      // one out of LIST_2TONE_DEC
3868
      u8 response; // one out of LIST_2TONE_RESPONSE
3869
      u8 alert;    // 1-16
3870
    } decs[4];
3871
    u8 unused04[4];
3872
  } _2tone_decode[15];
3873
  u8 unused05[16];
3874

    
3875
  struct {
3876
    ul16 freqA;
3877
    ul16 freqB;
3878
    ul16 freqC;
3879
    ul16 freqD;
3880
    // unknown what those values mean, but they are
3881
    // derived from configured frequencies
3882
    ul16 derived_from_freqA; // 2304000/freqA
3883
    ul16 derived_from_freqB; // 2304000/freqB
3884
    ul16 derived_from_freqC; // 2304000/freqC
3885
    ul16 derived_from_freqD; // 2304000/freqD
3886
  }freqs[15];
3887
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3888
} _2tone;
3889

    
3890
#seekto 0x3D80;
3891
struct {
3892
  u8 vhf_low[3];
3893
  u8 vhf_high[3];
3894
  u8 unknown1[4];
3895
  u8 unknown2[6];
3896
  u8 vhf2_low[3];
3897
  u8 vhf2_high[3];
3898
  u8 unknown3[4];
3899
  u8 unknown4[6];
3900
  u8 uhf_low[3];
3901
  u8 uhf_high[3];
3902
  u8 unknown5[4];
3903
  u8 unknown6[6];
3904
  u8 uhf2_low[3];
3905
  u8 uhf2_high[3];
3906
} ranges;
3907

    
3908
#seekto 0x3F70;
3909
struct {
3910
  char fp[6];
3911
} fingerprint;
3912

    
3913
"""
3914

    
3915

    
3916
class BTechColor(BTechMobileCommon):
3917
    """BTECH's Color LCD Mobile and alike radios"""
3918
    COLOR_LCD = True
3919
    NAME_LENGTH = 8
3920
    LIST_TMR = LIST_TMR16
3921

    
3922
    def process_mmap(self):
3923
        """Process the mem map into the mem object"""
3924

    
3925
        # Get it
3926
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
3927

    
3928
        # load specific parameters from the radio image
3929
        self.set_options()
3930

    
3931
    def set_options(self):
3932
        """This is to read the options from the image and set it in the
3933
        environment, for now just the limits of the freqs in the VHF/UHF
3934
        ranges"""
3935

    
3936
        # setting the correct ranges for each radio type
3937
        ranges = self._memobj.ranges
3938

    
3939
        # the normal dual bands
3940
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3941
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3942

    
3943
        # DEBUG
3944
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3945
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3946

    
3947
        # the additional bands
3948
        if self.MODEL in ["UV-25X4", "KT7900D"]:
3949
            # 200Mhz band
3950
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3951
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3952
            self._220_range = vhf2
3953

    
3954
            # 350Mhz band
3955
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
3956
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
3957
            self._350_range = uhf2
3958

    
3959
        # set the class with the real data
3960
        self._vhf_range = vhf
3961
        self._uhf_range = uhf
3962

    
3963

    
3964
# Declaring Aliases (Clones of the real radios)
3965
class SKT8900D(chirp_common.Alias):
3966
    VENDOR = "Surecom"
3967
    MODEL = "S-KT8900D"
3968

    
3969

    
3970
class QB25(chirp_common.Alias):
3971
    VENDOR = "Radioddity"
3972
    MODEL = "QB25"
3973

    
3974

    
3975
# real radios
3976
@directory.register
3977
class UV25X2(BTechColor):
3978
    """Baofeng Tech UV25X2"""
3979
    MODEL = "UV-25X2"
3980
    BANDS = 2
3981
    _vhf_range = (130000000, 180000000)
3982
    _uhf_range = (400000000, 521000000)
3983
    _magic = MSTRING_UV25X2
3984
    _fileid = [UV25X2_fp, ]
3985

    
3986

    
3987
@directory.register
3988
class UV25X4(BTechColor):
3989
    """Baofeng Tech UV25X4"""
3990
    MODEL = "UV-25X4"
3991
    BANDS = 4
3992
    _vhf_range = (130000000, 180000000)
3993
    _220_range = (200000000, 271000000)
3994
    _uhf_range = (400000000, 521000000)
3995
    _350_range = (350000000, 391000000)
3996
    _magic = MSTRING_UV25X4
3997
    _fileid = [UV25X4_fp, ]
3998

    
3999

    
4000
@directory.register
4001
class UV50X2(BTechColor):
4002
    """Baofeng Tech UV50X2"""
4003
    MODEL = "UV-50X2"
4004
    BANDS = 2
4005
    _vhf_range = (130000000, 180000000)
4006
    _uhf_range = (400000000, 521000000)
4007
    _magic = MSTRING_UV25X2
4008
    _fileid = [UV50X2_fp, ]
4009
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4010
                     chirp_common.PowerLevel("Low", watts=10)]
4011

    
4012

    
4013
@directory.register
4014
class KT7900D(BTechColor):
4015
    """QYT KT7900D"""
4016
    VENDOR = "QYT"
4017
    MODEL = "KT7900D"
4018
    BANDS = 4
4019
    LIST_TMR = LIST_TMR15
4020
    _vhf_range = (136000000, 175000000)
4021
    _220_range = (200000000, 271000000)
4022
    _uhf_range = (400000000, 481000000)
4023
    _350_range = (350000000, 371000000)
4024
    _magic = MSTRING_KT8900D
4025
    _fileid = [KT7900D_fp, KT7900D_fp1, KT7900D_fp2, KT7900D_fp3, KT7900D_fp4,
4026
               KT7900D_fp5, KT7900D_fp6, KT7900D_fp7, QB25_fp, ]
4027
    # Clones
4028
    ALIASES = [SKT8900D, QB25, ]
4029

    
4030

    
4031
@directory.register
4032
class KT8900D(BTechColor):
4033
    """QYT KT8900D"""
4034
    VENDOR = "QYT"
4035
    MODEL = "KT8900D"
4036
    BANDS = 2
4037
    LIST_TMR = LIST_TMR15
4038
    _vhf_range = (136000000, 175000000)
4039
    _uhf_range = (400000000, 481000000)
4040
    _magic = MSTRING_KT8900D
4041
    _fileid = [KT8900D_fp3, KT8900D_fp2, KT8900D_fp1, KT8900D_fp]
4042

    
4043
    # Clones
4044
    ALIASES = [OTGRadioV1]
4045

    
4046

    
4047
@directory.register
4048
class KT5800(BTechColor):
4049
    """QYT KT5800"""
4050
    VENDOR = "QYT"
4051
    MODEL = "KT5800"
4052
    BANDS = 2
4053
    LIST_TMR = LIST_TMR15
4054
    _vhf_range = (136000000, 175000000)
4055
    _uhf_range = (400000000, 481000000)
4056
    _magic = MSTRING_KT8900D
4057
    _fileid = [KT5800_fp, ]
4058

    
4059

    
4060
@directory.register
4061
class KT980PLUS(BTechColor):
4062
    """QYT KT980PLUS"""
4063
    VENDOR = "QYT"
4064
    MODEL = "KT980PLUS"
4065
    BANDS = 2
4066
    LIST_TMR = LIST_TMR15
4067
    _vhf_range = (136000000, 175000000)
4068
    _uhf_range = (400000000, 481000000)
4069
    _magic = MSTRING_KT8900D
4070
    _fileid = [KT980PLUS_fp1, KT980PLUS_fp]
4071
    _power_levels = [chirp_common.PowerLevel("High", watts=75),
4072
                     chirp_common.PowerLevel("Low", watts=55)]
4073

    
4074
    @classmethod
4075
    def match_model(cls, filedata, filename):
4076
        # This model is only ever matched via metadata
4077
        return False
4078

    
4079

    
4080
@directory.register
4081
class DB25G(BTechColor):
4082
    """Radioddity DB25-G"""
4083
    VENDOR = "Radioddity"
4084
    MODEL = "DB25-G"
4085
    BANDS = 2
4086
    LIST_TMR = LIST_TMR15
4087
    _vhf_range = (136000000, 175000000)
4088
    _uhf_range = (400000000, 481000000)
4089
    _magic = MSTRING_KT8900D
4090
    _fileid = [DB25G_fp1, DB25G_fp]
4091
    _gmrs = True
4092
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
4093
                     chirp_common.PowerLevel("Mid", watts=15),
4094
                     chirp_common.PowerLevel("Low", watts=5)]
4095

    
4096
    @classmethod
4097
    def match_model(cls, filedata, filename):
4098
        # This model is only ever matched via metadata
4099
        return False
4100

    
4101

    
4102
GMRS_MEM_FORMAT = """
4103
#seekto 0x0000;
4104
struct {
4105
  lbcd rxfreq[4];
4106
  lbcd txfreq[4];
4107
  ul16 rxtone;
4108
  ul16 txtone;
4109
  u8 unknown0:4,
4110
     scode:4;
4111
  u8 unknown1:2,
4112
     spmute:2,
4113
     unknown2:2,
4114
     optsig:2;
4115
  u8 unknown3:3,
4116
     scramble:1,
4117
     unknown4:2,
4118
     power:2;
4119
  u8 unknown5:1,
4120
     wide:1,
4121
     unknown6:2,
4122
     bcl:1,
4123
     add:1,
4124
     pttid:2;
4125
} memory[256];
4126

    
4127
#seekto 0x1000;
4128
struct {
4129
  char name[7];
4130
  u8 unknown1[9];
4131
} names[256];
4132

    
4133
#seekto 0x2400;
4134
struct {
4135
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4136
  u8 group_tone;
4137
  u8 repeat_tone;
4138
  u8 unused[13];
4139
} _5tone_std_settings[15];
4140

    
4141
#seekto 0x2500;
4142
struct {
4143
  u8 frame1[5];
4144
  u8 frame2[5];
4145
  u8 frame3[5];
4146
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4147
} _5tone_codes[15];
4148

    
4149
#seekto 0x25F0;
4150
struct {
4151
  u8 _5tone_delay1; // * 10ms
4152
  u8 _5tone_delay2; // * 10ms
4153
  u8 _5tone_delay3; // * 10ms
4154
  u8 _5tone_first_digit_ext_length;
4155
  u8 unknown1;
4156
  u8 unknown2;
4157
  u8 unknown3;
4158
  u8 unknown4;
4159
  u8 decode_standard;
4160
  u8 unknown5:5,
4161
     _5tone_decode_call_frame3:1,
4162
     _5tone_decode_call_frame2:1,
4163
     _5tone_decode_call_frame1:1;
4164
  u8 unknown6:5,
4165
     _5tone_decode_disp_frame3:1,
4166
     _5tone_decode_disp_frame2:1,
4167
     _5tone_decode_disp_frame1:1;
4168
  u8 decode_reset_time; // * 100 + 100ms
4169
} _5tone_settings;
4170

    
4171
#seekto 0x2900;
4172
struct {
4173
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4174
} dtmf_codes[15];
4175

    
4176
#seekto 0x29F0;
4177
struct {
4178
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4179
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4180
  u8 unknown0[14];
4181
  u8 inspection[16];
4182
  u8 monitor[16];
4183
  u8 alarmcode[16];
4184
  u8 stun[16];
4185
  u8 kill[16];
4186
  u8 revive[16];
4187
  u8 unknown1[16];
4188
  u8 unknown2[16];
4189
  u8 unknown3[16];
4190
  u8 unknown4[16];
4191
  u8 unknown5[16];
4192
  u8 unknown6[16];
4193
  u8 unknown7[16];
4194
  u8 masterid[16];
4195
  u8 viceid[16];
4196
  u8 unused01:7,
4197
     mastervice:1;
4198
  u8 unused02:3,
4199
     mrevive:1,
4200
     mkill:1,
4201
     mstun:1,
4202
     mmonitor:1,
4203
     minspection:1;
4204
  u8 unused03:3,
4205
     vrevive:1,
4206
     vkill:1,
4207
     vstun:1,
4208
     vmonitor:1,
4209
     vinspection:1;
4210
  u8 unused04:6,
4211
     txdisable:1,
4212
     rxdisable:1;
4213
  u8 groupcode;
4214
  u8 spacecode;
4215
  u8 delayproctime; // * 100 + 100ms
4216
  u8 resettime;     // * 100 + 100ms
4217
} dtmf_settings;
4218

    
4219
#seekto 0x2D00;
4220
struct {
4221
  struct {
4222
    ul16 freq1;
4223
    u8 unused01[6];
4224
    ul16 freq2;
4225
    u8 unused02[6];
4226
  } _2tone_encode[15];
4227
  u8 duration_1st_tone; // *10ms
4228
  u8 duration_2nd_tone; // *10ms
4229
  u8 duration_gap;      // *10ms
4230
  u8 unused03[13];
4231
  struct {
4232
    struct {
4233
      u8 dec;      // one out of LIST_2TONE_DEC
4234
      u8 response; // one out of LIST_2TONE_RESPONSE
4235
      u8 alert;    // 1-16
4236
    } decs[4];
4237
    u8 unused04[4];
4238
  } _2tone_decode[15];
4239
  u8 unused05[16];
4240

    
4241
  struct {
4242
    ul16 freqA;
4243
    ul16 freqB;
4244
    ul16 freqC;
4245
    ul16 freqD;
4246
    // unknown what those values mean, but they are
4247
    // derived from configured frequencies
4248
    ul16 derived_from_freqA; // 2304000/freqA
4249
    ul16 derived_from_freqB; // 2304000/freqB
4250
    ul16 derived_from_freqC; // 2304000/freqC
4251
    ul16 derived_from_freqD; // 2304000/freqD
4252
  }freqs[15];
4253
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4254
} _2tone;
4255

    
4256
#seekto 0x3000;
4257
struct {
4258
  u8 freq[8];
4259
  char broadcast_station_name[6];
4260
  u8 unknown[2];
4261
} fm_radio_preset[16];
4262

    
4263
#seekto 0x3200;
4264
struct {
4265
  u8 tmr;
4266
  u8 unknown1;
4267
  u8 sql;
4268
  u8 unknown2;
4269
  u8 autolk;
4270
  u8 tot;
4271
  u8 apo;
4272
  u8 unknown3;
4273
  u8 abr;
4274
  u8 beep;
4275
  u8 unknown4[4];
4276
  u8 dtmfst;
4277
  u8 unknown5[2];
4278
  u8 screv;
4279
  u8 unknown6[2];
4280
  u8 pttid;
4281
  u8 pttlt;
4282
  u8 unknown7;
4283
  u8 emctp;
4284
  u8 emcch;
4285
  u8 sigbp;
4286
  u8 unknown8;
4287
  u8 camdf;
4288
  u8 cbmdf;
4289
  u8 ccmdf;
4290
  u8 cdmdf;
4291
  u8 langua;
4292
  u8 sync;
4293

    
4294

    
4295
  u8 stfc;
4296
  u8 mffc;
4297
  u8 sfafc;
4298
  u8 sfbfc;
4299
  u8 sfcfc;
4300
  u8 sfdfc;
4301
  u8 subfc;
4302
  u8 fmfc;
4303
  u8 sigfc;
4304
  u8 modfc;
4305
  u8 menufc;
4306
  u8 txfc;
4307
  u8 txdisp;
4308
  u8 unknown9[5];
4309
  u8 anil;
4310
  u8 reps;
4311
  u8 repm;
4312
  u8 tmrmr;
4313
  u8 ste;
4314
  u8 rpste;
4315
  u8 rptdl;
4316
  u8 dtmfg;
4317
  u8 mgain;
4318
  u8 skiptx;
4319
  u8 scmode;
4320
} settings;
4321

    
4322
#seekto 0x3280;
4323
struct {
4324
  u8 unknown1;
4325
  u8 vfomr;
4326
  u8 keylock;
4327
  u8 unknown2;
4328
  u8 unknown3:4,
4329
     vfomren:1,
4330
     unknown4:1,
4331
     reseten:1,
4332
     menuen:1;
4333
  u8 unknown5[11];
4334
  u8 dispab;
4335
  u8 unknown6[2];
4336
  u8 smenu;
4337
  u8 unknown7[7];
4338
  u8 vfomra;
4339
  u8 vfomrb;
4340
  u8 vfomrc;
4341
  u8 vfomrd;
4342
  u8 mrcha;
4343
  u8 mrchb;
4344
  u8 mrchc;
4345
  u8 mrchd;
4346
} settings2;
4347

    
4348
struct settings_vfo {
4349
  u8 freq[8];
4350
  u8 offset[6];
4351
  u8 unknown2[2];
4352
  ul16 rxtone;
4353
  ul16 txtone;
4354
  u8 scode;
4355
  u8 spmute;
4356
  u8 optsig;
4357
  u8 scramble;
4358
  u8 wide;
4359
  u8 power;
4360
  u8 shiftd;
4361
  u8 step;
4362
  u8 unknown3[4];
4363
};
4364

    
4365
#seekto 0x3300;
4366
struct {
4367
  struct settings_vfo a;
4368
  struct settings_vfo b;
4369
  struct settings_vfo c;
4370
  struct settings_vfo d;
4371
} vfo;
4372

    
4373
#seekto 0x3D80;
4374
struct {
4375
  u8 vhf_low[3];
4376
  u8 vhf_high[3];
4377
  u8 unknown1[4];
4378
  u8 unknown2[6];
4379
  u8 vhf2_low[3];
4380
  u8 vhf2_high[3];
4381
  u8 unknown3[4];
4382
  u8 unknown4[6];
4383
  u8 uhf_low[3];
4384
  u8 uhf_high[3];
4385
  u8 unknown5[4];
4386
  u8 unknown6[6];
4387
  u8 uhf2_low[3];
4388
  u8 uhf2_high[3];
4389
} ranges;
4390

    
4391
#seekto 0x33B0;
4392
struct {
4393
  char line[16];
4394
} static_msg;
4395

    
4396
#seekto 0x3F70;
4397
struct {
4398
  char fp[6];
4399
} fingerprint;
4400

    
4401
"""
4402

    
4403

    
4404
class BTechGMRS(BTechMobileCommon):
4405
    """BTECH's GMRS Mobile"""
4406
    COLOR_LCD = True
4407
    COLOR_LCD2 = True
4408
    NAME_LENGTH = 7
4409
    UPLOAD_MEM_SIZE = 0X3400
4410

    
4411
    def process_mmap(self):
4412
        """Process the mem map into the mem object"""
4413

    
4414
        # Get it
4415
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
4416

    
4417
        # load specific parameters from the radio image
4418
        self.set_options()
4419

    
4420
    def set_options(self):
4421
        """This is to read the options from the image and set it in the
4422
        environment, for now just the limits of the freqs in the VHF/UHF
4423
        ranges"""
4424

    
4425
        # setting the correct ranges for each radio type
4426
        ranges = self._memobj.ranges
4427

    
4428
        # the normal dual bands
4429
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4430
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4431

    
4432
        # DEBUG
4433
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4434
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4435

    
4436
        # set the class with the real data
4437
        self._vhf_range = vhf
4438
        self._uhf_range = uhf
4439

    
4440

    
4441
# real radios
4442
@directory.register
4443
class GMRS50X1(BTechGMRS):
4444
    """Baofeng Tech GMRS50X1"""
4445
    MODEL = "GMRS-50X1"
4446
    BANDS = 2
4447
    LIST_TMR = LIST_TMR16
4448
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4449
                     chirp_common.PowerLevel("Mid", watts=10),
4450
                     chirp_common.PowerLevel("Low", watts=5)]
4451
    _vhf_range = (136000000, 175000000)
4452
    _uhf_range = (400000000, 521000000)
4453
    _upper = 255
4454
    _magic = MSTRING_GMRS50X1
4455
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
4456

    
4457

    
4458
COLORHT_MEM_FORMAT = """
4459
#seekto 0x0000;
4460
struct {
4461
  lbcd rxfreq[4];
4462
  lbcd txfreq[4];
4463
  ul16 rxtone;
4464
  ul16 txtone;
4465
  u8 unknown0:4,
4466
     scode:4;
4467
  u8 unknown1:2,
4468
     spmute:2,
4469
     unknown2:2,
4470
     optsig:2;
4471
  u8 unknown3:3,
4472
     scramble:1,
4473
     unknown4:3,
4474
     power:1;
4475
  u8 unknown5:1,
4476
     wide:1,
4477
     unknown6:2,
4478
     bcl:1,
4479
     add:1,
4480
     pttid:2;
4481
} memory[200];
4482

    
4483
#seekto 0x0E00;
4484
struct {
4485
  u8 tmr;
4486
  u8 unknownE01;
4487
  u8 sql;
4488
  u8 unknownE03[2];
4489
  u8 tot;
4490
  u8 save;
4491
  u8 unknownE07;
4492
  u8 abr;
4493
  u8 beep;
4494
  u8 unknownE0A[4];
4495
  u8 dsub;
4496
  u8 dtmfst;
4497
  u8 screv;
4498
  u8 unknownE11[3];
4499
  u8 pttid;
4500
  u8 unknownE15;
4501
  u8 pttlt;
4502
  u8 unknownE17;
4503
  u8 emctp;
4504
  u8 emcch;
4505
  u8 sigbp;
4506
  u8 unknownE1B;
4507
  u8 camdf;
4508
  u8 cbmdf;
4509
  u8 ccmdf;
4510
  u8 cdmdf;
4511
  u8 langua;
4512
  u8 voice;
4513
  u8 vox;
4514
  u8 voxt;
4515
  u8 sync;          // BTech radios use this as the display sync setting
4516
                    // other radios use this as the auto keypad lock setting
4517
  u8 stfc;
4518
  u8 mffc;
4519
  u8 sfafc;
4520
  u8 sfbfc;
4521
  u8 sfcfc;
4522
  u8 sfdfc;
4523
  u8 subfc;
4524
  u8 fmfc;
4525
  u8 sigfc;
4526
  u8 menufc;
4527
  u8 txfc;
4528
  u8 rxfc;
4529
  u8 unknownE31[5];
4530
  u8 anil;
4531
  u8 reps;
4532
  u8 tmrmr;
4533
  u8 ste;
4534
  u8 rpste;
4535
  u8 rptdl;
4536
  u8 dtmfg;
4537
  u8 tmrtx;
4538
} settings;
4539

    
4540
#seekto 0x0E80;
4541
struct {
4542
  u8 unknown1;
4543
  u8 vfomr;
4544
  u8 keylock;
4545
  u8 unknown2;
4546
  u8 unknown3:4,
4547
     vfomren:1,
4548
     unknown4:1,
4549
     reseten:1,
4550
     menuen:1;
4551
  u8 unknown5[11];
4552
  u8 dispab;
4553
  u8 unknown6[2];
4554
  u8 menu;
4555
  u8 unknown7[7];
4556
  u8 vfomra;
4557
  u8 vfomrb;
4558
  u8 vfomrc;
4559
  u8 vfomrd;
4560
  u8 mrcha;
4561
  u8 mrchb;
4562
  u8 mrchc;
4563
  u8 mrchd;
4564
} settings2;
4565

    
4566
struct settings_vfo {
4567
  u8 freq[8];
4568
  u8 offset[6];
4569
  u8 unknown2[2];
4570
  ul16 rxtone;
4571
  ul16 txtone;
4572
  u8 scode;
4573
  u8 spmute;
4574
  u8 optsig;
4575
  u8 scramble;
4576
  u8 wide;
4577
  u8 power;
4578
  u8 shiftd;
4579
  u8 step;
4580
  u8 unknown3[4];
4581
};
4582

    
4583
#seekto 0x0F00;
4584
struct {
4585
  struct settings_vfo a;
4586
  struct settings_vfo b;
4587
  struct settings_vfo c;
4588
  struct settings_vfo d;
4589
} vfo;
4590

    
4591
#seekto 0x0FE0;
4592
struct {
4593
  char line[16];
4594
} static_msg;
4595

    
4596
#seekto 0x1000;
4597
struct {
4598
  char name[8];
4599
  u8 unknown1[8];
4600
} names[200];
4601

    
4602
#seekto 0x2400;
4603
struct {
4604
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4605
  u8 group_tone;
4606
  u8 repeat_tone;
4607
  u8 unused[13];
4608
} _5tone_std_settings[15];
4609

    
4610
#seekto 0x2500;
4611
struct {
4612
  u8 frame1[5];
4613
  u8 frame2[5];
4614
  u8 frame3[5];
4615
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4616
} _5tone_codes[15];
4617

    
4618
#seekto 0x25F0;
4619
struct {
4620
  u8 _5tone_delay1; // * 10ms
4621
  u8 _5tone_delay2; // * 10ms
4622
  u8 _5tone_delay3; // * 10ms
4623
  u8 _5tone_first_digit_ext_length;
4624
  u8 unknown1;
4625
  u8 unknown2;
4626
  u8 unknown3;
4627
  u8 unknown4;
4628
  u8 decode_standard;
4629
  u8 unknown5:5,
4630
     _5tone_decode_call_frame3:1,
4631
     _5tone_decode_call_frame2:1,
4632
     _5tone_decode_call_frame1:1;
4633
  u8 unknown6:5,
4634
     _5tone_decode_disp_frame3:1,
4635
     _5tone_decode_disp_frame2:1,
4636
     _5tone_decode_disp_frame1:1;
4637
  u8 decode_reset_time; // * 100 + 100ms
4638
} _5tone_settings;
4639

    
4640
#seekto 0x2900;
4641
struct {
4642
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4643
} dtmf_codes[15];
4644

    
4645
#seekto 0x29F0;
4646
struct {
4647
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4648
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4649
  u8 unknown0[14];
4650
  u8 inspection[16];
4651
  u8 monitor[16];
4652
  u8 alarmcode[16];
4653
  u8 stun[16];
4654
  u8 kill[16];
4655
  u8 revive[16];
4656
  u8 unknown1[16];
4657
  u8 unknown2[16];
4658
  u8 unknown3[16];
4659
  u8 unknown4[16];
4660
  u8 unknown5[16];
4661
  u8 unknown6[16];
4662
  u8 unknown7[16];
4663
  u8 masterid[16];
4664
  u8 viceid[16];
4665
  u8 unused01:7,
4666
     mastervice:1;
4667
  u8 unused02:3,
4668
     mrevive:1,
4669
     mkill:1,
4670
     mstun:1,
4671
     mmonitor:1,
4672
     minspection:1;
4673
  u8 unused03:3,
4674
     vrevive:1,
4675
     vkill:1,
4676
     vstun:1,
4677
     vmonitor:1,
4678
     vinspection:1;
4679
  u8 unused04:6,
4680
     txdisable:1,
4681
     rxdisable:1;
4682
  u8 groupcode;
4683
  u8 spacecode;
4684
  u8 delayproctime; // * 100 + 100ms
4685
  u8 resettime;     // * 100 + 100ms
4686
} dtmf_settings;
4687

    
4688
#seekto 0x2D00;
4689
struct {
4690
  struct {
4691
    ul16 freq1;
4692
    u8 unused01[6];
4693
    ul16 freq2;
4694
    u8 unused02[6];
4695
  } _2tone_encode[15];
4696
  u8 duration_1st_tone; // *10ms
4697
  u8 duration_2nd_tone; // *10ms
4698
  u8 duration_gap;      // *10ms
4699
  u8 unused03[13];
4700
  struct {
4701
    struct {
4702
      u8 dec;      // one out of LIST_2TONE_DEC
4703
      u8 response; // one out of LIST_2TONE_RESPONSE
4704
      u8 alert;    // 1-16
4705
    } decs[4];
4706
    u8 unused04[4];
4707
  } _2tone_decode[15];
4708
  u8 unused05[16];
4709

    
4710
  struct {
4711
    ul16 freqA;
4712
    ul16 freqB;
4713
    ul16 freqC;
4714
    ul16 freqD;
4715
    // unknown what those values mean, but they are
4716
    // derived from configured frequencies
4717
    ul16 derived_from_freqA; // 2304000/freqA
4718
    ul16 derived_from_freqB; // 2304000/freqB
4719
    ul16 derived_from_freqC; // 2304000/freqC
4720
    ul16 derived_from_freqD; // 2304000/freqD
4721
  }freqs[15];
4722
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4723
} _2tone;
4724

    
4725
#seekto 0x3D80;
4726
struct {
4727
  u8 vhf_low[3];
4728
  u8 vhf_high[3];
4729
  u8 unknown1[4];
4730
  u8 unknown2[6];
4731
  u8 vhf2_low[3];
4732
  u8 vhf2_high[3];
4733
  u8 unknown3[4];
4734
  u8 unknown4[6];
4735
  u8 uhf_low[3];
4736
  u8 uhf_high[3];
4737
  u8 unknown5[4];
4738
  u8 unknown6[6];
4739
  u8 uhf2_low[3];
4740
  u8 uhf2_high[3];
4741
} ranges;
4742

    
4743
#seekto 0x3F70;
4744
struct {
4745
  char fp[6];
4746
} fingerprint;
4747

    
4748
"""
4749

    
4750

    
4751
class QYTColorHT(BTechMobileCommon):
4752
    """QTY's Color LCD Handheld and alike radios"""
4753
    COLOR_LCD = True
4754
    COLOR_LCD3 = True
4755
    NAME_LENGTH = 8
4756
    LIST_TMR = LIST_TMR15
4757

    
4758
    def process_mmap(self):
4759
        """Process the mem map into the mem object"""
4760

    
4761
        # Get it
4762
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4763

    
4764
        # load specific parameters from the radio image
4765
        self.set_options()
4766

    
4767
    def set_options(self):
4768
        """This is to read the options from the image and set it in the
4769
        environment, for now just the limits of the freqs in the VHF/UHF
4770
        ranges"""
4771

    
4772
        # setting the correct ranges for each radio type
4773
        ranges = self._memobj.ranges
4774

    
4775
        # the normal dual bands
4776
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4777
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4778

    
4779
        # DEBUG
4780
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4781
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4782

    
4783
        # the additional bands
4784
        if self.MODEL in ["KT-8R"]:
4785
            # 200Mhz band
4786
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
4787
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
4788
            self._220_range = vhf2
4789

    
4790
            # 350Mhz band
4791
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4792
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4793
            self._350_range = uhf2
4794

    
4795
        # set the class with the real data
4796
        self._vhf_range = vhf
4797
        self._uhf_range = uhf
4798

    
4799

    
4800
# real radios
4801
@directory.register
4802
class KT8R(QYTColorHT):
4803
    """QYT KT8R"""
4804
    VENDOR = "QYT"
4805
    MODEL = "KT-8R"
4806
    BANDS = 4
4807
    LIST_TMR = LIST_TMR16
4808
    _vhf_range = (136000000, 175000000)
4809
    _220_range = (200000000, 261000000)
4810
    _uhf_range = (400000000, 481000000)
4811
    _350_range = (350000000, 391000000)
4812
    _magic = MSTRING_KT8R
4813
    _fileid = [KT8R_fp2, KT8R_fp1, KT8R_fp, ]
4814
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
4815
                     chirp_common.PowerLevel("Low", watts=1)]
    (1-1/1)