added DI unit support

doublebuf
Ondřej Hruška 6 years ago
parent 02e368abce
commit 5d2810cacf
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 6
      gex/PayloadBuilder.py
  2. 7
      gex/PayloadParser.py
  3. 1
      gex/__init__.py
  4. 19
      gex/units/DIn.py
  5. 27
      main.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 """

@ -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

@ -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

@ -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) |<C6><C5><C0>|
"""
def _type(self):
return 'DI'
def read(self):
""" Read pins """
msg = self.query(0x00)
pp = gex.PayloadParser(msg)
return pp.u16()

@ -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)
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)

Loading…
Cancel
Save