|
| 1 | +# Developing Apps |
| 2 | + |
| 3 | +Apps are written in MicroPython and installed in `/apps/`. See [Filesystem Layout](../architecture/filesystem.md) for the app directory structure. |
| 4 | + |
| 5 | +Here we'll go over how to create and install a simple HelloWorld app. |
| 6 | + |
| 7 | +More advanced app examples are available in the [source code repository](https://github.com/MicroPythonOS/MicroPythonOS/tree/main/internal_filesystem/apps). |
| 8 | + |
| 9 | +## Structure |
| 10 | + |
| 11 | +Create the following file and folder structure: |
| 12 | + |
| 13 | +``` |
| 14 | +com.micropythonos.helloworld/ |
| 15 | +├── assets/ |
| 16 | +│ └── hello.py |
| 17 | +├── META-INF/ |
| 18 | +│ └── MANIFEST.JSON |
| 19 | +└── res/ |
| 20 | + └── mipmap-mdpi/ |
| 21 | + └── icon_64x64.png |
| 22 | +``` |
| 23 | + |
| 24 | +## App code |
| 25 | + |
| 26 | +In `hello.py`, put: |
| 27 | + |
| 28 | +``` |
| 29 | +from mpos.apps import Activity |
| 30 | +
|
| 31 | +class Hello(Activity): |
| 32 | +
|
| 33 | + def onCreate(self): |
| 34 | + screen = lv.obj() |
| 35 | + label = lv.label(screen) |
| 36 | + label.set_text('Hello World!') |
| 37 | + label.center() |
| 38 | + self.setContentView(screen) |
| 39 | +``` |
| 40 | + |
| 41 | +The code above creates a new screen, adds a label, sets the label text, centers the label and activates the screen. |
| 42 | + |
| 43 | +## Manifest |
| 44 | + |
| 45 | +In `MANIFEST.JSON`, put: |
| 46 | + |
| 47 | +``` |
| 48 | +{ |
| 49 | +"name": "HelloWorld", |
| 50 | +"publisher": "MicroPythonOS", |
| 51 | +"short_description": "Minimal app", |
| 52 | +"long_description": "Demonstrates the simplest app.", |
| 53 | +"fullname": "com.micropythonos.helloworld", |
| 54 | +"version": "0.0.2", |
| 55 | +"category": "development", |
| 56 | +"activities": [ |
| 57 | + { |
| 58 | + "entrypoint": "assets/hello.py", |
| 59 | + "classname": "Hello", |
| 60 | + "intent_filters": [ |
| 61 | + { |
| 62 | + "action": "main", |
| 63 | + "category": "launcher" |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + ] |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Icon |
| 72 | + |
| 73 | +The icon is a simple 64x64 pixel PNG image, which you can create with any tool, such as GIMP. |
| 74 | + |
| 75 | +It's recommended to keep it as small as possible by setting compression level to 9 and not storing any metadata such as background color, resolution, creation time, comments, Exif data, XMP data, thumbnail or color profile. |
| 76 | + |
| 77 | +The size will be somewhere between 3 and 7KB. |
| 78 | + |
| 79 | +## Installing the App |
| 80 | + |
| 81 | +The app can be installed by copying the top-level folder `com.micropythonos.helloworld/` (and its contents) to the `/apps/` folder. |
| 82 | + |
| 83 | +### On desktop |
| 84 | + |
| 85 | +You probably already have a clone of the [internal_filesystem](https://github.com/MicroPythonOS/MicroPythonOS/tree/main/internal_filesystem) that you're using to run MicroPythonOS on desktop. |
| 86 | + |
| 87 | +Just copy or move your the top-level folder `com.micropythonos.helloworld/` (and its contents) to `internal_filesystem/apps/` and you're good to go! |
| 88 | + |
| 89 | +### On ESP32 |
| 90 | + |
| 91 | +On the ESP32, you can use MicroPython tools such as [mpremote.py](https://github.com/micropython/micropython/tree/master/tools/mpremote) to copy files and folders from-to your device using the MicroPython REPL. |
| 92 | + |
| 93 | +You may need to change this one line: |
| 94 | + |
| 95 | +``` |
| 96 | +diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py |
| 97 | +index e6e3970..7f5d934 100644 |
| 98 | +--- a/tools/mpremote/mpremote/main.py |
| 99 | ++++ b/tools/mpremote/mpremote/main.py |
| 100 | +@@ -477,7 +477,7 @@ class State: |
| 101 | + self.ensure_connected() |
| 102 | + soft_reset = self._auto_soft_reset if soft_reset is None else soft_reset |
| 103 | + if soft_reset or not self.transport.in_raw_repl: |
| 104 | +- self.transport.enter_raw_repl(soft_reset=soft_reset) |
| 105 | ++ self.transport.enter_raw_repl(soft_reset=False) |
| 106 | + self._auto_soft_reset = False |
| 107 | + |
| 108 | + def ensure_friendly_repl(self): |
| 109 | +``` |
| 110 | + |
| 111 | +Then copy your app using: |
| 112 | + |
| 113 | +``` |
| 114 | +/path/to/mpremote.py fs cp -r com.micropythonos.helloworld/ :/apps/ |
| 115 | +``` |
| 116 | + |
| 117 | +## Starting the App |
| 118 | + |
| 119 | +If the app is installed into the /apps/ folder, it should show up in the launcher after refreshing it. |
| 120 | + |
| 121 | +You can also launch it manually by typing this in the MicroPython REPL: |
| 122 | + |
| 123 | +``` |
| 124 | +import mpos.apps; mpos.apps.start_app('apps/com.micropythonos.helloworld/') |
| 125 | +``` |
0 commit comments