Project

General

Profile

New Model #3363 » tk790.py

draft [I think] - Pavel Milanes, 12/06/2024 10:13 PM

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

import logging
import struct
import time

from chirp import chirp_common, directory, memmap, errors, util, bitwise
from textwrap import dedent
from chirp.settings import RadioSettingGroup, RadioSetting, \
RadioSettingValueBoolean, RadioSettingValueList, \
RadioSettingValueString, RadioSettingValueInteger, \
RadioSettings

LOG = logging.getLogger(__name__)

##### IMPORTANT MEM DATA #########################################
# This radios have a odd mem structure, it seems like you have to
# manage 3 memory sectors that we concatenate on just one big
# memmap, as follow
#
# Low memory (Main CPU)
# 0x0000 to 0x4000
# Mid memory (Unknown)
# 0x4000 to 0x4090
# High memory (Head)
# 0x4090 to 0x6090
###############################################################

MEM_FORMAT = """

#seekto 0x1480;
struct {
u8 start; // # of channel to start in this group
u8 length; // # of channel in the group
} group_limits[160];

#seekto 0x1600;
struct {
u8 number; // CH number relative to the group, 1-160
u8 index; // index relative to the memory space memory[index]
} group_belong[160];

#seekto 0x1800;
struct {
lbcd rxfreq[4]; // 00-03
lbcd txfreq[4]; // 04-07
ul16 rxtone;
ul16 txtone;
u8 unknown0:1,
power:1, // power 0 = high, 1 = low
beatshift:1, // beat shift, 1 = on
bcl:1, // busy channel lockout, 1 = on
pttid:1, // ptt id, 1 = on
unknown1:3;
u8 unknown2:4,
add:1, // scan add, 1 = add
unknown3:1,
wide:1, // Wide = 1, narrow = 0
unknown4:1;
u8 unknown5;
u8 nose;
} memory[160];

#seekto 0x3DF0;
char poweron_msg[14];

#seekto 0x3ED0;
struct {
u8 unknown10[10];
char soft[6];
u8 rid[10];
u8 unknown11[6];
u8 unknown12[11];
char soft_ver[5];
} properties;

#seekto 0x4090;
struct {
char name[16];
} grp_name[160];

#seekto 0x4a90;
struct {
char name[16];
} chs_chs_names[160];

"""

MEM_SIZE = 0x6090 # 24720 bytes, 24,720 KiB
ACK_CMD = "\x06"
RX_BLOCK_SIZE_L = 128
MEM_LR = range(0x0380, 0x0400)
RX_BLOCK_SIZE_M = 16
MEM_MR = range(1, 10)
RX_BLOCK_SIZE_H = 32
MEM_HR = range(0, 0x2000, RX_BLOCK_SIZE_H)
EMPTY_L = "\xFF" * RX_BLOCK_SIZE_L
EMPTY_H = "\xFF" * RX_BLOCK_SIZE_H
POWER_LEVELS = [chirp_common.PowerLevel("High", watts=45),
chirp_common.PowerLevel("Low", watts=5)]
MODES = ["NFM", "FM"] # 12.5 / 25 Khz
VALID_CHARS = chirp_common.CHARSET_UPPER_NUMERIC + "_-*()/\+=)"
NAME_CHARS = 8
SKIP_VALUES = ["S", ""]
TONES = chirp_common.TONES
DTCS_CODES = chirp_common.DTCS_CODES

def _raw_recv(radio, amount):
"""Raw read from the radio device"""
data = ""
try:
data = radio.pipe.read(amount)
except:
raise errors.RadioError("Error reading data from radio")

# DEBUG
LOG.debug("<== (%d) bytes: %s" % (len(data), util.hexprint(data)))
return data


def _raw_send(radio, data):
"""Raw send to the radio device"""
try:
radio.pipe.write(data)
# DEBUG
LOG.debug("==> (%d) bytes: %s" % (len(data), util.hexprint(data)))
except:
raise errors.RadioError("Error sending data to radio")


def _close_radio(radio):
"""Get the radio out of program mode"""
# 3 times, it will don't harm in normal work,
# but it help's a lot in the developer process
_raw_send(radio, "\x45")


