implemented DIn trigger captures
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#!/bin/env python3
|
||||
import gex
|
||||
|
||||
client = gex.Client(port='/dev/ttyACM0')
|
||||
|
||||
# Neopixel strip
|
||||
strip = gex.Neopixel(client, 'npx')
|
||||
# Load RGB to the strip
|
||||
strip.load([0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF])
|
||||
|
||||
# I2C bus
|
||||
i2c = gex.I2C(client, 'i2c')
|
||||
# Read device register
|
||||
print(i2c.read_reg(address=0x76, reg=0xD0))
|
||||
# Write value to a register
|
||||
i2c.write_reg(address=0x76, reg=0xF4, value=0xFA)
|
||||
|
||||
# SPI
|
||||
spi = gex.SPI(client, 'spi')
|
||||
# Query slave 0
|
||||
print(spi.query(0, [0xAA, 0xBB, 0xCC, 0xDD], rlen=2, rskip=4))
|
||||
# Write slaves 0 and 2
|
||||
spi.multicast(0b101, [0xDE, 0xAD, 0xBE, 0xEF])
|
||||
|
||||
# USART
|
||||
usart = gex.USART(client, 'serial')
|
||||
# Handle received data
|
||||
usart.listen(lambda x: print(x, end='', flush=True))
|
||||
# Write a string
|
||||
usart.write("AHOJ\r\n")
|
||||
|
||||
# Digital output (8 pins)
|
||||
display = gex.DOut(client, 'display')
|
||||
display.write(0b10110011)
|
||||
display.toggle(0b00010010)
|
||||
|
||||
@@ -12,6 +12,11 @@ class Unit:
|
||||
self._on_event(event, payload)
|
||||
|
||||
self.client.bind_report_listener(self.callsign, evt_hdl)
|
||||
self._init()
|
||||
|
||||
def _init(self):
|
||||
""" Do post-constructor init """
|
||||
pass
|
||||
|
||||
def _type(self) -> str:
|
||||
raise NotImplementedError("Missing _type() in Unit class \"%s\"" % self.__class__.__name__)
|
||||
|
||||
@@ -9,6 +9,9 @@ class DIn(gex.Unit):
|
||||
the read word has the format (bits) |<C6><C5><C0>|
|
||||
"""
|
||||
|
||||
def _init(self):
|
||||
self.handlers = {}
|
||||
|
||||
def _type(self):
|
||||
return 'DI'
|
||||
|
||||
@@ -17,3 +20,43 @@ class DIn(gex.Unit):
|
||||
msg = self._query(0x00)
|
||||
pp = gex.PayloadParser(msg)
|
||||
return pp.u16()
|
||||
|
||||
def arm(self, pins:int, auto:bool=False, confirm:bool=False):
|
||||
"""
|
||||
Arm pins for single shot event generation
|
||||
pins - array of pin indices to arm
|
||||
auto - use auto trigger (auto re-arm after hold-off)
|
||||
"""
|
||||
pb = gex.PayloadBuilder()
|
||||
pb.u16(pins)
|
||||
self._send(0x02 if auto else 0x01, pb.close())
|
||||
|
||||
def disarm(self, pins:int, confirm:bool=False):
|
||||
"""
|
||||
DisArm pins
|
||||
pins - array of pin indices to arm
|
||||
"""
|
||||
|
||||
pb = gex.PayloadBuilder()
|
||||
pb.u16(pins)
|
||||
self._send(0x03, pb.close())
|
||||
|
||||
def on_trigger(self, sensitive_pins, callback):
|
||||
"""
|
||||
Assign a trigger callback
|
||||
"""
|
||||
for i in range(0, 16):
|
||||
if sensitive_pins & (1 << i):
|
||||
self.handlers[i] = callback
|
||||
|
||||
def _on_event(self, event:int, payload):
|
||||
if event == 0x00:
|
||||
# trigger interrupt
|
||||
pp = gex.PayloadParser(payload)
|
||||
triggersource = pp.u16()
|
||||
snapshot = pp.u16()
|
||||
|
||||
for i in range(0,16):
|
||||
if triggersource & (1<<i):
|
||||
if i in self.handlers:
|
||||
self.handlers[i](snapshot)
|
||||
|
||||
+7
-1
@@ -21,6 +21,10 @@ class USART(gex.Unit):
|
||||
"""
|
||||
Write bytes. If 'sync' is True, wait for completion. sync implies confirm
|
||||
"""
|
||||
|
||||
if type(payload) is str:
|
||||
payload = payload.encode('utf-8')
|
||||
|
||||
pb = gex.PayloadBuilder()
|
||||
pb.blob(payload) # payload to write
|
||||
|
||||
@@ -30,5 +34,7 @@ class USART(gex.Unit):
|
||||
if event == 0:
|
||||
# Data received
|
||||
if self.handler:
|
||||
data = payload if self.handler_decode is None else payload.decode(self.handler_decode)
|
||||
data = payload if self.handler_decode is None \
|
||||
else payload.decode(self.handler_decode)
|
||||
|
||||
self.handler(data)
|
||||
|
||||
@@ -4,8 +4,6 @@ import gex
|
||||
|
||||
client = gex.Client(timeout=1.5)
|
||||
|
||||
#print(client.ini_read())
|
||||
|
||||
if False:
|
||||
s = client.ini_read()
|
||||
print(s)
|
||||
@@ -98,12 +96,27 @@ if False:
|
||||
|
||||
# time.sleep(.001)
|
||||
|
||||
if True:
|
||||
if False:
|
||||
usart = gex.USART(client, 'serial')
|
||||
usart.listen(lambda x: print(x, end='',flush=True))
|
||||
while True:
|
||||
client.poll()
|
||||
time.sleep(.01)
|
||||
|
||||
if True:
|
||||
print(client.ini_read())
|
||||
|
||||
trig = gex.DIn(client, 'trig')
|
||||
print(trig.read())
|
||||
|
||||
# Two pins are defined, PA10 and PA7. PA10 is the trigger, in the order from smallest to highest number 1
|
||||
trig.arm(0b10)
|
||||
trig.on_trigger(0b10, lambda snap: print("snap 0x%X" % snap))
|
||||
|
||||
while True:
|
||||
client.poll()
|
||||
time.sleep(.01)
|
||||
|
||||
#
|
||||
# for n in range(0,100):
|
||||
# print(n)
|
||||
|
||||
Reference in New Issue
Block a user