up BLE UART
This commit is contained in:
parent
d91153dbc5
commit
db0f4233fd
42
main.py
42
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))
|
||||
|
Loading…
Reference in New Issue
Block a user