def _checksum(data):
"""the radio block checksum algorithm"""
cs = 0
for byte in data:
cs += ord(byte)
return cs % 256


def _send(radio, frame):
"""Generic send data to the radio"""
_raw_send(radio, frame)


def _make_framel(cmd, addr):
"""Pack the info in the format it likes"""
# x52 x0F (x0380-x0400)
return struct.pack(">BBH", ord(cmd), 0x0F, addr)


def _make_framem(cmd, addr):
"""Pack the info in the format it likes"""
# x54 x0F (x00-x0A)
return struct.pack(">BBB", ord(cmd), 0x0F, addr)


def _make_frameh(cmd, addr):
"""Pack the info in the format it likes"""
# x53 x8F (x0000-x2000) x20
return struct.pack(">BBHB", ord(cmd), 0x8F, addr, RX_BLOCK_SIZE_H)


def _handshake(radio, msg="", full=True):
"""Make a full handshake"""
if full is True:
# flush input
radio.pipe.flushInput()
# send ACK
_raw_send(radio, ACK_CMD)

# receive ACK
ack = _raw_recv(radio, 1)

# check ACK
if ack != ACK_CMD:
_close_radio(radio)
mesg = "Handshake failed, got ack: '0x%02x': " % ord(ack)
mesg += msg
# DEBUG
LOG.debug(mesg)
raise errors.RadioError(mesg)


def _recvl(radio):
"""Receive low data block from the radio, 130 or 2 bytes"""
rxdata = _raw_recv(radio, RX_BLOCK_SIZE_L + 2)
if len(rxdata) == 2 and rxdata[0] == "\x5A" and rxdata[1] == "\xFF":
# when the RX block has 2 bytes and the paylod+CS is \x5A\xFF
# then the block is all \xFF
_handshake(radio, "short block")
return False
elif len(rxdata) == RX_BLOCK_SIZE_L + 2 and rxdata[0] == "W":
# Data block is W + Data(128) + CS
rcs = ord(rxdata[-1])
data = rxdata[1:-1]
ccs = _checksum(data)

if rcs != ccs:
msg = "Block Checksum Error! real %02x, calculated %02x" % \
(rcs, ccs)
LOG.error(msg)
_handshake(radio)
_close_radio(radio)
raise errors.RadioError(msg)

_handshake(radio, "After checksum in Low Mem")
return data
else:
raise errors.RadioError("Radio send an answer we don't understand")


def _recvh(radio):
"""Receive high data from the radio, 35 or 4 bytes"""
rxdata = _raw_recv(radio, RX_BLOCK_SIZE_H + 3)
# There are two valid options, the first byte is the content
if len(rxdata) == 4 and rxdata[0] == "\x5B" and rxdata[3] == "\xFF":
# 4 bytes, x5B = empty; payload = xFF (block is all xFF)
_handshake(radio, "Short block in High Mem")
return False
elif len(rxdata) == RX_BLOCK_SIZE_H + 3 and rxdata[0] == "\x58":
# 35 bytes, x58 + address(2) + data(32), no checksum
data = rxdata[3:]
_handshake(radio, "After data in High Mem")
return data
else:
raise errors.RadioError("Radio send a answer we don't understand")


def _open_radio(radio):
"""Open the radio into program mode and check if it's the correct model"""
# minimum timeout is 0.13, set to 0.25 to be safe
radio.pipe.setBaudrate(9600)
radio.pipe.setParity("N")
radio.pipe.setTimeout(0.1)
radio.pipe.flushInput()

# DEBUG
LOG.debug("Starting program mode.")

# send the MAGIC word
_raw_send(radio, "PROGRAM")
time.sleep(0.03)
ack = _raw_recv(radio, 1)

# DEBUG
LOG.debug("Magic send.")

if ack != ACK_CMD:
# DEBUG
LOG.debug("Radio didn't acepted program mode.")
raise errors.RadioError("The radio doesn't accept program mode.")

# DEBUG
LOG.debug("Received correct ACK to the MAGIC, send ID query.")
LOG.info("Radio entered Program mode.")

_raw_send(radio, "\x02\x0F")
rid = _raw_recv(radio, 10)

