Project

General

Profile

Bug #10445 » bf_t1_fm.py

Jim Unroe, 03/15/2023 02:12 PM

 
1
# Copyright 2017 Pavel Milanes, CO7WT, <pavelmc@gmail.com>
2
#
3
# This driver is a community effort as I don't have the radio on my hands, so
4
# I was only the director of the orchestra, without the players this may never
5
# came true, so special thanks to the following hams for their contribution:
6
# - Henk van der Laan, PA3CQN
7
#       - Setting Discovery.
8
#       - Special channels for RELAY and EMERGENCY.
9
# - Harold Hankins
10
#       - Memory limits, testing & bug hunting.
11
# - Dmitry Milkov
12
#       - Testing & bug hunting.
13
# - Many others participants in the issue page on Chirp's site.
14
#
15
# This program is free software: you can redistribute it and/or modify
16
# it under the terms of the GNU General Public License as published by
17
# the Free Software Foundation, either version 2 of the License, or
18
# (at your option) any later version.
19
#
20
# This program is distributed in the hope that it will be useful,
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
# GNU General Public License for more details.
24
#
25
# You should have received a copy of the GNU General Public License
26
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
27

    
28
from time import sleep
29
from chirp import chirp_common, directory, memmap
30
from chirp import bitwise, errors, util
31
from chirp.settings import RadioSetting, RadioSettingGroup, \
32
                RadioSettingValueBoolean, RadioSettingValueList, \
33
                RadioSettingValueInteger, RadioSettingValueString, \
34
                RadioSettingValueFloat, RadioSettings
35

    
36
import struct
37
import logging
38

    
39
LOG = logging.getLogger(__name__)
40

    
41
# A note about the memory in these radios
42
#
43
# The '9100' OEM software only manipulates the lower 0x0180 bytes on read/write
44
# operations as we know, the file generated by the OEM software IS NOT an exact
45
# eeprom image, it's a crude text file with a pseudo csv format
46
#
47
# Later investigations by Harold Hankins found that the eeprom extend up to 2k
48
# consistent with a hardware chip K24C16 a 2k x 8 bit serial eeprom
49

    
50
MEM_SIZE = 0x0800  # 2048 bytes
51
WRITE_SIZE = 0x0180  # 384 bytes
52
BLOCK_SIZE = 0x10
53
ACK_CMD = b"\x06"
54
MODES = ["NFM", "FM"]
55
SKIP_VALUES = ["S", ""]
56
TONES = chirp_common.TONES
57
DTCS = tuple(sorted(chirp_common.DTCS_CODES + (645,)))
58

    
59
# Special channels
60
SPECIALS = {
61
    "EMG": -2,
62
    "RLY": -1
63
    }
64

    
65
# Settings vars
66
TOT_LIST = ["Off"] + ["%s" % x for x in range(30, 210, 30)]
67
SCAN_TYPE_LIST = ["Time", "Carrier", "Search"]
68
LANGUAGE_LIST = ["Off", "English", "Chinese"]
69
TIMER_LIST = ["Off"] + ["%s h" % (x * 0.5) for x in range(1, 17)]
70
FM_RANGE_LIST = ["76-108", "65-76"]
71
RELAY_MODE_LIST = ["Off", "RX sync", "TX sync"]
72
BACKLIGHT_LIST = ["Off", "Key", "On"]
73
POWER_LIST = ["0.5 Watt", "1.0 Watt"]
74

    
75
# This is a general serial timeout for all serial read functions.
76
# Practice has show that about 0.07 sec will be enough to cover all radios.
77
STIMEOUT = 0.07
78

    
79
# this var controls the verbosity in the debug and by default it's low (False)
80
# make it True and you will to get a very verbose debug.log
81
debug = False
82

    
83
# #### ID strings #####################################################
84

    
85
# BF-T1 handheld
86
BFT1_magic = b"\x05PROGRAM"
87
BFT1_ident = b" BF9100S"
88

    
89

    
90
def _clean_buffer(radio):
91
    """Cleaning the read serial buffer, hard timeout to survive an infinite
92
    data stream"""
93

    
94
    dump = "1"
95
    datacount = 0
96

    
97
    try:
98
        while len(dump) > 0:
99
            dump = radio.pipe.read(100)
100
            datacount += len(dump)
101
            # hard limit to survive a infinite serial data stream
102
            # 5 times bigger than a normal rx block (20 bytes)
