Skip to content

Commit

Permalink
Improved OTA
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewWilkes committed May 24, 2024
1 parent 2297f67 commit f78c930
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 23 deletions.
12 changes: 6 additions & 6 deletions modules/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import time

from perf_timer import PerfTimer
from system.eventbus import eventbus
from system.scheduler.events import RequestForegroundPopEvent

Expand All @@ -19,13 +18,14 @@ async def run(self, render_update):
while True:
cur_time = time.ticks_ms()
delta_ticks = time.ticks_diff(cur_time, last_time)
with PerfTimer(f"Updating {self}"):
self.update(delta_ticks)
await render_update()
if self.update(delta_ticks) is not False:
await render_update()
else:
await asyncio.sleep(0.05)
last_time = cur_time

def update(self, delta):
pass
def update(self, delta: float) -> bool:
return False

def draw(self, ctx):
self.draw_overlays(ctx)
Expand Down
4 changes: 4 additions & 0 deletions modules/app_components/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"blue": (46, 173, 217),
}

colors = {
name: (c[0] / 256.0, c[1] / 256.0, c[2] / 256.0) for (name, c) in colors.items()
}

ui_colors = {"background": colors["dark_green"], "label": (232, 230, 227)}


Expand Down
24 changes: 18 additions & 6 deletions modules/async_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import sys
import _thread

if hasattr(sys.implementation, "_machine"): # MicroPython
from threadsafe import Message
Expand Down Expand Up @@ -27,25 +28,36 @@ def __iter__(self):
# Thanks to https://github.com/peterhinch/micropython-async/blob/master/v3/docs/THREADING.md
async def unblock(func, periodic_func, *args, **kwargs):
def wrap(func, message, args, kwargs):
nonlocal running
print("In thread")
try:
print(func, args, kwargs)
result = func(*args, **kwargs)
except Exception as e:
result = e
print(result)
running = False
message.set(result) # Run the blocking function.

# msg = Message()
running = True

async def periodic():
while running:
print("Periodic")
await periodic_func()
await asyncio.sleep(0.1)

msg = Message()
print("Starting thread")
# _thread.start_new_thread(print, ("Test", ))
# tid = _thread.start_new_thread(wrap, (func, msg, args, kwargs))
_thread.start_new_thread(wrap, (func, msg, args, kwargs))
# print(tid)
# time.sleep(1)
result = func(*args, **kwargs)
print("async unblock")
await periodic_func()
# result = await msg.wait()
# result = func(*args, **kwargs)
# await periodic_func()
periodic = asyncio.create_task(periodic())
result, _ = await asyncio.gather(msg.wait(), periodic)

print(result)
if isinstance(result, Exception):
raise result
Expand Down
1 change: 1 addition & 0 deletions modules/firmware_apps/intro_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def update(self, delta):
tildagonos.leds[i] = (0, 0, 0)

tildagonos.leds.write()
return True

def draw_background(self, ctx):
ctx.gray(1 - min(1, self.time_elapsed)).rectangle(-120, -120, 240, 240).fill()
Expand Down
2 changes: 2 additions & 0 deletions modules/firmware_apps/pingpong_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def update(self, delta):
if delta > 1000:
print("sync pong")
self.time_since_received = None
return True

def draw(self, ctx):
pass
Expand Down Expand Up @@ -70,6 +71,7 @@ def update(self, delta):
if delta > 1000:
print("sync ping")
self.time_since_received = None
return True

def draw(self, ctx):
pass
Expand Down
2 changes: 1 addition & 1 deletion modules/firmware_apps/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self):
)

def update(self, delta):
pass
return True

def draw_background(self, ctx):
ctx.gray(0).rectangle(-120, -120, 240, 240).fill()
Expand Down
1 change: 1 addition & 0 deletions modules/firmware_apps/tick_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def __init__(self):

def update(self, delta):
self.acc_time += delta
return True

def draw(self, ctx):
if self.acc_time > 1000_000:
Expand Down
1 change: 1 addition & 0 deletions modules/frontboards/twentyfour.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async def background_task(self):