if not (radio.TYPE in rid):
# bad response, properly close the radio before exception
_close_radio(radio)

# DEBUG
LOG.debug("Incorrect model ID:")
LOG.debug(util.hexprint(rid))

raise errors.RadioError(
"Incorrect model ID, got %s, it not contains %s" %
(rid.strip("\xff"), radio.TYPE))

# DEBUG
LOG.info("Positive ID on radio.")
LOG.debug("Full ident string is:")
LOG.debug(util.hexprint(rid))

_handshake(radio)


def do_download(radio):
""" The download function """
# UI progress
status = chirp_common.Status()
status.cur = 0
status.max = MEM_SIZE
status.msg = ""
radio.status_fn(status)

# open the radio
_open_radio(radio)

# initialize variables
data = ""
bar = 0

# DEBUG
LOG.debug("Starting the download from radio.")

# speed up the reading for this stage
radio.pipe.setTimeout(0.08) # never below 0.08 or you will get errors

for addr in MEM_LR:
radio.pipe.flushInput()
_send(radio, _make_framel("R", addr))
d = _recvl(radio)
# if empty block, return false = full of xFF
if d == False:
d = EMPTY_L

# aggregate the data
data += d

# UI update
bar += RX_BLOCK_SIZE_L
status.cur = bar
status.msg = "Cloning from Main MCU (Low mem)..."
radio.status_fn(status)

# DEBUG
LOG.debug("Main MCU (low) mem received")

# speed up the reading for this stage
radio.pipe.setTimeout(0.04) # never below 0.04 or you will get errors

for addr in MEM_MR:
_send(radio, _make_framem("T", addr))
d = _raw_recv(radio, 17)

if len(d) != 17 :
raise errors.RadioError(
"Problem receiving short block %d on mid mem" % addr)

# Aggregate data ans hansdhake
data += d[1:]
_handshake(radio, "Middle mem ack error")

# UI update
bar += RX_BLOCK_SIZE_M
status.cur = bar
status.msg = "Cloning from 'unknown' (mid mem)..."
radio.status_fn(status)

# DEBUG
LOG.debug("Middle mem received.")

# speed up the reading for this stage
radio.pipe.setTimeout(0.08) # never below 0.08 or you will get errors
for addr in MEM_HR:
_send(radio, _make_frameh("S", addr))
d = _recvh(radio)
# if empty block, return false = full of xFF
if d == False:
d = EMPTY_H

# aggregate the data
data += d

# UI update
bar += RX_BLOCK_SIZE_H
status.cur = bar
status.msg = "Cloning from Head (High mem)..."
radio.status_fn(status)

# DEBUG
LOG.debug("Head (high) mem received")
LOG.info("Full Memory received ok.")

_close_radio(radio)
return memmap.MemoryMap(data)


def do_upload(radio):
""" The upload function """
# UI progress
status = chirp_common.Status()
status.cur = 0
status.max = MEM_SIZE
status.msg = "Getting the radio into program mode."
radio.status_fn(status)
# open the radio
_open_radio(radio)

# initialize variables
bar = 0
img = radio.get_mmap()

# DEBUG
LOG.debug("Starting the upload to the radio")

# speed up the reading for this stage
radio.pipe.setTimeout(0.08) # never below 0.08 or you will get errors
for addr in MEM_LR:
# this is the data to write
data = img[bar:bar + RX_BLOCK_SIZE_L]
# this is the full packet to send
sdata = ""

# flag
short = False

# building the data to send
if data == EMPTY_L:
# empty block
sdata = _make_framel("Z", addr) + "\xFF"
short = True
else:
# normal
cs = _checksum(data)
sdata = _make_framel("W", addr) + data + chr(cs)

# send the data
radio.pipe.flushInput()
_send(radio, sdata)

# DEBUG
LOG.debug("Sended memmap pos 0x%04x" % bar)

# slow MCU
time.sleep(0.15)

# check ack
msg = "Bad ACK on low block %04x" % addr
_handshake(radio, msg, False)

# UI Update
bar += RX_BLOCK_SIZE_L
status.cur = bar
status.msg = "Cloning to Main MCU (Low mem)..."
radio.status_fn(status)

