mirror of
https://github.com/polhenarejos/pico-rng.git
synced 2026-04-28 09:43:22 +02:00
Added LICENSE.md, updated README.md, fixed test tool, and organized driver code.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import usb.core
|
||||
import usb.util
|
||||
import os
|
||||
import random
|
||||
import time
|
||||
import argparse
|
||||
@@ -11,18 +12,26 @@ parser = argparse.ArgumentParser(description="Raspberry Pi Pico Random Number Ge
|
||||
parser.add_argument("--performance", action="store_true", help="Performance test the RNG.")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Get the device
|
||||
rng = usb.core.find(idVendor=0x0000, idProduct=0x0004)
|
||||
assert rng is not None
|
||||
# If this is set, then the /dev/pico_rng file exists
|
||||
rng_chardev = None
|
||||
|
||||
# Get the configuration of the device
|
||||
cfg = rng.get_active_configuration()
|
||||
if os.path.exists("/dev/pico_rng"):
|
||||
rng_chardev = open("/dev/pico_rng", "rb")
|
||||
|
||||
# File does not exist, test with usb.core
|
||||
if not rng_chardev:
|
||||
# Get the device
|
||||
rng = usb.core.find(idVendor=0x0000, idProduct=0x0004)
|
||||
assert rng is not None
|
||||
|
||||
# Get the only interface of our device
|
||||
intf = cfg.interfaces()[0]
|
||||
# Get the configuration of the device
|
||||
cfg = rng.get_active_configuration()
|
||||
|
||||
# Get the endpoint
|
||||
endpt = intf.endpoints()[0]
|
||||
# Get the only interface of our device
|
||||
intf = cfg.interfaces()[0]
|
||||
|
||||
# Get the endpoint
|
||||
endpt = intf.endpoints()[0]
|
||||
|
||||
# Time tracking for bits/s
|
||||
count = 0
|
||||
@@ -31,11 +40,12 @@ start_time = (int(time.time()) - 1)
|
||||
if args.performance:
|
||||
while True:
|
||||
try:
|
||||
from_device = endpt.read(endpt.wMaxPacketSize, 500)
|
||||
from_device = rng_chardev.read(64) if rng_chardev else endpt.read(64, 500)
|
||||
count = count+1
|
||||
print(":".join("{:02x}".format(b) for b in from_device), end="")
|
||||
print(" KBps {0:.2f}".format((int((count * 64) / (int(time.time()) - start_time))) / 1024 ))
|
||||
print(from_device, end="")
|
||||
print("\t{0:.2f} KB/s".format((int((count * 64) / (int(time.time()) - start_time))) / 1024 ))
|
||||
except KeyboardInterrupt:
|
||||
exit(0)
|
||||
else:
|
||||
print(":".join("{:02x}".format(b) for b in endpt.read(endpt.wMaxPacketSize, 500)))
|
||||
from_device = rng_chardev.read(64) if rng_chardev else endpt.read(64, 500)
|
||||
print(from_device)
|
||||
|
||||
Reference in New Issue
Block a user