Skip to content

Commit

Permalink
fix(crc8): typing and removal of uneded code
Browse files Browse the repository at this point in the history
  • Loading branch information
rokam committed May 15, 2024
1 parent 9cdc1c3 commit 8bcf19d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 54 deletions.
19 changes: 10 additions & 9 deletions midealocal/crc8.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
crc8_854_table = [
"""CRC8 calculator."""

crc8_854_table: list[int] = [
0x00,
0x5E,
0xBC,
Expand Down Expand Up @@ -258,13 +260,12 @@
]


def calculate(data):
crc_value = 0
def calculate(data: bytearray) -> int:
"""
Calculate CRC8 value of a bytearray.
"""
crc_value: int = 0
for m in data:
k = crc_value ^ m
if k > 256:
k -= 256
if k < 0:
k += 256
crc_value = crc8_854_table[k]
crc_value = crc8_854_table[crc_value ^ m]
return crc_value
48 changes: 3 additions & 45 deletions tests/crc8_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,12 @@ def test_calculate() -> None:
"""Test calculate method."""
data: bytearray = bytearray(
[
# 2 bytes - StaicHeader
0x5A,
0x5A,
# 2 bytes - mMessageType
0x82,
0x01,
0x11,
# 2 bytes - PacketLenght
0x00,
0x00,
# 2 bytes
0xFF,
0x20,
0x00,
# 4 bytes - MessageId
0x00,
0x00,
0x00,
0x00,
# 8 bytes - Date&Time
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
# 6 bytes - mDeviceID
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
# 12 bytes
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
]
)
assert calculate(data) == 86
assert calculate(data) == 101

0 comments on commit 8bcf19d

Please sign in to comment.