# DEBUG
LOG.debug("Main MCU (low) mem received")

# speed up the reading for this stage
radio.pipe.setTimeout(0.04) # never below 0.04 or you will get errors
for addr in MEM_MR:
# this is the data to write
data = img[bar:bar + RX_BLOCK_SIZE_M]
sdata = _make_framem("Y", addr) + "\x00" + data

# send it
_send(radio, sdata)

# DEBUG
LOG.debug("Sended memmap pos 0x%04x" % bar)

# slow MCU
time.sleep(0.2)

# check ack
msg = "Bad ACK on mid block %04x" % addr
_handshake(radio, msg, not short)

# UI Update
bar += RX_BLOCK_SIZE_M
status.cur = bar
status.msg = "Cloning from middle mem..."
radio.status_fn(status)

# DEBUG
LOG.debug("Middle mem received")

# speed up the reading for this stage
radio.pipe.setTimeout(0.08) # never below 0.08 or you will get errors
for addr in MEM_HR:
# this is the data to write
data = img[bar:bar + RX_BLOCK_SIZE_H]
# this is the full packet to send
sdata = ""

# building the data to send
if data == EMPTY_H:
# empty block
sdata = _make_frameh("[", addr) + "\xFF"
else:
# normal
sdata = _make_frameh("X", addr) + data

# send the data
radio.pipe.flushInput()
_send(radio, sdata)

# DEBUG
LOG.debug("Sended memmap pos 0x%04x" % bar)

# slow MCU
time.sleep(0.15)

# check ack
msg = "Bad ACK on low block %04x" % addr
_handshake(radio, msg, False)

# UI Update
bar += RX_BLOCK_SIZE_H
status.cur = bar
status.msg = "Cloning to Head MCU (high mem)..."
radio.status_fn(status)

# DEBUG
LOG.debug("Head (high) mem received")
# DEBUG
LOG.info("Upload finished")


_close_radio(radio)


def model_match(cls, data):
"""Match the opened/downloaded image to the correct version"""
rid = _get_rid(data)

# DEBUG
LOG.debug("Radio ID is:")
LOG.debug(util.hexprint(rid))

if (rid in cls.VARIANTS):
LOG.info("File/Data match for ID.")
return True
else:
LOG.info("BAD File/Data match.")
return False


def _get_rid(data):
"""Get the radio ID string from a mem string"""
return data[0x03EE0:0x3EE6]


class Kenwoodx90BankModel(chirp_common.BankModel):
"""Testing the bank model on kennwood"""
channelAlwaysHasBank = True

def get_num_mappings(self):
return self._radio._num_banks

def get_mappings(self):
banks = []
for i in range(0, self._radio._num_banks):
bindex = i + 1
bank = self._radio._bclass(self, i, "%03i" % bindex)
bank.index = i
banks.append(bank)
return banks

def add_memory_to_mapping(self, memory, bank):
self._radio._set_bank(memory.number, bank.index)

def remove_memory_from_mapping(self, memory, bank):
if self._radio._get_bank(memory.number) != bank.index:
raise Exception("Memory %i not in bank %s. Cannot remove." %
(memory.number, bank))

# We can't "Remove" it for good
# the kenwood paradigm don't allow it
# instead we move it to bank 0
self._radio._set_bank(memory.number, 0)

def get_mapping_memories(self, bank):
memories = []
for i in range(0, self._radio._upper):
if self._radio._get_bank(i) == bank.index:
memories.append(self._radio.get_memory(i))
return memories

def get_memory_mappings(self, memory):
index = self._radio._get_bank(memory.number)
return [self.get_mappings()[index]]


class memBank(chirp_common.Bank):
"""A bank model for kenwood"""
# Integral index of the bank, not to be confused with per-memory
# bank indexes
index = 0


class Kenwoodx90(chirp_common.CloneModeRadio, chirp_common.ExperimentalRadio):
"""Kenwood TK-790 radio base class"""
VENDOR = "Kenwood"
BAUD_RATE = 9600
VARIANT = ""
MODEL = ""
NAME_LENGTH = 6
# others
_memsize = MEM_SIZE
_range = [136000000, 162000000]
_upper = 160
_chs_progs = 0
banks = dict()
_num_banks = 160
_bclass = memBank
_kind = ""

