added DI unit support
This commit is contained in:
@@ -15,15 +15,15 @@ class PayloadBuilder:
|
|||||||
|
|
||||||
def u8(self, num:int):
|
def u8(self, num:int):
|
||||||
""" Add a uint8_t """
|
""" 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):
|
def u16(self, num:int):
|
||||||
""" Add a uint16_t """
|
""" 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):
|
def u32(self, num:int):
|
||||||
""" Add a uint32_t """
|
""" 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):
|
def i8(self, num:int):
|
||||||
""" Add a int8_t """
|
""" Add a int8_t """
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import struct
|
import struct
|
||||||
|
|
||||||
|
from gex.TinyFrame import TF_Msg
|
||||||
|
|
||||||
|
|
||||||
class PayloadParser:
|
class PayloadParser:
|
||||||
"""
|
"""
|
||||||
Utility for parsing a binary payload
|
Utility for parsing a binary payload
|
||||||
@@ -7,6 +10,10 @@ class PayloadParser:
|
|||||||
|
|
||||||
def __init__(self, buf, endian:str='little'):
|
def __init__(self, buf, endian:str='little'):
|
||||||
""" buf - buffer to parse (bytearray or binary string) """
|
""" buf - buffer to parse (bytearray or binary string) """
|
||||||
|
|
||||||
|
if type(buf) == TF_Msg:
|
||||||
|
buf = buf.data
|
||||||
|
|
||||||
self.buf = buf
|
self.buf = buf
|
||||||
self.ptr = 0
|
self.ptr = 0
|
||||||
self.endian = endian
|
self.endian = endian
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from gex.Client import Client
|
|||||||
# import all the units
|
# import all the units
|
||||||
from gex.units.Pin import Pin
|
from gex.units.Pin import Pin
|
||||||
from gex.units.DOut import DOut
|
from gex.units.DOut import DOut
|
||||||
|
from gex.units.DIn import DIn
|
||||||
|
|
||||||
|
|
||||||
# General, low level
|
# 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()
|
led.toggle()
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
|
|
||||||
if True:
|
if False:
|
||||||
leds = gex.DOut(client, 'TST')
|
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):
|
for i in range(0,0x41):
|
||||||
leds.write(i&0x3F)
|
leds.write(i&0x3F)
|
||||||
@@ -34,4 +46,13 @@ if False:
|
|||||||
for i in range(0, 0x41):
|
for i in range(0, 0x41):
|
||||||
#leds.write(i & 0x3F)
|
#leds.write(i & 0x3F)
|
||||||
leds.toggle(0xFF)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user