button_states = {button: False for button in self.BUTTON_PINS.keys()}
while True:
tildagonos.read_egpios()
for button, pin in self.BUTTON_PINS.items():
button_down = not tildagonos.check_egpio_state(pin, readgpios=False)
if button_down and not button_states[button]:
Expand Down
3 changes: 3 additions & 0 deletions modules/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# main.py -- put your code here!
from esp32 import Partition

from system.scheduler import scheduler
from system.hexpansion.app import HexpansionManagerApp
Expand All @@ -19,4 +20,6 @@
# Start notification handler
scheduler.start_app(NotificationService(), always_on_top=True)

Partition.mark_app_valid_cancel_rollback()

scheduler.run_forever()
1 change: 1 addition & 0 deletions modules/system/hexpansion/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def close():
self.format_dialog_port = port

eventbus.emit(RequestForegroundPushEvent(self))
return True

def draw(self, ctx):
if self.format_dialog is not None:
Expand Down
6 changes: 2 additions & 4 deletions modules/system/launcher/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from app import App
from app_components.menu import Menu
from app_components import clear_background
from perf_timer import PerfTimer
from system.eventbus import eventbus
from system.scheduler.events import RequestStartAppEvent, RequestForegroundPushEvent
Expand Down Expand Up @@ -143,11 +144,8 @@ def back_handler(self):
# return
# self.set_menu("main")

def draw_background(self, ctx):
ctx.gray(0).rectangle(-120, -120, 240, 240).fill()

def draw(self, ctx):
self.draw_background(ctx)
clear_background(ctx)
self.menu.draw(ctx)

def update(self, delta):
Expand Down
1 change: 1 addition & 0 deletions modules/system/notification/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def update(self, delta):
except Exception as e:
print(e)
continue
return any(notification._open for notification in self.notifications)

def draw(self, ctx):
for notification in self.notifications:
Expand Down
1 change: 1 addition & 0 deletions modules/system/ota/ota.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def progress(self, version, val):

self.version = version
self.progress_pct = val
self.status = f"{val} %"
print(version, val)
# window.progress_bar(window.get_next_line(), val)
return True
Expand Down
4 changes: 2 additions & 2 deletions modules/system/scheduler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ async def start_update_tasks(self, app):
async def mark_update_finished():
# Unblock renderer
self.render_needed.set()
await asyncio.sleep(0)
await asyncio.sleep(0.05)

# If we're no longer foregounded, wait until it is before returning
did_lose_focus = False
while not self.app_is_foregrounded(app):
did_lose_focus = True
await asyncio.sleep(0.100)
await asyncio.sleep(0.250)

# Return control to the update task
return did_lose_focus
Expand Down
2 changes: 1 addition & 1 deletion patches/micropython.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/ports/esp32/esp32_common.cmake b/ports/esp32/esp32_common.cmake
index 89e46f9cf..ed2cc007c 100644
index e928fb439..fde5160a5 100644
--- a/ports/esp32/esp32_common.cmake
+++ b/ports/esp32/esp32_common.cmake
@@ -11,6 +11,9 @@ endif()
Expand Down
1 change: 1 addition & 0 deletions sim/fakes/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ def status(self, mode=None):

def config(self, a):
return None

6 changes: 3 additions & 3 deletions tildagon/partitions-tildagon.csv
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
nvs, data, nvs, 0x9000, 0x4000,
otadata, data, ota, 0xd000, 0x2000,
phy_init, data, phy, 0xf000, 0x1000,
ota_0, app, ota_0, 0x10000, 0x210000,
ota_1, app, ota_1, 0x220000, 0x210000,
vfs, data, fat, 0x430000, 0x3d0000,
ota_0, app, ota_0, 0x10000, 0x270000,
ota_1, app, ota_1, 0x280000, 0x270000,
vfs, data, fat, 0x4f0000, 0x310000,
2 changes: 2 additions & 0 deletions tildagon/sdkconfig.board
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
CONFIG_APP_EXCLUDE_PROJECT_VER_VAR=n
CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR=n
CONFIG_APP_PROJECT_VER_FROM_CONFIG=y

CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP=y

0 comments on commit f78c930

Please sign in to comment.