From 44d128254b8a8562a2a948b1d2d4fe83d255dfbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 27 Jan 2018 12:23:25 +0100 Subject: [PATCH] implemented DIn trigger captures --- demo_neopixel.py | 36 ++++++++++++++++++++++++++++++++++++ gex/Unit.py | 5 +++++ gex/units/DIn.py | 43 +++++++++++++++++++++++++++++++++++++++++++ gex/units/USART.py | 8 +++++++- main.py | 19 ++++++++++++++++--- 5 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 demo_neopixel.py diff --git a/demo_neopixel.py b/demo_neopixel.py new file mode 100644 index 0000000..338306c --- /dev/null +++ b/demo_neopixel.py @@ -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) + diff --git a/gex/Unit.py b/gex/Unit.py index 8d9ecfa..87c4ea6 100644 --- a/gex/Unit.py +++ b/gex/Unit.py @@ -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__) diff --git a/gex/units/DIn.py b/gex/units/DIn.py index 5447fd5..885f504 100644 --- a/gex/units/DIn.py +++ b/gex/units/DIn.py @@ -9,6 +9,9 @@ class DIn(gex.Unit): the read word has the format (bits) || """ + 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<