103
            if datacount > 101:
104
                seriale = "Please check your serial port selection."
105
                raise errors.RadioError(seriale)
106

    
107
    except Exception:
108
        raise errors.RadioError("Unknown error cleaning the serial buffer")
109

    
110

    
111
def _rawrecv(radio, amount=0):
112
    """Raw read from the radio device"""
113

    
114
    # var to hold the data to return
115
    data = b""
116

    
117
    try:
118
        if amount == 0:
119
            data = radio.pipe.read()
120
        else:
121
            data = radio.pipe.read(amount)
122

    
123
        # DEBUG
124
        if debug is True:
125
            LOG.debug("<== (%d) bytes:\n\n%s" %
126
                      (len(data), util.hexprint(data)))
127

    
128
        # fail if no data is received
129
        if len(data) == 0:
130
            raise errors.RadioError("No data received from radio")
131

    
132
    except:
133
        raise errors.RadioError("Error reading data from radio")
134

    
135
    return data
136

    
137

    
138
def _send(radio, data):
139
    """Send data to the radio device"""
140

    
141
    try:
142
        radio.pipe.write(data)
143

    
144
        # DEBUG
145
        if debug is True:
146
            LOG.debug("==> (%d) bytes:\n\n%s" %
147
                      (len(data), util.hexprint(data)))
148
    except:
149
        raise errors.RadioError("Error sending data to radio")
150

    
151

    
152
def _make_frame(cmd, addr, data=""):
153
    """Pack the info in the header format"""
154
    frame = struct.pack(">BHB", ord(cmd), addr, BLOCK_SIZE)
155

    
156
    # add the data if set
157
    if len(data) != 0:
158
        frame += data
159

    
160
    return frame
161

    
162

    
163
def _recv(radio, addr):
164
    """Get data from the radio"""
165

    
166
    # Get the full 20 bytes at a time
167
    # 4 bytes header + 16 bytes of data (BLOCK_SIZE)
168

    
169
    # get the whole block
170
    block = _rawrecv(radio, BLOCK_SIZE + 4)
171

    
172
    # short answer
173
    if len(block) < (BLOCK_SIZE + 4):
174
        raise errors.RadioError("Wrong block length (short) at 0x%04x" % addr)
175

    
176
    # long answer
177
    if len(block) > (BLOCK_SIZE + 4):
178
        raise errors.RadioError("Wrong block length (long) at 0x%04x" % addr)
179

    
180
    # header validation
181
    c, a, l = struct.unpack(">cHB", block[0:4])
182
    if c != b"W" or a != addr or l != BLOCK_SIZE:
183
        LOG.debug("Invalid header for block 0x%04x:" % addr)
184
        LOG.debug("CMD: %s  ADDR: %04x  SIZE: %02x" % (c, a, l))
185
        raise errors.RadioError("Invalid header for block 0x%04x:" % addr)
186

    
187
    # return the data, 16 bytes of payload
188
    return block[4:]
189

    
190

    
191
def _start_clone_mode(radio, status):
192
    """Put the radio in clone mode, 3 tries"""
193

    
194
    # cleaning the serial buffer
195
    _clean_buffer(radio)
196

    
197
    # prep the data to show in the UI
198
    status.cur = 0
199
    status.msg = "Identifying the radio..."
200
    status.max = 3
201
    radio.status_fn(status)
202

    
203
    try:
204
        for a in range(0, status.max):
205
            # Update the UI
206
            status.cur = a + 1
207
            radio.status_fn(status)
208

    
209
            # send the magic word
210
            _send(radio, radio._magic)
211

    
212
            # Now you get a x06 of ACK if all goes well
213
            ack = _rawrecv(radio, 1)
214

    
215
            if ack == ACK_CMD:
216
                # DEBUG
217
                LOG.info("Magic ACK received")
218
                status.cur = status.max
219
                radio.status_fn(status)
220

    
221
                return True
222

    
223
        return False
224

    
225
    except errors.RadioError:
226
        raise
227
    except Exception as e:
228
        raise errors.RadioError("Error sending Magic to radio:\n%s" % e)
229

    
230

    
231
def _do_ident(radio, status):
232
    """Put the radio in PROGRAM mode & identify it"""
233
    #  set the serial discipline (default)
234
    radio.pipe.baudrate = 9600
235
    radio.pipe.parity = "N"
236
    radio.pipe.bytesize = 8
