From 02e368abce899e58300db04128280bb3452d19ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 28 Dec 2017 15:30:33 +0100 Subject: [PATCH] implemented DOUT --- gex/Client.py | 4 ++++ gex/__init__.py | 1 + gex/units/DOut.py | 38 ++++++++++++++++++++++++++++++++++++++ main.py | 17 ++++++++++++++++- 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 gex/units/DOut.py diff --git a/gex/Client.py b/gex/Client.py index 0e8eae0..366205d 100644 --- a/gex/Client.py +++ b/gex/Client.py @@ -79,6 +79,10 @@ class Client: def get_callsign(self, name:str, type:str = None) -> int: """ Find unit by name and type """ + + if name not in self.unit_lu: + raise Exception("No %s unit called \"%s\"" % (type or '*', name)) + u = self.unit_lu[name] if type is not None: diff --git a/gex/__init__.py b/gex/__init__.py index d1b5d1f..24ab0a8 100644 --- a/gex/__init__.py +++ b/gex/__init__.py @@ -8,6 +8,7 @@ from gex.Client import Client # import all the units from gex.units.Pin import Pin +from gex.units.DOut import DOut # General, low level diff --git a/gex/units/DOut.py b/gex/units/DOut.py new file mode 100644 index 0000000..fe0115e --- /dev/null +++ b/gex/units/DOut.py @@ -0,0 +1,38 @@ +import gex + +class DOut(gex.Unit): + """ + Digital output port. + Pins are represented by bits of a control word, right-aligned. + + For example, if pins C6, C5 and C0 are selected for the unit, + calling the "set" function with a word 0b111 will set all three to 1, + 0b100 will set only C6. + """ + + def _type(self): + return 'DO' + + def write(self, pins:int): + """ Set pins to a value """ + pb = gex.PayloadBuilder() + pb.u16(pins) + self.send(0x00, pb.close()) + + def set(self, pins:int): + """ Set pins high """ + pb = gex.PayloadBuilder() + pb.u16(pins) + self.send(0x01, pb.close()) + + def clear(self, pins:int): + """ Set pins low """ + pb = gex.PayloadBuilder() + pb.u16(pins) + self.send(0x02, pb.close()) + + def toggle(self, pins:int): + """ Toggle pins """ + pb = gex.PayloadBuilder() + pb.u16(pins) + self.send(0x03, pb.close()) diff --git a/main.py b/main.py index 319e67a..33a649e 100644 --- a/main.py +++ b/main.py @@ -14,9 +14,24 @@ if False: client.bulk_write(gex.MSG_INI_WRITE, buf) -if True: +if False: led = gex.Pin(client, 'LED') for i in range(0,10): led.toggle() time.sleep(.1) + +if True: + leds = gex.DOut(client, 'TST') + + for i in range(0,0x41): + leds.write(i&0x3F) + time.sleep(.1) + +if False: + leds = gex.DOut(client, 'TST') + + for i in range(0, 0x41): + #leds.write(i & 0x3F) + leds.toggle(0xFF) + time.sleep(.1) \ No newline at end of file