Files
openhmi-nx/tests/helpers.py
T

37 lines
1.2 KiB
Python
Raw Normal View History

import os
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "src"))
FIXTURE = os.path.join(ROOT, "tests", "fixtures", "demo_editor_saved.HMI")
try:
import PIL # noqa: F401
HAVE_PIL = True
except ImportError:
HAVE_PIL = False
def demo_project():
"""The demo project, built from code (needs Pillow)."""
sys.path.insert(0, os.path.join(ROOT, "examples"))
import make_demo
return make_demo.build()
def bitwise_word_crc(data, crc=0xFFFFFFFF):
"""Reference: STM32-style CRC-32, each byte zero-extended to a 32-bit word, bit by bit."""
for b in data:
crc ^= b
for _ in range(32):
crc = ((crc << 1) ^ 0x04C11DB7) & 0xFFFFFFFF if crc & 0x80000000 else (crc << 1) & 0xFFFFFFFF
return crc
def bitwise_dword_crc(data, crc=0xFFFFFFFF):
"""Reference: STM32-style CRC-32 over little-endian 32-bit words, bit by bit."""
for i in range(0, len(data), 4):
crc ^= int.from_bytes(data[i:i + 4], "little")
for _ in range(32):
crc = ((crc << 1) ^ 0x04C11DB7) & 0xFFFFFFFF if crc & 0x80000000 else (crc << 1) & 0xFFFFFFFF
return crc