237
    radio.pipe.stopbits = 1
238
    radio.pipe.timeout = STIMEOUT
239

    
240
    # open the radio into program mode
241
    if _start_clone_mode(radio, status) is False:
242
        raise errors.RadioError("Radio did not enter clone mode, wrong model?")
243

    
244
    # Ok, poke it to get the ident string
245
    _send(radio, b"\x02")
246
    ident = _rawrecv(radio, len(radio._id))
247

    
248
    # basic check for the ident
249
    if len(ident) != len(radio._id):
250
        raise errors.RadioError("Radio send a odd identification block.")
251

    
252
    # check if ident is OK
253
    if ident != radio._id:
254
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
255
        raise errors.RadioError("Radio identification failed.")
256

    
257
    # handshake
258
    _send(radio, ACK_CMD)
259
    ack = _rawrecv(radio, 1)
260

    
261
    # checking handshake
262
    if len(ack) == 1 and ack == ACK_CMD:
263
        # DEBUG
264
        LOG.info("ID ACK received")
265
    else:
266
        LOG.debug("Radio handshake failed.")
267
        raise errors.RadioError("Radio handshake failed.")
268

    
269
    # DEBUG
270
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
271

    
272
    return True
273

    
274

    
275
def _download(radio):
276
    """Get the memory map"""
277

    
278
    # UI progress
279
    status = chirp_common.Status()
280

    
281
    # put radio in program mode and identify it
282
    _do_ident(radio, status)
283

    
284
    # reset the progress bar in the UI
285
    status.max = MEM_SIZE // BLOCK_SIZE
286
    status.msg = "Cloning from radio..."
287
    status.cur = 0
288
    radio.status_fn(status)
289

    
290
    # cleaning the serial buffer
291
    _clean_buffer(radio)
292

    
293
    data = b""
294
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
295
        # sending the read request
296
        _send(radio, _make_frame(b"R", addr))
297

    
298
        # read
299
        d = _recv(radio, addr)
300

    
301
        # aggregate the data
302
        data += d
303

    
304
        # UI Update
305
        status.cur = addr // BLOCK_SIZE
306
        status.msg = "Cloning from radio..."
307
        radio.status_fn(status)
308

    
309
    # close comms with the radio
310
    _send(radio, b"\x62")
311
    # DEBUG
312
    LOG.info("Close comms cmd sent, radio must reboot now.")
313

    
314
    return data
315

    
316

    
317
def _upload(radio):
318
    """Upload procedure, we only upload to the radio the Writable space"""
319

    
320
    # UI progress
321
    status = chirp_common.Status()
322

    
323
    # put radio in program mode and identify it
324
    _do_ident(radio, status)
325

    
326
    # get the data to upload to radio
327
    data = radio.get_mmap()
328

    
329
    # Reset the UI progress
330
    status.max = WRITE_SIZE // BLOCK_SIZE
331
    status.cur = 0
332
    status.msg = "Cloning to radio..."
333
    radio.status_fn(status)
334

    
335
    # cleaning the serial buffer
336
    _clean_buffer(radio)
337

    
338
    # the fun start here, we use WRITE_SIZE instead of the full MEM_SIZE
339
    for addr in range(0, WRITE_SIZE, BLOCK_SIZE):
340
        # getting the block of data to send
341
        d = data[addr:addr + BLOCK_SIZE]
342

    
343
        # build the frame to send
344
        frame = _make_frame(b"W", addr, d)
345

    
346
        # send the frame
347
        _send(radio, frame)
348

    
349
        # receiving the response
350
        ack = _rawrecv(radio, 1)
351

    
352
        # basic check
353
        if len(ack) != 1:
354
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
355

    
356
        if ack != ACK_CMD:
357
            raise errors.RadioError("Bad ACK writing block 0x%04x:" % addr)
358

    
359
        # UI Update
360
        status.cur = addr // BLOCK_SIZE
361
        status.msg = "Cloning to radio..."
362
        radio.status_fn(status)
363

    
364
    # close comms with the radio
365
    _send(radio, b"\x62")
366
    # DEBUG
367
    LOG.info("Close comms cmd sent, radio must reboot now.")
368

    
369

    
370
def _model_match(cls, data):
371
    """Match the opened/downloaded image to the correct version"""
372

    
373
    # a reliable fingerprint: the model name at
374
    rid = data[0x06f8:0x0700]
375

    
376
    if rid == BFT1_ident:
