Project

General

Profile

Bug #10257 » baofeng_uv3r.py

Dan Smith, 01/11/2023 10:43 PM

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

    
16
"""Baofeng UV3r radio management module"""
17

    
18
import time
19
import os
20
import logging
21

    
22
from chirp.drivers.wouxun_common import do_download, do_upload
23
from chirp import util, chirp_common, bitwise, errors, directory
24
from chirp.settings import RadioSetting, RadioSettingGroup, \
25
                RadioSettingValueBoolean, RadioSettingValueList, \
26
                RadioSettingValueInteger, RadioSettingValueString, \
27
                RadioSettingValueFloat, RadioSettings
28

    
29
LOG = logging.getLogger(__name__)
30

    
31

    
32
def _uv3r_prep(radio):
33
    radio.pipe.write(b"\x05PROGRAM")
34
    ack = radio.pipe.read(1)
35
    if ack != b"\x06":
36
        raise errors.RadioError("Radio did not ACK first command")
37

    
38
    radio.pipe.write(b"\x02")
39
    ident = radio.pipe.read(8)
40
    if len(ident) != 8:
41
        LOG.debug(util.hexprint(ident))
42
        raise errors.RadioError("Radio did not send identification")
43

    
44
    radio.pipe.write(b"\x06")
45
    if radio.pipe.read(1) != b"\x06":
46
        raise errors.RadioError("Radio did not ACK ident")
47

    
48

    
49
def uv3r_prep(radio):
50
    """Do the UV3R identification dance"""
51
    for _i in range(0, 10):
52
        try:
53
            return _uv3r_prep(radio)
54
        except errors.RadioError as e:
55
            time.sleep(1)
56

    
57
    raise e
58

    
59

    
60
def uv3r_download(radio):
61
    """Talk to a UV3R and do a download"""
62
    try:
63
        uv3r_prep(radio)
64
        return do_download(radio, 0x0000, 0x0E40, 0x0010)
65
    except errors.RadioError:
66
        raise
67
    except Exception as e:
68
        raise errors.RadioError("Failed to communicate with radio: %s" % e)
69

    
70

    
71
def uv3r_upload(radio):
72
    """Talk to a UV3R and do an upload"""
73
    try:
74
        uv3r_prep(radio)
75
        return do_upload(radio, 0x0000, 0x0E40, 0x0010)
76
    except errors.RadioError:
77
        raise
78
    except Exception as e:
79
        raise errors.RadioError("Failed to communicate with radio: %s" % e)
80

    
81

    
82
UV3R_MEM_FORMAT = """
83
#seekto 0x0010;
84
struct {
85
  lbcd rx_freq[4];
86
  u8 rxtone;
87
  lbcd offset[4];
88
  u8 txtone;
89
  u8 ishighpower:1,
90
     iswide:1,
91
     dtcsinvt:1,
92
     unknown1:1,
93
     dtcsinvr:1,
94
     unknown2:1,
95
     duplex:2;
96
  u8 unknown;
97
  lbcd tx_freq[4];
98
} tx_memory[99];
99

    
100
#seekto 0x0780;
101
struct {
102
  lbcd lower_vhf[2];
103
  lbcd upper_vhf[2];
104
  lbcd lower_uhf[2];
105
  lbcd upper_uhf[2];
106
} limits;
107

    
108
struct vfosettings {
109
  lbcd freq[4];
110
  u8   rxtone;
111
  u8   unknown1;
112
  lbcd offset[3];
113
  u8   txtone;
114
  u8   power:1,
115
       bandwidth:1,
116
       unknown2:4,
117
       duplex:2;
118
  u8   step;
119
  u8   unknown3[4];
120
};
121

    
122
#seekto 0x0790;
123
struct {
124
  struct vfosettings uhf;
125
  struct vfosettings vhf;
126
} vfo;
127

    
128
#seekto 0x07C2;
129
struct {
130
  u8 squelch;
131
  u8 vox;
132
  u8 timeout;
133
  u8 save:1,
134
     unknown_1:1,
135
     dw:1,
136
     ste:1,
137
     beep:1,
138
     unknown_2:1,
139
     bclo:1,
140
     ch_flag:1;
141
  u8 backlight:2,
142
     relaym:1,
143
     scanm:1,
144
     pri:1,
145
     unknown_3:3;
146
  u8 unknown_4[3];
147
  u8 pri_ch;
148
} settings;
149

    
150
#seekto 0x07E0;
151
u16 fm_presets[16];
152

    
153
#seekto 0x0810;
154
struct {
155
  lbcd rx_freq[4];
156
  u8 rxtone;
157
  lbcd offset[4];
158
  u8 txtone;
159
  u8 ishighpower:1,
160
     iswide:1,
161
     dtcsinvt:1,
162
     unknown1:1,
163
     dtcsinvr:1,
164
     unknown2:1,
165
     duplex:2;
166
  u8 unknown;
167
  lbcd tx_freq[4];
168
} rx_memory[99];
169

    
170
#seekto 0x1008;
171
struct {
172
  u8 unknown[8];
173
  u8 name[6];
174
  u8 pad[2];
175
} names[128];
176
"""
177

    
178
STEPS = [5.0, 6.25, 10.0, 12.5, 20.0, 25.0]
179
STEP_LIST = [str(x) for x in STEPS]
180
BACKLIGHT_LIST = ["Off", "Key", "Continuous"]
181
TIMEOUT_LIST = ["Off"] + ["%s sec" % x for x in range(30, 210, 30)]
182
SCANM_LIST = ["TO", "CO"]
183
PRI_CH_LIST = ["Off"] + ["%s" % x for x in range(1, 100)]
184
CH_FLAG_LIST = ["Freq Mode", "Channel Mode"]
185
POWER_LIST = ["Low", "High"]
186
BANDWIDTH_LIST = ["Narrow", "Wide"]
187
DUPLEX_LIST = ["Off", "-", "+"]
188
STE_LIST = ["On", "Off"]
189

    
190
UV3R_DUPLEX = ["", "-", "+", ""]
191
UV3R_POWER_LEVELS = [chirp_common.PowerLevel("High", watts=2.00),
192
                     chirp_common.PowerLevel("Low", watts=0.50)]
