From 5d2810cacf499149f97bbe261b110d5e8e89a920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 28 Dec 2017 21:56:51 +0100 Subject: [PATCH] added DI unit support --- gex/PayloadBuilder.py | 6 +++--- gex/PayloadParser.py | 7 +++++++ gex/__init__.py | 1 + gex/units/DIn.py | 19 +++++++++++++++++++ main.py | 27 ++++++++++++++++++++++++--- 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 gex/units/DIn.py diff --git a/gex/PayloadBuilder.py b/gex/PayloadBuilder.py index 361ae26..70b8e6d 100644 --- a/gex/PayloadBuilder.py +++ b/gex/PayloadBuilder.py @@ -15,15 +15,15 @@ class PayloadBuilder: def u8(self, num:int): """ Add a uint8_t """ - self.buf.extend(num.to_bytes(length=1, byteorder=self.endian, signed=False)) + self.buf.extend((num&0xFF).to_bytes(length=1, byteorder=self.endian, signed=False)) def u16(self, num:int): """ Add a uint16_t """ - self.buf.extend(num.to_bytes(length=2, byteorder=self.endian, signed=False)) + self.buf.extend((num&0xFFFF).to_bytes(length=2, byteorder=self.endian, signed=False)) def u32(self, num:int): """ Add a uint32_t """ - self.buf.extend(num.to_bytes(length=4, byteorder=self.endian, signed=False)) + self.buf.extend((num&0xFFFFFFFF).to_bytes(length=4, byteorder=self.endian, signed=False)) def i8(self, num:int): """ Add a int8_t """ diff --git a/gex/PayloadParser.py b/gex/PayloadParser.py index 951de2d..b2ab727 100644 --- a/gex/PayloadParser.py +++ b/gex/PayloadParser.py @@ -1,5 +1,8 @@ import struct +from gex.TinyFrame import TF_Msg + + class PayloadParser: """ Utility for parsing a binary payload @@ -7,6 +10,10 @@ class PayloadParser: def __init__(self, buf, endian:str='little'): """ buf - buffer to parse (bytearray or binary string) """ + + if type(buf) == TF_Msg: + buf = buf.data + self.buf = buf self.ptr = 0 self.endian = endian diff --git a/gex/__init__.py b/gex/__init__.py index 24ab0a8..e1db57a 100644 --- a/gex/__init__.py +++ b/gex/__init__.py @@ -9,6 +9,7 @@ from gex.Client import Client # import all the units from gex.units.Pin import Pin from gex.units.DOut import DOut +from gex.units.DIn import DIn # General, low level diff --git a/gex/units/DIn.py b/gex/units/DIn.py new file mode 100644 index 0000000..730db6d --- /dev/null +++ b/gex/units/DIn.py @@ -0,0 +1,19 @@ +import gex + +class DIn(gex.Unit): + """ + Digital input 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, + the read word has the format (bits) || + """ + + def _type(self): + return 'DI' + + def read(self): + """ Read pins """ + msg = self.query(0x00) + pp = gex.PayloadParser(msg) + return pp.u16() diff --git a/main.py b/main.py index 33a649e..1c8fcd5 100644 --- a/main.py +++ b/main.py @@ -21,8 +21,20 @@ if False: led.toggle() time.sleep(.1) -if True: - leds = gex.DOut(client, 'TST') +if False: + leds = gex.DOut(client, 'strip') + + nn = 3 + for i in range(0,20): + leds.write(nn) + time.sleep(.05) + nn<<=1 + nn|=(nn&0x40)>>6 + nn=nn&0x3F + leds.clear(0xFF) + +if False: + leds = gex.DOut(client, 'bargraph') for i in range(0,0x41): leds.write(i&0x3F) @@ -34,4 +46,13 @@ if False: for i in range(0, 0x41): #leds.write(i & 0x3F) leds.toggle(0xFF) - time.sleep(.1) \ No newline at end of file + time.sleep(.1) + +if True: + btn = gex.DIn(client, 'btn') + strip = gex.DOut(client, 'strip') + + for i in range(0, 10000): + b = btn.read() + strip.write((b << 2) | ((~b) & 1)) + time.sleep(.02)