377
        return True
378

    
379
    return False
380

    
381

    
382
def _decode_ranges(low, high):
383
    """Unpack the data in the ranges zones in the memmap and return
384
    a tuple with the integer corresponding to the Mhz it means"""
385
    return (int(low) * 100000, int(high) * 100000)
386

    
387

    
388
MEM_FORMAT = """
389

    
390
struct channel {
391
  lbcd rxfreq[4];       // rx freq.
392
  u8 rxtone;            // x00 = none
393
                        // x01 - x32 = index of the analog tones
394
                        // x33 - x9b = index of Digital tones
395
                        // Digital tone polarity is handled below by
396
                        // ttondinv & ttondinv settings
397
  lbcd txoffset[4];     // the difference against RX, direction handled by
398
                        // offplus & offminus
399
  u8 txtone;            // Idem to rxtone
400
  u8 noskip:1,      // if true is included in the scan
401
     wide:1,        // 1 = Wide, 0 = Narrow
402
     ttondinv:1,    // if true TX tone is Digital & Inverted
403
     unA:1,         //
404
     rtondinv:1,    // if true RX tone is Digital & Inverted
405
     unB:1,         //
406
     offplus:1,     // TX = RX + offset
407
     offminus:1;    // TX = RX - offset
408
  u8 empty[5];
409
};
410

    
411
#seekto 0x0000;
412
struct channel emg;             // channel 0 is Emergent CH
413
#seekto 0x0010;
414
struct channel channels[20];    // normal 1-20 mem channels
415

    
416
#seekto 0x0150;     // Settings
417
struct {
418
  lbcd vhfl[2];     // VHF low limit
419
  lbcd vhfh[2];     // VHF high limit
420
  lbcd uhfl[2];     // UHF low limit
421
  lbcd uhfh[2];     // UHF high limit
422
  u8 unk0[8];
423
  u8 unk1[2];       // start of 0x0160 <=======
424
  u8 squelch;       // byte: 0-9
425
  u8 vox;           // byte: 0-9
426
  u8 timeout;       // tot, 0 off, then 30 sec increments up to 180
427
  u8 batsave:1,     // battery save 0 = off, 1 = on
428
     fm_funct:1,    // fm-radio 0=off, 1=on ( off disables fm button on set )
429
     ste:1,         // squelch tail 0 = off, 1 = on
430
     blo:1,         // busy lockout 0 = off, 1 = on
431
     beep:1,        // key beep 0 = off, 1 = on
432
     lock:1,        // keylock 0 = ff,  = on
433
     backlight:2;   // backlight 00 = off, 01 = key, 10 = on
434
  u8 scantype;      // scan type 0 = timed, 1 = carrier, 2 = stop
435
  u8 channel;       // active channel 1-20, setting it works on upload
436
  u8 fmrange;       // fm range 1 = low[65-76](ASIA), 0 = high[76-108](AMERICA)
437
  u8 alarm;         // alarm (count down timer)
438
                    //    d0 - d16 in half hour increments => off, 0.5 - 8.0 h
439
  u8 voice;         // voice prompt 0 = off, 1 = english, 2 = chinese
440
  u8 volume;        // volume 1-7 as per the radio steps
441
                    //    set to #FF by original software on upload
442
                    //    chirp uploads actual value and works.
443
  u16 fm_vfo;       // the frequency of the fm receiver.
444
                    //    resulting frequency is 65 + value * 0.1 MHz
445
                    //    0x145 is then 65 + 325*0.1 = 97.5 MHz
446
  u8 relaym;        // relay mode, d0 = off, d2 = re-tx, d1 = re-rx
447
                    //    still a mystery on how it works
448
  u8 tx_pwr;        // tx pwr 0 = low (0.5W), 1 = high(1.0W)
449
} settings;
450

    
451
#seekto 0x0170;     // Relay CH
452
struct channel rly;
453

    
454
"""
455

    
456

    
457
@directory.register
458
class BFT1(chirp_common.CloneModeRadio, chirp_common.ExperimentalRadio):
459
    """Baofeng BT-F1 radio & possibly alike radios"""
460
    VENDOR = "Baofeng"
461
    MODEL = "BF-T1"
462
    NEEDS_COMPAT_SERIAL = False
463
    _vhf_range = (130000000, 174000000)
464
    _uhf_range = (400000000, 520000000)
465
    _upper = 20
466
    _magic = BFT1_magic
