Skip to content

Commit

Permalink
update pybricksdev to 1.0.0a45
Browse files Browse the repository at this point in the history
try it with newest 3.3.0b5 firmware as well

added some stdin tests that helped figure out pybricks/support#1052

This will enable new download and run strategy with cached hub programs

might not need frozen module after all, train program runs fine
  • Loading branch information
Novakasa committed Apr 28, 2023
1 parent 82986e6 commit 02dd465
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 11 deletions.
15 changes: 7 additions & 8 deletions ble-server/ble_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, name=None, program_name=None, out_queue=None):

self.input_lock = asyncio.Lock()
self.output_queue = asyncio.Queue()
self.hub.nus_observable.subscribe(self._on_hub_nus)
self.hub.stdout_observable.subscribe(self._on_hub_stdout)
self.msg_ack = asyncio.Queue()
self.line_buffer = bytearray()
self.output_buffer = bytearray()
Expand Down Expand Up @@ -109,17 +109,15 @@ def add_to_output_buffer(self, byte):
if len(self.output_buffer) == self.msg_len:
self.output_queue.put_nowait(self.output_buffer)
else:
# print("output buffer len!", self.output_bufferself.output_buffer)
asyncio.create_task(self.send_ack(False))
self.output_buffer = bytearray()
self.msg_len = None
return

self.output_buffer += bytes([byte])

def _on_hub_nus(self, data):
# print("nus:", data)
if self.hub._downloading_via_nus:
return
def _on_hub_stdout(self, data):

for byte in data:
self.add_to_output_buffer(byte)
Expand All @@ -139,8 +137,8 @@ async def hub_message_handler(self, bytes):
checksum = bytes[-1]
output_checksum = xor_checksum(bytes[:-1])
if not checksum == output_checksum:
print(f"received {bytes[:-1]}, checksum mismatch! {checksum} != {output_checksum}", self.output_buffer)
await self.send_ack(False)
print(f"received {bytes[:-1]}, checksum mismatch! {checksum} != {output_checksum}")
return
await self.send_ack(True)
data = bytes[1:-1] #strip out_id and checksum
Expand Down Expand Up @@ -188,7 +186,7 @@ async def rpc(self, funcname, args=None):

async def store_value(self, address, value):
assert self.io_hub_version!="1.0.0"
shifted = value
shifted = int(value)
data = []
while shifted > 0:
data.insert(0, shifted & 0xFF)
Expand All @@ -205,6 +203,7 @@ async def send_ack(self, success):

async def send_safe(self, data, unreliable=False, persistent=True):
assert len(data) <= _CHUNK_LENGTH
# print("sending safe:", data)
checksum = xor_checksum(data)
ack_result = False
try_counter = 0
Expand Down Expand Up @@ -282,7 +281,7 @@ async def run(self, program=None, wait=False):
async def run_coroutine():
self.program_stopped.clear()
self.hub_ready.clear()
await self.hub.run(program, print_output=False, wait=True)
await self.hub.run(program, print_output=False, wait=True, line_handler=False)
self.program_stopped.set()
self.to_out_queue("program_stopped", None)

Expand Down
4 changes: 4 additions & 0 deletions ble-server/hub_programs/io_hub_unfrozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def retry_last_output(self):
self.output_watch.reset()

def handle_input(self):
# print("handling input", self.input_buffer)
in_id = self.input_buffer[0]

if in_id == _IN_ID_MSG_ACK:
Expand Down Expand Up @@ -176,8 +177,10 @@ def update_input(self, byte):
if self.msg_len is None:
self.msg_len = byte
return
# print(byte, self.msg_len, len(self.input_buffer))
if len(self.input_buffer) >= self.msg_len and byte == _IN_ID_END:
if len(self.input_buffer) > self.msg_len:
# print("buffer len!")
self.emit_ack(False)
else:
self.handle_input()
Expand Down Expand Up @@ -206,6 +209,7 @@ def run_loop(self, max_delta = 0.01):
self.update_input(byte)
self.input_watch.reset()
if self.msg_len is not None and self.input_watch.time() > 200:
# print("buffer timeout", self.input_buffer)
self.emit_ack(False)
self.input_buffer = bytearray()
self.msg_len = None
Expand Down
2 changes: 1 addition & 1 deletion ble-server/hub_programs/layout_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pybricks.parameters import Port
from pybricks.tools import wait, StopWatch

from io_hub import IOHub
from io_hub_unfrozen import IOHub

_SWITCH_POS_LEFT = const(0)
_SWITCH_POS_RIGHT = const(1)
Expand Down
27 changes: 27 additions & 0 deletions ble-server/hub_programs/test_stdin_hub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# NOTE: Run this program with the latest
# firmware provided via https://beta.pybricks.com/

from pybricks.tools import wait

# Standard MicroPython modules
from usys import stdin, stdout
from uselect import poll

keyboard = poll()
keyboard.register(stdin)
buffer = bytearray()

for _ in range(3):
while True:

if keyboard.poll(50):
byte = stdin.buffer.read(1)[0]
print(byte, end=" ")
buffer.append(byte)
if byte == b"\n"[0]:
print("\nline received")
print(str(buffer))
buffer = bytearray()
break

wait(100)
2 changes: 2 additions & 0 deletions ble-server/test_io_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ async def test_response(data):
await test_hub.connect(device)
try:
await test_hub.run("test_io", wait=False)
print("run started!")
await asyncio.sleep(1.0)
print("first rpc call:")
await test_hub.rpc("respond", bytearray([1,4,5,253]))
await asyncio.sleep(1.0)
await test_hub.rpc("respond", bytearray([1,2,3,4]))
Expand Down
25 changes: 25 additions & 0 deletions ble-server/test_stdin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import asyncio
from pybricksdev.ble import find_device
from pybricksdev.connections.pybricks import PybricksHub

async def main():
dev = await find_device()
print(dev)
hub = PybricksHub()
await hub.connect(dev)
print("connected!")
await hub.run("ble-server/hub_programs/test_stdin_hub.py", print_output=True, wait=False)
await asyncio.sleep(0.5)
print("writing line...")
await hub.write_line("012345")
print("writing line...")
await hub.write(b"hello\r\n")
print("writing line...")
await hub.write(b"\r\n")
await asyncio.sleep(5.0)
await hub.disconnect()

print("OK!")

asyncio.run(main())
2 changes: 1 addition & 1 deletion brickrail-gui/layout/train/layout_train_inspector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func update_storage_controls():
$Storage.add_child(edit)

func _on_storage_val_edited(value, index):
train.ble_train.hub.store_value(index, value)
train.ble_train.hub.store_value(index, int(value))

func select_ble_train(ble_train):
if ble_train == null:
Expand Down
Binary file added firmware/cityhub-firmware-build-2730.zip
Binary file not shown.
Binary file added firmware/essentialhub-firmware-build-2730.zip
Binary file not shown.
Binary file added firmware/primehub-firmware-build-2730.zip
Binary file not shown.
Binary file added firmware/technichub-firmware-build-2730.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pybricksdev
pybricksdev == 1.0.0a45
websockets
pyinstaller

0 comments on commit 02dd465

Please sign in to comment.