You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
bitcoin_price: fix
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user