193
UV3R_DTCS_POL = ["NN", "NR", "RN", "RR"]
194

    
195

    
196
@directory.register
197
class UV3RRadio(chirp_common.CloneModeRadio):
198
    """Baofeng UV-3R"""
199
    VENDOR = "Baofeng"
200
    MODEL = "UV-3R"
201
    NEEDS_COMPAT_SERIAL = False
202

    
203
    def get_features(self):
204
        rf = chirp_common.RadioFeatures()
205
        rf.has_settings = True
206
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
207
        rf.valid_modes = ["FM", "NFM"]
208
        rf.valid_power_levels = UV3R_POWER_LEVELS
209
        rf.valid_bands = [(136000000, 235000000), (400000000, 529000000)]
210
        rf.valid_skips = []
211
        rf.valid_duplexes = ["", "-", "+", "split"]
212
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
213
                                "->Tone", "->DTCS"]
214
        rf.valid_tuning_steps = STEPS
215
        rf.has_ctone = True
216
        rf.has_cross = True
217
        rf.has_tuning_step = False
218
        rf.has_bank = False
219
        rf.has_name = False
220
        rf.can_odd_split = True
221
        rf.memory_bounds = (1, 99)
222
        return rf
223

    
224
    def sync_in(self):
225
        self._mmap = uv3r_download(self)
226
        self.process_mmap()
227

    
228
    def sync_out(self):
229
        uv3r_upload(self)
230

    
231
    def process_mmap(self):
232
        self._memobj = bitwise.parse(UV3R_MEM_FORMAT, self._mmap)
233

    
234
    def get_memory(self, number):
235
        _mem = self._memobj.rx_memory[number - 1]
236
        mem = chirp_common.Memory()
237
        mem.number = number
238

    
239
        if _mem.get_raw()[0] == "\xff":
240
            mem.empty = True
241
            return mem
242

    
243
        mem.freq = int(_mem.rx_freq) * 10
244
        mem.offset = int(_mem.offset) * 10
245
        mem.duplex = UV3R_DUPLEX[_mem.duplex]
246
        if mem.offset > 60000000:
247
            if mem.duplex == "+":
248
                mem.offset = mem.freq + mem.offset
249
            elif mem.duplex == "-":
250
                mem.offset = mem.freq - mem.offset
251
            mem.duplex = "split"
252
        mem.power = UV3R_POWER_LEVELS[1 - _mem.ishighpower]
253
        if not _mem.iswide:
254
            mem.mode = "NFM"
255

    
256
        dtcspol = (int(_mem.dtcsinvt) << 1) + _mem.dtcsinvr
257
        mem.dtcs_polarity = UV3R_DTCS_POL[dtcspol]
