diff --git a/relay/bluetooth_handler.py b/relay/bluetooth_handler.py index 44fdbeb..be45241 100644 --- a/relay/bluetooth_handler.py +++ b/relay/bluetooth_handler.py @@ -1,6 +1,9 @@ import bluetooth import struct +import time from micropython import const +from queue import Queue +import uasyncio # i just made a UUID up and changed the last number to change what protocol you're using @@ -39,6 +42,12 @@ class BluetoothHandler: def __init__(self): print("Initializing Bluetooth...") self.ble = bluetooth.BLE() + + print("Resetting bluetooth...") + # Deactivate BLE to clear the state + self.ble.active(False) + time.sleep(0.5) + print("Activating...") self.ble.active(True) print("Setting IRQ callback...") @@ -53,11 +62,12 @@ class BluetoothHandler: self.connections = set() + self.packets = Queue() + self.advertise() def deserialize_msg(self, s: bytes): # returns packet type (int) and deserialized data - print(s[1:].decode()) return s[0], eval(s[1:].decode()) def _get_mac_address(self): @@ -118,4 +128,5 @@ class BluetoothHandler: packet_type, msg = self.deserialize_msg(msg) print(f"Received: \"{msg}\"") + uasyncio.create_task(self.packets.put((packet_type, msg))) diff --git a/relay/connection.py b/relay/connection.py index 432226f..06ecf16 100644 --- a/relay/connection.py +++ b/relay/connection.py @@ -6,7 +6,8 @@ import uasyncio as asyncio class Connection: - def __init__(self): + def __init__(self, bluetooth_handler=None): + self.bluetooth_handler = bluetooth_handler self.load_public_constants() # generate diffie-hellman keys @@ -19,16 +20,23 @@ class Connection: # start lora handler self.lora = LoRaHandler() + async def bluetooth_listener(self): + while True: + msg_type, payload = await self.bluetooth_handler.packets.get() + + if msg_type == 3: # advertise self + self.lora.advertise() + async def start(self): print("Starting connection...") - self.lora.advertise() + + if self.bluetooth_handler: + asyncio.create_task(self.bluetooth_listener()) while True: msg: LoRaMessage = await self.lora.message_queue.get() print(f"Received message from {msg.sender_id}: {msg.payload}") - print("Sending response...") - self.lora.send(b"Pong!", msg.sender_id) def load_public_constants(self): with open("public_constants.txt", "r") as f: diff --git a/relay/lora_handler.py b/relay/lora_handler.py index 1d25852..de54347 100644 --- a/relay/lora_handler.py +++ b/relay/lora_handler.py @@ -3,29 +3,11 @@ import uuid from sx1262 import SX1262 import uasyncio as asyncio +from queue import Queue from machine import Pin led = Pin(25, Pin.OUT) -class Queue: - def __init__(self): - self.items = [] - self.waiters = [] - - async def put(self, item): - self.items.append(item) - if self.waiters: - waiter = self.waiters.pop(0) - waiter.set() - - async def get(self): - while not self.items: - event = asyncio.Event() - self.waiters.append(event) - await event.wait() - - return self.items.pop(0) - class FixedDeque: def __init__(self, maxlen): self.maxlen = maxlen diff --git a/relay/main.py b/relay/main.py index 0e0e7aa..2a34148 100644 --- a/relay/main.py +++ b/relay/main.py @@ -7,16 +7,16 @@ import time import crypt_random import uasyncio as asyncio -BLUETOOTH_ENABLED = False - def main(): bluetooth_handler = None - if BLUETOOTH_ENABLED: + try: from bluetooth_handler import BluetoothHandler bluetooth_handler = BluetoothHandler() + except ImportError: + print("Bluetooth is disabled. Continuing without it...") - conn = Connection() + conn = Connection(bluetooth_handler) asyncio.run(conn.start()) diff --git a/relay/queue.py b/relay/queue.py new file mode 100644 index 0000000..aed27c1 --- /dev/null +++ b/relay/queue.py @@ -0,0 +1,21 @@ +import uasyncio as asyncio + + +class Queue: + def __init__(self): + self.items = [] + self.waiters = [] + + async def put(self, item): + self.items.append(item) + if self.waiters: + waiter = self.waiters.pop(0) + waiter.set() + + async def get(self): + while not self.items: + event = asyncio.Event() + self.waiters.append(event) + await event.wait() + + return self.items.pop(0) \ No newline at end of file