Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelburgos committed May 7, 2024
1 parent f193921 commit 3c341f0
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 98 deletions.
85 changes: 0 additions & 85 deletions Gateware/glasgowcontrib/applet/open_beam_interface/__init__.py
Expand Up @@ -1115,91 +1115,6 @@ def connect_pin(pin_name: str, signal):
import struct


class OBIInterface:
def __init__(self, iface):
self._synchronized = False
self._next_cookie = random.randrange(0, 0x10000, 2) # even cookies only
self.lower = iface

@property
def synchronized(self):
"""`True` if the instrument is ready to accept commands, `False` otherwise."""
return self._synchronized

async def _synchronize(self):
print("synchronizing")
if self.synchronized:
print("already synced")
return

print("not synced")
cookie, self._next_cookie = self._next_cookie, (self._next_cookie + 2) & 0xffff # even cookie
#self._logger.debug(f'synchronizing with cookie {cookie:#06x}')
print("synchronizing with cookie")

cmd = struct.pack(">BHBB",
Command.Type.Synchronize.value, cookie, 0,
Command.Type.Flush.value)
await self.lower.write(cmd)
await self.lower.flush()
res = struct.pack(">HH", 0xffff, cookie)
data = await self.readuntil(res)
print(str(list(data)))

async def readuntil(self, separator=b'\n', *, flush=True):
def find_sep(buffer, separator=b'\n', offset=0):
if buffer._chunk is None:
if not buffer._queue:
raise IncompleteReadError
buffer._chunk = buffer._queue.popleft()
buffer._offset = 0
return buffer._chunk.obj.find(separator)

if flush and len(self.lower._out_buffer) > 0:
# Flush the buffer, so that everything written before the read reaches the device.
await self.lower.flush(wait=False)

seplen = len(separator)
if seplen == 0:
raise ValueError('Separator should be at least one-byte string')
chunks = []

# Loop until we find `separator` in the buffer, exceed the buffer size,
# or an EOF has happened.
while True:
buflen = len(self.lower._in_buffer)

# Check if we now have enough data in the buffer for `separator` to fit.
if buflen >= seplen:
isep = find_sep(self.lower._in_buffer, separator)
if isep != -1:
print(f"found {isep=}")
# `separator` is in the buffer. `isep` will be used later
# to retrieve the data.
break
else:
await self.lower._in_tasks.wait_one()

async with self.lower._in_pushback:
chunk = self.lower._in_buffer.read()
self.lower._in_pushback.notify_all()
chunks.append(chunk)

async with self.lower._in_pushback:
chunk = self.lower._in_buffer.read(isep+seplen)
self.lower._in_pushback.notify_all()
chunks.append(chunk)

# Always return a memoryview object, to avoid hard to detect edge cases downstream.
result = memoryview(b"".join(chunks))
return result

async def transfer(self, seq): #CommandSequence
await self.lower.write(seq.message)
await self.lower.flush()
data = await self.lower.read(seq._response_length)
return seq.unpack(data)


class OBIApplet(GlasgowApplet):
required_revision = "C3"
Expand Down

0 comments on commit 3c341f0

Please sign in to comment.