258

    
259
        if _mem.txtone in [0, 0xFF]:
260
            txmode = ""
261
        elif _mem.txtone < 0x33:
262
            mem.rtone = chirp_common.TONES[_mem.txtone - 1]
263
            txmode = "Tone"
264
        elif _mem.txtone >= 0x33:
265
            tcode = chirp_common.DTCS_CODES[_mem.txtone - 0x33]
266
            mem.dtcs = tcode
267
            txmode = "DTCS"
268
        else:
269
            LOG.warn("Bug: tx_mode is %02x" % _mem.txtone)
270

    
271
        if _mem.rxtone in [0, 0xFF]:
272
            rxmode = ""
273
        elif _mem.rxtone < 0x33:
274
            mem.ctone = chirp_common.TONES[_mem.rxtone - 1]
275
            rxmode = "Tone"
276
        elif _mem.rxtone >= 0x33:
277
            rcode = chirp_common.DTCS_CODES[_mem.rxtone - 0x33]
278
            mem.dtcs = rcode
279
            rxmode = "DTCS"
280
        else:
281
            LOG.warn("Bug: rx_mode is %02x" % _mem.rxtone)
282

    
283
        if txmode == "Tone" and not rxmode:
284
            mem.tmode = "Tone"
285
        elif txmode == rxmode and txmode == "Tone" and mem.rtone == mem.ctone:
286
            mem.tmode = "TSQL"
287
        elif txmode == rxmode and txmode == "DTCS":
288
            mem.tmode = "DTCS"
289
        elif rxmode or txmode:
290
            mem.tmode = "Cross"
291
            mem.cross_mode = "%s->%s" % (txmode, rxmode)
292

    
293
        return mem
294

    
295
    def _set_tone(self, _mem, which, value, mode):
296
        if mode == "Tone":
297
            val = chirp_common.TONES.index(value) + 1
298
        elif mode == "DTCS":
299
            val = chirp_common.DTCS_CODES.index(value) + 0x33
300
        elif mode == "":
301
            val = 0
302
        else:
303
            raise errors.RadioError("Internal error: tmode %s" % mode)
304

    
305
        setattr(_mem, which, val)
306

    
307
    def _set_memory(self, mem, _mem):
308
        if mem.empty:
309
            _mem.set_raw("\xff" * 16)
310
            return
311

    
312
        _mem.rx_freq = mem.freq / 10
313
        if mem.duplex == "split":
314
            diff = mem.freq - mem.offset
315
            _mem.offset = abs(diff) / 10
316
            _mem.duplex = UV3R_DUPLEX.index(diff < 0 and "+" or "-")
317
            for i in range(0, 4):
318
                _mem.tx_freq[i].set_raw("\xFF")
319
        else:
320
            _mem.offset = mem.offset / 10
321
            _mem.duplex = UV3R_DUPLEX.index(mem.duplex)
322
            _mem.tx_freq = (mem.freq + mem.offset) / 10
323

    
324
        _mem.ishighpower = mem.power == UV3R_POWER_LEVELS[0]
325
        _mem.iswide = mem.mode == "FM"
326

    
327
        _mem.dtcsinvt = mem.dtcs_polarity[0] == "R"
328
        _mem.dtcsinvr = mem.dtcs_polarity[1] == "R"
329

    
330
        rxtone = txtone = 0
331
        rxmode = txmode = ""
332

    
333
        if mem.tmode == "DTCS":
334
            rxmode = txmode = "DTCS"
335
            rxtone = txtone = mem.dtcs
336
        elif mem.tmode and mem.tmode != "Cross":
337
            rxtone = txtone = mem.tmode == "Tone" and mem.rtone or mem.ctone
338
            txmode = "Tone"
339
            rxmode = mem.tmode == "TSQL" and "Tone" or ""
340
        elif mem.tmode == "Cross":
341
            txmode, rxmode = mem.cross_mode.split("->", 1)
342

    
343
            if txmode == "DTCS":
344
                txtone = mem.dtcs
345
            elif txmode == "Tone":
346
                txtone = mem.rtone
347

    
348
            if rxmode == "DTCS":
349
                rxtone = mem.dtcs
350
            elif rxmode == "Tone":
351
                rxtone = mem.ctone
352

    
353
        self._set_tone(_mem, "txtone", txtone, txmode)
354
        self._set_tone(_mem, "rxtone", rxtone, rxmode)
355

    
356
    def set_memory(self, mem):
