Skip to content

Commit

Permalink
add tools scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Jun 28, 2024
1 parent 5bb96a1 commit 02d0ebd
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 8 deletions.
26 changes: 26 additions & 0 deletions docs/doc/en/basic/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ Using MaixPy + MaixVison makes it easy to develop, package, and install applicat

If you have developed a relatively simple application without a user interface and a back button, you can exit the application by pressing the device's function button (usually labeled as USER, FUNC, or OK) or the back button (if available, MaixCAM does not have this button by default).

## Installing Applications

* **Method 1**: Use the `App Store` application on the device. Find the application on the [App Store](https://maixhub.com/app), connect the device to the internet, and scan the code to install.

* **Method 2**: Install using a local installation package. Transfer the package to the device's file system, for example, to `/root/my_app_v1.0.0.zip`, and then run the following code. Make sure to modify the `pkg_path` variable to the correct path, you can also find this script in `MaixPy`'s `examples/tools/install_app.py`:
```python
import os

def install_app(pkg_path):
if not os.path.exists(pkg_path):
raise Exception(f"Package {pkg_path} not found")
cmd = f"/maixapp/apps/app_store/app_store install {pkg_path}"
err_code = os.system(cmd)
if err_code != 0:
print("[ERROR] Install failed, error code:", err_code)
else:
print(f"Install {pkg_path} success")

pkg_path = "/root/my_app_v1.0.0.zip"

install_app(pkg_path)
```

* **Method 3**:
* For applications developed using `MaixPy`, run `maixtool deploy` in the project root directory (which contains `app.yaml` and `main.py`). A QR code will be displayed. Keep the device and computer on the same local network, and use the App Store on the device to scan the QR code corresponding to the local network address for online installation.
* For applications developed using `MaixCDK`, run `maixcdk deploy` in the project root directory. A QR code will be displayed. Keep the device and computer on the same local network, and use the App Store on the device to scan the QR code corresponding to the local network address for online installation.

## Basic Guidelines for Application Development

Expand Down
63 changes: 63 additions & 0 deletions docs/doc/en/basic/auto_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,69 @@ First, package and install the application, then go to `Settings -> Auto-Start`

## Method Two for Setting Application Auto-Start

Run the Python script to set up, and modify the `new_autostart_app_id` variable in the script to the `app_id` you want to set. All installed `app_id`s will be printed out when you run the script, so you can run it once to find the desired `app_id`, modify the variable, and then run it again. To cancel the autostart setting, set it to `None`.

This script can also be found in the `MaixPy` examples under `examples/tools` as `set_autostart.py`:

```python
import configparser, os

def parse_apps_info():
info_path = "/maixapp/apps/app.info"
conf = configparser.ConfigParser()
conf.read(info_path)
version = conf["basic"]["version"]
apps = {}
for id in list(conf.keys()):
if id in ["basic", "DEFAULT"]:
continue
apps[id] = conf[id]
return apps

def list_apps():
apps = parse_apps_info()
print(f"APP num: {len(apps)}")
for i, (id, info) in enumerate(apps.items()):
name_zh = info.get("name[zh]", "")
print(f"{i + 1}. [{info['name']}] {name_zh}:")
print(f" id: {id}")
print(f" exec: {info['exec']}")
print(f" author: {info['author']}")
print(f" desc: {info['desc']}")
print(f" desc_zh: {info.get('desc', 'None')}")
print("")


def get_curr_autostart_app():
path = "/maixapp/auto_start.txt"
if os.path.exists(path):
with open(path, "r") as f:
app_id = f.readline().strip()
return app_id
return None

def set_autostart_app(app_id):
path = "/maixapp/auto_start.txt"
if not app_id:
if os.path.exists(path):
os.remove(path)
return
with open(path, "w") as f:
f.write(app_id)

if __name__ == "__main__":
# new_autostart_app_id = "settings" # change to app_id you want to set
new_autostart_app_id = None # remove autostart

list_apps()
print("Before set autostart appid:", get_curr_autostart_app())
set_autostart_app(new_autostart_app_id)
print("Current autostart appid:", get_curr_autostart_app())

```

## Method Three for Setting Application Auto-Start

You can also modify the `/maixapp/auto_start.txt` file in your device to set it up. For methods on file transfer, refer to the previous documentation.
* First, determine the `id` of the application you want to set. This is set when you package the application; if it's not an application you packaged yourself, you can install it on the device and check the folder names under the device's `/maixapp/apps/` directory, which are the application names (or you can download and check the device's `/maixapp/apps/app.info` file, where the application `id` is indicated inside the `[]` brackets).
* Then write the `id` into the `/maixapp/auto_start.txt` file. (You can create the file locally on your computer, and then transfer it to the device using `MaixVision`.)
Expand Down
25 changes: 24 additions & 1 deletion docs/doc/zh/basic/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,30 @@ title: 应用开发和应用商店

如果你只是写了比较简单的应用,没有做界面和返回按钮,默认可以按设备上的功能按键(一般是 USER 或者 FUNC 或者 OK 按钮)或者返回按钮(如果有这个按键,MaixCAM 默认没有这个按键)来退出应用。


## 安装应用

* **方法一**: 设备使用`应用商店`应用,从[应用商店](https://maixhub.com/app)找到应用,设备联网后,扫码安装。
* **方法二**: 使用安装包本地安装,将安装包传输到设备文件系统,比如`/root/my_app_v1.0.0.zip`,然后执行代码,注意修改`pkg_path`变量的路径,你也可以在`MaixPy``examples/tools/install_app.py`找到本代码:
```python
import os

def install_app(pkg_path):
if not os.path.exists(pkg_path):
raise Exception(f"package {pkg_path} not found")
cmd = f"/maixapp/apps/app_store/app_store install {pkg_path}"
err_code = os.system(cmd)
if err_code != 0:
print("[ERROR] Install failed, error code:", err_code)
else:
print(f"Install {pkg_path} success")

pkg_path = "/root/my_app_v1.0.0.zip"

install_app(pkg_path)
```
* **方法三**:
* 如果是使用`MaixPy`开发的应用,在项目根目录(包含`app.yaml``main.py`)执行`maixtool deploy`会弹出一个二维码,保持设备和电脑在同一局域网,设备使用应用商店扫描对应的局域网地址二维码就能在线安装。
* 如果是使用`MaixCDK`开发的应用,在项目根目录执行`maixcdk deploy`也会出现二维码,保持设备和电脑在同一局域网,设备使用应用商店扫描对应的局域网地址二维码就能在线安装。

## 应用开发基本准则

Expand Down
63 changes: 62 additions & 1 deletion docs/doc/zh/basic/auto_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,70 @@ title: MaixPy/MaixCAM 应用开机自启

先打包安装好应用,然后在设备`设置 -> 开机自启` 设置中选择需要自动启动的应用即可,取消开机自启也是在这里设置。


## 设置应用开机自启方法二

运行 Python 脚本设置,修改脚本中的`new_autostart_app_id` 变量为你想设置的 `app_id`, 所有已经安装了的`app_id`会在执行脚本时打印出来,可以先执行一遍找到你想设置的`app_id`,修改变量再执行一遍即可,取消自动启动设置为`None`即可。
此脚本也可以在`MaixPy``examples/tools`中找到`set_autostart.py`

```python
import configparser, os

def parse_apps_info():
info_path = "/maixapp/apps/app.info"
conf = configparser.ConfigParser()
conf.read(info_path)
version = conf["basic"]["version"]
apps = {}
for id in list(conf.keys()):
if id in ["basic", "DEFAULT"]:
continue
apps[id] = conf[id]
return apps

def list_apps():
apps = parse_apps_info()
print(f"APP num: {len(apps)}")
for i, (id, info) in enumerate(apps.items()):
name_zh = info.get("name[zh]", "")
print(f"{i + 1}. [{info['name']}] {name_zh}:")
print(f" id: {id}")
print(f" exec: {info['exec']}")
print(f" author: {info['author']}")
print(f" desc: {info['desc']}")
print(f" desc_zh: {info.get('desc', 'None')}")
print("")


def get_curr_autostart_app():
path = "/maixapp/auto_start.txt"
if os.path.exists(path):
with open(path, "r") as f:
app_id = f.readline().strip()
return app_id
return None

def set_autostart_app(app_id):
path = "/maixapp/auto_start.txt"
if not app_id:
if os.path.exists(path):
os.remove(path)
return
with open(path, "w") as f:
f.write(app_id)

if __name__ == "__main__":
# new_autostart_app_id = "settings" # change to app_id you want to set
new_autostart_app_id = None # remove autostart

list_apps()
print("Before set autostart appid:", get_curr_autostart_app())
set_autostart_app(new_autostart_app_id)
print("Current autostart appid:", get_curr_autostart_app())

```

## 设置应用开机自启方法三

你也可以通过修改设备中的 `/maixapp/auto_start.txt` 文件来设置,和传输文件的方法请看前面的文档。
* 首先知道你需要设置的应用的 `id` 是什么。在你打包应用的时候设置的;如果不是你自己打包的应用,可以先安装到设备,查看设备`/maixapp/apps/` 目录下的文件夹名就是应用名,(也可以下载查看设备的`/maixapp/apps/app.info` 文件,`[]`中括号部分就是应用`id`)。
* 然后写入 `id``/maixapp/auto_start.txt` 文件即可。(可以在电脑本地创建文件,然后 `MaixVision` 传输到设备。)
Expand Down
30 changes: 30 additions & 0 deletions docs/doc/zh/basic/maixpy_upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@ import os
os.system("pip install MaixPy -U")
```

由于默认从`pypi.org`下载,中国国内速度可能比较慢,可以设置国内的镜像站点,修改下面代码的 `server` 变量来选择,此脚本在`MaixPy``examples/tools` 目录下也有,可以直接在`MaixVision`中运行。

```python
import os

def install_maixpy(server):
cmd = f"pip install maixpy -U -i {server}"
print("Start install now, wait patiently ...")
err = os.system(cmd)
if err != 0:
print("[ERROR] execute failed, code:", err)
else:
print("Install complete")


servers = {
"pypi": "https://pypi.org/simple",
"aliyun": "https://mirrors.aliyun.com/pypi/simple",
"ustc": "https://pypi.mirrors.ustc.edu.cn/simple",
"163": "https://mirrors.163.com/pypi/simple",
"douban": "https://pypi.douban.com/simple",
"tuna": "https://pypi.tuna.tsinghua.edu.cn/simple"
}

# Select server based on your network
server = servers["tuna"]

install_maixpy(server)
```

> 如果你会使用终端, 也可以直接在终端中使用 `pip install MaixPy -U` 来更新 MaixPy。
另外你也可以手动下载`wheel` 文件(`.whl`格式)传输到设备(传输方法见后文[MaixVision 使用](./maixvision.md))后通过 `pip install ******.whl` 命令来安装。
Expand Down
14 changes: 8 additions & 6 deletions examples/network/wifi_connect.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from maix import network, err

w = network.wifi.Wifi()
print("ip:", w.get_ip())
def connect_wifi(ssid, password):
w = network.wifi.Wifi()
e = w.connect(ssid, password, wait=True, timeout=60)
err.check_raise(e, "connect wifi failed")
print("Connect success, got ip:", w.get_ip())


SSID = "Sipeed_Guest"
PASSWORD = "qwert123"
print("connect to", SSID)

e = w.connect(SSID, PASSWORD, wait=True, timeout=60)
err.check_raise(e, "connect wifi failed")
print("ip:", w.get_ip())
print("connect to", SSID)
connect_wifi(SSID, PASSWORD)

15 changes: 15 additions & 0 deletions examples/tools/install_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

def install_app(pkg_path):
if not os.path.exists(pkg_path):
raise Exception(f"package {pkg_path} not found")
cmd = f"/maixapp/apps/app_store/app_store install {pkg_path}"
err_code = os.system(cmd)
if err_code != 0:
print("[ERROR] Install failed, error code:", err_code)
else:
print(f"Install {pkg_path} success")

pkg_path = "/root/my_app_v1.0.0.zip"

install_app(pkg_path)
25 changes: 25 additions & 0 deletions examples/tools/install_maixpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os

def install_maixpy(server):
cmd = f"pip install maixpy -U -i {server}"
print("Start install now, wait patiently ...")
err = os.system(cmd)
if err != 0:
print("[ERROR] execute failed, code:", err)
else:
print("Install complete")


servers = {
"pypi": "https://pypi.org/simple",
"aliyun": "https://mirrors.aliyun.com/pypi/simple",
"ustc": "https://pypi.mirrors.ustc.edu.cn/simple",
"163": "https://mirrors.163.com/pypi/simple",
"douban": "https://pypi.douban.com/simple",
"tuna": "https://pypi.tuna.tsinghua.edu.cn/simple"
}

# Select server based on your network
server = servers["tuna"]

install_maixpy(server)
11 changes: 11 additions & 0 deletions examples/tools/install_runtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

def install_runtime():
cmd = f"/maixapp/apps/app_store/settings install_runtime"
err_code = os.system(cmd)
if err_code != 0:
print("[ERROR] Install failed, error code:", err_code)
else:
print(f"Install runtime success")

install_runtime()
26 changes: 26 additions & 0 deletions examples/tools/list_apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import configparser

def parse_apps_info():
info_path = "/maixapp/apps/app.info"
conf = configparser.ConfigParser()
conf.read(info_path)
version = conf["basic"]["version"]
apps = {}
for id in list(conf.keys()):
if id in ["basic", "DEFAULT"]:
continue
apps[id] = conf[id]
return apps

apps = parse_apps_info()

print(f"APP num: {len(apps)}")
for i, (id, info) in enumerate(apps.items()):
name_zh = info.get("name[zh]", "")
print(f"{i + 1}. [{info['name']}] {name_zh}:")
print(f" id: {id}")
print(f" exec: {info['exec']}")
print(f" author: {info['author']}")
print(f" desc: {info['desc']}")
print(f" desc_zh: {info.get('desc', 'None')}")
print("")
Loading

0 comments on commit 02d0ebd

Please sign in to comment.