serialization and deserialization

This commit is contained in:
2026-03-08 14:08:34 +11:00
parent 0a560dca17
commit 9195a461dd

View File

@@ -55,6 +55,10 @@ class BluetoothHandler:
self.advertise()
def deserialize_msg(self, s: bytes):
# returns packet type (int) and deserialized data
return s[0], eval(s[1:].decode())
def _get_mac_address(self):
mac = self.ble.config("mac")[1]
return ':'.join('{:02X}'.format(b) for b in mac)
@@ -109,9 +113,8 @@ class BluetoothHandler:
elif event == IRQ_GATTS_WRITE:
conn_handle, value_handle = data
if value_handle == self.tx_handle:
msg = self.ble.gatts_read(self.tx_handle)
msg = self.ble.gatts_read(value_handle)
packet_type, msg = self.deserialize_msg(msg)
print(f"Received: \"{msg}\"")
if msg == b"ping":
for conn in self.connections:
self.ble.gatts_notify(conn, self.rx_handle, b"pong")