flash: generate bin thru py

This commit is contained in:
occheung 2020-10-06 15:01:04 +08:00
parent 468ffa4e15
commit 5fd7f36c65
1 changed files with 49 additions and 0 deletions

49
flash.py Normal file
View File

@ -0,0 +1,49 @@
import argparse
import struct
def get_argparser():
parser = argparse.ArgumentParser(description="MQTT-controlled 4-channel DDS signal generator")
parser.add_argument("cidr", nargs=1,
metavar=("Client_CIDR"),
help="IPv4 Address of Humpback-DDS in CIDR notation")
parser.add_argument("mac_addr", nargs=1,
metavar=("Client_MAC"),
help="MAC Address of Humpback-DDS")
parser.add_argument("broker_ip", nargs=1,
metavar=("Broker_IP"),
help="IP address of MQTT broker")
parser.add_argument("name", nargs=1,
metavar=("Name"),
help="Device name of Humpback-DDS")
return parser
def write_record(f, key, value):
key_size = len(key) + 1
value_size = len(value)
record_size = key_size + value_size + 4
f.write(struct.pack("<l", record_size))
f.write(key.encode())
f.write(b"\x00")
f.write(value)
def write_end_marker(f):
f.write(b"\xff\xff\xff\xff")
def main():
args = get_argparser().parse_args()
with open("flash_config.bin", "wb") as fo:
write_record(fo, "CIDR", args.cidr[0].encode())
write_record(fo, "MAC", args.mac_addr[0].encode())
write_record(fo, "BrokerIP", args.broker_ip[0].encode())
write_record(fo, "Name", args.name[0].encode())
write_end_marker(fo)
if __name__ == "__main__":
main()