diff options
author | Vadim Yanitskiy <axilirator@gmail.com> | 2018-12-16 08:39:44 +0700 |
---|---|---|
committer | Vadim Yanitskiy <axilirator@gmail.com> | 2018-12-18 05:47:11 +0700 |
commit | 8b3d49bd34271e1ffc5bb6910020e1479cd58b5d (patch) | |
tree | fd48197b67df34e90d3955c30d7c829ddd0c4705 /src/target/trx_toolkit | |
parent | 016361cda162e9dbe84b13ef005e0fa48244b3df (diff) |
trx_toolkit/data_msg.py: use struct API for fn and toa256 coding
The built-in struct module is already used for toa256 decoding,
so let's use it for toa256 encoding, and TDMA frame number
coding too - no need to (re)implement the wheel!
Change-Id: I10d2e15ac57a0524e9bc1c80ed6a0f6f5a263436
Diffstat (limited to 'src/target/trx_toolkit')
-rw-r--r-- | src/target/trx_toolkit/data_msg.py | 20 |
1 files changed, 4 insertions, 16 deletions
diff --git a/src/target/trx_toolkit/data_msg.py b/src/target/trx_toolkit/data_msg.py index 95ec9dc1..bbc9d936 100644 --- a/src/target/trx_toolkit/data_msg.py +++ b/src/target/trx_toolkit/data_msg.py @@ -143,24 +143,13 @@ class DATAMSG: # Generates frame number to bytes def gen_fn(self, fn): - # Allocate an empty byte-array - buf = bytearray() - # Big endian, 4 bytes - buf.append((fn >> 24) & 0xff) - buf.append((fn >> 16) & 0xff) - buf.append((fn >> 8) & 0xff) - buf.append((fn >> 0) & 0xff) - - return buf + return struct.pack(">L", fn) # Parses frame number from bytes def parse_fn(self, buf): # Big endian, 4 bytes - return (buf[0] << 24) \ - | (buf[1] << 16) \ - | (buf[2] << 8) \ - | (buf[3] << 0) + return struct.unpack(">L", buf)[0] # Generates a TRX DATA message def gen_msg(self): @@ -197,7 +186,7 @@ class DATAMSG: raise ValueError("Message is to short") # Parse both fn and tn - self.fn = self.parse_fn(msg[1:]) + self.fn = self.parse_fn(msg[1:5]) self.tn = msg[0] # Specific message part @@ -381,8 +370,7 @@ class DATAMSG_TRX2L1(DATAMSG): # Encode ToA (Time of Arrival) # Big endian, 2 bytes (int32_t) - buf.append((self.toa256 >> 8) & 0xff) - buf.append(self.toa256 & 0xff) + buf += struct.pack(">h", self.toa256) return buf |