467
    _id = BFT1_ident
468
    _bw_shift = False
469

    
470
    @classmethod
471
    def get_prompts(cls):
472
        rp = chirp_common.RadioPrompts()
473
        rp.experimental = \
474
            ('This driver is experimental.\n'
475
             '\n'
476
             'Please keep a copy of your memories with the original software '
477
             'if you treasure them, this driver is new and may contain'
478
             ' bugs.\n'
479
             '\n'
480
             '"Emergent CH" & "Relay CH" are implemented via special channels,'
481
             'be sure to click on the button on the interface to access them.'
482
             )
483
        rp.pre_download = _(
484
            "Follow these instructions to download your info:\n"
485
            "1 - Turn off your radio\n"
486
            "2 - Connect your interface cable\n"
487
            "3 - Turn on your radio\n"
488
            "4 - Do the download of your radio data\n")
489
        rp.pre_upload = _(
490
            "Follow these instructions to upload your info:\n"
491
            "1 - Turn off your radio\n"
492
            "2 - Connect your interface cable\n"
493
            "3 - Turn on your radio\n"
494
            "4 - Do the upload of your radio data\n")
495
        return rp
496

    
497
    def get_features(self):
498
        """Get the radio's features"""
499

    
500
        rf = chirp_common.RadioFeatures()
501
        rf.valid_special_chans = list(SPECIALS.keys())
502
        rf.has_settings = True
503
        rf.has_bank = False
504
        rf.has_tuning_step = False
505
        rf.can_odd_split = True
506
        rf.has_name = False
507
        rf.has_offset = True
508
        rf.has_mode = True
509
        rf.valid_modes = MODES
510
        rf.has_dtcs = True
511
        rf.has_rx_dtcs = True
512
        rf.has_dtcs_polarity = True
513
        rf.has_ctone = True
514
        rf.has_cross = True
515
        rf.valid_duplexes = ["", "-", "+", "split"]
516
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
517
        rf.valid_cross_modes = [
518
            "Tone->Tone",
519
            "DTCS->",
520
            "->DTCS",
521
            "Tone->DTCS",
522
            "DTCS->Tone",
523
            "->Tone",
524
            "DTCS->DTCS"]
525
        rf.valid_skips = SKIP_VALUES
526
        rf.valid_dtcs_codes = DTCS
527
        rf.memory_bounds = (1, self._upper)
528
        rf.valid_tuning_steps = [2.5, 5., 6.25, 10., 12.5, 25.]
529

    
530
        # normal dual bands
531
        rf.valid_bands = [self._vhf_range, self._uhf_range]
532

    
533
        return rf
534

    
535
    def process_mmap(self):
536
        """Process the mem map into the mem object"""
537

    
538
        # Get it
539
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
540

    
541
        # set the band limits as the memmap
542
        settings = self._memobj.settings
543
        self._vhf_range = _decode_ranges(settings.vhfl, settings.vhfh)
544
        self._uhf_range = _decode_ranges(settings.uhfl, settings.uhfh)
545

    
546
    def sync_in(self):
547
        """Download from radio"""
548
        data = _download(self)
549
        self._mmap = memmap.MemoryMapBytes(data)
550
        self.process_mmap()
551

    
552
    def sync_out(self):
553
        """Upload to radio"""
554

    
555
        try:
556
            _upload(self)
557
        except errors.RadioError:
558
            raise
559
        except Exception as e:
560
            raise errors.RadioError("Error: %s" % e)
561

    
562
    def _decode_tone(self, val, inv):
