feat: Add CRC checksum calculation and display
This commit is contained in:
+79
-16
@@ -1,6 +1,13 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Optional
|
||||
|
||||
def decode_hamming_8_4(byte_val):
|
||||
# Extract data bits: bits 1, 3, 5, 7
|
||||
return ((byte_val >> 1) & 1) | \
|
||||
(((byte_val >> 3) & 1) << 1) | \
|
||||
(((byte_val >> 5) & 1) << 2) | \
|
||||
(((byte_val >> 7) & 1) << 3)
|
||||
|
||||
@dataclass
|
||||
class Packet:
|
||||
"""
|
||||
@@ -27,22 +34,6 @@ class Packet:
|
||||
b2 = self.original_data[1]
|
||||
|
||||
# De-interleave Hamming bits to get M (3 bits) and R (5 bits)
|
||||
# This is the "basic" interpretation.
|
||||
# For a robust editor we assume the input T42 is valid or we just store bytes.
|
||||
# But we need Mag/Row to organize pages.
|
||||
|
||||
# Decode Hamming 8/4 logic is complex to implementation from scratch correctly
|
||||
# without a reference, but usually D1, D2, D3, D4 are at bit positions 1, 3, 5, 7
|
||||
# (0-indexed, where 0 is LSB).
|
||||
# Let's perform a simple extraction assuming no bit errors for now.
|
||||
|
||||
def decode_hamming_8_4(byte_val):
|
||||
# Extract data bits: bits 1, 3, 5, 7
|
||||
return ((byte_val >> 1) & 1) | \
|
||||
(((byte_val >> 3) & 1) << 1) | \
|
||||
(((byte_val >> 5) & 1) << 2) | \
|
||||
(((byte_val >> 7) & 1) << 3)
|
||||
|
||||
d1 = decode_hamming_8_4(b1)
|
||||
d2 = decode_hamming_8_4(b2)
|
||||
|
||||
@@ -84,6 +75,78 @@ class Page:
|
||||
# Format as Hex to support A-F pages
|
||||
return f"{self.magazine}{self.page_number:02X}"
|
||||
|
||||
def calculate_crc(self) -> int:
|
||||
"""
|
||||
Calculates the CRC-16 (CCITT) checksum for the page.
|
||||
Covers Rows 0 to 23.
|
||||
Row 0: Skips first 8 bytes (Header/Control). Uses bytes 8-39.
|
||||
Rows 1-23: Uses all 40 bytes.
|
||||
Data is 7-bit (stripped parity).
|
||||
"""
|
||||
crc = 0xFFFF
|
||||
poly = 0x1021
|
||||
|
||||
# Helper to update CRC with a byte
|
||||
def update_crc(c, val):
|
||||
v = (val << 8) & 0xFFFF
|
||||
for _ in range(8):
|
||||
if (c ^ v) & 0x8000:
|
||||
c = (c << 1) ^ poly
|
||||
else:
|
||||
c = c << 1
|
||||
v <<= 1
|
||||
c &= 0xFFFF
|
||||
return c
|
||||
|
||||
# Organize packets by row
|
||||
rows = {}
|
||||
for p in self.packets:
|
||||
rows[p.row] = p
|
||||
|
||||
for r in range(24): # 0 to 23
|
||||
if r in rows:
|
||||
data = rows[r].data
|
||||
start_col = 8 if r == 0 else 0
|
||||
|
||||
for i in range(start_col, 40):
|
||||
byte_val = data[i] & 0x7F # Strip parity
|
||||
crc = update_crc(crc, byte_val)
|
||||
else:
|
||||
# Missing row? Usually treated as spaces (0x20)
|
||||
start_col = 8 if r == 0 else 0
|
||||
for i in range(start_col, 40):
|
||||
crc = update_crc(crc, 0x20)
|
||||
|
||||
return crc
|
||||
|
||||
def get_stored_crc(self) -> Optional[int]:
|
||||
"""
|
||||
Attempts to retrieve the stored CRC from Packet 27/0 if present.
|
||||
Returns None if not found.
|
||||
"""
|
||||
# Look for Packet 27
|
||||
for p in self.packets:
|
||||
if p.row == 27:
|
||||
# Check Designation Code (Byte 0)
|
||||
try:
|
||||
b0 = p.data[0]
|
||||
# Decode Hamming 8/4
|
||||
designation = decode_hamming_8_4(b0)
|
||||
|
||||
if designation == 0:
|
||||
# Packet 27/0
|
||||
# Checksum is in bytes 38 and 39
|
||||
if len(p.data) >= 40:
|
||||
hi = p.data[38]
|
||||
lo = p.data[39]
|
||||
|
||||
# Strip parity
|
||||
crc = ((hi & 0x7F) << 8) | (lo & 0x7F)
|
||||
return crc
|
||||
except:
|
||||
pass
|
||||
return None
|
||||
|
||||
@dataclass
|
||||
class TeletextService:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user