Skip to content

Commit 2e7daa7

Browse files
committed
fix: ms parsing and decoding
1 parent f59e5c3 commit 2e7daa7

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

sd_protocols/message_synced.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def demodulate_ms(self, msg_data: Dict[str, Any], msg_type: str = "MS") -> List[
126126
representation = symbol_map.get(key, '')
127127

128128
pstr = pattern_exists(search_pattern, norm_patterns, raw_data)
129-
129+
130130
print(f"DEBUG: Protocol {pid} Key {key} Pattern {search_pattern} Result {pstr}")
131131

132132
if pstr != -1:
@@ -220,10 +220,9 @@ def demodulate_ms(self, msg_data: Dict[str, Any], msg_type: str = "MS") -> List[
220220

221221
bit_str = "".join(bit_msg)
222222

223-
try:
224-
hex_val = f"{int(bit_str, 2):X}"
225-
dmsg = hex_val
226-
except ValueError:
223+
# Perl: my $dmsg = lib::SD_Protocols::binStr2hexStr(join '', @bit_msg);
224+
dmsg = self.bin_str_2_hex_str(bit_str)
225+
if dmsg is None:
227226
continue
228227

229228
preamble = self.check_property(pid, 'preamble', '')

tests/test_ms_demodulation.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
from sd_protocols.sd_protocols import SDProtocols
3+
4+
class TestMSDemodulation:
5+
@pytest.fixture
6+
def protocols(self):
7+
return SDProtocols()
8+
9+
def test_ms_demodulate_protocol_3_1(self, protocols):
10+
# Using Protocol 3.1 (IT V3 / self-learning?)
11+
# sync: [1, -44]
12+
# zero: [1, -3.8]
13+
# one: [3.5, -1]
14+
# length_min: 24
15+
# preamble: "i"
16+
17+
# Clock = 330
18+
# P0 = 330 (1)
19+
# P1 = -14520 (-44)
20+
# P2 = -1254 (-3.8)
21+
# P3 = 1155 (3.5)
22+
# P4 = -330 (-1)
23+
24+
# Sync: P0, P1 -> "01"
25+
# Zero: P0, P2 -> "02"
26+
# One: P3, P4 -> "34"
27+
28+
# Send 23 zeros and 1 one to satisfy pattern matching requirements
29+
# Data: "01" + "02"*23 + "34"
30+
31+
msg_data = {
32+
"P0": "330",
33+
"P1": "-14520",
34+
"P2": "-1254",
35+
"P3": "1155",
36+
"P4": "-330",
37+
"data": "01" + "02"*23 + "34",
38+
"CP": "0",
39+
"SP": "0", # irrelevant
40+
"R": "0"
41+
}
42+
43+
results = protocols.demodulate(msg_data, "MS")
44+
45+
found = False
46+
for res in results:
47+
if res['protocol_id'] == '3.1':
48+
found = True
49+
assert res['meta']['bit_length'] == 24
50+
# 23 zeros + 1 one = 0000...01
51+
# Hex: 000001
52+
53+
# Check payload starts with 'i'
54+
assert res['payload'].startswith('i')
55+
# 24 bits = 6 hex digits. Last digit 1.
56+
assert '000001' in res['payload']
57+
58+
assert found

0 commit comments

Comments
 (0)