357
        _tmem = self._memobj.tx_memory[mem.number - 1]
358
        _rmem = self._memobj.rx_memory[mem.number - 1]
359

    
360
        self._set_memory(mem, _tmem)
361
        self._set_memory(mem, _rmem)
362

    
363
    def get_settings(self):
364
        _settings = self._memobj.settings
365
        _vfo = self._memobj.vfo
366
        basic = RadioSettingGroup("basic", "Basic Settings")
367
        group = RadioSettings(basic)
368

    
369
        rs = RadioSetting("squelch", "Squelch Level",
370
                          RadioSettingValueInteger(0, 9, _settings.squelch))
371
        basic.append(rs)
372

    
373
        rs = RadioSetting("backlight", "LCD Back Light",
374
                          RadioSettingValueList(
375
                              BACKLIGHT_LIST,
376
                              BACKLIGHT_LIST[_settings.backlight]))
377
        basic.append(rs)
378

    
379
        rs = RadioSetting("beep", "Keypad Beep",
380
                          RadioSettingValueBoolean(_settings.beep))
381
        basic.append(rs)
382

    
383
        rs = RadioSetting("vox", "VOX Level (0=OFF)",
384
                          RadioSettingValueInteger(0, 9, _settings.vox))
385
        basic.append(rs)
386

    
387
        rs = RadioSetting("dw", "Dual Watch",
388
                          RadioSettingValueBoolean(_settings.dw))
389
        basic.append(rs)
390

    
391
        rs = RadioSetting("ste", "Squelch Tail Eliminate",
392
                          RadioSettingValueList(
393
                              STE_LIST, STE_LIST[_settings.ste]))
394
        basic.append(rs)
395

    
396
        rs = RadioSetting("save", "Battery Saver",
397
                          RadioSettingValueBoolean(_settings.save))
398
        basic.append(rs)
399

    
400
        rs = RadioSetting("timeout", "Time Out Timer",
401
                          RadioSettingValueList(
402
                              TIMEOUT_LIST, TIMEOUT_LIST[_settings.timeout]))
403
        basic.append(rs)
404

    
405
        rs = RadioSetting("scanm", "Scan Mode",
406
                          RadioSettingValueList(
407
                              SCANM_LIST, SCANM_LIST[_settings.scanm]))
408
        basic.append(rs)
409

    
410
        rs = RadioSetting("relaym", "Repeater Sound Response",
411
                          RadioSettingValueBoolean(_settings.relaym))
412
        basic.append(rs)
413

    
414
        rs = RadioSetting("bclo", "Busy Channel Lock Out",
415
                          RadioSettingValueBoolean(_settings.bclo))
416
        basic.append(rs)
417

    
418
        rs = RadioSetting("pri", "Priority Channel Scanning",
419
                          RadioSettingValueBoolean(_settings.pri))
420
        basic.append(rs)
421

    
422
        rs = RadioSetting("pri_ch", "Priority Channel",
423
                          RadioSettingValueList(
424
                              PRI_CH_LIST, PRI_CH_LIST[_settings.pri_ch]))
425
        basic.append(rs)
426

    
427
        rs = RadioSetting("ch_flag", "Display Mode",
428
                          RadioSettingValueList(
429
                              CH_FLAG_LIST, CH_FLAG_LIST[_settings.ch_flag]))
430
        basic.append(rs)
431

    
432
        _limit = int(self._memobj.limits.lower_vhf) / 10
433
        if _limit < 115 or _limit > 239:
434
            _limit = 144
435
        rs = RadioSetting("limits.lower_vhf", "VHF Lower Limit (115-239 MHz)",
436
                          RadioSettingValueInteger(115, 235, _limit))
437

    
438
        def apply_limit(setting, obj):
439
            value = int(setting.value) * 10
440
            obj.lower_vhf = value
441
        rs.set_apply_callback(apply_limit, self._memobj.limits)
442
        basic.append(rs)
443

    
444
        _limit = int(self._memobj.limits.upper_vhf) / 10
445
        if _limit < 115 or _limit > 239:
446
            _limit = 146
447
        rs = RadioSetting("limits.upper_vhf", "VHF Upper Limit (115-239 MHz)",
448
                          RadioSettingValueInteger(115, 235, _limit))
449

    
450
        def apply_limit(setting, obj):
451
            value = int(setting.value) * 10
452
            obj.upper_vhf = value
453
        rs.set_apply_callback(apply_limit, self._memobj.limits)