@classmethod
def get_prompts(cls):
rp = chirp_common.RadioPrompts()
rp.experimental = \
('This driver is experimental and for personal use only.'
'It has a limited set of features, but the most used.'
''
)
rp.pre_download = _(dedent("""\
Follow this instructions to read from your radio info:
1 - Turn off your radio
2 - Connect your interface cable
3 - Turn on your radio (unblock it if password protected)
4 - Do the download of your radio data
"""))
rp.pre_upload = _(dedent("""\
Follow this instructions to write to your radio info:
1 - Turn off your radio
2 - Connect your interface cable
3 - Turn on your radio (unblock it if password protected)
4 - Do the upload of your radio data
"""))
return rp

def get_features(self):
"""Return information about this radio's features"""
rf = chirp_common.RadioFeatures()
rf.has_settings = False #True
rf.has_bank = True
rf.has_tuning_step = False
rf.has_name = True
rf.has_offset = True
rf.has_mode = True
rf.has_dtcs = True
rf.has_rx_dtcs = True
rf.has_dtcs_polarity = True
rf.has_ctone = True
rf.has_cross = True
rf.valid_modes = MODES
rf.valid_duplexes = ["", "-", "+", "off"]
rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
rf.valid_cross_modes = [
"Tone->Tone",
"DTCS->",
"->DTCS",
"Tone->DTCS",
"DTCS->Tone",
"->Tone",
"DTCS->DTCS"]
rf.valid_power_levels = POWER_LEVELS
rf.valid_characters = VALID_CHARS
rf.valid_skips = SKIP_VALUES
rf.valid_dtcs_codes = DTCS_CODES
rf.valid_bands = [self._range]
rf.valid_name_length = NAME_CHARS
rf.memory_bounds = (1, self._upper)
return rf

def _fill(self, offset, data):
"""Fill an specified area of the memmap with the passed data"""
for addr in range(0, len(data)):
self._mmap[offset + addr] = data[addr]

def _prep_data(self):
"""Prepare the areas in the memmap to do a consistend write
it has to make an update on the x1600 area with banks and channel
info; other in the x1000 with banks and channel counts
and a last one in x7000 with flog data"""
rchs = 0
data = dict()

# sorting the data
for ch in range(0, self._upper):
mem = self._memobj.memory[ch]
bnumb = int(mem.bnumb)
bank = int(mem.bank)
if bnumb != 255 and (bank != 255 and bank != 0):
try:
data[bank].append(ch)
except:
data[bank] = list()
data[bank].append(ch)
data[bank].sort()
# counting the real channels
rchs = rchs + 1

# updating the channel/bank count
self._memobj.settings.channels = rchs
self._chs_progs = rchs
self._memobj.settings.banks = len(data)

# building the data for the memmap
fdata = ""

for k, v in data.iteritems():
# posible bad data
if k == 0:
k = 1
raise errors.InvalidValueError(
"Invalid bank value '%k', bad data in the image? \
Triying to fix this, review your bank data!" % k)
c = 1
for i in v:
fdata += chr(k) + chr(c) + chr(k - 1) + chr(i)
c = c + 1

# fill to match a full 256 bytes block
fdata += (len(fdata) % 256) * "\xFF"

# updating the data in the memmap [x300]
self._fill(0x300, fdata)

# update the info in x1000; it has 2 bytes with
# x00 = bank , x01 = bank's channel count
# the rest of the 14 bytes are \xff
bdata = ""
for i in range(1, len(data) + 1):
line = chr(i) + chr(len(data[i]))
line += "\xff" * 14
bdata += line

# fill to match a full 256 bytes block
bdata += (256 - (len(bdata)) % 256) * "\xFF"

# fill to match the whole area
bdata += (16 - len(bdata) / 256) * EMPTY_BLOCK

# updating the data in the memmap [x1000]
self._fill(0x1000, bdata)

