change BLE handler to async

This commit is contained in:
Tomas Krejci 2024-06-06 15:51:24 +02:00
parent 448ca2dbcf
commit 772244153e
5 changed files with 17 additions and 8 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,6 @@
import bluetooth
from ble_uart.ble_advertising import advertising_payload
import uasyncio as asyncio
from micropython import const
_IRQ_CENTRAL_CONNECT = const(1)
@ -67,7 +67,7 @@ class Ble_uart:
if conn_handle in self._connections and value_handle == self._rx_handle:
self._rx_buffer += self._ble.gatts_read(self._rx_handle)
if self._handler:
self._handler()
asyncio.create_task(self._handler())
def any(self):
return len(self._rx_buffer)

21
main.py
View File

@ -84,16 +84,25 @@ import bluetooth
import time
def ble_rx_handler():
async def ble_rx_handler():
message = uart.read().decode().strip()
logger.debug(f"BLE: {message}")
logger.debug(f"BLE: {message}")
try:
if "bomb" in message:
logger.info("BLE: Bomb Explode send to IR")
asyncio.create_task(
olt_sent_ir(ir_tx, COMMANDS[1][1], COMMANDS[1][2], COMMANDS[1][3])
)
logger.info("BLE: Bomb Explode send to IR")
asyncio.create_task(
olt_sent_ir(ir_tx, COMMANDS[1][1], COMMANDS[1][2], COMMANDS[1][3])
)
uart.write("BLE: Bomb Explode send to IR\n")
while True:
time.sleep_ms(2000)
logger.info(
"BLE: Bomb Exploded, Device is LOCKED, please restart Device"
)
uart.write(
"BLE: Bomb Exploded, Device is LOCKED\n Please restart Device\n"
)
message_int = int(message, 16)
logger.debug(f"BLE: {message_int:08x}")