From b19ad3ce82a6390d22dc08a15ea54a2a849fd412 Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Sat, 7 Mar 2026 08:43:54 +1100 Subject: [PATCH] tiny code cleanup --- relay/bluetooth_handler.py | 6 +++--- relay/lora_handler.py | 17 +++++++++++++++++ relay/main.py | 36 +++++------------------------------- 3 files changed, 25 insertions(+), 34 deletions(-) create mode 100644 relay/lora_handler.py diff --git a/relay/bluetooth_handler.py b/relay/bluetooth_handler.py index 91a2644..395ac8b 100644 --- a/relay/bluetooth_handler.py +++ b/relay/bluetooth_handler.py @@ -19,7 +19,7 @@ IRQ_GATTS_WRITE = const(3) class BluetoothHandler: def __init__(self): - print("initting bluetooth") + print("Initializing Bluetooth...") self.ble = bluetooth.BLE() self.ble.active(True) self.ble.irq(self.irq) @@ -31,12 +31,12 @@ class BluetoothHandler: self.advertise() def advertise(self): - print("advertising") + print("Advertising Bluetooth...") # note: \x02\x01\x06\x0C\x09 is the BLE header that tells other devices we're BLE only and discoverable self.ble.gap_advertise(100_000, b"\x02\x01\x06\x0C\x09MeshConnectorNode") def irq(self, event, data): - print(f"IRQ | EVENT: {event}, DATA: {data}") + print(f"BLUETOOTH IRQ | EVENT: {event}, DATA: {data}") if event == IRQ_CONNECT: conn_handle, _, _ = data self.connections.add(conn_handle) diff --git a/relay/lora_handler.py b/relay/lora_handler.py new file mode 100644 index 0000000..3da3992 --- /dev/null +++ b/relay/lora_handler.py @@ -0,0 +1,17 @@ +class LoRaHandler: + def __init__(self): + print("Initializing LoRa...") + + # initialize our radio, im using the HAT SX1262 hat for the pico + self.radio = SX1262(spi_bus=1, clk=10, mosi=11, miso=12, cs=3, irq=20, rst=15, gpio=2) + self.radio.begin(freq=915, bw=125, power=22) + self.radio.setBlockingCallback(False, self.irq) + + def irq(self, events): + print(f"LORA EVENT: {events}") + if events & SX1262.RX_DONE: + msg, err = sx.recv() + error = SX1262.STATUS[err] + print('Receive: {}, {}'.format(msg, error)) + elif events & SX1262.TX_DONE: + print('TX done.') \ No newline at end of file diff --git a/relay/main.py b/relay/main.py index 5a07d77..96277d5 100644 --- a/relay/main.py +++ b/relay/main.py @@ -3,45 +3,19 @@ from _sx126x import * import time from bluetooth_handler import BluetoothHandler +from lora_handler import LoRaHandler LORA_ENABLED = False -def cb(events): - if events & SX1262.RX_DONE: - msg, err = sx.recv() - error = SX1262.STATUS[err] - print('Receive: {}, {}'.format(msg, error)) - elif events & SX1262.TX_DONE: - print('TX done.') - -def init_lora(): - print("Creating class...") - # initialize our radio, im using the HAT SX1262 hat for the pico - radio = SX1262(spi_bus=1, clk=10, mosi=11, miso=12, cs=3, irq=20, rst=15, gpio=2) - - # start LoRa - print("Beginning LoRa...") - - #radio.reset() - radio.begin(freq=915, bw=125, power=22) - radio.setBlockingCallback(False, cb) - -def init_bluetooth(): +def main(): bluetooth_handler = BluetoothHandler() - - -def main(): + lora_handler = None if LORA_ENABLED: - init_lora() + lora_handler = LoRaHandler() - init_bluetooth() - - if LORA_ENABLED: - while True: - print(f"Noise Floor: {radio.getRSSIInst()} | SNR: {radio.getSNR()}") - time.sleep(0.5) + if __name__ == "__main__":