Added --size to output a fixed number of bytes.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2022-08-22 00:03:20 +02:00
parent d95f2157d1
commit b175a1bef3

View File

@@ -12,6 +12,7 @@ import sys
parser = argparse.ArgumentParser(description="Raspberry Pi Pico Random Number Generator Test Tool")
parser.add_argument("--performance", action="store_true", help="Performance test the RNG.")
parser.add_argument("--endless", action="store_true", help="Outputs random bytes endlessly.")
parser.add_argument("--size", default="100", help="Number of bytes to output.")
args = parser.parse_args()
# If this is set, then the /dev/pico_rng file exists
@@ -42,24 +43,32 @@ start_time = (int(time.time()) - 1)
def get_data():
return rng_chardev.read(64) if rng_chardev else endpt.read(64, 500)
def get_and_print():
data = get_data()
sys.stdout.buffer.write(data)
if args.performance:
while True:
try:
from_device = get_data()
count = count+1
#print(from_device, end="")
print("\t{0:.2f} KB/s".format((int((count * 64) / (int(time.time()) - start_time))) / 1024 ), end='\r')
print("Speed: {0:.2f} KB/s".format((int((count * 64) / (int(time.time()) - start_time))) / 1024 ), end='\r')
except KeyboardInterrupt:
exit(0)
elif args.endless:
while True:
try:
data = get_data()
sys.stdout.buffer.write(data)
get_and_print()
except KeyboardInterrupt:
exit(0)
except BrokenPipeError:
exit(0)
elif args.size:
size = int(float(args.size))
for i in range(0,size,64):
data = get_data()
sys.stdout.buffer.write(data[:min(size-i,len(data))])
else:
from_device = get_data()
print(from_device)