From db0f4233fdc12a8ffafd4cff4bbaf5cd95e352e8 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Wed, 29 May 2024 17:02:53 +0200 Subject: [PATCH] up BLE UART --- main.py | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 983c5b3..a9e3146 100644 --- a/main.py +++ b/main.py @@ -60,12 +60,25 @@ import bluetooth import time -def print_handler(): - print(uart.read().decode().strip()) +def ble_rx_handler(): + message = uart.read().decode().strip() + logger.debug(f"BLE: {message}") + try: + message_int = int(message, 16) + if message_int == 0x01: + uart.write(f"BLE: uart recive {message_int:08x}\n") + else: + logger.debug(f"BLE: message is {message_int}") + + except TypeError: + logger.debug("BLE: TypeError") + pass + + except ValueError: + logger.debug("BLE: ValueError") + pass -ble = bluetooth.BLE() -uart = ble_uart.Ble_uart(ble, on_rx=print_handler, name="OpenLaserTag " + device_id_str) #### OLT IR ### COMMAND_MASK = 0x800000 @@ -160,6 +173,13 @@ async def alive_check(): ### OLT Command ### +async def olt_sent_ir(ir_tx, tx1, tx2, tx3): + # IR TX send + logger.debug(f"Command IR TX transmit") + asyncio.create_task(ir_tx.transmit(tx1, tx2, tx3, False)) + await asyncio.sleep_ms(50) + + async def olt_command(cmd, btn, rgb, ir_tx, tx1, tx2, tx3): logger.debug(f"Init OLT game command {cmd}") await rgb.set_red() @@ -173,9 +193,10 @@ async def olt_command(cmd, btn, rgb, ir_tx, tx1, tx2, tx3): logger.debug(f"Command {cmd} button pressed") await rgb.set_green() # IR TX send - logger.debug(f"Command {cmd} IR TX transmit") - asyncio.create_task(ir_tx.transmit(tx1, tx2, tx3, False)) - await asyncio.sleep_ms(50) + asyncio.create_task(olt_sent_ir(ir_tx, tx1, tx2, tx3)) + # logger.debug(f"Command {cmd} IR TX transmit") + # asyncio.create_task(ir_tx.transmit(tx1, tx2, tx3, False)) + # await asyncio.sleep_ms(50) await btn.release.wait() logger.debug(f"Command {cmd} button released") await rgb.set_red() @@ -245,6 +266,13 @@ async def main(proto): # Uncomment the following to print transmit timing ir_tx.timeit = True + ### BLUETOOTH UART ### + ble = bluetooth.BLE() + global uart + uart = ble_uart.Ble_uart( + ble, on_rx=ble_rx_handler, name="OpenLaserTag " + device_id_str + ) + ### Buttons ### logger.debug("Button init") PINS = ((COMMANDS[0], 20), (COMMANDS[1], 21), (COMMANDS[2], 18), (COMMANDS[3], 19))