Skip to content

Commit 08fb82a

Browse files
bitcoin_price: fix
1 parent 1adfe54 commit 08fb82a

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

internal_filesystem/apps/com.example.btcticker/assets/bitcoin_price.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
appscreen = lv.screen_active()
2-
31
import usocket
42
import ssl
53
import ubinascii
@@ -177,13 +175,34 @@ def get_price(json_str):
177175
print(f"Error: An unexpected error occurred - {str(e)}")
178176
return None
179177

178+
def refresh_price(timer):
179+
global summary, status
180+
data = ws_read_frame(sock)
181+
if data:
182+
print(f"Response: {data}")
183+
price = get_price(data)
184+
if price:
185+
print(f"Price: {price}")
186+
summary += f"{price}\n"
187+
status.set_text(summary)
188+
189+
def janitor_cb(timer):
190+
if lv.screen_active() != appscreen:
191+
print("bitcoin_price.py backgrounded, cleaning up...")
192+
janitor.delete()
193+
sock.close()
194+
get_price_timer.delete()
195+
196+
appscreen = lv.screen_active()
197+
janitor = lv.timer_create(janitor_cb, 500, None)
198+
180199
status = lv.label(appscreen)
181200
status.align(lv.ALIGN.TOP_LEFT, 5, 10)
182201
status.set_style_text_color(lv.color_hex(0xFFFFFF), 0)
183-
184202
summary = "Bitcoin USD price\nfrom Coinbase's Websocket API:\n\n"
185203
status.set_text(summary)
186204

205+
187206
port = 443
188207
host = "ws-feed.exchange.coinbase.com"
189208
path = "/"
@@ -199,22 +218,11 @@ def get_price(json_str):
199218
break
200219
print("Subscribing to price updates...")
201220
ws_send_text(sock, ujson.dumps({"type": "subscribe","product_ids": ["BTC-USD"],"channels": ["ticker_batch"]}))
202-
while appscreen == lv.screen_active():
203-
time.sleep(1)
204-
data = ws_read_frame(sock)
205-
if data:
206-
print(f"Response: {data}")
207-
price = get_price(data)
208-
if price:
209-
print(f"Price: {price}")
210-
summary += f"{price}\n"
211-
status.set_text(summary)
212-
sock.close()
221+
get_price_timer = lv.timer_create(refresh_price, 1000, None)
213222
except Exception as e:
214223
print(f"Error: {str(e)}")
215224
try:
216225
sock.close()
217226
except:
218227
pass
219228

220-

internal_filesystem/apps/com.example.camtest/assets/camtest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def init_cam():
236236

237237
def check_running(timer):
238238
if lv.screen_active() != appscreen:
239-
print("camtest.py lost foreground, cleaning up...")
239+
print("camtest.py backgrounded, cleaning up...")
240240
check_running_timer.delete()
241241
if capture_timer:
242242
capture_timer.delete()

internal_filesystem/apps/com.example.helloworld/assets/hello.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
def janitor_cb(timer):
2121
if lv.screen_active() != scr:
22-
print("hello.py lost foreground, cleaning up...")
22+
print("app backgrounded, cleaning up...")
2323
janitor.delete()
2424
# No cleanups to do, but in a real app, you might stop timers, deinitialize hardware devices you used, close network connections, etc.
2525

internal_filesystem/apps/com.example.lvgltest/assets/lvgltest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def add_spinner(timer):
2828

2929
def janitor_cb(timer):
3030
if lv.screen_active() != appscreen:
31-
print("lvgltest.py lost foreground, cleaning up...")
31+
print("lvgltest.py backgrounded, cleaning up...")
3232
janitor.delete()
3333
add_spinner_timer.delete()
3434

internal_filesystem/lib/mpos/apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def start_app(app_dir, is_launcher=False):
9292
manifest_path = f"{app_dir}/META-INF/MANIFEST.JSON"
9393
app = parse_manifest(manifest_path)
9494
start_script_fullpath = f"{app_dir}/{app.entrypoint}"
95-
#execute_script_new_thread(start_script_fullpath, True, is_launcher, True)
95+
#execute_script_new_thread(start_script_fullpath, True, is_launcher, True) # Starting (GUI?) apps in a new thread can cause hangs (GIL lock?)
9696
execute_script(start_script_fullpath, True, is_launcher, True)
9797
# Launchers have the bar, other apps don't have it
9898
if is_launcher:

0 commit comments

Comments
 (0)