preliminary timestamps support + uint64_t parsing/building

doublebuf
Ondřej Hruška 7 years ago
parent 44d128254b
commit ba93523fb1
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 3
      gex/Client.py
  2. 10
      gex/PayloadBuilder.py
  3. 10
      gex/PayloadParser.py
  4. 8
      gex/Unit.py
  5. 7
      gex/units/DIn.py
  6. 6
      gex/units/USART.py
  7. 2
      main.py

@ -37,10 +37,11 @@ class Client:
pp = PayloadParser(msg.data)
callsign = pp.u8()
event = pp.u8()
timestamp = pp.u64()
payload = pp.tail()
if callsign in self.report_handlers:
self.report_handlers[callsign](event, payload)
self.report_handlers[callsign](event, payload, timestamp)
else:
print("Unhandled event report: callsign %d, event %d" % (callsign, event))
print(payload)

@ -27,7 +27,11 @@ class PayloadBuilder:
def u32(self, num:int):
""" Add a uint32_t """
self.buf.extend((num&0xFFFFFFFF).to_bytes(length=4, byteorder=self.endian, signed=False))
self.buf.extend(num.to_bytes(length=4, byteorder=self.endian, signed=False))
def u64(self, num:int):
""" Add a uint64_t """
self.buf.extend(num.to_bytes(length=8, byteorder=self.endian, signed=False))
def i8(self, num:int):
""" Add a int8_t """
@ -41,6 +45,10 @@ class PayloadBuilder:
""" Add a int32_t """
self.buf.extend(num.to_bytes(length=4, byteorder=self.endian, signed=True))
def i64(self, num:int):
""" Add a int64_t """
self.buf.extend(num.to_bytes(length=8, byteorder=self.endian, signed=True))
def float(self, num:float):
""" Add a float (4 bytes) """
fmt = '<f' if self.endian == 'little' else '>f'

@ -55,6 +55,11 @@ class PayloadParser:
slice = self._slice(4)
return int.from_bytes(slice, byteorder=self.endian, signed=False)
def u64(self) -> int:
""" Read a uint64_t """
slice = self._slice(8)
return int.from_bytes(slice, byteorder=self.endian, signed=False)
def i8(self) -> int:
""" Read a int8_t """
slice = self._slice(1)
@ -70,6 +75,11 @@ class PayloadParser:
slice = self._slice(4)
return int.from_bytes(slice, byteorder=self.endian, signed=True)
def i64(self) -> int:
""" Read a int64_t """
slice = self._slice(8)
return int.from_bytes(slice, byteorder=self.endian, signed=True)
def float(self) -> float:
""" Read a float (4 bytes) """
slice = self._slice(4)

@ -8,8 +8,8 @@ class Unit:
self.unit_type = self._type()
self.callsign = client.get_callsign(name, self.unit_type)
def evt_hdl(event: int, payload):
self._on_event(event, payload)
def evt_hdl(event: int, payload, timestamp):
self._on_event(event, payload, timestamp)
self.client.bind_report_listener(self.callsign, evt_hdl)
self._init()
@ -50,6 +50,6 @@ class Unit:
"""
self.client.bulk_write(cs=self.callsign, cmd=cmd, id=id, pld=pld, bulk=bulk)
def _on_event(self, event:int, payload):
def _on_event(self, event:int, payload, timestamp:int):
""" Stub for an event handler """
raise NotImplementedError("Missing on_event() in Unit class \"%s\"" % self.__class__.__name__)
raise NotImplementedError("Missing _on_event() in Unit class \"%s\"" % self.__class__.__name__)

@ -43,13 +43,14 @@ class DIn(gex.Unit):
def on_trigger(self, sensitive_pins, callback):
"""
Assign a trigger callback
Assign a trigger callback.
Arguments are: pins snapshot, timestamp
"""
for i in range(0, 16):
if sensitive_pins & (1 << i):
self.handlers[i] = callback
def _on_event(self, event:int, payload):
def _on_event(self, event:int, payload, timestamp:int):
if event == 0x00:
# trigger interrupt
pp = gex.PayloadParser(payload)
@ -59,4 +60,4 @@ class DIn(gex.Unit):
for i in range(0,16):
if triggersource & (1<<i):
if i in self.handlers:
self.handlers[i](snapshot)
self.handlers[i](snapshot, timestamp)

@ -13,6 +13,8 @@ class USART(gex.Unit):
Attach a Rx listener callback.
decode can be: None, 'utf-8', 'ascii' (any valid encoding for bytearray.decode())
None decoding returns bytearray
handler receives args: (bytes, timestamp)
"""
self.handler_decode = decode
self.handler = handler
@ -30,11 +32,11 @@ class USART(gex.Unit):
self._send(0x01 if sync else 0x00, pb.close(), confirm=confirm or sync)
def _on_event(self, event:int, payload):
def _on_event(self, event:int, payload, timestamp:int):
if event == 0:
# Data received
if self.handler:
data = payload if self.handler_decode is None \
else payload.decode(self.handler_decode)
self.handler(data)
self.handler(data, timestamp)

@ -111,7 +111,7 @@ if True:
# 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))
trig.on_trigger(0b10, lambda snap,ts: print("snap 0x%X, ts %d" % (snap,ts)))
while True:
client.poll()

Loading…
Cancel
Save