Skip to content

Commit

Permalink
This is actually handled by structs lib
Browse files Browse the repository at this point in the history
  • Loading branch information
maslyankov committed Jan 20, 2025
1 parent 94133a1 commit d0a9135
Showing 1 changed file with 0 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/sunsynk/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,9 @@ def pack_value(value: int, bits: int = 16, signed: bool = True) -> RegType:
For 32-bit: tuple of (low, high) register values
"""
if bits == 16:
if not signed and value < 0:
# Convert negative value to two's complement for unsigned
value = value + (1 << 16)
fmt = 'h' if signed else 'H'
return struct.unpack('H', struct.pack(fmt, value))
if bits == 32:
if not signed and value < 0:
# Convert negative value to two's complement for unsigned
value = value + (1 << 32)
fmt = 'i' if signed else 'I'
return struct.unpack('2H', struct.pack(fmt, value))
raise ValueError(f"Unsupported number of bits: {bits}")
Expand Down

0 comments on commit d0a9135

Please # to comment.