1# 电量与LED灯颜色的定制开发指导
2
3## 概述
4
5### 简介
6
7OpenHarmony默认提供了电量与LED灯颜色的映射关系。对于部分产品形态(如Pad),会使用LED灯的颜色来展示当前设备充电时的电量信息,如高电量使用绿色、低电量黄色、极低电量使用红色。这种电量与LED灯颜色的映射关系,需要根据产品进行自定义。为此OpenHarmony提供了电量与LED灯颜色映射的定制方式,产品定制开发者可根据产品的设计规格来定制此特性。
8
9### 约束与限制
10
11配置策略:
12产品定制的配置路径,需要根据[配置策略](https://gitee.com/openharmony/customization_config_policy)决定。本开发指导中的定制路径以/vendor进行举例,请开发者根据具体的产品配置策略,修改定制路径。
13
14## 开发指导
15
16### 搭建环境
17
18设备要求:
19
20标准系统开发板,如DAYU200/Hi3516DV300开源套件。
21
22环境要求:
23
24Linux调测环境,相关要求和配置可参考《[快速入门](../quick-start/quickstart-overview.md)》。
25
26### 开发步骤
27
28本文以[DAYU200](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568)为例介绍电量与LED灯颜色的定制方法。
29
301. 在产品目录[(/vendor/hihope/rk3568)](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568)下创建battery文件夹。
31
322. 参考[默认电量与LED灯颜色映射的配置文件夹](https://gitee.com/openharmony/powermgr_battery_manager/tree/master/services/native/profile)创建目标文件夹,并安装到`//vendor/hihope/rk3568/battery`目录下,文件格式如下:
33
34    ```text
35    profile
36    ├── BUILD.gn
37    ├── battery_config.json
38    ```
39
403. 参考[默认电量与LED灯颜色映射的配置文件夹中的battery_config.json](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/services/native/profile/battery_config.json)编写定制的battery_config.json。包含定制后的电量与LED灯颜色映射配置如下:
41
42    ```json
43    {
44        "light": {
45            "low": {
46                "soc": [0, 20],
47                "rgb": [255, 192, 203]
48            },
49            "normal": {
50                "soc": [20, 95],
51                "rgb": [255, 0, 255]
52            },
53            "high": {
54                "soc": [95, 100],
55                "rgb": [0, 0, 255]
56            }
57        }
58    }
59    ```
60
61    **表1** 电量等级说明
62
63    | 电量等级 | 描述 |
64    | -------- | -------- |
65    | low | 低电量 |
66    | normal | 正常电量 |
67    | high | 高电量 |
68
69    **表2** 电量区间与LED灯颜色映射配置说明
70
71    | 配置项 | 描述 |
72    | -------- | -------- |
73    | soc | 电量区间 |
74    | rgb | LED灯RGB组合 |
75
76
774. 参考[默认电量与LED灯颜色映射的配置文件夹中的BUILD.gn](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/services/native/profile/BUILD.gn)编写BUILD.gn文件,将battery_config.json打包到`//vendor/etc/battery`目录下,配置如下:
78
79    ```shell
80    import("//build/ohos.gni")                #引用build/ohos.gni
81
82    ohos_prebuilt_etc("battery_config") {
83        source = "battery_config.json"
84        relative_install_dir = "battery"
85        install_images = [ chipset_base_dir ] #安装到vendor目录下的必要配置
86        part_name = "product_rk3568"          #part_name为product_rk3568,以实现后续编译
87    }
88    ```
89
905. 将编译目标添加到`/vendor/hihope/rk3568`目录下[ohos.build](https://gitee.com/openharmony/vendor_hihope/blob/master/rk3568/ohos.build)的"module_list"中,例如:
91
92    ```json
93    {
94    "parts": {
95        "product_rk3568": {
96        "module_list": [
97            "//vendor/hihope/rk3568/default_app_config:default_app_config",
98            "//vendor/hihope/rk3568/image_conf:custom_image_conf",
99            "//vendor/hihope/rk3568/preinstall-config:preinstall-config",
100            "//vendor/hihope/rk3568/resourceschedule:resourceschedule",
101            "//vendor/hihope/rk3568/etc:product_etc_conf",
102            "//vendor/hihope/rk3568/battery/profile:battery_config" #添加battery_config的编译
103        ]
104        }
105    },
106    "subsystem": "product_hihope"
107    }
108    ```
109    “//vendor/hihope/rk3568/battery/”为文件夹路径,“profile”为创建的文件夹名字,“battery_config”为编译目标。
110
1116. 参考《[快速入门](../quick-start/quickstart-overview.md)》编译定制版本,编译命令如下:
112
113    ```shell
114    ./build.sh --product-name rk3568 --ccache
115    ```
116
1177. 将定制版本烧录到DAYU200开发板中。
118
119### 调测验证
120
1211. 开机后,进入shell命令行。
122    ```
123    hdc shell
124    ```
125
1262. 进入电池电量的节点路径(以当前DAYU开发版路径为例)。
127    ```
128    cd /data/service/el0/battery/battery
129    ```
130
1313. 以默认的电量与LED灯颜色的映射配置数据为例,修改电量数值,模拟电池信息上报,并观察LED亮灯颜色变化。
132
133    1. 修改电量数值
134    ```
135    echo 5 > capacity
136    ```
137    2. 上报电量信息改变,触发LED灯映射
138    ```
139    hidumper -s 3302 -a -r
140    ```
141    ![low_power_led](figures/low_power_led.jpg)
142
143    3. 修改电量数值
144    ```
145    echo 50 > capacity
146    ```
147    4. 上报电量信息改变,触发LED灯映射
148    ```
149    hidumper -s 3302 -a -r
150    ```
151    ![normal_power_led](figures/normal_power_led.jpg)
152
153    5. 修改电量数值
154    ```
155    echo 100 > capacity
156    ```
157    6. 上报电量信息改变,触发LED灯映射
158    ```
159    hidumper -s 3302 -a -r
160    ```
161    ![high_power_led](figures/high_power_led.jpg)
162
1634. 以新的电量与LED灯颜色的映射配置数据为例,更改之后:
164    ```json
165    {
166        "light": {
167            "low": {
168                "soc": [0, 20],
169                "rgb": [255, 192, 203]
170            },
171            "normal": {
172                "soc": [20, 95],
173                "rgb": [255, 0, 255]
174            },
175            "high": {
176                "soc": [95, 100],
177                "rgb": [0, 0, 255]
178            }
179        }
180    }
181    ```
182
183    1. 修改电量数值
184    ```
185    echo 15 > capacity
186    ```
187    2. 上报电量信息改变,触发LED灯映射
188    ```
189    hidumper -s 3302 -a -r
190    ```
191    ![low_power_led_alter](figures/normal_power_led_alter.jpg)
192
193    3. 修改电量数值
194    ```
195    echo 95 > capacity
196    ```
197    4. 上报电量信息改变,触发LED灯映射
198    ```
199    hidumper -s 3302 -a -r
200    ```
201    ![normal_power_led_alter](figures/low_power_led_alter.jpg)
202
203    5. 修改电量数值
204    ```
205    echo 99 > capacity
206    ```
207    6. 上报电量信息改变,触发LED灯映射
208    ```
209    hidumper -s 3302 -a -r
210    ```
211    ![high_power_led_alter](figures/high_power_led_alter.jpg)
212
213## 参考
214开发过程中可参考的配置文件路径:[系统默认亮度范围配置源码路径](https://gitee.com/openharmony/powermgr_battery_manager/tree/master/services/native/profile/)
215
216默认配置:
217
218```json
219{
220    "light": {
221        "low": {
222            "soc": [0, 10],
223            "rgb": [255, 0, 0]
224        },
225        "normal": {
226            "soc": [10, 90],
227            "rgb": [255, 255, 0]
228        },
229        "high": {
230            "soc": [90, 100],
231            "rgb": [0, 255, 0]
232        }
233    }
234}
235```
236
237打包路径:/system/etc/battery
238