diff --git a/README.md b/README.md index e4f9a75..bf2352a 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,11 @@ and data resolution. ## Getting Started PyCayenneLPP does not have any external dependencies and only uses builtin -functions and types of Python 3. It is compatible with all the latest and -officially supported Python versions 3.6 and above, though even Python 3.4 +functions and types of Python 3. It is compatible with all the latest and +officially supported Python versions 3.6 and above, though even Python 3.4 will do. -Since PyCayenneLPP 1.2.0 MicroPython is officially supported, and published +Since PyCayenneLPP 1.2.0 MicroPython is officially supported, and published as a separate package under `micropython-pycayennelpp`. ### Python 3 Prerequisites @@ -112,6 +112,10 @@ frame.add_humidity(6, 34.5) buffer = bytes(frame) ``` +**Note:** MicroPython does not support `bytes(frame)` utilising the internal +method `LppFrame.__bytes__(self)` (yet). +Hence, you need to use `LppFrame.to_bytes(self)` instead. + ***Decoding*** ```Python @@ -129,7 +133,7 @@ print(frame) ***JSON Encoding*** -The LppUtil class provides helper function for proper JSON encoding of +The LppUtil class provides helper function for proper JSON encoding of PyCayenneLpp types, i.e. LppFrame, LppData and LppType. ```python @@ -146,7 +150,7 @@ frame.add_humidity(6, 34.5) print(json.dumps(frame, default=LppUtil.json_encode, indent=2)) ``` -There are two wrapper functions to explicitly encode the LPP type as a +There are two wrapper functions to explicitly encode the LPP type as a number or string, number being default for `LppUtil.json_encode` (see above): ```python diff --git a/cayennelpp/lpp_data.py b/cayennelpp/lpp_data.py index 1b2af47..2188f16 100644 --- a/cayennelpp/lpp_data.py +++ b/cayennelpp/lpp_data.py @@ -47,6 +47,10 @@ def __str__(self): return 'LppData(channel = {}, type = {}, value = {})'.format( self.channel, self.type.name, str(self.value)) + def to_bytes(self): + """Explicit wrapper for MicroPython support.""" + return self.__bytes__() + @classmethod def from_bytes(class_object, buf): """Parse a given byte string and return a LppData object.""" diff --git a/cayennelpp/lpp_frame.py b/cayennelpp/lpp_frame.py index 5f52297..9dc4420 100644 --- a/cayennelpp/lpp_frame.py +++ b/cayennelpp/lpp_frame.py @@ -46,7 +46,14 @@ def __bytes__(self): """Return this LppFrame object as a byte string.""" buf = bytearray() for d in self._data: - buf = buf + bytes(d) + buf += bytes(d) + return bytes(buf) + + def to_bytes(self): + """Return this LppFrame object as a byte string (for MicroPython).""" + buf = bytearray() + for d in self._data: + buf += d.to_bytes() return bytes(buf) @classmethod diff --git a/cayennelpp/tests/test_lpp_data.py b/cayennelpp/tests/test_lpp_data.py index 353b544..32fb496 100644 --- a/cayennelpp/tests/test_lpp_data.py +++ b/cayennelpp/tests/test_lpp_data.py @@ -21,6 +21,7 @@ def test_generic_from_bytes(): buff = bytes([0x00, 0x64, 0xff, 0xff, 0xff, 0xfb]) data = LppData.from_bytes(buff) assert buff == bytes(data) + assert buff == data.to_bytes() assert int(data.type) == 100 assert data.value == (4294967291,) diff --git a/cayennelpp/tests/test_lpp_frame.py b/cayennelpp/tests/test_lpp_frame.py index ae6396a..d126c23 100644 --- a/cayennelpp/tests/test_lpp_frame.py +++ b/cayennelpp/tests/test_lpp_frame.py @@ -82,22 +82,26 @@ def test_frame_from_bytes(): buf = bytes([0x03, 0x67, 0x01, 0x10, 0x05, 0x67, 0x00, 0xff]) frame = LppFrame.from_bytes(buf) assert buf == bytes(frame) + assert buf == frame.to_bytes() assert len(frame) == 2 # 01 67 FF D7 buf = bytes([0x01, 0x67, 0xFF, 0xD7]) frame = LppFrame.from_bytes(buf) assert buf == bytes(frame) + assert buf == frame.to_bytes() assert len(frame) == 1 # 06 71 04 D2 FB 2E 00 00 buf = bytes([0x06, 0x71, 0x04, 0xD2, 0xFB, 0x2E, 0x00, 0x00]) frame = LppFrame.from_bytes(buf) assert buf == bytes(frame) + assert buf == frame.to_bytes() assert len(frame) == 1 # 01 88 06 76 5f f2 96 0a 00 03 e8 buf = bytes([0x01, 0x88, 0x06, 0x76, 0x5f, 0xf2, 0x96, 0x0a, 0x00, 0x03, 0xe8]) frame = LppFrame.from_bytes(buf) assert buf == bytes(frame) + assert buf == frame.to_bytes() assert len(frame) == 1 diff --git a/setup.py b/setup.py index cf33a29..5e88e56 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def readme(): setup( name='pycayennelpp', - version='2.3.0', + version='2.4.0', python_requires='>=3.6', description='Encoder and Decoder for CayenneLLP', long_description=readme(), diff --git a/usetup.py b/usetup.py index 423c4fb..385766e 100644 --- a/usetup.py +++ b/usetup.py @@ -9,7 +9,7 @@ def readme(): setup( name='micropython-pycayennelpp', - version='2.3.0', + version='2.4.0', description='Encoder and Decoder for CayenneLLP', long_description=readme(), long_description_content_type='text/markdown',