563
        """Parse the tone data to decode from mem, it returns:
564
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
565

    
566
        if val == 0:
567
            return '', None, None
568
        elif val < 51:  # analog tone
569
            return 'Tone', TONES[val - 1], None
570
        elif val > 50:  # digital tone
571
            pol = "N"
572
            # polarity?
573
            if inv == 1:
574
                pol = "R"
575

    
576
            return 'DTCS', DTCS[val - 51], pol
577

    
578
    def _encode_tone(self, memtone, meminv, mode, tone, pol):
579
        """Parse the tone data to encode from UI to mem"""
580

    
581
        if mode == '' or mode is None:
582
            memtone.set_value(0)
583
            meminv.set_value(0)
584
        elif mode == 'Tone':
585
            # caching errors for analog tones.
586
            try:
587
                memtone.set_value(TONES.index(tone) + 1)
588
                meminv.set_value(0)
589
            except:
590
                msg = "TCSS Tone '%d' is not supported" % tone
591
                LOG.error(msg)
592
                raise errors.RadioError(msg)
593

    
594
        elif mode == 'DTCS':
595
            # caching errors for digital tones.
596
            try:
597
                memtone.set_value(DTCS.index(tone) + 51)
598
                if pol == "R":
599
                    meminv.set_value(True)
600
                else:
601
                    meminv.set_value(False)
602
            except:
603
                msg = "Digital Tone '%d' is not supported" % tone
604
                LOG.error(msg)
605
                raise errors.RadioError(msg)
606
        else:
607
            msg = "Internal error: invalid mode '%s'" % mode
608
            LOG.error(msg)
609
            raise errors.InvalidDataError(msg)
610

    
611
    def get_raw_memory(self, number):
612
        return repr(self._memobj.memory[number])
613

    
614
    def _get_special(self, number):
615
        if isinstance(number, str):
616
            return (getattr(self._memobj, number.lower()))
617
        elif number < 0:
618
            for k, v in SPECIALS.items():
619
                if number == v:
620
                    return (getattr(self._memobj, k.lower()))
621
        else:
622
            return self._memobj.channels[number-1]
623

    
624
    def get_memory(self, number):
625
        """Get the mem representation from the radio image"""
626
        _mem = self._get_special(number)
627

    
628
        # Create a high-level memory object to return to the UI
629
        mem = chirp_common.Memory()
630

    
631
        # Check if special or normal
632
        if isinstance(number, str):
633
            mem.number = SPECIALS[number]
634
            mem.extd_number = number
635
        else:
636
            mem.number = number
637

    
638
        if _mem.get_raw()[0] == "\xFF":
639
            mem.empty = True
640
            return mem
641

    
642
        # Freq and offset
643
        mem.freq = int(_mem.rxfreq) * 10
644

    
645
        # TX freq (Stored as a difference)
646
        mem.offset = int(_mem.txoffset) * 10
647
        mem.duplex = ""
648

    
649
        # must work out the polarity
650
        if mem.offset != 0:
651
            if _mem.offminus == 1:
652
                mem.duplex = "-"
653
                #  tx below RX
654

    
655
            if _mem.offplus == 1:
656
                #  tx above RX
657
                mem.duplex = "+"
658

    
659
            # split RX/TX in different bands
660
            if mem.offset > 71000000:
661
                mem.duplex = "split"
662

    
663
                # show the actual value in the offset, depending on the shift
664
                if _mem.offminus == 1:
665
                    mem.offset = mem.freq - mem.offset
666
                if _mem.offplus == 1:
667
                    mem.offset = mem.freq + mem.offset
668

    
669
        # wide/narrow
670
        mem.mode = MODES[int(_mem.wide)]
671

    
672
        # skip
673
        mem.skip = SKIP_VALUES[_mem.noskip]
674

    
675
        # tone data
676
        rxtone = txtone = None
677
        txtone = self._decode_tone(_mem.txtone, _mem.ttondinv)
678
        rxtone = self._decode_tone(_mem.rxtone, _mem.rtondinv)
679
        chirp_common.split_tone_decode(mem, txtone, rxtone)
680

    
681
        return mem
682

    
683
    def set_memory(self, mem):
684
        """Set the memory data in the eeprom img from the UI"""
685
        # get the eprom representation of this channel
686
        _mem = self._get_special(mem.number)
687

    
688
        # if empty memory
689
        if mem.empty:
690
            # the channel itself
691
            _mem.set_raw("\xFF" * 16)
692
            # return it
693
            return mem
694

    
695
        # frequency
696
        _mem.rxfreq = mem.freq / 10
697

    
698
        # duplex/ offset Offset is an absolute value
699
        _mem.txoffset = mem.offset / 10
700

    
701
        # must work out the polarity
702
        if mem.duplex == "":
703
            _mem.offplus = 0
704
            _mem.offminus = 0
705
        elif mem.duplex == "+":
706
            _mem.offplus = 1
707
            _mem.offminus = 0
708
        elif mem.duplex == "-":
709
            _mem.offplus = 0
710
            _mem.offminus = 1
711
        elif mem.duplex == "split":
712
            if mem.freq > mem.offset:
713
                _mem.offplus = 0
714
                _mem.offminus = 1
715
                _mem.txoffset = (mem.freq - mem.offset) / 10
716
            else:
717
                _mem.offplus = 1
718
                _mem.offminus = 0
719
                _mem.txoffset = (mem.offset - mem.freq) / 10
720

    
721
        # wide/narrow
722
        _mem.wide = MODES.index(mem.mode)
723

    
724
        # skip
725
        _mem.noskip = SKIP_VALUES.index(mem.skip)
726

    
727
        # tone data
728
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
729
            chirp_common.split_tone_encode(mem)
730
        self._encode_tone(_mem.txtone, _mem.ttondinv, txmode, txtone, txpol)
731
        self._encode_tone(_mem.rxtone, _mem.rtondinv, rxmode, rxtone, rxpol)
732

    
733
        return mem
734

    
735
    def get_settings(self):
736
        _settings = self._memobj.settings
737
        basic = RadioSettingGroup("basic", "Basic Settings")
738
        fm = RadioSettingGroup("fm", "FM Radio")
739
        adv = RadioSettingGroup("adv", "Advanced Settings")
740
        group = RadioSettings(basic, fm, adv)
741

    
742
        # ## Basic Settings
743
        rs = RadioSetting("tx_pwr", "TX Power",
744
                          RadioSettingValueList(
745
                            POWER_LIST, POWER_LIST[_settings.tx_pwr]))
746
        basic.append(rs)
747

    
748
        rs = RadioSetting("channel", "Active Channel",
749
                          RadioSettingValueInteger(1, 20, _settings.channel))
750
        basic.append(rs)
751

    
752
        rs = RadioSetting("squelch", "Squelch Level",
753
                          RadioSettingValueInteger(0, 9, _settings.squelch))
754
        basic.append(rs)
755

    
756
        rs = RadioSetting("vox", "VOX Level",
757
                          RadioSettingValueInteger(0, 9, _settings.vox))
758
        basic.append(rs)
759

    
760
        # volume validation, as the OEM software set 0xFF on write
761
        _volume = _settings.volume
762
        if _volume > 7:
763
            _volume = 7
764
        rs = RadioSetting("volume", "Volume Level",
765
                          RadioSettingValueInteger(0, 7, _volume))
766
        basic.append(rs)
767

    
768
        rs = RadioSetting("scantype", "Scan Type",
769
                          RadioSettingValueList(SCAN_TYPE_LIST, SCAN_TYPE_LIST[
770
                              _settings.scantype]))
771
        basic.append(rs)
772

    
773
        rs = RadioSetting("timeout", "Time Out Timer (seconds)",
774
                          RadioSettingValueList(
775
                            TOT_LIST, TOT_LIST[_settings.timeout]))
776
        basic.append(rs)
777

    
778
        rs = RadioSetting("voice", "Voice Prompt",
779
                          RadioSettingValueList(
780
                            LANGUAGE_LIST, LANGUAGE_LIST[_settings.voice]))
781
        basic.append(rs)
782

    
783
        rs = RadioSetting("alarm", "Alarm Time",
784
                          RadioSettingValueList(
785
                            TIMER_LIST, TIMER_LIST[_settings.alarm]))
786
        basic.append(rs)
787

    
788
        rs = RadioSetting("backlight", "Backlight",
789
                          RadioSettingValueList(
790
                            BACKLIGHT_LIST,
791
                            BACKLIGHT_LIST[_settings.backlight]))
792
        basic.append(rs)
793

    
794
        rs = RadioSetting("blo", "Busy Lockout",
795
                          RadioSettingValueBoolean(_settings.blo))
796
        basic.append(rs)
797

    
798
        rs = RadioSetting("ste", "Squelch Tail Eliminate",
799
                          RadioSettingValueBoolean(_settings.ste))
800
        basic.append(rs)
801

    
802
        rs = RadioSetting("batsave", "Battery Save",
803
                          RadioSettingValueBoolean(_settings.batsave))
804
        basic.append(rs)
805

    
806
        rs = RadioSetting("lock", "Key Lock",
807
                          RadioSettingValueBoolean(_settings.lock))
808
        basic.append(rs)
809

    
810
        rs = RadioSetting("beep", "Key Beep",
811
                          RadioSettingValueBoolean(_settings.beep))
812
        basic.append(rs)
813

    
814
        # ## FM Settings
815
        rs = RadioSetting("fm_funct", "FM Function",
816
                          RadioSettingValueBoolean(_settings.fm_funct))
817
        fm.append(rs)
818

    
819
        rs = RadioSetting("fmrange", "FM Range",
820
                          RadioSettingValueList(
821
                            FM_RANGE_LIST, FM_RANGE_LIST[_settings.fmrange]))
822
        fm.append(rs)
823

    
824
        # callbacks for the FM VFO
825
        def apply_fm_freq(setting, obj):
826
            value = int(setting.value.get_value() * 10) - 650
827
            LOG.debug("Setting fm_vfo = %s" % (value))
828
            if self._bw_shift:
829
                value = ((value & 0x00FF) << 8) | ((value & 0xFF00) >> 8)
830
            setattr(obj, setting.get_name(), value)
831

    
832
        # broadcast FM setting
833
        value = _settings.fm_vfo
834
        value_shifted = ((value & 0x00FF) << 8) | ((value & 0xFF00) >> 8)
835
        if value_shifted <= 108.0 * 10 - 650:
836
            # storage method 3 (discovered 2022)
837
            self._bw_shift = True
838
            _fm_vfo = value_shifted / 10.0 + 65
839
        elif value <= 108.0 * 10 - 650:
840
            # original storage method (2012)
841
            _fm_vfo = value / 10.0 + 65
842
        else:
843
            # unknown (undiscovered method or no FM chip?)
844
            _fm_vfo = False
845
        if _fm_vfo:
846
            rs = RadioSetting("fm_vfo", "FM Station",
847
                              RadioSettingValueFloat(65, 108, _fm_vfo))
848
            rs.set_apply_callback(apply_fm_freq, _settings)
849
            fm.append(rs)
850

    
851
        # ## Advanced
852
        def apply_limit(setting, obj):
853
            setattr(obj, setting.get_name(), int(setting.value) * 10)
854

    
855
        rs = RadioSetting("vhfl", "VHF Low Limit",
856
                          RadioSettingValueInteger(130, 174, int(
857
                              _settings.vhfl) / 10))
858
        rs.set_apply_callback(apply_limit, _settings)
859
        adv.append(rs)
860

    
861
        rs = RadioSetting("vhfh", "VHF High Limit",
862
                          RadioSettingValueInteger(130, 174, int(
863
                              _settings.vhfh) / 10))
864
        rs.set_apply_callback(apply_limit, _settings)
865
        adv.append(rs)
866

    
867
        rs = RadioSetting("uhfl", "UHF Low Limit",
868
                          RadioSettingValueInteger(400, 520, int(
869
                              _settings.uhfl) / 10))
870
        rs.set_apply_callback(apply_limit, _settings)
871
        adv.append(rs)
872

    
873
        rs = RadioSetting("uhfh", "UHF High Limit",
874
                          RadioSettingValueInteger(400, 520, int(
875
                              _settings.uhfh) / 10))
876
        rs.set_apply_callback(apply_limit, _settings)
877
        adv.append(rs)
878

    
879
        rs = RadioSetting("relaym", "Relay Mode",
880
                          RadioSettingValueList(RELAY_MODE_LIST,
881
                              RELAY_MODE_LIST[_settings.relaym]))
882
        adv.append(rs)
883

    
884
        return group
885

    
886
    def set_settings(self, uisettings):
887
        _settings = self._memobj.settings
888

    
889
        for element in uisettings:
890
            if not isinstance(element, RadioSetting):
891
                self.set_settings(element)
892
                continue
893
            if not element.changed():
894
                continue
895

    
896
            try:
897
                name = element.get_name()
898
                value = element.value
899

    
900
                if element.has_apply_callback():
901
                    LOG.debug("Using apply callback")
902
                    element.run_apply_callback()
903
                else:
904
                    obj = getattr(_settings, name)
905
                    setattr(_settings, name, value)
906

    
907
                LOG.debug("Setting %s: %s" % (name, value))
908
            except Exception as e:
909
                LOG.debug(element.get_name())
910
                raise
911

    
912
    @classmethod
913
    def match_model(cls, filedata, filename):
914
        match_size = False
915
        match_model = False
916

    
917
        # testing the file data size
918
        if len(filedata) == MEM_SIZE:
919
            match_size = True
920

    
921
            # DEBUG
922
            if debug is True:
923
                LOG.debug("BF-T1 matched!")
924

    
925
        # testing the firmware model fingerprint
926
        match_model = _model_match(cls, filedata)
927

    
928
        return match_size and match_model
(11-11/12)