1## 利用NAPI提供的接口实现APP与pwm舵机硬件交互 2 3**PS:不了解 pwm 的可以先阅读 unionman\unionpi_Tiger\sample\hardware\pwm 中的README.md** 4 5**NAPI**在 `unionman\unionpi_Tiger\sample\app\gpioled_app\README.md` 中有详细介绍及说明 6 7**工程目录如下:** 8 9``` 10├── entry 11 └── src 12 ├── main 13 │ ├── ets 14 │ │ ├── entryability 15 │ │ │ └── EntryAbility.ts 16 │ │ └── pages 17 │ │ └── Index.ets // 主要代码 18 │ ├── module.json5 // 配置文件 19 │ 20 └── ohosTest 21 ├── ets 22 │ ├── test 23 │ │ ├── Ability.test.ets // 测试代码 24 │ │ └── List.test.ets 25 │ ├── testability 26 │ │ ├── pages 27 │ │ │ └── Index.ets // 测试UI 28 │ │ └── TestAbility.ets 29 │ └── testrunner 30 │ └── OpenHarmonyTestRunner.ts 31 ├── module.json5 32``` 33 34流程如下: 35 36### 1. 添加子系统 37 38打开`build/subsystem_config.json`文件,添加下列语句 39 40**添加 napisubsys 子系统** 41 42```json 43 "napisubsys": { 44 "path": "vendor/unionman/unionpi_tiger/sample/napi/napisubsys", 45 "name": "napisubsys" 46 }, 47``` 48 49### 2. 添加组件 50 51打开`unionpi_tiger/sample/napi/napisubsys/ohos.build`文件,在`"parts":`中添加下列语句 52 53```json 54 "pwmnapipart": { 55 "variants": [ 56 "phone" 57 ], 58 "module_list": [ 59 "//vendor/unionman/unionpi_tiger/sample/napi/napisubsys/pwmnapipart/pwmnapidemo:pwmtest" 60 ] 61 } 62``` 63 64### 3.添加产品定义 65 66打开`vendor/unionman/unionpi_tiger/config.json`文件,在`"subsystems":`中添加下列语句 67 68**添加 pwmnapipart 子组件** 69 70```json 71 { 72 "subsystem": "napisubsys", 73 "components": [ 74 { 75 "component": "pwmnapipart", 76 "features": [] 77 } 78 ] 79 }, 80``` 81 82**注意:在这些配置文件中,不要多加空行、符号,可能会导致开发板无法开机** 83 84### 4.编译和烧录 85 86参考:[unionpi_tiger/README_zh.md · OpenHarmony/device_board_unionman - Gitee.com](https://gitee.com/openharmony/device_board_unionman/blob/master/unionpi_tiger/README_zh.md#编译与调试) 87 88在编译完成后,会在 `out\unionpi_tiger\packages\phone\system\lib\module` 目录下面生成对应的 **libpwmtest.z.so** 文件,该文件就是我们所需的库文件。 89 90### 5.APP使用 91 92#### 1.修改系统配置文件 93 94修改系统权限,使 APP 能够使用开发板 pwm 的系统文件 95 96打开 `device\board\unionman\unionpi_tiger\config\init\arm\init.A311D.cfg` 文件,在 **"jobs - name :init" 的 "cmds"** 中添加下面语句 97 98``` 99"write /sys/class/pwm/pwmchip0/export 0", 100 "write /sys/class/pwm/pwmchip2/export 0", 101"chmod 666 /sys/class/pwm/pwmchip0/pwm0/duty_cycle", 102"chmod 666 /sys/class/pwm/pwmchip0/pwm0/period", 103"chmod 666 /sys/class/pwm/pwmchip0/pwm0/enable", 104"chmod 666 /sys/class/pwm/pwmchip0/pwm0/polarity", 105"chmod 666 /sys/class/pwm/pwmchip2/pwm0/duty_cycle", 106"chmod 666 /sys/class/pwm/pwmchip2/pwm0/period", 107"chmod 666 /sys/class/pwm/pwmchip2/pwm0/enable", 108"chmod 666 /sys/class/pwm/pwmchip2/pwm0/polarity", 109``` 110 111**注意:在这些配置文件中,不要多加空行、符号,可能会导致开发板无法开机** 112 113#### 2.安装APP 114 115DevEco Studio 的安装可参考: 116 117[搭建开发环境流程-快速开始-HUAWEI DevEco Studio For OpenHarmony使用指南-工具-HarmonyOS应用开发 | HarmonyOS](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-deveco-studio-setup-flow-0000001218600628) 118 119使用 DevEco Studio 打开位于 `vendor\unionman\unionpi_tiger\sample\app` 的 `pwm_app` 文件夹,并设置应用签名 120 121点击 DevEco Studio 左上角的 **文件**,然后点击 **项目结构**,进入项目结构界面: 122 123 124 125选择 **Signing Configs** ,在 **Automatically generate signature** 处打勾,然后点击 Apply,最后点击 OK 126 127将位于`unionman\unionpi_tiger\sample\napi\napisubsys`中的`@ohos.pwmtest.d.ts`复制并放在`X:\Users\XXXXX\AppData\Local\OpenHarmony\Sdk\ets\3.1.6.6\api`文件夹内 128 129*此路径是由SDK安装目录决定,如不清楚自己路径可打开DevEco Studio—工具—SDK管理—外观和行为—OpenHarmony SDK中查看* 130 131在DevEco Studio右上角点击运行按钮,将APP安装至开发板 132 133### 6.APP演示 134 135 136