reference

This commit is contained in:
Bastian Wagner
2026-08-15 09:04:38 +02:00
parent f6da346e18
commit a2387341bd
3 changed files with 556 additions and 0 deletions

39
reference/fit_crc.py Normal file
View File

@@ -0,0 +1,39 @@
from __future__ import annotations
CRC_TABLE = (
0x0000,
0xCC01,
0xD801,
0x1400,
0xF001,
0x3C00,
0x2800,
0xE401,
0xA001,
0x6C00,
0x7800,
0xB401,
0x5000,
0x9C01,
0x8801,
0x4400,
)
def update_crc(crc: int, byte: int) -> int:
tmp = CRC_TABLE[crc & 0xF]
crc = (crc >> 4) & 0x0FFF
crc = crc ^ tmp ^ CRC_TABLE[byte & 0xF]
tmp = CRC_TABLE[crc & 0xF]
crc = (crc >> 4) & 0x0FFF
crc = crc ^ tmp ^ CRC_TABLE[(byte >> 4) & 0xF]
return crc & 0xFFFF
def fit_crc(data: bytes | bytearray | memoryview) -> int:
crc = 0
for byte in data:
crc = update_crc(crc, byte)
return crc