Skip to content

Commit 0925860

Browse files
committed
interface api
1 parent 9bad91a commit 0925860

File tree

1 file changed

+55
-13
lines changed
  • software/glasgow/applet/interface/gpib_controller

1 file changed

+55
-13
lines changed

software/glasgow/applet/interface/gpib_controller/__init__.py

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
| Listen || IN | IN | IN | OUT | OUT | OUT | IN | OUT | OUT |
6161
+--------++------+-----+-----+------+------+-----+-----+-----+-----+
6262
63-
All lines use active low signalling. It probably would have made sense to invert the pins.
64-
6563
When a line is marked as an input, it should passively pull up the
6664
line to 5v. Otherwise, the lines voltage should be dictatd by other
6765
devices on the bus.
@@ -334,20 +332,68 @@ async def write(self, message: GPIBMessage, data=bytes([0])):
334332
ack = (await self.interface.read(1))[0]
335333
assert GPIBMessage(ack) == GPIBMessage._Acknowledge
336334

337-
async def read(self, to_eoi=True):
338-
await self.device.set_pulls(self.port_spec, high={pin.number for pin in self.listen_pull_high})
335+
async def read(self, *, to_eoi=True):
336+
await self.interface.device.set_pulls(self.port_spec, high={pin.number for pin in self.listen_pull_high})
339337

340338
eoi = False
341339
while not eoi:
342340
await self.interface.write(bytes([GPIBMessage.Listen.value]))
343341
await self.interface.write(bytes([0]))
344-
await self.interface.flush()
345342

346-
eoi = bool((await self.interface.read(1)).tobytes()[0] & 2)
343+
eoi = bool((await self.interface.read(1))[0] & 2)
347344
if not to_eoi:
348345
eoi = True
349346

350-
yield (await self.interface.read(1)).tobytes()
347+
yield (await self.interface.read(1))
348+
349+
async def send_to(self, address, data):
350+
await self.cmd_talk(address)
351+
await self.write(GPIBMessage.Data, data)
352+
await self.write(GPIBMessage.DataEOI, b'\n')
353+
await self.cmd_untalk()
354+
355+
async def read_from(self, address, *, to_eoi=True):
356+
await self.cmd_listen(address)
357+
all = bytes([])
358+
async for data in self.read(to_eoi=to_eoi):
359+
all += data
360+
361+
return all
362+
363+
async def iter_from(self, address, *, to_eoi=True):
364+
await self.cmd_listen(address)
365+
async for data in self.read(to_eoi=to_eoi):
366+
yield data
367+
368+
async def cmd_talk(self, address):
369+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.MTA.value | address]))
370+
371+
async def cmd_untalk(self):
372+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.UNT.value]))
373+
374+
async def cmd_listen(self, address):
375+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.MLA.value | address]))
376+
377+
async def cmd_unlisten(self):
378+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.UNL.value]))
379+
380+
async def cmd_ppe(self, address):
381+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.PPE.value | address]))
382+
383+
async def cmd_ppd(self, address):
384+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.PPD.value | address]))
385+
386+
async def cmd_spe(self):
387+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.SPE.value]))
388+
389+
async def cmd_spd(self):
390+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.SPD.value]))
391+
392+
async def cmd_llo(self):
393+
await self.write(GPIBMessage.Command, bytes([GPIBCommand.LLO.value]))
394+
395+
async def ifc(self):
396+
await self.write(GPIBMessage.InterfaceClear)
351397

352398

353399
class GPIBControllerApplet(GlasgowApplet):
@@ -430,14 +476,10 @@ def check_address(value):
430476

431477
async def interact(self, device, args, iface):
432478
if args.command:
433-
await iface.write(GPIBMessage.Command, bytes([GPIBCommand.MTA.value | args.address]))
434-
await iface.write(GPIBMessage.Data, bytes(command.encode("ascii")))
435-
await iface.write(GPIBMessage.DataEOI, b'\n')
436-
await iface.write(GPIBMessage.Command, bytes([GPIBCommand.UNT.value]))
479+
await iface.send_to(args.address, args.command.encode("ascii"))
437480

438481
if args.read_eoi:
439-
await iface.write(GPIBMessage.Command, bytes([GPIBCommand.MLA.value | args.address]))
440-
async for data in iface.read(True):
482+
async for data in iface.iter_from(args.address, to_eoi=True):
441483
sys.stdout.buffer.write(data)
442484
sys.stdout.buffer.flush()
443485

0 commit comments

Comments
 (0)