# DTMF id for each channel, 5 bytes lbcd at x7000
# ############## TODO ###################
fldata = "\x00\xf0\xff\xff\xff" * self._chs_progs + \
"\xff" * (5 * (self._upper - self._chs_progs))

# write it
# updating the data in the memmap [x7000]
self._fill(0x7000, fldata)

def _set_variant(self):
"""Select and set the correct variables for the class acording
to the correct variant of the radio"""
rid = _get_rid(self.get_mmap())

# indentify the radio variant and set the enviroment to it's values
try:
self._upper, low, high, self._kind = self.VARIANTS[rid]
self._range = [low * 1000000, high * 1000000]

# put the VARIANT in the class, clean the model / CHs / Type
# in the same layout as the KPG program
self._VARIANT = self.MODEL + " [" + str(self._upper) + "CH]: "
self._VARIANT += self._kind + ", " + str(self._range[0]/1000000) + "-"
self._VARIANT += str(self._range[1]/1000000) + " Mhz"

except KeyError:
LOG.debug("Wrong Kenwood radio, ID or unknown variant")
LOG.debug(util.hexprint(rid))
raise errors.RadioError(
"Wrong Kenwood radio, ID or unknown variant, see LOG output.")
return False

def sync_in(self):
"""Do a download of the radio eeprom"""
self._mmap = do_download(self)
self.process_mmap()

def sync_out(self):
"""Do an upload to the radio eeprom"""
#try:
#do_upload(self)
#except errors.RadioError:
#raise
#except Exception, e:
#raise errors.RadioError("Failed to communicate with radio: %s" % e)

do_upload(self)

def _get_bank_struct(self):
"""Parse the bank data in the mem into the self.bank variable"""
# Variables
gl = self._memobj.group_limits
gb = self._memobj.group_belong
bank_count = 0

for bg in gl:
# check to see if it's the end
if bg.start == 255 and bg.length == 255:
# we have processed all the valid entries
# DEBUG
LOG.info("Bank %d, is the last bank aborting.." %
int(bank_count - 1))
break

for i in range(bg.length):
# bank inside this channel
position = bg.start + i
index = gb[position].index

try:
self.banks[bank_count].append(index)
except KeyError:
self.banks[bank_count] = list()
self.banks[bank_count].append(index)

# increment the bank count
bank_count += 1

def process_mmap(self):
"""Process the memory object"""
# load the memobj
self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)

# to ser the vars on the class to the correct ones
self._set_variant()

# load the bank data
self._get_bank_struct()

def get_raw_memory(self, number):
"""Return a raw representation of the memory object, which
is very helpful for development"""
return repr(self._memobj.memory[number])

