diff --git a/internal_filesystem/apps/com.example.btcticker/assets/bitcoin_price.py b/internal_filesystem/apps/com.example.btcticker/assets/bitcoin_price.py index 43413a42..cb8475cc 100644 --- a/internal_filesystem/apps/com.example.btcticker/assets/bitcoin_price.py +++ b/internal_filesystem/apps/com.example.btcticker/assets/bitcoin_price.py @@ -1,5 +1,3 @@ -appscreen = lv.screen_active() - import usocket import ssl import ubinascii @@ -177,13 +175,34 @@ def get_price(json_str): print(f"Error: An unexpected error occurred - {str(e)}") return None +def refresh_price(timer): + global summary, status + data = ws_read_frame(sock) + if data: + print(f"Response: {data}") + price = get_price(data) + if price: + print(f"Price: {price}") + summary += f"{price}\n" + status.set_text(summary) + +def janitor_cb(timer): + if lv.screen_active() != appscreen: + print("bitcoin_price.py backgrounded, cleaning up...") + janitor.delete() + sock.close() + get_price_timer.delete() + +appscreen = lv.screen_active() +janitor = lv.timer_create(janitor_cb, 500, None) + status = lv.label(appscreen) status.align(lv.ALIGN.TOP_LEFT, 5, 10) status.set_style_text_color(lv.color_hex(0xFFFFFF), 0) - summary = "Bitcoin USD price\nfrom Coinbase's Websocket API:\n\n" status.set_text(summary) + port = 443 host = "ws-feed.exchange.coinbase.com" path = "/" @@ -199,17 +218,7 @@ try: break print("Subscribing to price updates...") ws_send_text(sock, ujson.dumps({"type": "subscribe","product_ids": ["BTC-USD"],"channels": ["ticker_batch"]})) - while appscreen == lv.screen_active(): - time.sleep(1) - data = ws_read_frame(sock) - if data: - print(f"Response: {data}") - price = get_price(data) - if price: - print(f"Price: {price}") - summary += f"{price}\n" - status.set_text(summary) - sock.close() + get_price_timer = lv.timer_create(refresh_price, 1000, None) except Exception as e: print(f"Error: {str(e)}") try: @@ -217,4 +226,3 @@ except Exception as e: except: pass - diff --git a/internal_filesystem/apps/com.example.camtest/assets/camtest.py b/internal_filesystem/apps/com.example.camtest/assets/camtest.py index dfc07045..48d958d3 100644 --- a/internal_filesystem/apps/com.example.camtest/assets/camtest.py +++ b/internal_filesystem/apps/com.example.camtest/assets/camtest.py @@ -236,7 +236,7 @@ def init_cam(): def check_running(timer): if lv.screen_active() != appscreen: - print("camtest.py lost foreground, cleaning up...") + print("camtest.py backgrounded, cleaning up...") check_running_timer.delete() if capture_timer: capture_timer.delete() diff --git a/internal_filesystem/apps/com.example.helloworld/assets/hello.py b/internal_filesystem/apps/com.example.helloworld/assets/hello.py index 5f4dd123..250b30ae 100644 --- a/internal_filesystem/apps/com.example.helloworld/assets/hello.py +++ b/internal_filesystem/apps/com.example.helloworld/assets/hello.py @@ -19,7 +19,7 @@ lv.screen_load(scr) def janitor_cb(timer): if lv.screen_active() != scr: - print("hello.py lost foreground, cleaning up...") + print("app backgrounded, cleaning up...") janitor.delete() # No cleanups to do, but in a real app, you might stop timers, deinitialize hardware devices you used, close network connections, etc. diff --git a/internal_filesystem/apps/com.example.lvgltest/assets/lvgltest.py b/internal_filesystem/apps/com.example.lvgltest/assets/lvgltest.py index d3488b19..f89cf562 100644 --- a/internal_filesystem/apps/com.example.lvgltest/assets/lvgltest.py +++ b/internal_filesystem/apps/com.example.lvgltest/assets/lvgltest.py @@ -28,7 +28,7 @@ def add_spinner(timer): def janitor_cb(timer): if lv.screen_active() != appscreen: - print("lvgltest.py lost foreground, cleaning up...") + print("lvgltest.py backgrounded, cleaning up...") janitor.delete() add_spinner_timer.delete() diff --git a/internal_filesystem/lib/mpos/apps.py b/internal_filesystem/lib/mpos/apps.py index fc802fb4..b533ae1c 100644 --- a/internal_filesystem/lib/mpos/apps.py +++ b/internal_filesystem/lib/mpos/apps.py @@ -92,7 +92,7 @@ def start_app(app_dir, is_launcher=False): manifest_path = f"{app_dir}/META-INF/MANIFEST.JSON" app = parse_manifest(manifest_path) start_script_fullpath = f"{app_dir}/{app.entrypoint}" - #execute_script_new_thread(start_script_fullpath, True, is_launcher, True) + #execute_script_new_thread(start_script_fullpath, True, is_launcher, True) # Starting (GUI?) apps in a new thread can cause hangs (GIL lock?) execute_script(start_script_fullpath, True, is_launcher, True) # Launchers have the bar, other apps don't have it if is_launcher: