Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add to_bytes() helper for MicroPython #105

Merged
merged 5 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions cayennelpp/lpp_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
9 changes: 8 additions & 1 deletion cayennelpp/lpp_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cayennelpp/tests/test_lpp_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,)

Expand Down
4 changes: 4 additions & 0 deletions cayennelpp/tests/test_lpp_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion usetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down