454
        basic.append(rs)
455

    
456
        _limit = int(self._memobj.limits.lower_uhf) / 10
457
        if _limit < 200 or _limit > 529:
458
            _limit = 420
459
        rs = RadioSetting("limits.lower_uhf", "UHF Lower Limit (200-529 MHz)",
460
                          RadioSettingValueInteger(200, 529, _limit))
461

    
462
        def apply_limit(setting, obj):
463
            value = int(setting.value) * 10
464
            obj.lower_uhf = value
465
        rs.set_apply_callback(apply_limit, self._memobj.limits)
466
        basic.append(rs)
467

    
468
        _limit = int(self._memobj.limits.upper_uhf) / 10
469
        if _limit < 200 or _limit > 529:
470
            _limit = 450
471
        rs = RadioSetting("limits.upper_uhf", "UHF Upper Limit (200-529 MHz)",
472
                          RadioSettingValueInteger(200, 529, _limit))
473

    
474
        def apply_limit(setting, obj):
475
            value = int(setting.value) * 10
476
            obj.upper_uhf = value
477
        rs.set_apply_callback(apply_limit, self._memobj.limits)
478
        basic.append(rs)
479

    
480
        vfo_preset = RadioSettingGroup("vfo_preset", "VFO Presets")
481
        group.append(vfo_preset)
482

    
483
        def convert_bytes_to_freq(bytes):
484
            real_freq = 0
485
            real_freq = bytes
486
            return chirp_common.format_freq(real_freq * 10)
487

    
488
        def apply_vhf_freq(setting, obj):
489
            value = chirp_common.parse_freq(str(setting.value)) / 10
490
            obj.vhf.freq = value
491

    
492
        val = RadioSettingValueString(
493
                0, 10, convert_bytes_to_freq(int(_vfo.vhf.freq)))
494
        rs = RadioSetting("vfo.vhf.freq",
495
                          "VHF RX Frequency (115.00000-236.00000)", val)
496
        rs.set_apply_callback(apply_vhf_freq, _vfo)
497
        vfo_preset.append(rs)
498

    
499
        rs = RadioSetting("vfo.vhf.duplex", "Shift Direction",
500
                          RadioSettingValueList(
501
                              DUPLEX_LIST, DUPLEX_LIST[_vfo.vhf.duplex]))
502
        vfo_preset.append(rs)
503

    
504
        def convert_bytes_to_offset(bytes):
505
            real_offset = 0
506
            real_offset = bytes
507
            return chirp_common.format_freq(real_offset * 10000)
508

    
509
        def apply_vhf_offset(setting, obj):
510
            value = chirp_common.parse_freq(str(setting.value)) / 10000
511
            obj.vhf.offset = value
512

    
513
        val = RadioSettingValueString(
514
                0, 10, convert_bytes_to_offset(int(_vfo.vhf.offset)))
515
        rs = RadioSetting("vfo.vhf.offset", "Offset (0.00-37.995)", val)
516
        rs.set_apply_callback(apply_vhf_offset, _vfo)
517
        vfo_preset.append(rs)
518

    
519
        rs = RadioSetting("vfo.vhf.power", "Power Level",
520
                          RadioSettingValueList(
521
                              POWER_LIST, POWER_LIST[_vfo.vhf.power]))
522
        vfo_preset.append(rs)
523

    
524
        rs = RadioSetting("vfo.vhf.bandwidth", "Bandwidth",
525
                          RadioSettingValueList(
526
                              BANDWIDTH_LIST,
527
                              BANDWIDTH_LIST[_vfo.vhf.bandwidth]))
528
        vfo_preset.append(rs)
529

    
530
        rs = RadioSetting("vfo.vhf.step", "Step",
531
                          RadioSettingValueList(
532
                              STEP_LIST, STEP_LIST[_vfo.vhf.step]))
533
        vfo_preset.append(rs)
534

    
535
        def apply_uhf_freq(setting, obj):
536
            value = chirp_common.parse_freq(str(setting.value)) / 10
537
            obj.uhf.freq = value
538

    
539
        val = RadioSettingValueString(
540
                0, 10, convert_bytes_to_freq(int(_vfo.uhf.freq)))
541
        rs = RadioSetting("vfo.uhf.freq",
542
                          "UHF RX Frequency (200.00000-529.00000)", val)
543
        rs.set_apply_callback(apply_uhf_freq, _vfo)
544
        vfo_preset.append(rs)
