mirror of
https://github.com/mjg59/python-broadlink.git
synced 2024-11-21 22:51:41 +01:00
Segregate CRC16.get_table() from CRC16.calculate() (#567)
This commit is contained in:
parent
4e1e690762
commit
67b674859f
@ -2,7 +2,7 @@
|
|||||||
import typing as t
|
import typing as t
|
||||||
|
|
||||||
|
|
||||||
class CRC16: # pylint: disable=R0903
|
class CRC16:
|
||||||
"""Helps with CRC-16 calculation.
|
"""Helps with CRC-16 calculation.
|
||||||
|
|
||||||
CRC tables are cached for performance.
|
CRC tables are cached for performance.
|
||||||
@ -11,13 +11,8 @@ class CRC16: # pylint: disable=R0903
|
|||||||
_cache = {}
|
_cache = {}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def calculate(
|
def get_table(cls, polynomial: int):
|
||||||
cls,
|
"""Return the CRC-16 table for a polynomial."""
|
||||||
sequence: t.Sequence[int],
|
|
||||||
polynomial: int = 0xA001, # Modbus CRC-16.
|
|
||||||
init_value: int = 0xFFFF,
|
|
||||||
) -> int:
|
|
||||||
"""Calculate the CRC-16 of a sequence of integers."""
|
|
||||||
try:
|
try:
|
||||||
crc_table = cls._cache[polynomial]
|
crc_table = cls._cache[polynomial]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -31,7 +26,17 @@ class CRC16: # pylint: disable=R0903
|
|||||||
remainder = remainder >> 1
|
remainder = remainder >> 1
|
||||||
crc_table.append(remainder)
|
crc_table.append(remainder)
|
||||||
cls._cache[polynomial] = crc_table
|
cls._cache[polynomial] = crc_table
|
||||||
|
return crc_table
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def calculate(
|
||||||
|
cls,
|
||||||
|
sequence: t.Sequence[int],
|
||||||
|
polynomial: int = 0xA001, # CRC-16-ANSI.
|
||||||
|
init_value: int = 0xFFFF,
|
||||||
|
) -> int:
|
||||||
|
"""Calculate the CRC-16 of a sequence of integers."""
|
||||||
|
crc_table = cls.get_table(polynomial)
|
||||||
crc = init_value
|
crc = init_value
|
||||||
for item in sequence:
|
for item in sequence:
|
||||||
crc = crc >> 8 ^ crc_table[(crc ^ item) & 0xFF]
|
crc = crc >> 8 ^ crc_table[(crc ^ item) & 0xFF]
|
||||||
|
Loading…
Reference in New Issue
Block a user