def _decode_tone(self, val):
"""Parse the tone data to decode from mem, it returns:
Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
val = int(val)
if val == 65535:
return '', None, None
elif val >= 0x2800:
code = int("%03o" % (val & 0x07FF))
pol = (val & 0x8000) and "R" or "N"
return 'DTCS', code, pol
else:
a = val / 10.0
return 'Tone', a, None

def _encode_tone(self, memval, mode, value, pol):
"""Parse the tone data to encode from UI to mem"""
if mode == '':
memval.set_raw("\xff\xff")
elif mode == 'Tone':
memval.set_value(int(value * 10))
elif mode == 'DTCS':
val = int("%i" % value, 8) + 0x2800
if pol == "R":
val += 0xA000
memval.set_value(val)
else:
raise Exception("Internal error: invalid mode `%s'" % mode)

def get_memory(self, number):
"""Get the mem representation from the radio image"""
_mem = self._memobj.memory[number - 1]
_chs_names = self._memobj.chs_chs_names[number - 1]

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

# Memory number
mem.number = number

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

# Freq and offset
mem.freq = int(_mem.rxfreq) * 10
# tx freq can be blank
if _mem.get_raw()[4] == "\xFF":
# TX freq not set
mem.offset = 0
mem.duplex = "off"
else:
# TX feq set
offset = (int(_mem.txfreq) * 10) - mem.freq
if offset < 0:
mem.offset = abs(offset)
mem.duplex = "-"
elif offset > 0:
mem.offset = offset
mem.duplex = "+"
else:
mem.offset = 0

# name TAG of the channel
mem.name = str(_chs_names.name).rstrip(" ")[:NAME_CHARS + 1]

# power (0 = high, 1 = low)
mem.power = POWER_LEVELS[int(_mem.power)]

# wide/marrow
mem.mode = MODES[int(_mem.wide)]

# skip
mem.skip = SKIP_VALUES[int(_mem.add)]

# tone data
rxtone = txtone = None
txtone = self._decode_tone(_mem.txtone)
rxtone = self._decode_tone(_mem.rxtone)
chirp_common.split_tone_decode(mem, txtone, rxtone)

# Extra
mem.extra = RadioSettingGroup("extra", "Extra")

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

pttid = RadioSetting("pttid", "PTT ID",
RadioSettingValueBoolean(bool(_mem.pttid)))
mem.extra.append(pttid)

beat = RadioSetting("beatshift", "Beat Shift",
RadioSettingValueBoolean(bool(_mem.beatshift)))
mem.extra.append(beat)

return mem

def set_memory(self, mem):
"""Set the memory data in the eeprom img from the UI"""
# get the eprom representation of this channel
_mem = self._memobj.memory[mem.number - 1]
_chs_names = self._memobj.names[mem.number - 1]

# if empty memmory
if mem.empty:
# the channel it self
_mem.set_raw("\xFF" * 16)
# the name tag
_chs_names.name.set_raw("\xFF" * 16)
return

# frequency
_mem.rxfreq = mem.freq / 10

# duplex
if mem.duplex == "+":
_mem.txfreq = (mem.freq + mem.offset) / 10
elif mem.duplex == "-":
_mem.txfreq = (mem.freq - mem.offset) / 10
elif mem.duplex == "off":
_mem.txfreq.set_raw("\xFF" * 4)
else:
_mem.txfreq = mem.freq / 10

# tone data
((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
chirp_common.split_tone_encode(mem)
self._encode_tone(_mem.txtone, txmode, txtone, txpol)
self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)

# name TAG of the channel
_chs_names.name = str(mem.name).ljust(16, " ")

# power, # default power is low (0 = high, 1 = low)
_mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)

# wide/marrow
_mem.wide = MODES.index(mem.mode)

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

# reseting unknowns, this have to be set !?!?!?!?
_mem.nose.set_raw("\xFE")

# extra settings
if len(mem.extra) > 0:
# there are setting, parse
for setting in mem.extra:
setattr(_mem, setting.get_name(), bool(setting.value))
else:
msg = "Channel #%d has no extra data, loading defaults" % \
int(mem.number - 1)
LOG.info(msg)
# there is no extra settings, load defaults
_mem.bcl = 0
_mem.pttid = 0
_mem.beatshift = 0
# unknowns
_mem.unknown1 = 0
_mem.unknown4 = 0
_mem.unknown7 = 0
_mem.unknown8 = 0
_mem.unknown9 = 0

return mem

@classmethod
def match_model(cls, filedata, filename):
match_size = False
match_model = False

# testing the file data size
if len(filedata) == MEM_SIZE:
match_size = True
LOG.info("File match for size")

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

if match_size and match_model:
return True
else:
return False

def get_bank_model(self):
"""Pass the bank model to the UI part"""
rf = self.get_features()
if rf.has_bank is True:
return Kenwoodx90BankModel(self)
else:
return None

def _get_bank(self, loc):
"""Get the bank data for a specific channel"""
for k in self.banks:
if loc in self.banks[k]:
return k

return 0


def _set_bank(self, loc, bank):
"""Set the bank data for a specific channel"""
# is the bank created?
try:
if not (loc - 1) in self.banks[bank]:
self.banks[bank].append(loc -1)
else:
msg = "You can reassign a channel to it self"
raise errors.InvalidValueError(msg)
except KeyError:
self.banks[bank] = list()
self.banks[bank].append(loc -1)


@directory.register
class TK790_Radios(Kenwoodx90):
"""Kenwood TK-790 K/K2"""
MODEL = "TK-790"
TYPE = "M0790"
VARIANTS = {
"M0790\x04": (160, 148, 174, "K"),
"M0790\x05": (160, 136, 156, "K2")
}
(3-3/8)