Skip to content

Commit 47caf88

Browse files
committed
ESP32/init: timer:sleep(infinity) on exit when devmode is enabled
Application is started inside of `try .. catch` block and `timer:sleep(infinity)` is used after application exit according to devmode settings. Signed-off-by: Davide Bettio <[email protected]>
1 parent 6fe8c61 commit 47caf88

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

libs/esp32boot/esp32init.erl

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,26 @@
2424

2525
start() ->
2626
console:print(<<"AtomVM init.\n">>),
27+
boot().
2728

28-
io:format("Starting application...~n"),
29-
30-
Exit =
31-
try boot() of
32-
Result -> {exit, Result}
33-
catch
34-
Error -> {crash, Error}
35-
end,
36-
erlang:display(Exit),
37-
38-
io:format("Looping...~n"),
39-
40-
loop().
41-
42-
loop() ->
43-
receive
44-
Msg ->
45-
erlang:display({received_message, Msg}),
46-
loop()
47-
end.
48-
49-
maybe_start_dev_mode(SystemStatus) ->
29+
is_dev_mode_enabled(SystemStatus) ->
5030
case {SystemStatus, esp:nvs_get_binary(atomvm, dev_mode)} of
5131
{_, <<"always">>} ->
52-
ep32devmode:start_dev_mode();
32+
true;
5333
{_, <<"never">>} ->
54-
not_started;
34+
false;
5535
{ok, undefined} ->
56-
not_started;
57-
{failed_app_start, undefined} ->
58-
esp32devmode:start_dev_mode()
36+
false;
37+
{app_exit, undefined} ->
38+
false;
39+
{app_fail, undefined} ->
40+
true
41+
end.
42+
43+
maybe_start_dev_mode(SystemStatus) ->
44+
case is_dev_mode_enabled(SystemStatus) of
45+
true -> esp32devmode:start_dev_mode();
46+
false -> not_started
5947
end.
6048

6149
% TODO: add support for multiple apps
@@ -67,11 +55,29 @@ boot() ->
6755
case atomvm:add_avm_pack_file(BootPath, [{name, app}]) of
6856
ok ->
6957
StartModule = get_start_module(),
70-
maybe_start_dev_mode(ok),
71-
StartModule:start();
58+
DevOnExit = is_dev_mode_enabled(app_exit),
59+
StartedDevMode = maybe_start_dev_mode(ok),
60+
61+
io:format("Starting application...~n"),
62+
case DevOnExit of
63+
true ->
64+
try StartModule:start() of
65+
Result -> io:format("Exited: ~p.~n", [Result])
66+
catch
67+
Error -> io:format("Crashed: ~p.~n", [Error])
68+
end,
69+
case StartedDevMode of
70+
started -> ok;
71+
_NotStarted -> maybe_start_dev_mode(app_exit)
72+
end,
73+
timer:sleep(infinity);
74+
false ->
75+
StartModule:start()
76+
end;
7277
{error, Reason} ->
7378
io:format("Failed app start: ~p.~n", [Reason]),
74-
maybe_start_dev_mode(failed_app_start)
79+
maybe_start_dev_mode(app_fail),
80+
timer:sleep(infinity)
7581
end.
7682

7783
get_boot_path() ->

0 commit comments

Comments
 (0)