Skip to content

Instantly share code, notes, and snippets.

@panqiincs
Last active October 4, 2020 13:06
Show Gist options
  • Save panqiincs/55605b763f3d05be3f62885f4db2f7d2 to your computer and use it in GitHub Desktop.
Save panqiincs/55605b763f3d05be3f62885f4db2f7d2 to your computer and use it in GitHub Desktop.
Binary file padding and splitting.
#!/usr/bin/env python3
import os
import sys
import time
def doThings(file_name="./sample.jpg", stm32_bufsize=32768, rpi_bufsize=4096, interval=0.0):
file_size = os.path.getsize(file_name)
print(f"### Original file size: {file_size} bytes.")
print("")
print("### Start padding file:")
remainder = file_size % stm32_bufsize
if remainder != 0:
padding_cnt = stm32_bufsize - remainder
padding_cmd = "dd if=/dev/zero bs=1 count=" + str(padding_cnt) + " >> " + file_name
print(f"File padding command is:\n{padding_cmd}")
os.system(padding_cmd)
print("")
print("### Start splitting file into chunks:")
chunk_cnt = int(os.path.getsize(file_name) / rpi_bufsize)
for cnt in range(chunk_cnt):
extract_cmd = "dd skip=" + str(cnt) + " count=1 if=" + file_name + " of=chunk" + str(cnt) + ".bin bs=" + str(rpi_bufsize)
print(f"File extracting command is:\n{extract_cmd}")
os.system(extract_cmd)
print("")
print("### Start sending file chunks:")
for cnt in range(chunk_cnt):
print("chunk" + str(cnt) + ".bin")
time.sleep(interval)
#os.system("rm chunk*.bin")
if __name__ == "__main__":
if len(sys.argv) == 5:
file_name = sys.argv[1]
stm32_bufsize = int(sys.argv[2])
rpi_bufsize = int(sys.argv[3])
interval = float(sys.argv[4])
doThings(file_name, stm32_bufsize, rpi_bufsize, interval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment