add shot decode

This commit is contained in:
Tomas Krejci 2024-05-29 14:28:40 +02:00
parent a4c6cea3ef
commit 4f5674ada5

38
main.py
View File

@ -35,6 +35,12 @@ def print_handler():
ble = bluetooth.BLE()
uart = ble_uart.Ble_uart(ble, on_rx=print_handler, name="OpenLaserTag " + device_id_str)
#### OLT IR ###
COMMAND_MASK = 0x800000
DEMAGE = [0, 1, 4, 5, 7, 10, 15, 17, 20, 25, 30, 35, 40, 50, 75, 100]
TEAM = ["RED", "BLUE", "YELLOW", "GREEN"]
#### OLT IR RX imports ###
import ustruct
from sys import platform
@ -87,12 +93,12 @@ from primitives import Switch, Pushbutton
from rgb import RGB
#### OLT Commands ####
LAST_BYTE = 0xE8
COMMAND_END = 0xE8
COMMANDS = [
["NewGame", 0x83, 0x05],
["AdminKill", 0x83, 0x00],
["Explode", 0x83, 0x0B],
["Test", 0x83, 0x15],
["NewGame", 0x83, 0x05, COMMAND_END],
["AdminKill", 0x83, 0x00, COMMAND_END],
["Explode", 0x83, 0x0B, COMMAND_END],
["Test", 0x83, 0x15, COMMAND_END],
]
#### Logging ####
@ -145,8 +151,8 @@ async def olt_command(cmd, btn, rgb, ir_tx, tx1, tx2, tx3):
# IR RX callback
async def cb(byte1, byte2, byte3, packet):
# logger.debug(f"{(packet & 0x800000):0X}")
if packet & 0x800000:
# logger.debug(f"{(packet & COMMAND_MASK):0X}")
if packet & COMMAND_MASK:
logger.debug("Command packet")
print(
f"byte1 0x{byte1:02x} byte2 0x{byte2:02x} byte3 0x{byte3:02x} packet 0x{packet:06x}"
@ -159,6 +165,18 @@ async def cb(byte1, byte2, byte3, packet):
)
uart.write(f"IR RX Shot 0x{packet:06x}\n")
packet = packet >> 10
id = packet >> 6
team = (packet >> 4) & 0x3
demage = packet & 0xF
logger.debug(
f"Shot: player ID:{id}, team {TEAM[team]}, demage {DEMAGE[demage]}"
)
uart.write(
f"Shot: player ID:{id}, team {TEAM[team]}, demage {DEMAGE[demage]}\n"
)
await rgb1.set_blue()
await asyncio.sleep_ms(200)
await rgb1.set_red()
@ -196,11 +214,11 @@ async def main(proto):
PINS = ((COMMANDS[0], 20), (COMMANDS[1], 21), (COMMANDS[2], 18), (COMMANDS[3], 19))
for cmd, pin in PINS:
btn = Pushbutton(machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_UP))
command, tx1, tx2 = cmd
command, tx1, tx2, tx3 = cmd
logger.debug(
f"New command task: {command}, IR 0x{tx1:02X} 0x{tx2:02X} 0x{LAST_BYTE:02X}, btn: {btn._pin} "
f"New command task: {command}, IR 0x{tx1:02X} 0x{tx2:02X} 0x{tx3:02X}, btn: {btn._pin} "
)
asyncio.create_task(olt_command(command, btn, rgb1, ir_tx, tx1, tx2, LAST_BYTE))
asyncio.create_task(olt_command(command, btn, rgb1, ir_tx, tx1, tx2, tx3))
### Main loop ###
while True: