PowerBroker2/pySerialTransfer

Sending a Boolean

jmdelahanty opened this issue · 5 comments

Hello glorious PowerBroker!

I'm trying to send along a boolean value in a struct but I'm not sure how to do it with pySerialTransfer! Any advice?

Hey Jeremy!

Bools are 1 byte objects both in Arduino/C++ and Python, so they should be compatible automatically. Bools are also automatically supported in pySerialTransfer.

You're the best! I'll close this for now. If I run into something weird when I'm putting it into the code I'll just comment here.

To double check, this means I don't need to do any val_type_overrides if what's fed into the tx_buffer is type bool? See this line for where I'm doing it.

So I could just add this to the transfer:

metaData_size = link.tx_obj(arduino_metadata['lickContingency'], metaData_size)

lickContingency is a Boolean value (literally True or False)

metaData_size = link.tx_obj(arduino_metadata['lickContingency'], metaData_size) should be fine because Python will automatically create a value type format for the transmission if the payload object is of a built-in Python type (i.e. bool):

tx_obj():

if type(val) == str:
val = val.encode()
format_str = '%ds' % len(val)
elif type(val) == dict:
val = json.dumps(val).encode()
format_str = '%ds' % len(val)
elif type(val) == float:
format_str = 'f'
elif type(val) == int:
format_str = 'i'
elif type(val) == bool:
format_str = '?'
elif type(val) == list:
for el in val:
start_pos = self.tx_obj(el, start_pos)
return start_pos

I'd say give it a shot and see if it works

This totally works by the way!