antonvh / PUPRemote

Communication protocol over LEGO PoweredUP (PUP) for hubs running Pybricks
GNU General Public License v3.0
6 stars 2 forks source link

Size always in powers of 2 #2

Closed ste7anste7an closed 1 year ago

ste7anste7an commented 1 year ago

https://github.com/antonvh/PUPRemote/blob/7cffd7331b237a5f96c6d25186238bdeab2b913b/LPF2/pupremote.py#L55

Round the size to the next power of 2. When working with two format strings for each direction, we should take the max of struct.calcsize of both format strings

Proposal:

def next_power_of_2(v):
  v-=1
  v |= v >> 1
  v |= v >> 2
  v |= v >> 4
  v+=1
  return v

  size_pup_to_hub=struct.calcsize(format_pup_to_hub)
  size_hub_to_pub=struct.calcsize(format_hub_to_pup)
  size=next_power_of_2(max(size_pup_to_hub,size_hub_to_pub)
ste7anste7an commented 1 year ago

This is not needed. The size parameter in the mode strutcture can be any number (not rounded to power of 2). Only when the payload is created, the number of bytes that are send are rounded up to the next power of 2. This happens already in LPF2/lpf2_new.py in load_payload https://github.com/antonvh/PUPRemote/blob/6acafcbe56bcd3a99f395b0d8c2eac0c18ed23ca/LPF2/lpf2_new.py#L117