Skip to content

Commit

Permalink
Merge pull request #3 from a2800276/iss1-eyecandy
Browse files Browse the repository at this point in the history
issue1 be more responsive while waiting for device (eyecandy)
  • Loading branch information
renzenicolai committed Jul 10, 2022
2 parents 7e5469d + e92801f commit a932756
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
53 changes: 46 additions & 7 deletions webusb.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1,
if iteration == total:
print()

def printProgressTime(
totalMilliSeconds,
steps=10,
prefix="",
suffix="",
decimals=1,
length=20,
fill="█",
printEnd="\r",
):
"""
prints progress while sleeping totalMilliSeconds, bar is updated every `steps` milliseconds
other params
"""
for i in range(0, totalMilliSeconds, steps):
printProgressBar(
i,
totalMilliSeconds - steps,
prefix,
suffix,
decimals,
length,
fill,
printEnd,
)
time.sleep(steps / 1000)

class Commands(Enum):
EXECFILE = 0
HEARTBEAT = 1
Expand Down Expand Up @@ -100,18 +127,28 @@ def bootWebUSB(self):
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_MODE, 0x0001, self.webusb_esp32.bInterfaceNumber)
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_RESET, 0x0000, self.webusb_esp32.bInterfaceNumber)
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_BAUDRATE, 9216, self.webusb_esp32.bInterfaceNumber)

time.sleep(4) #Wait for the badge to have boot into webusb mode

print("Booting into WebUSB, please wait ...")
printProgressTime(4000) # Wait for the badge to have boot into webusb mode
while True:
try:
self.ep_in.read(128)
except Exception as e:
break

def receiveResponse(self):
def receiveResponse(self, show_hourglass=False):
"""
receveives the response, for longish response times,
you can keep the user occupied by setting show_hourglass to True
"""
response = bytes([])
starttime = time.time()
anim = 0
while (time.time() - starttime) < self.TIMEOUT:
if show_hourglass:
print(f"\rWaiting for response { (1+(anim%3)) * '.'} ", end="\r")
anim += 1

data = None
try:
data = bytes(self.ep_in.read(128))
Expand All @@ -122,6 +159,8 @@ def receiveResponse(self):
response += data

if len(response) >= self.PAYLOADHEADERLEN:
if show_hourglass:
print()
command, payloadlen, verif, message_id = struct.unpack_from("<HIHI", response)
if verif != 0xADDE:
raise Exception("Failed verification")
Expand All @@ -130,7 +169,7 @@ def receiveResponse(self):
raise Exception("Timeout in receiving")


def sendPacket(self, packet, transfersize=2048):
def sendPacket(self, packet, transfersize=2048, show_response_hourglass=False):
transfersize = 2048
msg = packet.getMessage()
starttime = time.time()
Expand All @@ -143,8 +182,8 @@ def sendPacket(self, packet, transfersize=2048):
if len(msg) > transfersize:
printProgressBar(i//transfersize, len(msg) // transfersize, length=20)
#self.ep_out.write(packet.getMessage())
print(f"transfer speed: {len(msg)/(time.time()-starttime)}")
command, message_id, data = self.receiveResponse()
print(f"transfer speed: {(len(msg)/(time.time()-starttime)/1024):0.2f} kb/s")
command, message_id, data = self.receiveResponse(show_hourglass=show_response_hourglass)
if message_id != packet.message_id:
raise Exception("Mismatch in id")
if command != packet.command.value:
Expand Down Expand Up @@ -222,7 +261,7 @@ def appfsUpload(self, appname, file):
"""

payload = appname.encode(encoding="ascii") + b"\x00" + file
data = self.sendPacket(WebUSBPacket(Commands.APPFSWRITE, self.getMessageId(), payload))
data = self.sendPacket(WebUSBPacket(Commands.APPFSWRITE, self.getMessageId(), payload), show_response_hourglass=True)
return data.decode().rstrip('\x00') == "ok"

def appfsRemove(self, appname):
Expand Down
1 change: 1 addition & 0 deletions webusb_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
name = args.name
run = args.run

print(f"Loading: {args.application} for installation as: {name} ...")
with open(args.application, "rb") as f:
application = f.read()

Expand Down

0 comments on commit a932756

Please sign in to comment.