|
| 1 | +import lvgl as lv |
| 2 | + |
| 3 | +import uio |
| 4 | +import ujson |
| 5 | +import uos |
| 6 | + |
| 7 | +import _thread |
| 8 | +import traceback |
| 9 | + |
| 10 | +from mpos import ui |
| 11 | +import mpos.info |
| 12 | + |
| 13 | +# Run the script in the current thread: |
| 14 | +def execute_script(script_source, is_file, is_launcher, is_graphical): |
| 15 | + thread_id = _thread.get_ident() |
| 16 | + print(f"Thread {thread_id}: executing script") |
| 17 | + try: |
| 18 | + if is_file: |
| 19 | + print(f"Thread {thread_id}: reading script from file {script_source}") |
| 20 | + with open(script_source, 'r') as f: # TODO: check if file exists first? |
| 21 | + script_source = f.read() |
| 22 | + if not is_graphical: |
| 23 | + script_globals = { |
| 24 | + '__name__': "__main__" |
| 25 | + } |
| 26 | + else: # is_graphical |
| 27 | + if is_launcher: |
| 28 | + prevscreen = None |
| 29 | + newscreen = ui.rootscreen |
| 30 | + else: |
| 31 | + prevscreen = lv.screen_active() |
| 32 | + newscreen=lv.obj() |
| 33 | + newscreen.set_size(lv.pct(100),lv.pct(100)) |
| 34 | + lv.screen_load(newscreen) |
| 35 | + script_globals = { |
| 36 | + 'lv': lv, |
| 37 | + 'NOTIFICATION_BAR_HEIGHT': ui.NOTIFICATION_BAR_HEIGHT, # for apps that want to leave space for notification bar |
| 38 | + 'appscreen': newscreen, |
| 39 | + 'start_app': start_app, # for launcher apps |
| 40 | + 'parse_manifest': parse_manifest, # for launcher apps |
| 41 | + 'restart_launcher': restart_launcher, # for appstore apps |
| 42 | + 'show_launcher': ui.show_launcher, # for apps that want to show the launcher |
| 43 | + 'CURRENT_OS_VERSION': mpos.info.CURRENT_OS_VERSION, # for osupdate |
| 44 | + '__name__': "__main__" |
| 45 | + } |
| 46 | + print(f"Thread {thread_id}: starting script") |
| 47 | + try: |
| 48 | + compile_name = 'script' if not is_file else long_path_to_filename(script_source) # Only filename, to avoid 'name too long' error |
| 49 | + compiled_script = compile(script_source, compile_name, 'exec') |
| 50 | + exec(compiled_script, script_globals) |
| 51 | + except Exception as e: |
| 52 | + print(f"Thread {thread_id}: exception during execution:") |
| 53 | + # Print stack trace with exception type, value, and traceback |
| 54 | + tb = getattr(e, '__traceback__', None) |
| 55 | + traceback.print_exception(type(e), e, tb) |
| 56 | + print(f"Thread {thread_id}: script {compile_name} finished") |
| 57 | + # Note that newscreen isn't deleted, as it might still be foreground, or it might be ui.rootscreen |
| 58 | + except Exception as e: |
| 59 | + print(f"Thread {thread_id}: error:") |
| 60 | + tb = getattr(e, '__traceback__', None) |
| 61 | + traceback.print_exception(type(e), e, tb) |
| 62 | + |
| 63 | +# Run the script in a new thread: |
| 64 | +def execute_script_new_thread(scriptname, is_file, is_launcher, is_graphical): |
| 65 | + print(f"main.py: execute_script_new_thread({scriptname},{is_file},{is_launcher})") |
| 66 | + try: |
| 67 | + # 168KB maximum at startup but 136KB after loading display, drivers, LVGL gui etc so let's go for 128KB for now, still a lot... |
| 68 | + # But then no additional threads can be created. A stacksize of 32KB allows for 4 threads, so 3 in the app itself, which might be tight. |
| 69 | + # 16KB allows for 10 threads in the apps, but seems too tight for urequests on unix (desktop) targets |
| 70 | + _thread.stack_size(24576) |
| 71 | + _thread.start_new_thread(execute_script, (scriptname, is_file, is_launcher, is_graphical)) |
| 72 | + except Exception as e: |
| 73 | + print("main.py: execute_script_new_thread(): error starting new thread thread: ", e) |
| 74 | + |
| 75 | +def start_app_by_name(app_name, is_launcher=False): |
| 76 | + ui.set_foreground_app(app_name) |
| 77 | + custom_app_dir=f"apps/{app_name}" |
| 78 | + builtin_app_dir=f"builtin/apps/{app_name}" |
| 79 | + try: |
| 80 | + stat = uos.stat(custom_app_dir) |
| 81 | + start_app(custom_app_dir, is_launcher) |
| 82 | + except OSError: |
| 83 | + start_app(builtin_app_dir, is_launcher) |
| 84 | + |
| 85 | +def start_app(app_dir, is_launcher=False): |
| 86 | + print(f"main.py start_app({app_dir},{is_launcher}") |
| 87 | + ui.set_foreground_app(app_dir) # would be better to store only the app name... |
| 88 | + manifest_path = f"{app_dir}/META-INF/MANIFEST.JSON" |
| 89 | + app = parse_manifest(manifest_path) |
| 90 | + start_script_fullpath = f"{app_dir}/{app.entrypoint}" |
| 91 | + execute_script_new_thread(start_script_fullpath, True, is_launcher, True) |
| 92 | + # Launchers have the bar, other apps don't have it |
| 93 | + if is_launcher: |
| 94 | + ui.open_bar() |
| 95 | + else: |
| 96 | + ui.close_bar() |
| 97 | + |
| 98 | + |
| 99 | +def restart_launcher(): |
| 100 | + # No need to stop the other launcher first, because it exits after building the screen |
| 101 | + start_app_by_name("com.example.launcher", True) |
| 102 | + |
1 | 103 |
|
2 | 104 | def is_launcher(app_name): |
3 | 105 | print(f"checking is_launcher for {app_name}") |
4 | 106 | # Simple check, could be more elaborate by checking the MANIFEST.JSON for the app... |
5 | 107 | return "launcher" in app_name |
| 108 | + |
| 109 | + |
| 110 | +class App: |
| 111 | + def __init__(self, name, publisher, short_description, long_description, icon_url, download_url, fullname, version, entrypoint, category): |
| 112 | + self.name = name |
| 113 | + self.publisher = publisher |
| 114 | + self.short_description = short_description |
| 115 | + self.long_description = long_description |
| 116 | + self.icon_url = icon_url |
| 117 | + self.download_url = download_url |
| 118 | + self.fullname = fullname |
| 119 | + self.version = version |
| 120 | + self.entrypoint = entrypoint |
| 121 | + self.category = category |
| 122 | + self.image = None |
| 123 | + self.image_dsc = None |
| 124 | + |
| 125 | +def parse_manifest(manifest_path): |
| 126 | + # Default values for App object |
| 127 | + default_app = App( |
| 128 | + name="Unknown", |
| 129 | + publisher="Unknown", |
| 130 | + short_description="", |
| 131 | + long_description="", |
| 132 | + icon_url="", |
| 133 | + download_url="", |
| 134 | + fullname="Unknown", |
| 135 | + version="0.0.0", |
| 136 | + entrypoint="assets/start.py", |
| 137 | + category="" |
| 138 | + ) |
| 139 | + try: |
| 140 | + with open(manifest_path, 'r') as f: |
| 141 | + app_info = ujson.load(f) |
| 142 | + # Create App object with values from manifest, falling back to defaults |
| 143 | + return App( |
| 144 | + name=app_info.get("name", default_app.name), |
| 145 | + publisher=app_info.get("publisher", default_app.publisher), |
| 146 | + short_description=app_info.get("short_description", default_app.short_description), |
| 147 | + long_description=app_info.get("long_description", default_app.long_description), |
| 148 | + icon_url=app_info.get("icon_url", default_app.icon_url), |
| 149 | + download_url=app_info.get("download_url", default_app.download_url), |
| 150 | + fullname=app_info.get("fullname", default_app.fullname), |
| 151 | + version=app_info.get("version", default_app.version), |
| 152 | + entrypoint=app_info.get("entrypoint", default_app.entrypoint), |
| 153 | + category=app_info.get("category", default_app.category) |
| 154 | + ) |
| 155 | + except OSError: |
| 156 | + print(f"parse_manifest: error loading manifest_path: {manifest_path}") |
| 157 | + return default_app |
| 158 | + |
| 159 | +def long_path_to_filename(path): |
| 160 | + try: |
| 161 | + if not path or not isinstance(path, str): |
| 162 | + return None |
| 163 | + # Extract filename using rsplit and take the last part |
| 164 | + filename = path.rsplit('/', 1)[-1] |
| 165 | + # Limit to the first 7 characters |
| 166 | + return filename[:7] |
| 167 | + except Exception as e: |
| 168 | + print(f"Error extracting filename: {str(e)}") |
| 169 | + return None |
| 170 | + |
| 171 | +def auto_connect(): |
| 172 | + # A generic "start at boot" mechanism hasn't been implemented yet, so do it like this: |
| 173 | + custom_auto_connect = "apps/com.example.wificonf/assets/auto_connect.py" |
| 174 | + builtin_auto_connect = "builtin/apps/com.example.wificonf/assets/auto_connect.py" |
| 175 | + # Maybe start_app_by_name() and start_app_by_name() could be merged so the try-except logic is not duplicated... |
| 176 | + try: |
| 177 | + stat = uos.stat(custom_auto_connect) |
| 178 | + execute_script_new_thread(custom_auto_connect, True, False, False) |
| 179 | + except OSError: |
| 180 | + try: |
| 181 | + print(f"Couldn't execute {custom_auto_connect}, trying {builtin_auto_connect}...") |
| 182 | + stat = uos.stat(builtin_auto_connect) |
| 183 | + execute_script_new_thread(builtin_auto_connect, True, False, False) |
| 184 | + except OSError: |
| 185 | + print("Couldn't execute {builtin_auto_connect}, continuing...") |
| 186 | + |
0 commit comments