From 0e3df150676f6a560e1f3c8626ca75dca8d56638 Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Wed, 31 Jan 2024 11:51:06 +1030 Subject: [PATCH] Add testing script to payloads module --- horusdemodlib/encoder.py | 114 ++++++++++++++++++++++++++++++++------ horusdemodlib/payloads.py | 21 ++++--- 2 files changed, 110 insertions(+), 25 deletions(-) diff --git a/horusdemodlib/encoder.py b/horusdemodlib/encoder.py index a0c3c10..9ba6a36 100644 --- a/horusdemodlib/encoder.py +++ b/horusdemodlib/encoder.py @@ -5,6 +5,7 @@ import ctypes from ctypes import * import codecs +import datetime import logging import sys from enum import Enum @@ -12,27 +13,12 @@ import os import logging from .decoder import decode_packet, hex_to_bytes -PACKET_MAX_SIZE = 1024 - class Encoder(): """ - HorusLib provides a binding to horuslib to demoulate frames. + Horus Binary Encoder class. - Example usage: - - from horuslib import HorusLib, Mode - with HorusLib(, mode=Mode.BINARY, verbose=False) as horus: - with open("test.wav", "rb") as f: - while True: - data = f.read(horus.nin*2) - if horus.nin != 0 and data == b'': #detect end of file - break - output = horus.demodulate(data) - if output.crc_pass and output.data: - print(f'{output.data.hex()} SNR: {output.snr}') - for x in range(horus.mfsk): - print(f'F{str(x)}: {float(output.extended_stats.f_est[x])}') + Allows creation of a Horus Binary packet. """ @@ -97,6 +83,8 @@ class Encoder(): """ pass + # Wrappers for libhorus C functions that we need. + def get_num_tx_data_bytes(self, packet_size) -> int: """ Calculate the number of transmit data bytes (uw+packet+fec) for a given @@ -161,6 +149,97 @@ class Encoder(): self.c_lib.horus_l2_decode_rx_packet(_decoded, _encoded, num_payload_bytes) return bytes(_decoded) + + + def create_horus_v2_packet(self, + payload_id = 256, + sequence_number = 0, + time_dt = datetime.datetime.utcnow(), + latitude = 0.0, + longitude = 0.0, + altitude = 0.0, + speed = 0.0, + satellites = 0, + temperature = 0.0, + battery_voltage = 0.0, + # Default fields used for the 'custom' section of the packet. + ascent_rate = 0.0, + ext_temperature = 0.0, + ext_humidity = 0.0, + ext_pressure = 0.0, + # Alternate custom data - must be bytes, and length=9 + custom_data = None + ): + pass + + # Sanity check input data. + if payload_id < 256 or payload_id > 65535: + raise ValueError("Invalid Horus v2 Payload ID. (Must be 256-65535)") + + # Clip sequence number + sequence_number = int(sequence_number) % 65536 + + # Try and extract HHMMSS from time + try: + hours = int(time_dt.hour) + minutes = int(time_dt.minute) + seconds = int(time_dt.second) + except: + raise ValueError("Could not parse input datetime object.") + + # Assume lat/lon are fine. They are just sent as floats anyway. + + # Clip Altitude + altitude = int(altitude) + if altitude < 0: + altitude = 0 + if altitude > 65535: + altitude = 65535 + + # Clip Speed (kph) + speed = int(speed) + if speed < 0: + speed = 0 + if speed > 255: + speed = 255 + + # Clip sats + satellites = int(satellites) + if satellites < 0: + satellites = 0 + if satellites > 255: + satellites = 255 + + # Temperature + + # Battery voltage clip and conversion + + + + # Custom data + + # Ascent rate + + # PTU data + + + # 'struct': '