From ba93523fb1c3352aa622e866bb91aa584698eb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 27 Jan 2018 20:12:19 +0100 Subject: [PATCH] preliminary timestamps support + uint64_t parsing/building --- gex/Client.py | 3 ++- gex/PayloadBuilder.py | 10 +++++++++- gex/PayloadParser.py | 10 ++++++++++ gex/Unit.py | 8 ++++---- gex/units/DIn.py | 7 ++++--- gex/units/USART.py | 6 ++++-- main.py | 2 +- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/gex/Client.py b/gex/Client.py index 05de782..d7cb8df 100644 --- a/gex/Client.py +++ b/gex/Client.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) diff --git a/gex/PayloadBuilder.py b/gex/PayloadBuilder.py index d9209aa..618b000 100644 --- a/gex/PayloadBuilder.py +++ b/gex/PayloadBuilder.py @@ -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' diff --git a/gex/PayloadParser.py b/gex/PayloadParser.py index 93e099b..cca9514 100644 --- a/gex/PayloadParser.py +++ b/gex/PayloadParser.py @@ -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) diff --git a/gex/Unit.py b/gex/Unit.py index 87c4ea6..01b6e90 100644 --- a/gex/Unit.py +++ b/gex/Unit.py @@ -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__) diff --git a/gex/units/DIn.py b/gex/units/DIn.py index 885f504..d81da67 100644 --- a/gex/units/DIn.py +++ b/gex/units/DIn.py @@ -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<