Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic provisioning of the frontboard, from mpy files in github #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ jobs:
micropython/ports/esp32/build-tildagon/partition_table/partition-table.bin
micropython/ports/esp32/build-tildagon/ota_data_initial.bin
micropython/ports/esp32/build-tildagon/tildagon.txt
micropython/ports/esp32/build-tildagon/frozen_mpy/frontboards/TwentyFour/app.mpy
micropython/ports/esp32/build-tildagon/frozen_mpy/frontboards/TwentyFour/tokens.mpy
- name: Create latest release for tags
uses: "marvinpinto/action-automatic-releases@latest"
if: github.event_name == 'push'
Expand Down
6 changes: 6 additions & 0 deletions modules/app_components/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ def clear_background(ctx):

def set_color(ctx, color):
ctx.rgb(*ui_colors.get(color, colors.get(color, color)))


try:
from frontboard.tokens import * # noqa: F403
except ImportError:
pass
22 changes: 22 additions & 0 deletions modules/frontboards/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
from app import App
from system.hexpansion.util import read_hexpansion_header, get_hexpansion_block_devices
import vfs


class FrontBoard(App):
year: int


def mount_frontboard(i2c, readonly=True):
header = read_hexpansion_header(i2c, eeprom_addr=0x57)
if header is None:
return False

try:
eep, partition = get_hexpansion_block_devices(i2c, header, addr=0x57)
except Exception:
return False

mountpoint = "/frontboard"

try:
vfs.mount(partition, mountpoint, readonly=readonly)
except OSError:
return False

return True
43 changes: 0 additions & 43 deletions modules/frontboards/twentyfour.py

This file was deleted.

Binary file removed modules/lib/requests/__init__.mpy
Binary file not shown.
Binary file removed modules/lib/urequests.mpy
Binary file not shown.
44 changes: 37 additions & 7 deletions modules/main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
# main.py -- put your code here!
from esp32 import Partition
import machine
import os

from system.scheduler import scheduler
from system.hexpansion.app import HexpansionManagerApp
from system.patterndisplay.app import PatternDisplay
from system.notification.app import NotificationService
from system.launcher.app import Launcher
from system.power.handler import PowerEventHandler
import display
import frontboards
import tildagonos

from frontboards.twentyfour import TwentyTwentyFour

tildagonos.tildagonos.init_gpio()
display.gfx_init()


# Start front-board interface
scheduler.start_app(TwentyTwentyFour())

fb_i2c = machine.I2C(0)
frontboard = 0x57 in fb_i2c.scan()
if frontboard:
# We have a frontboard, try to mount it
mounted = frontboards.mount_frontboard(fb_i2c)
print(f"Frontboard mounted {mounted}")
if not mounted or "app.mpy" not in os.listdir("/frontboard"):
# Provision the board if not mountable
import provision_fb

provision_fb.populate_fb()


# Do main imports after mounting the frontboard so they can
# import the year's design tokens
from system.scheduler import scheduler # noqa: E402
from system.hexpansion.app import HexpansionManagerApp # noqa: E402
from system.patterndisplay.app import PatternDisplay # noqa: E402
from system.notification.app import NotificationService # noqa: E402
from system.launcher.app import Launcher # noqa: E402


# Start expansion interface
scheduler.start_app(HexpansionManagerApp())
Expand All @@ -28,6 +51,13 @@

PowerEventHandler.RegisterDefaultCallbacks(PowerEventHandler)

if frontboard:
# Import the interface and start the app
import frontboard.app

scheduler.start_app(frontboard.app.__app_export__())


Partition.mark_app_valid_cancel_rollback()

scheduler.run_forever()
91 changes: 91 additions & 0 deletions modules/provision_fb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from machine import I2C
from system.hexpansion.util import get_hexpansion_block_devices, HexpansionHeader
import vfs
import wifi
import display

base = "/template"

"""
import os, wifi, requests
os.mkdir("/template")
wifi.connect("emf2024", "badge", "badge")
wifi.wait()
for filename in ["tokens.mpy", "app.mpy"]:
with open(f"/template/{filename}", "wb") as fb_file:
data = requests.get(f"https://0d3d512069d3.ngrok.app/{filename}")
print(data.status_code)
fb_file.write(data.content)
print(fb_file.tell())
"""


def status(msg=""):
try:
ctx = display.get_ctx()
ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
ctx.text_align = ctx.CENTER
ctx.font_size = 18.0
ctx.rgb(1, 1, 1).move_to(0, 0).text("Provisioning ...")
ctx.rgb(1, 0, 0).move_to(0, 20).text(msg)
display.end_frame(ctx)
except Exception:
pass


def populate_fb():
status()
# Ensure the board isn't mounted
mountpoint = "/frontboard"

try:
vfs.umount(mountpoint)
except OSError:
pass

port = 0
addr = 0x57
i2c = I2C(port)
wifi.connect()

status("Header")

h = HexpansionHeader(
manifest_version="2024",
fs_offset=32,
eeprom_page_size=32,
eeprom_total_size=1024 * 8,
vid=0xBAD3,
pid=0x2400,
unique_id=0x0,
friendly_name="TwentyTwentyFour",
)

status("Writing EEPROM")
# Write header to 0x00 of the eeprom
i2c.writeto(addr, bytes([0, 0]) + h.to_bytes())

_, partition = get_hexpansion_block_devices(i2c, h, addr)

success = True

status("Creating filesystem")

vfs.VfsLfs2.mkfs(partition)
vfs.mount(partition, mountpoint, readonly=False)

for filename in ["tokens.mpy", "app.mpy"]:
with open(f"{mountpoint}/{filename}", "wb") as fb_file:
with open(f"{base}/{filename}", "rb") as template_file:
fb_file.write(template_file.read())
if fb_file.tell() != template_file.tell():
status("Failed write")
success = False

if not success:
raise ValueError("Retry")

status("Remounting")
vfs.umount(mountpoint)
vfs.mount(partition, mountpoint, readonly=True)
status("Done")
Loading