Skip to content

Instantly share code, notes, and snippets.

@delfick
Last active July 3, 2021 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delfick/29615468c1e626c45b9c6ef4b381f65f to your computer and use it in GitHub Desktop.
Save delfick/29615468c1e626c45b9c6ef4b381f65f to your computer and use it in GitHub Desktop.

Assuming you've already done something like:

python3 -m venv photons
./photons/bin/python -m pip install lifx-photons-core

Run this for the raw values:

./photons/bin/python print_bytes.py 2400001400036500d073d563525a00004c49465856320401002302dc710000002d000000

And this for the more "human" values:

./photons/bin/python print_values.py 2400001400036500d073d563525a00004c49465856320401002302dc710000002d000000

Note that the 0s and 1s are probably not in the order you expect. The LIFX protocol is big bit endian and little byte endian, but photons presents the 0s and 1s with little bit endian and little byte endian. So to send the 0s and 1s you'd have to reverse the bits in each byte like so:

b = "001010101010101010101...."

converted = []
for i in range(0, len(b), 8):
    converted.append("".join(reversed(b[i:i+8])))

to_send = "".join(converted)
#!/usr/bin/env python3
from photons_protocol.messages import Messages
from photons_messages import protocol_register
import sys
if len(sys.argv) < 2:
sys.exit("Please say bytes")
bts = sys.argv[1]
pkt = Messages.create(bts, protocol_register=protocol_register)
print(repr(pkt))
maxlength = max(len(name) for name in pkt.Meta.all_names)
for name in pkt.Meta.all_names:
val = pkt.actual(name)
if isinstance(val, list):
result = []
for thing in val:
result.append(thing.pack())
val = f"\n{' ' * maxlength} ".join([str(r) for r in result])
print(f"{name}{(maxlength - len(name)) * ' '}: {val}")
#!/usr/bin/env python3
from photons_protocol.messages import Messages
from photons_messages import protocol_register
from bitarray import bitarray
import binascii
import sys
def reprer(o):
if type(o) is bytes:
return binascii.hexlify(o).decode()
elif type(o) is bitarray:
return binascii.hexlify(o.tobytes()).decode()
return repr(o)
if len(sys.argv) < 2:
sys.exit("Please say bytes")
bts = sys.argv[1]
pkt = Messages.create(bts, protocol_register=protocol_register)
print(repr(pkt))
maxlength = max(len(name) for name in pkt.Meta.all_names)
for name in pkt.Meta.all_names:
val = pkt[name]
if isinstance(val, list):
val = f"\n{' ' * maxlength} ".join([str(r) for r in val])
else:
val = reprer(val)
print(f"{name}{(maxlength - len(name)) * ' '}: {val}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment