From 1bea9dfa0ba0dca558ce445dec13fcccd8879258 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Tue, 28 May 2024 17:19:12 +0200 Subject: [PATCH] fix debug --- .vscode/extensions.json | 3 +- main.py | 99 ++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index fbc7999..710fc99 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -3,6 +3,7 @@ "ms-python.python", "visualstudioexptteam.vscodeintellicode", "ms-python.vscode-pylance", - "paulober.pico-w-go" + "paulober.pico-w-go", + "charliermarsh.ruff" ] } \ No newline at end of file diff --git a/main.py b/main.py index 335fa23..da20d18 100644 --- a/main.py +++ b/main.py @@ -4,9 +4,10 @@ import machine, time from sys import platform -ESP32 = platform == 'esp32' -RP2 = platform == 'rp2' -PYBOARD = platform == 'pyboard' + +ESP32 = platform == "esp32" +RP2 = platform == "rp2" +PYBOARD = platform == "pyboard" if ESP32 or RP2: from machine import Pin else: @@ -20,7 +21,7 @@ import time import gc from machine import Pin, freq from olt_lib.ir_rx.print_error import print_error # Optional print of error codes -from olt_lib.ir_rx.olt import LT_24 as LT_24_RX # Import all implemented classes +from olt_lib.ir_rx.olt import LT_24 as LT_24_RX # Import all implemented classes # Define IR RX pin according to platform if platform == "pyboard": @@ -35,9 +36,10 @@ elif platform == "rp2": #### OLT IR TX imports ### from sys import platform -ESP32 = platform == 'esp32' -RP2 = platform == 'rp2' -PYBOARD = platform == 'pyboard' + +ESP32 = platform == "esp32" +RP2 = platform == "rp2" +PYBOARD = platform == "pyboard" if ESP32 or RP2: from machine import Pin else: @@ -45,16 +47,17 @@ else: import uasyncio as asyncio from primitives.switch import Switch from primitives.delay_ms import Delay_ms + # Import all implemented classes from olt_lib.ir_tx.olt import LT_24 as LT_24_TX # Define IR TX pin according to platform if ESP32: # Pins for IR LED gate - ir_tx_pin = Pin(23, Pin.OUT, value = 0) + ir_tx_pin = Pin(23, Pin.OUT, value=0) elif RP2: - ir_tx_pin = Pin(17, Pin.OUT, value = 0) + ir_tx_pin = Pin(17, Pin.OUT, value=0) else: - ir_tx_pin = Pin('X1') + ir_tx_pin = Pin("X1") #### Other imports ### from primitives.delay_ms import Delay_ms @@ -63,36 +66,39 @@ from primitives import Switch, Pushbutton from rgb import RGB #### OLT Commands #### -LAST_BYTE = (0xE8) -COMMANDS = [['NewGame', 0x83, 0x05], - ['AdminKill', 0x83, 0x00], - ['Explode', 0x83, 0x0B], - ['Test', 0x83, 0x15]] +LAST_BYTE = 0xE8 +COMMANDS = [ + ["NewGame", 0x83, 0x05], + ["AdminKill", 0x83, 0x00], + ["Explode", 0x83, 0x0B], + ["Test", 0x83, 0x15], +] #### Logging #### import logging + global logger init_ticks = time.ticks_ms() # change logging level here, logging.ERROR is default, logging.DEBUG for more info, and reset device log_level = logging.DEBUG -#log_level = logging.ERROR -logging.basicConfig(level=log_level, format='%(name)s - %(levelname)s - %(message)s') +# log_level = logging.ERROR +logging.basicConfig(level=log_level, format="%(name)s - %(levelname)s - %(message)s") logger = logging.getLogger(__name__) ### Alive check ### -async def alive_check(): +async def alive_check(): # loop for checked is alive logger.debug("alive check start") - onboar_led = machine.Pin("LED", machine.Pin.OUT) # Pi Pico onboard LED - - - while True: - logger.debug(f"running ms: {time.ticks_ms() - init_ticks}") + onboar_led = machine.Pin("LED", machine.Pin.OUT) # Pi Pico onboard LED + + while True: + logger.debug(f"running ms: {time.ticks_ms() - init_ticks}") onboar_led.on() await asyncio.sleep_ms(100) onboar_led.off() - await asyncio.sleep_ms(900) + await asyncio.sleep_ms(900) + ### OLT Command ### async def olt_command(cmd, btn, rgb, ir_tx, tx1, tx2, tx3): @@ -100,85 +106,84 @@ async def olt_command(cmd, btn, rgb, ir_tx, tx1, tx2, tx3): await rgb.set_red() btn.press_func(None) btn.release_func(None) - + while True: btn.press.clear() btn.release.clear() await btn.press.wait() - logger.debug("Command {cmd} button pressed") + logger.debug(f"Command {cmd} button pressed") await rgb.set_green() - # IR TX send - logger.debug("Command {cmd} IR TX transmit") + # 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) await btn.release.wait() - logger.debug("Command {cmd} button released") + logger.debug(f"Command {cmd} button released") await rgb.set_red() - - ### main ### async def main(proto): - logger.debug("main start") - + ### alive check ### asyncio.create_task(alive_check()) - + ### RGB ### logger.debug("RGB init") global rgb1 rgb1 = RGB() - + ### OLT IR RX ### logger.debug("OLT IR RX init") + # User callback async def cb(byte1, byte2, byte3, packet): - print(f"byte1 0x{byte1:02x} byte2 0x{byte2:02x} byte3 0x{byte3:02x} packet 0x{packet:06x}") + print( + f"byte1 0x{byte1:02x} byte2 0x{byte2:02x} byte3 0x{byte3:02x} packet 0x{packet:06x}" + ) await rgb1.set_blue() await asyncio.sleep_ms(200) await rgb1.set_red() ir_rx = LT_24_RX(ir_rx_pin, cb) # Instantiate receiver - ir_rx.error_function(print_error) # Show debug information + ir_rx.error_function(print_error) # Show debug information ### OLT IR TX ### logger.debug("OLT IR TX init") - + ir_tx = LT_24_TX(ir_tx_pin, 56000) # My decoder chip is 56KHz # Uncomment the following to print transmit timing ir_tx.timeit = True - ### Buttons ### logger.debug("Button init") 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 - logger.debug(f"New command task: {command}, IR 0x{tx1:02X} 0x{tx2:02X} 0x{LAST_BYTE:02X}, btn: {btn._pin} ") + logger.debug( + f"New command task: {command}, IR 0x{tx1:02X} 0x{tx2:02X} 0x{LAST_BYTE:02X}, btn: {btn._pin} " + ) asyncio.create_task(olt_command(command, btn, rgb1, ir_tx, tx1, tx2, LAST_BYTE)) - ### Main loop ### while True: await asyncio.sleep(1) + def exception_handler(loop, context): - # log exception - print(context['exception']) + # log exception + print(context["exception"]) + ### asyncio start main ### try: - asyncio.run(main(0)) - except KeyboardInterrupt: logger.exception("App Interrupted") finally: asyncio.new_event_loop() - logger.exception('End') - + logger.exception("End")