• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "battery_led.h"
17 #include "battery_log.h"
18 
19 using namespace OHOS::HDI::Light::V1_0;
20 using namespace std;
21 
22 namespace OHOS {
23 namespace HDI {
24 namespace Battery {
25 namespace V1_1 {
26 namespace {
27 constexpr uint32_t MOVE_RIGHT_16 = 16;
28 constexpr uint32_t MOVE_RIGHT_8 = 8;
29 }
InitLight()30 void BatteryLed::InitLight()
31 {
32     batteryLight_ = ILightInterface::Get();
33     if (batteryLight_ == nullptr) {
34         BATTERY_HILOGW(COMP_HDI, "Light interface is null");
35         return;
36     }
37 
38     vector<HdfLightInfo> lightInfo;
39     if (batteryLight_->GetLightInfo(lightInfo) < HDF_SUCCESS) {
40         BATTERY_HILOGW(COMP_HDI, "Get battert light failed");
41         return;
42     }
43 
44     available_ = std::any_of(lightInfo.begin(), lightInfo.end(), [](const auto &item) {
45         return item.lightId == HdfLightId::HDF_LIGHT_ID_BATTERY;
46     });
47     BATTERY_HILOGI(COMP_HDI, "Battery light is available: %{public}d", available_);
48 }
49 
TurnOff()50 void BatteryLed::TurnOff()
51 {
52     if (!available_) {
53         return;
54     }
55     int32_t ret = batteryLight_->TurnOffLight(HdfLightId::HDF_LIGHT_ID_BATTERY);
56     if (ret < HDF_SUCCESS) {
57         BATTERY_HILOGW(COMP_HDI, "Failed to turn off the battery light");
58     }
59     lightColor_ = (ret < HDF_SUCCESS) ? lightColor_ : 0;
60 }
61 
TurnOn(uint32_t color)62 void BatteryLed::TurnOn(uint32_t color)
63 {
64     if (!available_) {
65         return;
66     }
67     struct HdfLightEffect effect = {
68         .lightColor.colorValue.rgbColor.r = (color >> MOVE_RIGHT_16) & 0xFF,
69         .lightColor.colorValue.rgbColor.g = (color >> MOVE_RIGHT_8) & 0xFF,
70         .lightColor.colorValue.rgbColor.b = color & 0xFF,
71     };
72     BATTERY_HILOGD(COMP_HDI, "battery light color is %{public}d", color);
73     int32_t ret = batteryLight_->TurnOnLight(HdfLightId::HDF_LIGHT_ID_BATTERY, effect);
74     if (ret < HDF_SUCCESS) {
75         BATTERY_HILOGW(COMP_HDI, "Failed to turn on the battery light");
76     }
77     lightColor_ = (ret < HDF_SUCCESS) ? lightColor_ : color;
78 }
79 
UpdateColor(int32_t chargeState,int32_t capacity)80 bool BatteryLed::UpdateColor(int32_t chargeState, int32_t capacity)
81 {
82     if ((chargeState == static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_NONE)) ||
83         (chargeState == static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_RESERVED)) || !available_) {
84         BATTERY_HILOGD(COMP_HDI, "not in charging state, turn off battery light");
85         TurnOff();
86         return false;
87     }
88 
89     const auto &lightConf = BatteryConfig::GetInstance().GetLightConf();
90     for (const auto &it : lightConf) {
91         if ((capacity >= it.beginSoc) && (capacity <= it.endSoc)) {
92             if (lightColor_ == it.rgb) {
93                 return true;
94             }
95             TurnOff();
96             TurnOn(it.rgb);
97             return true;
98         }
99     }
100     return false;
101 }
102 
IsAvailable() const103 bool BatteryLed::IsAvailable() const
104 {
105     return available_;
106 }
107 
GetLightColor() const108 uint32_t BatteryLed::GetLightColor() const
109 {
110     return lightColor_;
111 }
112 } // namespace V1_1
113 } // namespace Battery
114 } // namespace HDI
115 } // namespace OHOS
116