Skip to content

Commit

Permalink
Increase config size to 64k for HX890, addressing #39, fix codestyle …
Browse files Browse the repository at this point in the history
…warnings
  • Loading branch information
cr committed May 31, 2024
1 parent 2d028c4 commit 2b1b03b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions hxtool/cli/nmea.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ def nmea_dump(h):


def print_nmea(h):
for l in nmea_dump(h):
sys.stdout.write(l)
for line in nmea_dump(h):
sys.stdout.write(line)
sys.stdout.flush()
16 changes: 9 additions & 7 deletions hxtool/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

class GenericHXConfig(object):

CONFIG_SIZE = 0x8000

def __init__(self, protocol: GenericHXProtocol):
self.p = protocol

def config_read(self, progress=False):
config_data = b''
bytes_to_go = 0x8000
for offset in range(0x0000, 0x8000, 0x40):
bytes_to_go = self.CONFIG_SIZE
for offset in range(0x0000, self.CONFIG_SIZE, 0x40):
if progress:
percent_done = int(100.0 * offset / bytes_to_go)
if offset % 0x1000 == 0:
Expand All @@ -29,10 +31,10 @@ def config_read(self, progress=False):

def config_write(self, data, check_region=True, progress=False):
bytes_to_go = len(data)
if bytes_to_go != 0x8000:
if bytes_to_go != self.CONFIG_SIZE:
raise ProtocolError("Unexpected config data size")
magic = self.p.read_config_memory(0x0000, 2)
magic_end = self.p.read_config_memory(0x7ffe, 2)
magic_end = self.p.read_config_memory(self.CONFIG_SIZE-2, 2)
if magic != data[:2] or magic_end != data[-2:]:
raise ProtocolError("Unexpected config magic in device")
region = self.p.read_config_memory(0x010f, 1)
Expand All @@ -48,13 +50,13 @@ def config_write(self, data, check_region=True, progress=False):
logger.info(f"0 / {bytes_to_go} bytes (0%)")
self.p.write_config_memory(0x0002, data[0x0002:0x000f])
self.p.write_config_memory(0x0010, data[0x0010:0x0040])
for offset in range(0x0040, 0x7fc0, 0x40):
for offset in range(0x0040, self.CONFIG_SIZE-0x40, 0x40):
if progress:
percent_done = int(100.0 * offset / bytes_to_go)
if offset % 0x1000 == 0:
logger.info(f"{offset} / {bytes_to_go} bytes ({percent_done}%)")
self.p.write_config_memory(offset, data[offset:offset+0x40])
self.p.write_config_memory(0x7fc0, data[0x7fc0:0x7ffe])
self.p.write_config_memory(self.CONFIG_SIZE-0x40, data[self.CONFIG_SIZE-0x40:self.CONFIG_SIZE-2])
if progress:
logger.info(f"{bytes_to_go} / {bytes_to_go} bytes (100%)")

Expand Down Expand Up @@ -148,4 +150,4 @@ class HX870Config(GenericHXConfig):


class HX890Config(GenericHXConfig):
pass
CONFIG_SIZE = 0x10000
17 changes: 9 additions & 8 deletions hxtool/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ def pack_waypoint(wp):
"NONE": 255
}


HX870Segments = {
"Magic": (0x0000, 0x0004),
"DeviceSetup": (0x0020, 0x0070),
"ChannelGroupNames": (0x0070, 0x00b0),
"DSCSetup": (0x00b0,0x0100),
"DSCSetup": (0x00b0, 0x0100),
"FlashID": (0x0100, 0x0108),
"Region": (0x010f, 0x0110),
"ChannelEnabled": (0x0120, 0x0190),
Expand All @@ -120,6 +121,7 @@ def pack_waypoint(wp):
"ChannelNames": (0x0ba0, 0x3500),
}


def unpack_channels(data: bytes) -> dict:
channels = {
"group1": {"list": []},
Expand All @@ -135,7 +137,7 @@ def unpack_channels(data: bytes) -> dict:
for group, offset, length in (
("group1", HX870Segments["ChannelSetup"][0] + 0x0000, 96),
("group2", HX870Segments["ChannelSetup"][0] + 0x0180, 96),
("group3", HX870Segments["ChannelSetup"][0] + 0x0300, 96)):
("group3", HX870Segments["ChannelSetup"][0] + 0x0300, 96)):
for i, p in zip(range(length), range(offset, offset + length*4, 4)):
chid, rxshift, rxtxshift, hpallowed, txallowed, lpdefault, unused, dscshipship = unpack_marine_channel_flags(data[p:p+4])
if chid == "": # FIXME: work off enable list
Expand All @@ -154,7 +156,7 @@ def unpack_channels(data: bytes) -> dict:

for group, offset, length in (
("regional", HX870Segments["ChannelSetup"][0] + 0x04a0, 12),
("expansion", HX870Segments["ChannelSetup"][0] + 0x0500, 20)):
("expansion", HX870Segments["ChannelSetup"][0] + 0x0500, 20)):
for i, p in zip(range(length), range(offset, offset + length*8, 8)):
chid, rxfreq, txfreq, lponly, unused4, unused2, hpallowed = unpack_private_channel_flags(data[p:p+8])
if chid == "": # FIXME: work off enable list
Expand All @@ -168,7 +170,6 @@ def unpack_channels(data: bytes) -> dict:
"unused4": unused4,
"unused2": unused2,
})

channels["weather"]["list"] = names["weather"]

return channels
Expand Down Expand Up @@ -210,18 +211,18 @@ def unpack_marine_channel_flags(data: bytes) -> object:
suffixa = "A" if bool(flags & 0x0100) else ""
dscshipship = bool(flags & 0x0080)
prefix = "" if flags & 0x7f == 0x7f else f"{flags & 0x7f:02d}"
chid = "" if data[0] == 255 else f"{prefix}{data[0]:02d}{suffixa}{suffixb}"
chid = "" if data[0] == 255 else f"{prefix}{data[0]:02d}{suffixa}{suffixb}"

return chid, rxshift, rxtxshift, hpallowed, txallowed, lpdefault, unused, dscshipship


def unpack_channel_group_definition(data:bytes) -> list:
def unpack_channel_group_definition(data: bytes) -> list:
"""
0x0 channel group enabled 0x00=no, 0x01=yes
0x1 DSC enabled 0x00=no, 0x01=yes
0x2 ATIS enabled 0x00=no, 0x01=yes (see note below)
0x3-0x7 channel group name 0xff padded
0x8-0xf model name 0xff padded
0x8-0xf model name 0xff padded
"""

enabled, dsc, atis, name, model = unpack(b'>???5s8s', data)
Expand Down Expand Up @@ -270,7 +271,7 @@ def unpack_channel_names(data: bytes) -> object:
("group3", 0x1800, 96),
("regional", 0x2400, 12),
("expansion", 0x2580, 20),
("weather", 0x2820, 10)):
("weather", 0x2820, 10)):
for p in range(offset, offset + length*16, 16):
name = data[p:p+16].strip(b'\xff').decode("ascii")
names[group].append(name)
Expand Down

0 comments on commit 2b1b03b

Please sign in to comment.