545

    
546
        rs = RadioSetting("vfo.uhf.duplex", "Shift Direction",
547
                          RadioSettingValueList(
548
                              DUPLEX_LIST, DUPLEX_LIST[_vfo.uhf.duplex]))
549
        vfo_preset.append(rs)
550

    
551
        def apply_uhf_offset(setting, obj):
552
            value = chirp_common.parse_freq(str(setting.value)) / 10000
553
            obj.uhf.offset = value
554

    
555
        val = RadioSettingValueString(
556
                0, 10, convert_bytes_to_offset(int(_vfo.uhf.offset)))
557
        rs = RadioSetting("vfo.uhf.offset", "Offset (0.00-69.995)", val)
558
        rs.set_apply_callback(apply_uhf_offset, _vfo)
559
        vfo_preset.append(rs)
560

    
561
        rs = RadioSetting("vfo.uhf.power", "Power Level",
562
                          RadioSettingValueList(
563
                              POWER_LIST, POWER_LIST[_vfo.uhf.power]))
564
        vfo_preset.append(rs)
565

    
566
        rs = RadioSetting("vfo.uhf.bandwidth", "Bandwidth",
567
                          RadioSettingValueList(
568
                              BANDWIDTH_LIST,
569
                              BANDWIDTH_LIST[_vfo.uhf.bandwidth]))
570
        vfo_preset.append(rs)
571

    
572
        rs = RadioSetting("vfo.uhf.step", "Step",
573
                          RadioSettingValueList(
574
                              STEP_LIST, STEP_LIST[_vfo.uhf.step]))
575
        vfo_preset.append(rs)
576

    
577
        fm_preset = RadioSettingGroup("fm_preset", "FM Radio Presets")
578
        group.append(fm_preset)
579

    
580
        for i in range(0, 16):
581
            if self._memobj.fm_presets[i] < 0x01AF:
582
                used = True
583
                preset = self._memobj.fm_presets[i] / 10.0 + 65
584
            else:
585
                used = False
586
                preset = 65
587
            rs = RadioSetting("fm_presets_%1i" % i, "FM Preset %i" % (i + 1),
588
                              RadioSettingValueBoolean(used),
589
                              RadioSettingValueFloat(65, 108, preset, 0.1, 1))
590
            fm_preset.append(rs)
591

    
592
        return group
593

    
594
    def set_settings(self, settings):
595
        _settings = self._memobj.settings
596
        for element in settings:
597
            if not isinstance(element, RadioSetting):
598
                if element.get_name() == "fm_preset":
599
                    self._set_fm_preset(element)
600
                else:
601
                    self.set_settings(element)
602
                    continue
603
            else:
604
                try:
605
                    name = element.get_name()
606
                    if "." in name:
607
                        bits = name.split(".")
608
                        obj = self._memobj
609
                        for bit in bits[:-1]:
610
                            if "/" in bit:
611
                                bit, index = bit.split("/", 1)
612
                                index = int(index)
613
                                obj = getattr(obj, bit)[index]
614
                            else:
615
                                obj = getattr(obj, bit)
616
                        setting = bits[-1]
617
                    else:
618
                        obj = _settings
619
                        setting = element.get_name()
620

    
621
                    if element.has_apply_callback():
622
                        LOG.debug("Using apply callback")
623
                        element.run_apply_callback()
624
                    else:
625
                        LOG.debug("Setting %s = %s" % (setting, element.value))
626
                        setattr(obj, setting, element.value)
627
                except Exception as e:
628
                    LOG.debug(element.get_name())
629
                    raise
630

    
631
    def _set_fm_preset(self, settings):
632
        for element in settings:
633
            try:
634
                index = (int(element.get_name().split("_")[-1]))
635
                val = list(element.value)
636
                if val[0].get_value():
637
                    value = int(val[1].get_value() * 10 - 650)
638
                else:
639
                    value = 0x01AF
640
                LOG.debug("Setting fm_presets[%1i] = %s" % (index, value))
641
                setting = self._memobj.fm_presets
642
                setting[index] = value
643
            except Exception as e:
644
                LOG.debug(element.get_name())
645
                raise
646

    
647
    @classmethod
648
    def match_model(cls, filedata, filename):
649
        return len(filedata) == 3648
650

    
651
    def get_raw_memory(self, number):
652
        _rmem = self._memobj.tx_memory[number - 1]
653
        _tmem = self._memobj.rx_memory[number - 1]
654
        return repr(_rmem) + repr(_tmem)
(2-2/2)